1<?php
2/**
3 * Comment Syntax support for DokuWiki; html comment syntax component
4 * allows HTML comments to be retained in the output. The HTML comment will not
5 * rendered by the browser, but can be viewed with “View source code” command.
6 *
7 * Note: adopted original HTML Comment Plugin by Christopher Arndt
8 *       https://www.dokuwiki.org/plugin:htmlcomment
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Christopher Arndt <chris@chrisarndt.de>
12 * @author     Danny Lin <danny0838@gmail.com>
13 * @author     Satoshi Sahara <sahara.satoshi@gmail.com>
14 */
15class syntax_plugin_commentsyntax_htmlcomment extends DokuWiki_Syntax_Plugin
16{
17    /** syntax type */
18    public function getType()
19    {
20        return 'substition';
21    }
22
23    /** sort number used to determine priority of this mode */
24    public function getSort()
25    {
26        return 325;
27    }
28
29    /**
30     * Connect lookup pattern to lexer
31     */
32    protected $mode, $pattern;
33
34    public function preConnect()
35    {
36        // syntax mode, drop 'syntax_' from class name
37        $this->mode = substr(__CLASS__, 7);
38        // syntax pattern
39        $this->pattern = [
40            5 => '<\!--.*?-->',
41        ];
42    }
43
44    public function connectTo($mode)
45    {
46        $this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
47    }
48
49    /**
50     * Handle the match
51     */
52    public function handle($match, $state, $pos, Doku_Handler $handler)
53    {
54        if ($state == DOKU_LEXER_SPECIAL) {
55             // strip <!-- from start and --> from end
56            return array($state, substr($match, 4, -3));
57        }
58        return false;
59    }
60
61    /**
62     * Create output
63     */
64    public function render($format, Doku_Renderer $renderer, $data)
65    {
66        if ($format == 'xhtml') {
67            list($state, $comment) = $data;
68            if ($state == DOKU_LEXER_SPECIAL) {
69                $renderer->doc .= '<!--'.$comment.'-->';
70            }
71            return true;
72        }
73        return true;
74    }
75}
76