xref: /dokuwiki/inc/parser/renderer.php (revision b288bdefdcc243374f385a3adaea329256016e4c)
1<?php
2/**
3 * Renderer output base class
4 *
5 * @author Harry Fuecks <hfuecks@gmail.com>
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
8if(!defined('DOKU_INC')) die('meh.');
9require_once DOKU_INC . 'inc/plugin.php';
10require_once DOKU_INC . 'inc/pluginutils.php';
11
12/**
13 * An empty renderer, produces no output
14 *
15 * Inherits from DokuWiki_Plugin for giving additional functions to render plugins
16 */
17class Doku_Renderer extends DokuWiki_Plugin {
18    var $info = array(
19        'cache' => true, // may the rendered result cached?
20        'toc'   => true, // render the TOC?
21    );
22
23    var $doc = '';
24
25    // keep some config options
26    var $acronyms = array();
27    var $smileys = array();
28    var $badwords = array();
29    var $entities = array();
30    var $interwiki = array();
31
32    // allows renderer to be used again, clean out any per-use values
33    function reset() {
34    }
35
36    function nocache() {
37        $this->info['cache'] = false;
38    }
39
40    function notoc() {
41        $this->info['toc'] = false;
42    }
43
44    /**
45     * Returns the format produced by this renderer.
46     *
47     * Has to be overidden by decendend classes
48     */
49    function getFormat(){
50        trigger_error('getFormat() not implemented in '.get_class($this), E_USER_WARNING);
51    }
52
53    /**
54     * Allow the plugin to prevent DokuWiki from reusing an instance
55     *
56     * @return bool   false if the plugin has to be instantiated
57     */
58    function isSingleton() {
59        return false;
60    }
61
62
63    //handle plugin rendering
64    function plugin($name,$data){
65        $plugin = plugin_load('syntax',$name);
66        if($plugin != null){
67            $plugin->render($this->getFormat(),$this,$data);
68        }
69    }
70
71    /**
72     * handle nested render instructions
73     * this method (and nest_close method) should not be overloaded in actual renderer output classes
74     */
75    function nest($instructions) {
76
77        foreach ( $instructions as $instruction ) {
78            // execute the callback against ourself
79            if (method_exists($this,$instruction[0])) {
80                call_user_func_array(array($this, $instruction[0]), $instruction[1] ? $instruction[1] : array());
81            }
82        }
83    }
84
85    // dummy closing instruction issued by Doku_Handler_Nest, normally the syntax mode should
86    // override this instruction when instantiating Doku_Handler_Nest - however plugins will not
87    // be able to - as their instructions require data.
88    function nest_close() {}
89
90    function document_start() {}
91
92    function document_end() {}
93
94    function render_TOC() { return ''; }
95
96    function toc_additem($id, $text, $level) {}
97
98    function header($text, $level, $pos) {}
99
100    function section_open($level) {}
101
102    function section_close() {}
103
104    function cdata($text) {}
105
106    function p_open() {}
107
108    function p_close() {}
109
110    function linebreak() {}
111
112    function hr() {}
113
114    function strong_open() {}
115
116    function strong_close() {}
117
118    function emphasis_open() {}
119
120    function emphasis_close() {}
121
122    function underline_open() {}
123
124    function underline_close() {}
125
126    function monospace_open() {}
127
128    function monospace_close() {}
129
130    function subscript_open() {}
131
132    function subscript_close() {}
133
134    function superscript_open() {}
135
136    function superscript_close() {}
137
138    function deleted_open() {}
139
140    function deleted_close() {}
141
142    function footnote_open() {}
143
144    function footnote_close() {}
145
146    function listu_open() {}
147
148    function listu_close() {}
149
150    function listo_open() {}
151
152    function listo_close() {}
153
154    function listitem_open($level) {}
155
156    function listitem_close() {}
157
158    function listcontent_open() {}
159
160    function listcontent_close() {}
161
162    function unformatted($text) {}
163
164    function php($text) {}
165
166    function phpblock($text) {}
167
168    function html($text) {}
169
170    function htmlblock($text) {}
171
172    function preformatted($text) {}
173
174    function quote_open() {}
175
176    function quote_close() {}
177
178    function file($text, $lang = null, $file = null ) {}
179
180    function code($text, $lang = null, $file = null ) {}
181
182    function acronym($acronym) {}
183
184    function smiley($smiley) {}
185
186    function wordblock($word) {}
187
188    function entity($entity) {}
189
190    // 640x480 ($x=640, $y=480)
191    function multiplyentity($x, $y) {}
192
193    function singlequoteopening() {}
194
195    function singlequoteclosing() {}
196
197    function apostrophe() {}
198
199    function doublequoteopening() {}
200
201    function doublequoteclosing() {}
202
203    // $link like 'SomePage'
204    function camelcaselink($link) {}
205
206    function locallink($hash, $name = null) {}
207
208    // $link like 'wiki:syntax', $title could be an array (media)
209    function internallink($link, $title = null) {}
210
211    // $link is full URL with scheme, $title could be an array (media)
212    function externallink($link, $title = null) {}
213
214    function rss ($url,$params) {}
215
216    // $link is the original link - probably not much use
217    // $wikiName is an indentifier for the wiki
218    // $wikiUri is the URL fragment to append to some known URL
219    function interwikilink($link, $title = null, $wikiName, $wikiUri) {}
220
221    // Link to file on users OS, $title could be an array (media)
222    function filelink($link, $title = null) {}
223
224    // Link to a Windows share, , $title could be an array (media)
225    function windowssharelink($link, $title = null) {}
226
227//  function email($address, $title = null) {}
228    function emaillink($address, $name = null) {}
229
230    function internalmedia ($src, $title=null, $align=null, $width=null,
231                            $height=null, $cache=null, $linking=null) {}
232
233    function externalmedia ($src, $title=null, $align=null, $width=null,
234                            $height=null, $cache=null, $linking=null) {}
235
236    function internalmedialink (
237        $src,$title=null,$align=null,$width=null,$height=null,$cache=null
238        ) {}
239
240    function externalmedialink(
241        $src,$title=null,$align=null,$width=null,$height=null,$cache=null
242        ) {}
243
244    function table_open($maxcols = null, $numrows = null, $pos = null){}
245
246    function table_close($pos = null){}
247
248    function tablerow_open(){}
249
250    function tablerow_close(){}
251
252    function tableheader_open($colspan = 1, $align = null, $rowspan = 1){}
253
254    function tableheader_close(){}
255
256    function tablecell_open($colspan = 1, $align = null, $rowspan = 1){}
257
258    function tablecell_close(){}
259
260
261    // util functions follow, you probably won't need to reimplement them
262
263
264    /**
265     * Removes any Namespace from the given name but keeps
266     * casing and special chars
267     *
268     * @author Andreas Gohr <andi@splitbrain.org>
269     */
270    function _simpleTitle($name){
271        global $conf;
272
273        //if there is a hash we use the ancor name only
274        list($name,$hash) = explode('#',$name,2);
275        if($hash) return $hash;
276
277        if($conf['useslash']){
278            $name = strtr($name,';/',';:');
279        }else{
280            $name = strtr($name,';',':');
281        }
282
283        return noNSorNS($name);
284    }
285
286    /**
287     * Resolve an interwikilink
288     */
289    function _resolveInterWiki(&$shortcut,$reference){
290        //get interwiki URL
291        if ( isset($this->interwiki[$shortcut]) ) {
292            $url = $this->interwiki[$shortcut];
293        } else {
294            // Default to Google I'm feeling lucky
295            $url = 'http://www.google.com/search?q={URL}&amp;btnI=lucky';
296            $shortcut = 'go';
297        }
298
299        //split into hash and url part
300        list($reference,$hash) = explode('#',$reference,2);
301
302        //replace placeholder
303        if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){
304            //use placeholders
305            $url = str_replace('{URL}',rawurlencode($reference),$url);
306            $url = str_replace('{NAME}',$reference,$url);
307            $parsed = parse_url($reference);
308            if(!$parsed['port']) $parsed['port'] = 80;
309            $url = str_replace('{SCHEME}',$parsed['scheme'],$url);
310            $url = str_replace('{HOST}',$parsed['host'],$url);
311            $url = str_replace('{PORT}',$parsed['port'],$url);
312            $url = str_replace('{PATH}',$parsed['path'],$url);
313            $url = str_replace('{QUERY}',$parsed['query'],$url);
314        }else{
315            //default
316            $url = $url.rawurlencode($reference);
317        }
318        if($hash) $url .= '#'.rawurlencode($hash);
319
320        return $url;
321    }
322}
323
324
325//Setup VIM: ex: et ts=4 :
326