xref: /template/strap/syntax/comment.php (revision 977ce05d19d8dab0a70c9a27f8da0b7039299e82)
1<?php
2
3
4// must be run within Dokuwiki
5use ComboStrap\PluginUtility;
6use ComboStrap\PageEdit;
7
8if (!defined('DOKU_INC')) die();
9
10/**
11 * Class syntax_plugin_combo_itext
12 * Setting text attributes on words
13 *
14 */
15class syntax_plugin_combo_comment extends DokuWiki_Syntax_Plugin
16{
17
18    const TAG = "comment";
19
20    /**
21     * If yes, the comment are in the rendering
22     */
23    const CONF_OUTPUT_COMMENT = "outputComment";
24    const CANONICAL = "comment";
25
26    private static function shouldPrint($content): bool
27    {
28        /**
29         * {@link PluginUtility::startSection()} }
30         * section edit added at {@link action_plugin_combo_headingpostprocessing}
31         * if there is no heading at all
32         */
33        $normalizedContent = trim($content);
34        if (strpos($normalizedContent, PageEdit::SEC_EDIT_PREFIX)===0){
35            return true;
36        }
37        $confValue = PluginUtility::getConfValue(self::CONF_OUTPUT_COMMENT, 0);
38        if ($confValue === 1) {
39            return true;
40        }
41        return false;
42    }
43
44    /**
45     * Syntax Type.
46     *
47     * Needs to return one of the mode types defined in {@link $PARSER_MODES} in parser.php
48     * @see DokuWiki_Syntax_Plugin::getType()
49     */
50    function getType()
51    {
52        return 'formatting';
53    }
54
55    /**
56     * How Dokuwiki will add P element
57     *
58     *  * 'normal' - The plugin can be used inside paragraphs
59     *  * 'block'  - Open paragraphs need to be closed before plugin output - block should not be inside paragraphs
60     *  * 'stack'  - Special case. Plugin wraps other paragraphs. - Stacks can contain paragraphs
61     *
62     * @see DokuWiki_Syntax_Plugin::getPType()
63     */
64    function getPType()
65    {
66        return 'normal';
67    }
68
69    /**
70     * @return array
71     * Allow which kind of plugin inside
72     *
73     * No one of array('baseonly','container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs')
74     * because we manage self the content and we call self the parser
75     *
76     * Return an array of one or more of the mode types {@link $PARSER_MODES} in Parser.php
77     */
78    function getAllowedTypes()
79    {
80        return array();
81    }
82
83
84    function getSort()
85    {
86        return 201;
87    }
88
89
90    function connectTo($mode)
91    {
92
93
94        $this->Lexer->addEntryPattern("<!--", $mode, PluginUtility::getModeFromTag($this->getPluginComponent()));
95
96    }
97
98
99    function postConnect()
100    {
101
102        $this->Lexer->addExitPattern("-->", PluginUtility::getModeFromTag($this->getPluginComponent()));
103
104
105    }
106
107    function handle($match, $state, $pos, Doku_Handler $handler)
108    {
109
110        switch ($state) {
111
112            case DOKU_LEXER_ENTER :
113            case DOKU_LEXER_EXIT :
114                return array(
115                    PluginUtility::STATE => $state
116                );
117
118            case DOKU_LEXER_UNMATCHED :
119                return PluginUtility::handleAndReturnUnmatchedData(self::TAG, $match, $handler);
120
121        }
122        return array();
123
124    }
125
126    /**
127     * Render the output
128     * @param string $format
129     * @param Doku_Renderer $renderer
130     * @param array $data - what the function handle() return'ed
131     * @return boolean - rendered correctly? (however, returned value is not used at the moment)
132     * @see DokuWiki_Syntax_Plugin::render()
133     *
134     *
135     */
136    function render($format, Doku_Renderer $renderer, $data): bool
137    {
138
139        $print = self::shouldPrint($data[PluginUtility::PAYLOAD]);
140
141        if ($format === "xhtml" && $print) {
142            if ($data[PluginUtility::STATE] === DOKU_LEXER_UNMATCHED) {
143                $renderer->doc .= "<!--" . PluginUtility::renderUnmatched($data) . "-->";
144            }
145        }
146        // unsupported $mode
147        return false;
148
149    }
150
151
152}
153
154