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