xref: /dokuwiki/lib/plugins/info/syntax.php (revision 62147bf8b4df365c8f4e0cf74f115edd2d154406)
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, &$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, &$renderer, $data) {
60        if($format == 'xhtml'){
61            //handle various info stuff
62            switch ($data[0]){
63                case 'syntaxmodes':
64                    $renderer->doc .= $this->_syntaxmodes_xhtml();
65                    break;
66                case 'syntaxtypes':
67                    $renderer->doc .= $this->_syntaxtypes_xhtml();
68                    break;
69                case 'syntaxplugins':
70                    $this->_plugins_xhtml('syntax', $renderer);
71                    break;
72                case 'adminplugins':
73                    $this->_plugins_xhtml('admin', $renderer);
74                    break;
75                case 'actionplugins':
76                    $this->_plugins_xhtml('action', $renderer);
77                    break;
78                case 'rendererplugins':
79                    $this->_plugins_xhtml('renderer', $renderer);
80                    break;
81                case 'helperplugins':
82                    $this->_plugins_xhtml('helper', $renderer);
83                    break;
84                case 'helpermethods':
85                    $this->_helpermethods_xhtml($renderer);
86                    break;
87                default:
88                    $renderer->doc .= "no info about ".htmlspecialchars($data[0]);
89            }
90            return true;
91        }
92        return false;
93    }
94
95    /**
96     * list all installed plugins
97     *
98     * uses some of the original renderer methods
99     */
100    function _plugins_xhtml($type, Doku_Renderer &$renderer){
101        global $lang;
102        $renderer->doc .= '<ul>';
103
104        $plugins = plugin_list($type);
105        $plginfo = array();
106
107        // remove subparts
108        foreach($plugins as $p){
109            if (!$po =& plugin_load($type,$p)) continue;
110            list($name,$part) = explode('_',$p,2);
111            $plginfo[$name] = $po->getInfo();
112        }
113
114        // list them
115        foreach($plginfo as $info){
116            $renderer->doc .= '<li><div class="li">';
117            $renderer->externallink($info['url'],$info['name']);
118            $renderer->doc .= ' ';
119            $renderer->doc .= '<em>'.$info['date'].'</em>';
120            $renderer->doc .= ' ';
121            $renderer->doc .= $lang['by'];
122            $renderer->doc .= ' ';
123            $renderer->emaillink($info['email'],$info['author']);
124            $renderer->doc .= '<br />';
125            $renderer->doc .= strtr(hsc($info['desc']),array("\n"=>"<br />"));
126            $renderer->doc .= '</div></li>';
127            unset($po);
128        }
129
130        $renderer->doc .= '</ul>';
131    }
132
133    /**
134     * list all installed plugins
135     *
136     * uses some of the original renderer methods
137     */
138    function _helpermethods_xhtml(Doku_Renderer &$renderer){
139        global $lang;
140
141        $plugins = plugin_list('helper');
142        foreach($plugins as $p){
143            if (!$po =& plugin_load('helper',$p)) continue;
144
145            if (!method_exists($po, 'getMethods')) continue;
146            $methods = $po->getMethods();
147            $info = $po->getInfo();
148
149            $hid = $this->_addToTOC($info['name'], 2, $renderer);
150            $doc = '<h2><a name="'.$hid.'" id="'.$hid.'">'.hsc($info['name']).'</a></h2>';
151            $doc .= '<div class="level2">';
152            $doc .= '<p>'.strtr(hsc($info['desc']), array("\n"=>"<br />")).'</p>';
153            $doc .= '<pre class="code">$'.$p." =& plugin_load('helper', '".$p."');</pre>";
154            $doc .= '</div>';
155            foreach ($methods as $method){
156                $title = '$'.$p.'->'.$method['name'].'()';
157                $hid = $this->_addToTOC($title, 3, $renderer);
158                $doc .= '<h3><a name="'.$hid.'" id="'.$hid.'">'.hsc($title).'</a></h3>';
159                $doc .= '<div class="level3">';
160                $doc .= '<div class="table"><table class="inline"><tbody>';
161                $doc .= '<tr><th>Description</th><td colspan="2">'.$method['desc'].
162                    '</td></tr>';
163                if ($method['params']){
164                    $c = count($method['params']);
165                    $doc .= '<tr><th rowspan="'.$c.'">Parameters</th><td>';
166                    $params = array();
167                    foreach ($method['params'] as $desc => $type){
168                        $params[] = hsc($desc).'</td><td>'.hsc($type);
169                    }
170                    $doc .= join($params, '</td></tr><tr><td>').'</td></tr>';
171                }
172                if ($method['return']){
173                    $doc .= '<tr><th>Return value</th><td>'.hsc(key($method['return'])).
174                        '</td><td>'.hsc(current($method['return'])).'</td></tr>';
175                }
176                $doc .= '</tbody></table></div>';
177                $doc .= '</div>';
178            }
179            unset($po);
180
181            $renderer->doc .= $doc;
182        }
183    }
184
185    /**
186     * lists all known syntax types and their registered modes
187     */
188    function _syntaxtypes_xhtml(){
189        global $PARSER_MODES;
190        $doc  = '';
191
192        $doc .= '<div class="table"><table class="inline"><tbody>';
193        foreach($PARSER_MODES as $mode => $modes){
194            $doc .= '<tr>';
195            $doc .= '<td class="leftalign">';
196            $doc .= $mode;
197            $doc .= '</td>';
198            $doc .= '<td class="leftalign">';
199            $doc .= join(', ',$modes);
200            $doc .= '</td>';
201            $doc .= '</tr>';
202        }
203        $doc .= '</tbody></table></div>';
204        return $doc;
205    }
206
207    /**
208     * lists all known syntax modes and their sorting value
209     */
210    function _syntaxmodes_xhtml(){
211        $modes = p_get_parsermodes();
212        $doc  = '';
213
214        foreach ($modes as $mode){
215            $doc .= $mode['mode'].' ('.$mode['sort'].'), ';
216        }
217        return $doc;
218    }
219
220    /**
221     * Adds a TOC item
222     */
223    function _addToTOC($text, $level, Doku_Renderer_xhtml &$renderer){
224        global $conf;
225
226        if (($level >= $conf['toptoclevel']) && ($level <= $conf['maxtoclevel'])){
227            $hid  = $renderer->_headerToLink($text, 'true');
228            $renderer->toc[] = array(
229                'hid'   => $hid,
230                'title' => $text,
231                'type'  => 'ul',
232                'level' => $level - $conf['toptoclevel'] + 1
233            );
234        }
235        return $hid;
236    }
237}
238
239//Setup VIM: ex: et ts=4 :
240