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