xref: /plugin/combo/syntax/preformatted.php (revision 32b85071e019dd3646a67c17fac4051338e495eb)
1<?php
2
3
4use ComboStrap\PluginUtility;
5
6
7/**
8 * Overwrite {@link \dokuwiki\Parsing\ParserMode\Preformatted}
9 */
10if (!defined('DOKU_INC')) die();
11
12
13class syntax_plugin_combo_preformatted extends DokuWiki_Syntax_Plugin
14{
15
16    const TAG='preformatted';
17
18    /**
19     * Enable or disable preformatted
20     */
21    const CONF_PREFORMATTED_ENABLE = 'preformattedEnable';
22
23    /**
24     * Syntax Type.
25     *
26     * Needs to return one of the mode types defined in $PARSER_MODES in parser.php
27     * @see https://www.dokuwiki.org/devel:syntax_plugins#syntax_types
28     * @see DokuWiki_Syntax_Plugin::getType()
29     */
30    function getType()
31    {
32        return 'container';
33    }
34
35    /**
36     * How DokuWiki will add P element
37     *
38     *  * 'normal' - The plugin can be used inside paragraphs
39     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
40     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
41     *
42     * @see DokuWiki_Syntax_Plugin::getPType()
43     */
44    function getPType()
45    {
46        return 'normal';
47    }
48
49
50
51    /**
52     * @return array
53     * Allow which kind of plugin inside
54     *
55     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
56     * because we manage self the content and we call self the parser
57     *
58     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
59     */
60    function getAllowedTypes()
61    {
62        return array('baseonly', 'container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
63    }
64
65    function getSort()
66    {
67        /**
68         * Should be less than the preformatted mode
69         * which is 20
70         **/
71        return 19;
72    }
73
74
75    function connectTo($mode)
76    {
77
78        if (!$this->getConf(self::CONF_PREFORMATTED_ENABLE)) {
79
80            $patterns = array('\n  (?![\*\-])', '\n\t(?![\*\-])');
81            foreach ($patterns as $pattern) {
82                $this->Lexer->addEntryPattern($pattern, $mode, PluginUtility::getModeForComponent($this->getPluginComponent()));
83            }
84
85        }
86
87
88    }
89
90
91    function postConnect()
92    {
93        $patterns = array('\n  ', '\n\t');
94        foreach ($patterns as $pattern) {
95            $this->Lexer->addExitPattern($pattern, PluginUtility::getModeForComponent($this->getPluginComponent()));
96        }
97    }
98
99    /**
100     *
101     * The handle function goal is to parse the matched syntax through the pattern function
102     * and to return the result for use in the renderer
103     * This result is always cached until the page is modified.
104     * @param string $match
105     * @param int $state
106     * @param int $pos - byte position in the original source file
107     * @param Doku_Handler $handler
108     * @return array|bool
109     * @see DokuWiki_Syntax_Plugin::handle()
110     *
111     */
112    function handle($match, $state, $pos, Doku_Handler $handler)
113    {
114
115        return PluginUtility::handleAndReturnUnmatchedData(self::TAG,$match,$handler);
116
117    }
118
119    /**
120     * Render the output
121     * @param string $format
122     * @param Doku_Renderer $renderer
123     * @param array $data - what the function handle() return'ed
124     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
125     * @see DokuWiki_Syntax_Plugin::render()
126     *
127     *
128     */
129    function render($format, Doku_Renderer $renderer, $data)
130    {
131        if ($format=="xhtml") {
132            $renderer->doc .= PluginUtility::renderUnmatched($data);
133        }
134        return false;
135    }
136
137
138}
139
140