1<?php
2/**
3 * DokuWiki Syntax Plugin Web Component.
4 *
5 */
6if (!defined('DOKU_INC')) {
7    die();
8}
9
10require_once(__DIR__ . '/../webcomponent.php');
11
12/**
13 * All DokuWiki plugins to extend the parser/rendering mechanism
14 * need to inherit from this class
15 *
16 * The name of the class must follow a pattern (don't change it)
17 * ie:
18 *    syntax_plugin_PluginName_ComponentName
19 */
20class syntax_plugin_webcomponent_cardcolumns extends DokuWiki_Syntax_Plugin
21{
22
23
24    /**
25     * Syntax Type.
26     *
27     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
28     * @see DokuWiki_Syntax_Plugin::getType()
29     */
30    function getType()
31    {
32        return 'container';
33    }
34
35    /**
36     * @return array
37     * Allow which kind of plugin inside
38     * All
39     */
40    public function getAllowedTypes()
41    {
42        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
43    }
44
45    /**
46     * How Dokuwiki will add P element
47     *
48     * * 'normal' - The plugin can be used inside paragraphs
49     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
50     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
51     *
52     * @see DokuWiki_Syntax_Plugin::getPType()
53     */
54    function getPType()
55    {
56        return 'block';
57    }
58
59    /**
60     * @see Doku_Parser_Mode::getSort()
61     *
62     * the mode with the lowest sort number will win out
63     * the container (parent) must then have a lower number than the child
64     */
65    function getSort()
66    {
67        return 100;
68    }
69
70    /**
71     * Create a pattern that will called this plugin
72     *
73     * @see Doku_Parser_Mode::connectTo()
74     * @param string $mode
75     */
76    function connectTo($mode)
77    {
78        foreach (self::getTags() as $tag) {
79            $pattern = '<' . $tag . '.*?>(?=.*?</' . $tag . '>)';
80            $this->Lexer->addEntryPattern($pattern, $mode, 'plugin_' . webcomponent::PLUGIN_NAME . '_' . $this->getPluginComponent());
81        }
82
83    }
84
85    public function postConnect()
86    {
87        foreach (self::getTags() as $tag) {
88            $this->Lexer->addExitPattern('</' . $tag . '>', 'plugin_' . webcomponent::PLUGIN_NAME . '_' . $this->getPluginComponent());
89        }
90
91    }
92
93    /**
94     *
95     * The handle function goal is to parse the matched syntax through the pattern function
96     * and to return the result for use in the renderer
97     * This result is always cached until the page is modified.
98     * @see DokuWiki_Syntax_Plugin::handle()
99     *
100     * @param string $match
101     * @param int $state
102     * @param int $pos
103     * @param Doku_Handler $handler
104     * @return array|bool
105     */
106    function handle($match, $state, $pos, Doku_Handler $handler)
107    {
108
109        switch ($state) {
110
111            case DOKU_LEXER_ENTER:
112
113                // Suppress the <>
114                $match = utf8_substr($match, 1, -1);
115                // Suppress the tag name
116                foreach (self::getTags() as $tag) {
117                    $match = str_replace( $tag, "",$match);
118                }
119                $parameters = webcomponent::parseMatch($match);
120                return array($state, $parameters);
121
122            case DOKU_LEXER_EXIT :
123
124                return array($state, '');
125
126
127        }
128
129        return array();
130
131    }
132
133    /**
134     * Render the output
135     * @see DokuWiki_Syntax_Plugin::render()
136     *
137     * @param string $mode
138     * @param Doku_Renderer $renderer
139     * @param array $data - what the function handle() return'ed
140     * @return bool
141     */
142    function render($mode, Doku_Renderer $renderer, $data)
143    {
144
145        if ($mode == 'xhtml') {
146
147            /** @var Doku_Renderer_xhtml $renderer */
148            list($state, $parameters) = $data;
149            switch ($state) {
150
151                case DOKU_LEXER_ENTER :
152                    $renderer->doc .= '<div class="card-columns">' . DOKU_LF;
153                    break;
154
155                case DOKU_LEXER_EXIT :
156
157                    $renderer->doc .= '</div>'.DOKU_LF;
158                    break;
159            }
160            return true;
161        }
162        return false;
163    }
164
165
166    public static function getTags()
167    {
168        return array ('card-columns','teaser-columns');
169    }
170
171
172}
173