xref: /dokuwiki/lib/plugins/info/syntax.php (revision 3dc2d50c5fda9c4bf708ff4c26e266ba239af62c)
1<?php
2/**
3 * Info Plugin: Displays information about various DokuWiki internals
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Andreas Gohr <andi@splitbrain.org>
7 * @author     Esther Brunner <wikidesign@gmail.com>
8 */
9class syntax_plugin_info extends DokuWiki_Syntax_Plugin {
10
11    /**
12     * What kind of syntax are we?
13     */
14    public function getType(){
15        return 'substition';
16    }
17
18    /**
19     * What about paragraphs?
20     */
21    public function getPType(){
22        return 'block';
23    }
24
25    /**
26     * Where to sort in?
27     */
28    public function getSort(){
29        return 155;
30    }
31
32
33    /**
34     * Connect pattern to lexer
35     */
36    public function connectTo($mode) {
37        $this->Lexer->addSpecialPattern('~~INFO:\w+~~',$mode,'plugin_info');
38    }
39
40    /**
41     * Handle the match
42     *
43     * @param   string       $match   The text matched by the patterns
44     * @param   int          $state   The lexer state for the match
45     * @param   int          $pos     The character position of the matched text
46     * @param   Doku_Handler $handler The Doku_Handler object
47     * @return  array Return an array with all data you want to use in render
48     */
49    public function handle($match, $state, $pos, Doku_Handler $handler){
50        $match = substr($match,7,-2); //strip ~~INFO: from start and ~~ from end
51        return array(strtolower($match));
52    }
53
54    /**
55     * Create output
56     *
57     * @param string $format   string     output format being rendered
58     * @param Doku_Renderer    $renderer  the current renderer object
59     * @param array            $data      data created by handler()
60     * @return  boolean                 rendered correctly?
61     */
62    public function render($format, Doku_Renderer $renderer, $data) {
63        if($format == 'xhtml'){
64            /** @var Doku_Renderer_xhtml $renderer */
65            //handle various info stuff
66            switch ($data[0]){
67                case 'syntaxmodes':
68                    $renderer->doc .= $this->_syntaxmodes_xhtml();
69                    break;
70                case 'syntaxtypes':
71                    $renderer->doc .= $this->_syntaxtypes_xhtml();
72                    break;
73                case 'syntaxplugins':
74                    $this->_plugins_xhtml('syntax', $renderer);
75                    break;
76                case 'adminplugins':
77                    $this->_plugins_xhtml('admin', $renderer);
78                    break;
79                case 'actionplugins':
80                    $this->_plugins_xhtml('action', $renderer);
81                    break;
82                case 'rendererplugins':
83                    $this->_plugins_xhtml('renderer', $renderer);
84                    break;
85                case 'helperplugins':
86                    $this->_plugins_xhtml('helper', $renderer);
87                    break;
88                case 'authplugins':
89                    $this->_plugins_xhtml('auth', $renderer);
90                    break;
91                case 'remoteplugins':
92                    $this->_plugins_xhtml('remote', $renderer);
93                    break;
94                case 'helpermethods':
95                    $this->_helpermethods_xhtml($renderer);
96                    break;
97                default:
98                    $renderer->doc .= "no info about ".htmlspecialchars($data[0]);
99            }
100            return true;
101        }
102        return false;
103    }
104
105    /**
106     * list all installed plugins
107     *
108     * uses some of the original renderer methods
109     *
110     * @param string $type
111     * @param Doku_Renderer_xhtml $renderer
112     */
113    protected function _plugins_xhtml($type, Doku_Renderer_xhtml $renderer){
114        global $lang;
115        $renderer->doc .= '<ul>';
116
117        $plugins = plugin_list($type);
118        $plginfo = array();
119
120        // remove subparts
121        foreach($plugins as $p){
122            if (!$po = plugin_load($type,$p)) continue;
123            list($name,/* $part */) = explode('_',$p,2);
124            $plginfo[$name] = $po->getInfo();
125        }
126
127        // list them
128        foreach($plginfo as $info){
129            $renderer->doc .= '<li><div class="li">';
130            $renderer->externallink($info['url'],$info['name']);
131            $renderer->doc .= ' ';
132            $renderer->doc .= '<em>'.$info['date'].'</em>';
133            $renderer->doc .= ' ';
134            $renderer->doc .= $lang['by'];
135            $renderer->doc .= ' ';
136            $renderer->emaillink($info['email'],$info['author']);
137            $renderer->doc .= '<br />';
138            $renderer->doc .= strtr(hsc($info['desc']),array("\n"=>"<br />"));
139            $renderer->doc .= '</div></li>';
140            unset($po);
141        }
142
143        $renderer->doc .= '</ul>';
144    }
145
146    /**
147     * list all installed plugins
148     *
149     * uses some of the original renderer methods
150     *
151     * @param Doku_Renderer_xhtml $renderer
152     */
153    protected function _helpermethods_xhtml(Doku_Renderer_xhtml $renderer){
154        $plugins = plugin_list('helper');
155        foreach($plugins as $p){
156            if (!$po = plugin_load('helper',$p)) continue;
157
158            if (!method_exists($po, 'getMethods')) continue;
159            $methods = $po->getMethods();
160            $info = $po->getInfo();
161
162            $hid = $this->_addToTOC($info['name'], 2, $renderer);
163            $doc = '<h2><a name="'.$hid.'" id="'.$hid.'">'.hsc($info['name']).'</a></h2>';
164            $doc .= '<div class="level2">';
165            $doc .= '<p>'.strtr(hsc($info['desc']), array("\n"=>"<br />")).'</p>';
166            $doc .= '<pre class="code">$'.$p." = plugin_load('helper', '".$p."');</pre>";
167            $doc .= '</div>';
168            foreach ($methods as $method){
169                $title = '$'.$p.'->'.$method['name'].'()';
170                $hid = $this->_addToTOC($title, 3, $renderer);
171                $doc .= '<h3><a name="'.$hid.'" id="'.$hid.'">'.hsc($title).'</a></h3>';
172                $doc .= '<div class="level3">';
173                $doc .= '<div class="table"><table class="inline"><tbody>';
174                $doc .= '<tr><th>Description</th><td colspan="2">'.$method['desc'].
175                    '</td></tr>';
176                if ($method['params']){
177                    $c = count($method['params']);
178                    $doc .= '<tr><th rowspan="'.$c.'">Parameters</th><td>';
179                    $params = array();
180                    foreach ($method['params'] as $desc => $type){
181                        $params[] = hsc($desc).'</td><td>'.hsc($type);
182                    }
183                    $doc .= join($params, '</td></tr><tr><td>').'</td></tr>';
184                }
185                if ($method['return']){
186                    $doc .= '<tr><th>Return value</th><td>'.hsc(key($method['return'])).
187                        '</td><td>'.hsc(current($method['return'])).'</td></tr>';
188                }
189                $doc .= '</tbody></table></div>';
190                $doc .= '</div>';
191            }
192            unset($po);
193
194            $renderer->doc .= $doc;
195        }
196    }
197
198    /**
199     * lists all known syntax types and their registered modes
200     *
201     * @return string
202     */
203    protected function _syntaxtypes_xhtml(){
204        global $PARSER_MODES;
205        $doc  = '';
206
207        $doc .= '<div class="table"><table class="inline"><tbody>';
208        foreach($PARSER_MODES as $mode => $modes){
209            $doc .= '<tr>';
210            $doc .= '<td class="leftalign">';
211            $doc .= $mode;
212            $doc .= '</td>';
213            $doc .= '<td class="leftalign">';
214            $doc .= join(', ',$modes);
215            $doc .= '</td>';
216            $doc .= '</tr>';
217        }
218        $doc .= '</tbody></table></div>';
219        return $doc;
220    }
221
222    /**
223     * lists all known syntax modes and their sorting value
224     *
225     * @return string
226     */
227    protected function _syntaxmodes_xhtml(){
228        $modes = p_get_parsermodes();
229
230        $compactmodes = array();
231        foreach($modes as $mode){
232            $compactmodes[$mode['sort']][] = $mode['mode'];
233        }
234        $doc  = '';
235        $doc .= '<div class="table"><table class="inline"><tbody>';
236
237        foreach($compactmodes as $sort => $modes){
238            $rowspan = '';
239            if(count($modes) > 1) {
240                $rowspan = ' rowspan="'.count($modes).'"';
241            }
242
243            foreach($modes as $index => $mode) {
244                $doc .= '<tr>';
245                $doc .= '<td class="leftalign">';
246                $doc .= $mode;
247                $doc .= '</td>';
248
249                if($index === 0) {
250                    $doc .= '<td class="rightalign" '.$rowspan.'>';
251                    $doc .= $sort;
252                    $doc .= '</td>';
253                }
254                $doc .= '</tr>';
255            }
256        }
257
258        $doc .= '</tbody></table></div>';
259        return $doc;
260    }
261
262    /**
263     * Adds a TOC item
264     *
265     * @param string $text
266     * @param int $level
267     * @param Doku_Renderer_xhtml $renderer
268     * @return string
269     */
270    protected function _addToTOC($text, $level, Doku_Renderer_xhtml $renderer){
271        global $conf;
272
273        $hid = '';
274        if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){
275            $hid  = $renderer->_headerToLink($text, true);
276            $renderer->toc[] = array(
277                'hid'   => $hid,
278                'title' => $text,
279                'type'  => 'ul',
280                'level' => $level - $conf['toptoclevel'] + 1
281            );
282        }
283        return $hid;
284    }
285}
286
287//Setup VIM: ex: et ts=4 :
288