1<?php
2
3// implementation of
4// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/cite
5
6// must be run within Dokuwiki
7use ComboStrap\PluginUtility;
8use ComboStrap\TagAttributes;
9use ComboStrap\XmlTagProcessing;
10
11require_once(__DIR__ . '/../ComboStrap/StringUtility.php');
12
13if (!defined('DOKU_INC')) die();
14
15
16class syntax_plugin_combo_cite extends DokuWiki_Syntax_Plugin
17{
18    const TAG = "cite";
19
20
21    function getType()
22    {
23        return 'container';
24    }
25
26    /**
27     * How Dokuwiki will add P element
28     *
29     *  * 'normal' - The plugin can be used inside paragraphs
30     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
31     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
32     *
33     * @see DokuWiki_Syntax_Plugin::getPType()
34     */
35    function getPType()
36    {
37        return 'normal';
38    }
39
40    /**
41     * @return array
42     * Allow which kind of plugin inside
43     *
44     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
45     * because we manage self the content and we call self the parser
46     *
47     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
48     */
49    function getAllowedTypes()
50    {
51        return array('baseonly', 'container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
52    }
53
54    function getSort()
55    {
56        /**
57         * Should be less than the cite syntax plugin
58         **/
59        return 200;
60    }
61
62
63    function connectTo($mode)
64    {
65
66        $pattern = XmlTagProcessing::getContainerTagPattern(self::TAG);
67        $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
68
69    }
70
71
72    function postConnect()
73    {
74
75        $this->Lexer->addExitPattern('</' . syntax_plugin_combo_cite::TAG . '>', PluginUtility::getModeFromTag($this->getPluginComponent()));
76
77    }
78
79    /**
80     *
81     * The handle function goal is to parse the matched syntax through the pattern function
82     * and to return the result for use in the renderer
83     * This result is always cached until the page is modified.
84     * @param string $match
85     * @param int $state
86     * @param int $pos - byte position in the original source file
87     * @param Doku_Handler $handler
88     * @return array|bool
89     * @see DokuWiki_Syntax_Plugin::handle()
90     *
91     */
92    function handle($match, $state, $pos, Doku_Handler $handler)
93    {
94
95        switch ($state) {
96
97            case DOKU_LEXER_ENTER :
98                $attributes = TagAttributes::createFromTagMatch($match)->toCallStackArray();
99                return array(
100                    PluginUtility::STATE => $state,
101                    PluginUtility::ATTRIBUTES => $attributes
102                );
103
104            case DOKU_LEXER_UNMATCHED :
105                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
106
107            case DOKU_LEXER_EXIT :
108                // Important otherwise we don't get an exit in the render
109                return array(PluginUtility::STATE => $state);
110
111
112        }
113        return array();
114
115    }
116
117    /**
118     * Render the output
119     * @param string $format
120     * @param Doku_Renderer $renderer
121     * @param array $data - what the function handle() return'ed
122     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
123     * @see DokuWiki_Syntax_Plugin::render()
124     *
125     *
126     */
127    function render($format, Doku_Renderer $renderer, $data)
128    {
129
130        if ($format == 'xhtml') {
131
132            /** @var Doku_Renderer_xhtml $renderer */
133            $state = $data [PluginUtility::STATE];
134            switch ($state) {
135                case DOKU_LEXER_ENTER :
136
137                    $attributes = $data[PluginUtility::ATTRIBUTES];
138                    $tagAttributes = TagAttributes::createFromCallStackArray($attributes, self::TAG);
139                    $renderer->doc .= $tagAttributes->toHtmlEnterTag("cite");
140
141                    break;
142
143                case DOKU_LEXER_UNMATCHED :
144                    $renderer->doc .= PluginUtility::renderUnmatched($data);
145                    break;
146
147                case DOKU_LEXER_EXIT :
148
149                    $renderer->doc .= '</cite>';
150                    break;
151
152            }
153            return true;
154        }
155
156        // unsupported $mode
157        return false;
158    }
159
160
161}
162
163