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