1<?php
2
3/**
4 * DokuWiki Plugin navpath (Syntax Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Olaf Steinicke <olaf.steinicke@ascilium.com>
8 */
9
10// must be run within Dokuwiki
11if (!defined('DOKU_INC')) {
12    die();
13}
14
15class syntax_plugin_navpath extends DokuWiki_Syntax_Plugin
16{
17    private $split = '';
18
19    /**
20     * @return string Plugin Info
21     */
22    public function getInfo()
23    {
24        return confToHash(dirname(__FILE__) . '/plugin.info.txt');
25    }
26
27    /**
28     * @return string Syntax mode type
29     */
30    public function getType()
31    {
32        return 'formatting';
33    }
34
35    public function getAllowedTypes()
36    {
37        return array('formatting', 'substitution', 'disabled');
38    }
39    /**
40     * @return string Paragraph type
41     */
42    public function getPType()
43    {
44        return 'normal';
45    }
46
47    /**
48     * @return int Sort order - Low numbers go before high numbers
49     */
50    public function getSort()
51    {
52        return 999;
53    }
54
55    /**
56     * Connect lookup pattern to lexer.
57     *
58     * @param string $mode Parser mode
59     */
60    public function connectTo($mode)
61    {
62        $this->Lexer->addEntryPattern('<navpath.*?>(?=.*?<\/navpath>)', $mode, 'plugin_navpath');
63    }
64
65    public function postConnect()
66    {
67        $this->Lexer->addExitPattern('</navpath>', 'plugin_navpath');
68    }
69
70    /**
71     * Handle matches of the navpath syntax
72     *
73     * @param string       $match   The match of the syntax
74     * @param int          $state   The state of the handler
75     * @param int          $pos     The position in the document
76     * @param Doku_Handler $handler The handler
77     *
78     * @return array Data for the renderer
79     */
80    public function handle($match, $state, $pos, Doku_Handler $handler)
81    {
82        switch ($state) {
83            case DOKU_LEXER_ENTER: {
84                    $split = preg_split("/split=/", substr($match, 8, -1), 2)[1];
85                    $this->split = strlen($split) > 0 ? $split : ',';
86                    return array($state, '');
87                }
88            case DOKU_LEXER_UNMATCHED: {
89                    $data = explode($this->split, $match);
90                    return array($state, $data);
91                }
92            case DOKU_LEXER_EXIT: {
93                    return array($state, '');
94                }
95        }
96        return false;
97    }
98
99    /**
100     * Render xhtml output or metadata
101     *
102     * @param string        $mode     Renderer mode (supported modes: xhtml)
103     * @param Doku_Renderer $renderer The renderer
104     * @param array         $data     The data from the handler() function
105     *
106     * @return bool If rendering was successful.
107     */
108    public function render($mode, Doku_Renderer $renderer, $indata)
109    {
110        if ($mode !== 'xhtml') {
111            return false;
112        }
113
114        list($state, $data) = $indata;
115        switch ($state) {
116            case DOKU_LEXER_ENTER:
117                $renderer->doc .= '<span class="navpath"><img src="lib/plugins/navpath/images/nav_start.gif">&nbsp;';
118                break;
119
120            case DOKU_LEXER_UNMATCHED:
121                foreach ($data as $key => $value) {
122                    if ($key !== 0) {
123                        $renderer->doc .= '&nbsp;<img src="lib/plugins/navpath/images/nav_step.gif">&nbsp;';
124                    }
125                    $renderer->emphasis_open();
126                    $renderer->strong_open();
127                    $renderer->doc .= $renderer->_xmlEntities($value);
128                    $renderer->strong_close();
129                    $renderer->emphasis_close();
130                }
131                break;
132
133            case DOKU_LEXER_EXIT:
134                $renderer->doc .= '&nbsp;<img src="lib/plugins/navpath/images/nav_end.gif"></span>';
135                break;
136        }
137        return true;
138    }
139}
140