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