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