1<?php
2/**
3 * DokuWiki Plugin json (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Janez Paternoster <janez.paternoster@siol.net>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class syntax_plugin_json_section extends DokuWiki_Syntax_Plugin
15{
16    /**
17     * @return string Syntax mode type
18     */
19    public function getType() {
20        return 'container';
21    }
22
23    /**
24     * What kind of syntax do we allow (optional)
25     */
26    function getAllowedTypes() {
27        return array('container', 'formatting', 'substition', 'protected', 'disabled', 'paragraphs');
28    }
29
30    /**
31     * @return string Paragraph type
32     */
33    public function getPType() {
34        return 'normal';
35    }
36
37    /**
38     * @return int Sort order - Low numbers go before high numbers
39     */
40    public function getSort() {
41        return 140;
42    }
43
44    /**
45     * Connect lookup pattern to lexer.
46     *
47     * @param string $mode Parser mode
48     */
49    public function connectTo($mode) {
50        $this->Lexer->addEntryPattern('\%\$-starti?\s*\([^[\]{}%]*?\)%(?=.*?%\$end\%)', $mode, 'plugin_json_section');
51    }
52
53    public function postConnect() {
54        $this->Lexer->addExitPattern('%\$end%', 'plugin_json_section');
55    }
56
57    /**
58     * Handle matches of the json syntax
59     *
60     * @param string       $match   The match of the syntax
61     * @param int          $state   The state of the handler
62     * @param int          $pos     The position in the document
63     * @param Doku_Handler $handler The handler
64     *
65     * @return array Data for the renderer
66     */
67    public function handle($match, $state, $pos, Doku_Handler $handler) {
68        $data = ['state' => $state];
69        $json_o = $this->loadHelper('json');
70        switch ($state) {
71            case DOKU_LEXER_ENTER:
72                preg_match('/^%\$-start(i)?\s*\((.*)\)%/s', $match, $mt);
73                $data['filter'] = $json_o->parse_filter($mt[2]);
74                $data['inline'] = $mt[1];
75                break;
76
77            case DOKU_LEXER_UNMATCHED:
78                $data['match'] = $match;
79                break;
80
81            case DOKU_LEXER_EXIT:
82                break;
83        }
84
85        return $data;
86    }
87
88    /**
89     * Render xhtml output or metadata
90     *
91     * @param string        $mode     Renderer mode (supported modes: xhtml)
92     * @param Doku_Renderer $renderer The renderer
93     * @param array         $data     The data from the handler() function
94     *
95     * @return bool If rendering was successful.
96     */
97    public function render($mode, Doku_Renderer $renderer, $data) {
98        static $hidden = false;
99        static $tag = "div";
100        $json_o = $this->loadHelper('json');
101
102        if ($mode === 'xhtml') {
103            switch ($data['state']) {
104                case DOKU_LEXER_ENTER:
105                    $hidden = false;
106                    $var = $json_o->get(array());
107
108                    if(!$json_o->filter($var, $data['filter'])) {
109                        $display = " style='display:none;'";
110                        $hidden = true;
111                    }
112                    else {
113                        $display = '';
114                    }
115
116                    $tag = $data['inline'] ? "span" : "div";
117                    $renderer->doc .= "<$tag$display>";
118                    break;
119
120                case DOKU_LEXER_UNMATCHED:
121                    //this part runs for each unmatched line separtelly. Other dokuwiki matches are excluded, except headres.
122                    if($hidden === false) {
123                        $text = $renderer->_xmlEntities($data['match']);
124                        //find headers
125                        if(preg_match("/^[ \t]*={2,}[^\n]+={2,}[ \t]*$/", $text, $match)) {
126                            $title = trim($match[0]);
127                            $level = 7 - strspn($title,'=');
128                            if($level < 1) $level = 1;
129                            $title = trim($title,'=');
130                            $title = trim($title);
131                            $renderer->header($title, $level, 0);
132                        } else {
133                            $renderer->doc .= $text;
134                        }
135                    }
136                    break;
137
138                case DOKU_LEXER_EXIT:
139                    $renderer->doc .= "</$tag>";
140                    break;
141            }
142            return true;
143        }
144
145        return false;
146    }
147}
148