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