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