1<?php
2
3namespace dokuwiki\Extension;
4
5use dokuwiki\Parsing\ParserMode\Plugin;
6use Doku_Handler;
7use Doku_Renderer;
8
9/**
10 * Syntax Plugin Prototype
11 *
12 * All DokuWiki plugins to extend the parser/rendering mechanism
13 * need to inherit from this class
14 *
15 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
16 * @author     Andreas Gohr <andi@splitbrain.org>
17 */
18abstract class SyntaxPlugin extends Plugin
19{
20    protected $allowedModesSetup = false;
21
22    /**
23     * Syntax Type
24     *
25     * Needs to return one of the mode types defined in $PARSER_MODES in Parser.php
26     *
27     * @return string
28     */
29    abstract public function getType();
30
31    /**
32     * Allowed Mode Types
33     *
34     * Defines the mode types for other dokuwiki markup that maybe nested within the
35     * plugin's own markup. Needs to return an array of one or more of the mode types
36     * defined in $PARSER_MODES in Parser.php
37     *
38     * @return array
39     */
40    public function getAllowedTypes()
41    {
42        return [];
43    }
44
45    /**
46     * Paragraph Type
47     *
48     * Defines how this syntax is handled regarding paragraphs. This is important
49     * for correct XHTML nesting. Should return one of the following:
50     *
51     * 'normal' - The plugin can be used inside paragraphs
52     * 'block'  - Open paragraphs need to be closed before plugin output
53     * 'stack'  - Special case. Plugin wraps other paragraphs.
54     *
55     * @see Doku_Handler_Block
56     *
57     * @return string
58     */
59    public function getPType()
60    {
61        return 'normal';
62    }
63
64    /**
65     * Handler to prepare matched data for the rendering process
66     *
67     * This function can only pass data to render() via its return value - render()
68     * may be not be run during the object's current life.
69     *
70     * Usually you should only need the $match param.
71     *
72     * @param   string $match The text matched by the patterns
73     * @param   int $state The lexer state for the match
74     * @param   int $pos The character position of the matched text
75     * @param   Doku_Handler $handler The Doku_Handler object
76     * @return  bool|array Return an array with all data you want to use in render, false don't add an instruction
77     */
78    abstract public function handle($match, $state, $pos, Doku_Handler $handler);
79
80    /**
81     * Handles the actual output creation.
82     *
83     * The function must not assume any other of the classes methods have been run
84     * during the object's current life. The only reliable data it receives are its
85     * parameters.
86     *
87     * The function should always check for the given output format and return false
88     * when a format isn't supported.
89     *
90     * $renderer contains a reference to the renderer object which is
91     * currently handling the rendering. You need to use it for writing
92     * the output. How this is done depends on the renderer used (specified
93     * by $format
94     *
95     * The contents of the $data array depends on what the handler() function above
96     * created
97     *
98     * @param string $format output format being rendered
99     * @param Doku_Renderer $renderer the current renderer object
100     * @param array $data data created by handler()
101     * @return  boolean                 rendered correctly? (however, returned value is not used at the moment)
102     */
103    abstract public function render($format, Doku_Renderer $renderer, $data);
104
105    /**
106     *  There should be no need to override this function
107     *
108     * @param string $mode
109     * @return bool
110     */
111    public function accepts($mode)
112    {
113
114        if (!$this->allowedModesSetup) {
115            global $PARSER_MODES;
116
117            $allowedModeTypes = $this->getAllowedTypes();
118            foreach ($allowedModeTypes as $mt) {
119                $this->allowedModes = array_merge($this->allowedModes, $PARSER_MODES[$mt]);
120            }
121
122            $idx = array_search(substr(get_class($this), 7), (array)$this->allowedModes, true);
123            if ($idx !== false) {
124                unset($this->allowedModes[$idx]);
125            }
126            $this->allowedModesSetup = true;
127        }
128
129        return parent::accepts($mode);
130    }
131}
132