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