xref: /dokuwiki/inc/parser/renderer.php (revision 8daa2c9f98ef02857c1d92f2f226288c313146a7)
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     *
65     * @param string $name Plugin name
66     * @param mixed  $data custom data set by handler
67     * @param string $state matched state if any
68     * @param string $match raw matched syntax
69     */
70    function plugin($name,$data,$state='',$match=''){
71        $plugin = plugin_load('syntax',$name);
72        if($plugin != null){
73            $plugin->render($this->getFormat(),$this,$data);
74        }
75    }
76
77    /**
78     * handle nested render instructions
79     * this method (and nest_close method) should not be overloaded in actual renderer output classes
80     */
81    function nest($instructions) {
82
83        foreach ( $instructions as $instruction ) {
84            // execute the callback against ourself
85            if (method_exists($this,$instruction[0])) {
86                call_user_func_array(array($this, $instruction[0]), $instruction[1] ? $instruction[1] : array());
87            }
88        }
89    }
90
91    // dummy closing instruction issued by Doku_Handler_Nest, normally the syntax mode should
92    // override this instruction when instantiating Doku_Handler_Nest - however plugins will not
93    // be able to - as their instructions require data.
94    function nest_close() {}
95
96    function document_start() {}
97
98    function document_end() {}
99
100    function render_TOC() { return ''; }
101
102    function toc_additem($id, $text, $level) {}
103
104    function header($text, $level, $pos) {}
105
106    function section_open($level) {}
107
108    function section_close() {}
109
110    function cdata($text) {}
111
112    function p_open() {}
113
114    function p_close() {}
115
116    function linebreak() {}
117
118    function hr() {}
119
120    function strong_open() {}
121
122    function strong_close() {}
123
124    function emphasis_open() {}
125
126    function emphasis_close() {}
127
128    function underline_open() {}
129
130    function underline_close() {}
131
132    function monospace_open() {}
133
134    function monospace_close() {}
135
136    function subscript_open() {}
137
138    function subscript_close() {}
139
140    function superscript_open() {}
141
142    function superscript_close() {}
143
144    function deleted_open() {}
145
146    function deleted_close() {}
147
148    function footnote_open() {}
149
150    function footnote_close() {}
151
152    function listu_open() {}
153
154    function listu_close() {}
155
156    function listo_open() {}
157
158    function listo_close() {}
159
160    function listitem_open($level) {}
161
162    function listitem_close() {}
163
164    function listcontent_open() {}
165
166    function listcontent_close() {}
167
168    function unformatted($text) {}
169
170    function php($text) {}
171
172    function phpblock($text) {}
173
174    function html($text) {}
175
176    function htmlblock($text) {}
177
178    function preformatted($text) {}
179
180    function quote_open() {}
181
182    function quote_close() {}
183
184    function file($text, $lang = null, $file = null ) {}
185
186    function code($text, $lang = null, $file = null ) {}
187
188    function acronym($acronym) {}
189
190    function smiley($smiley) {}
191
192    function wordblock($word) {}
193
194    function entity($entity) {}
195
196    // 640x480 ($x=640, $y=480)
197    function multiplyentity($x, $y) {}
198
199    function singlequoteopening() {}
200
201    function singlequoteclosing() {}
202
203    function apostrophe() {}
204
205    function doublequoteopening() {}
206
207    function doublequoteclosing() {}
208
209    // $link like 'SomePage'
210    function camelcaselink($link) {}
211
212    function locallink($hash, $name = null) {}
213
214    // $link like 'wiki:syntax', $title could be an array (media)
215    function internallink($link, $title = null) {}
216
217    // $link is full URL with scheme, $title could be an array (media)
218    function externallink($link, $title = null) {}
219
220    function rss ($url,$params) {}
221
222    // $link is the original link - probably not much use
223    // $wikiName is an indentifier for the wiki
224    // $wikiUri is the URL fragment to append to some known URL
225    function interwikilink($link, $title = null, $wikiName, $wikiUri) {}
226
227    // Link to file on users OS, $title could be an array (media)
228    function filelink($link, $title = null) {}
229
230    // Link to a Windows share, , $title could be an array (media)
231    function windowssharelink($link, $title = null) {}
232
233//  function email($address, $title = null) {}
234    function emaillink($address, $name = null) {}
235
236    function internalmedia ($src, $title=null, $align=null, $width=null,
237                            $height=null, $cache=null, $linking=null) {}
238
239    function externalmedia ($src, $title=null, $align=null, $width=null,
240                            $height=null, $cache=null, $linking=null) {}
241
242    function internalmedialink (
243        $src,$title=null,$align=null,$width=null,$height=null,$cache=null
244        ) {}
245
246    function externalmedialink(
247        $src,$title=null,$align=null,$width=null,$height=null,$cache=null
248        ) {}
249
250    function table_open($maxcols = null, $numrows = null, $pos = null){}
251
252    function table_close($pos = null){}
253
254    function tablerow_open(){}
255
256    function tablerow_close(){}
257
258    function tableheader_open($colspan = 1, $align = null, $rowspan = 1){}
259
260    function tableheader_close(){}
261
262    function tablecell_open($colspan = 1, $align = null, $rowspan = 1){}
263
264    function tablecell_close(){}
265
266
267    // util functions follow, you probably won't need to reimplement them
268
269
270    /**
271     * Removes any Namespace from the given name but keeps
272     * casing and special chars
273     *
274     * @author Andreas Gohr <andi@splitbrain.org>
275     */
276    function _simpleTitle($name){
277        global $conf;
278
279        //if there is a hash we use the ancor name only
280        list($name,$hash) = explode('#',$name,2);
281        if($hash) return $hash;
282
283        if($conf['useslash']){
284            $name = strtr($name,';/',';:');
285        }else{
286            $name = strtr($name,';',':');
287        }
288
289        return noNSorNS($name);
290    }
291
292    /**
293     * Resolve an interwikilink
294     */
295    function _resolveInterWiki(&$shortcut,$reference){
296        //get interwiki URL
297        if ( isset($this->interwiki[$shortcut]) ) {
298            $url = $this->interwiki[$shortcut];
299        } else {
300            // Default to Google I'm feeling lucky
301            $url = 'http://www.google.com/search?q={URL}&amp;btnI=lucky';
302            $shortcut = 'go';
303        }
304
305        //split into hash and url part
306        list($reference,$hash) = explode('#',$reference,2);
307
308        //replace placeholder
309        if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){
310            //use placeholders
311            $url = str_replace('{URL}',rawurlencode($reference),$url);
312            $url = str_replace('{NAME}',$reference,$url);
313            $parsed = parse_url($reference);
314            if(!$parsed['port']) $parsed['port'] = 80;
315            $url = str_replace('{SCHEME}',$parsed['scheme'],$url);
316            $url = str_replace('{HOST}',$parsed['host'],$url);
317            $url = str_replace('{PORT}',$parsed['port'],$url);
318            $url = str_replace('{PATH}',$parsed['path'],$url);
319            $url = str_replace('{QUERY}',$parsed['query'],$url);
320        }else{
321            //default
322            $url = $url.rawurlencode($reference);
323        }
324        if($hash) $url .= '#'.rawurlencode($hash);
325
326        return $url;
327    }
328}
329
330
331//Setup VIM: ex: et ts=4 :
332