1<?php
2/**
3 * Comment Syntax support for DokuWiki; preventive syntax component
4 * preventive macro comment in Wiki source text.
5 *
6 * Assume invalid macro directive caused by a typo error as a source comment
7 * in the page, eg. "~~NO CACHE~~" (correct sytax is "~~NOCACHE~~").
8 * You may disable the directive temporary by intentional typo.
9 *
10 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
11 * @author     Satoshi Sahara <sahara.satoshi@gmail.com>
12 */
13class syntax_plugin_commentsyntax_preventive extends DokuWiki_Syntax_Plugin
14{
15    /** syntax type */
16    public function getType()
17    {
18        return 'substition';
19    }
20
21    /** sort number used to determine priority of this mode */
22    public function getSort()
23    {
24        return 9999; // very low priority
25    }
26
27    /**
28     * Connect lookup pattern to lexer
29     */
30    protected $mode, $pattern;
31
32    public function preConnect()
33    {
34        // syntax mode, drop 'syntax_' from class name
35        $this->mode = substr(__CLASS__, 7);
36        // syntax pattern
37        $this->pattern = [
38            5 => '~~[^\n~]+~~',
39        ];
40    }
41
42    public function connectTo($mode)
43    {
44        $this->Lexer->addSpecialPattern($this->pattern[5], $mode, $this->mode);
45    }
46
47    /**
48     * Handle the match
49     */
50    public function handle($match, $state, $pos, Doku_Handler $handler)
51    {
52        global $ID, $ACT;
53        if ($ACT == 'preview') {
54            return $data = $match;
55        } else if ($this->getConf('log_invalid_macro')) {
56            error_log($this->mode.': match='.$match.' |'.$ID);
57        }
58        return false;
59    }
60
61    /**
62     * Create output
63     */
64    public function render($format, Doku_Renderer $renderer, $data)
65    {
66        global $ACT;
67        if ($format == 'xhtml' && $ACT == 'preview') {
68            $renderer->doc .= $renderer->_xmlEntities($data);
69        }
70        return true;
71    }
72}
73