xref: /dokuwiki/inc/parser/xhtml.php (revision 6037be5aa20f17baed31d2f061481c3a31341ef8)
1<?php
2/**
3 * Renderer for XHTML output
4 *
5 * @author Harry Fuecks <hfuecks@gmail.com>
6 * @author Andreas Gohr <andi@splitbrain.org>
7 */
8
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10
11if ( !defined('DOKU_LF') ) {
12    // Some whitespace to help View > Source
13    define ('DOKU_LF',"\n");
14}
15
16if ( !defined('DOKU_TAB') ) {
17    // Some whitespace to help View > Source
18    define ('DOKU_TAB',"\t");
19}
20
21require_once DOKU_INC . 'inc/parser/renderer.php';
22require_once DOKU_INC . 'inc/pluginutils.php';
23
24/**
25 * The Renderer
26 */
27class Doku_Renderer_xhtml extends Doku_Renderer {
28
29    var $doc = '';
30
31    var $headers = array();
32
33    var $footnotes = array();
34
35    var $acronyms = array();
36    var $smileys = array();
37    var $badwords = array();
38    var $entities = array();
39    var $interwiki = array();
40
41    var $lastsec = 0;
42
43    var $store = '';
44
45    function document_start() {
46    }
47
48    function document_end() {
49        // add button for last section if any and more than one
50        if($this->lastsec > 1) $this->_secedit($this->lastsec,'');
51
52        if ( count ($this->footnotes) > 0 ) {
53            $this->doc .= '<div class="footnotes">'.DOKU_LF;
54            foreach ( $this->footnotes as $footnote ) {
55                $this->doc .= $footnote;
56            }
57            $this->doc .= '</div>'.DOKU_LF;
58        }
59    }
60
61    //handles plugin rendering
62    function plugin($name,$data){
63        $plugin = null;
64        if(plugin_load('syntax',$name,$plugin)){
65            $plugin->render('xhtml',$this,$data);
66        }
67    }
68
69    function toc_open() {
70        global $lang;
71        $this->doc .= '<div class="toc">'.DOKU_LF;
72        $this->doc .= '<div class="tocheader">';
73/*      The (+/-) version will work too but needs some CSS tweaking to look fine */
74/*      $this->doc .= ' <script type="text/javascript">showTocToggle("+","-")';*/
75        $this->doc .=  '<script type="text/javascript">showTocToggle(';
76        $this->doc .=    '\'<img src="'.DOKU_BASE.'images/arrow_down.png">\',';
77        $this->doc .=    '\'<img src="'.DOKU_BASE.'images/arrow_up.png">\')';
78        $this->doc .=  '</script>';
79        $this->doc .= $lang['toc'];
80        $this->doc .= '</div>'.DOKU_LF;
81        $this->doc .= '<div id="tocinside">'.DOKU_LF;
82    }
83
84    function tocbranch_open($level) {
85        $this->doc .= '<ul class="toc">'.DOKU_LF;
86    }
87
88    function tocitem_open($level, $empty = FALSE) {
89        if ( !$empty ) {
90            $this->doc .= '<li class="level'.$level.'">';
91        } else {
92            $this->doc .= '<li class="clear">';
93        }
94    }
95
96    function tocelement($level, $title) {
97        $this->doc .= '<span class="li"><a href="#'.$this->_headerToLink($title).'" class="toc">';
98        $this->doc .= $this->_xmlEntities($title);
99        $this->doc .= '</a></span>';
100    }
101
102    function tocitem_close($level) {
103        $this->doc .= '</li>'.DOKU_LF;
104    }
105
106    function tocbranch_close($level) {
107        $this->doc .= '</ul>'.DOKU_LF;
108    }
109
110    function toc_close() {
111        $this->doc .= '</div>'.DOKU_LF.'</div>'.DOKU_LF;
112    }
113
114    function header($text, $level, $pos) {
115        global $conf;
116        //handle section editing
117        if($level <= $conf['maxseclevel']){
118            // add button for last section if any
119            if($this->lastsec) $this->_secedit($this->lastsec,$pos-1);
120            // remember current position
121            $this->lastsec = $pos;
122        }
123
124        $this->doc .= DOKU_LF.'<a name="'.$this->_headerToLink($text).'"></a><h'.$level.'>';
125        $this->doc .= $this->_xmlEntities($text);
126        $this->doc .= "</h$level>".DOKU_LF;
127    }
128
129    function section_open($level) {
130        $this->doc .= "<div class=\"level$level\">".DOKU_LF;
131    }
132
133    function section_close() {
134        $this->doc .= DOKU_LF.'</div>'.DOKU_LF;
135    }
136
137    function cdata($text) {
138        $this->doc .= $this->_xmlEntities($text);
139    }
140
141    function p_open() {
142        $this->doc .= DOKU_LF.'<p>'.DOKU_LF;
143    }
144
145    function p_close() {
146        $this->doc .= DOKU_LF.'</p>'.DOKU_LF;
147    }
148
149    function linebreak() {
150        $this->doc .= '<br/>'.DOKU_LF;
151    }
152
153    function hr() {
154        $this->doc .= '<hr noshade="noshade" size="1" />'.DOKU_LF;
155    }
156
157    function strong_open() {
158        $this->doc .= '<strong>';
159    }
160
161    function strong_close() {
162        $this->doc .= '</strong>';
163    }
164
165    function emphasis_open() {
166        $this->doc .= '<em>';
167    }
168
169    function emphasis_close() {
170        $this->doc .= '</em>';
171    }
172
173    function underline_open() {
174        $this->doc .= '<u>';
175    }
176
177    function underline_close() {
178        $this->doc .= '</u>';
179    }
180
181    function monospace_open() {
182        $this->doc .= '<code>';
183    }
184
185    function monospace_close() {
186        $this->doc .= '</code>';
187    }
188
189    function subscript_open() {
190        $this->doc .= '<sub>';
191    }
192
193    function subscript_close() {
194        $this->doc .= '</sub>';
195    }
196
197    function superscript_open() {
198        $this->doc .= '<sup>';
199    }
200
201    function superscript_close() {
202        $this->doc .= '</sup>';
203    }
204
205    function deleted_open() {
206        $this->doc .= '<del>';
207    }
208
209    function deleted_close() {
210        $this->doc .= '</del>';
211    }
212
213    /**
214     * Callback for footnote start syntax
215     *
216     * All following content will go to the footnote instead of
217     * the document. To achive this the previous rendered content
218     * is moved to $store and $doc is cleared
219     *
220     * @author Andreas Gohr <andi@splitbrain.org>
221     */
222    function footnote_open() {
223        #$id = $this->_newFootnoteId();
224        $id = count($this->footnotes)+1;
225        $this->doc .= '<a href="#fn'.$id.'" name="fnt'.$id.'" class="fn_top">'.$id.')</a>';
226
227        // move current content to store and record footnote
228        $this->store = $this->doc;
229        $this->doc   = '';
230
231        $this->doc .= '<div class="fn">';
232        $this->doc .= '<a href="#fnt'.$id.'" name="fn'.$id.'" class="fn_bot">';
233        $this->doc .= $id.')</a> '.DOKU_LF;
234    }
235
236    /**
237     * Callback for footnote end syntax
238     *
239     * All rendered content is moved to the $footnotes array and the old
240     * content is restored from $store again
241     *
242     * @author Andreas Gohr
243     */
244    function footnote_close() {
245        $this->doc .= '</div>' . DOKU_LF;
246
247        // put recorded footnote into the stack and restore old content
248        $this->footnotes[count($this->footnotes)] = $this->doc;
249        $this->doc = $this->store;
250        $this->store = '';
251    }
252
253    function listu_open() {
254        $this->doc .= '<ul>'.DOKU_LF;
255    }
256
257    function listu_close() {
258        $this->doc .= '</ul>'.DOKU_LF;
259    }
260
261    function listo_open() {
262        $this->doc .= '<ol>'.DOKU_LF;
263    }
264
265    function listo_close() {
266        $this->doc .= '</ol>'.DOKU_LF;
267    }
268
269    function listitem_open($level) {
270        $this->doc .= '<li class="level'.$level.'">';
271    }
272
273    function listitem_close() {
274        $this->doc .= '</li>'.DOKU_LF;
275    }
276
277    function listcontent_open() {
278        $this->doc .= '<span class="li">';
279    }
280
281    function listcontent_close() {
282        $this->doc .= '</span>'.DOKU_LF;
283    }
284
285    function unformatted($text) {
286        $this->doc .= $this->_xmlEntities($text);
287    }
288
289    /**
290     * Execute PHP code if allowed
291     *
292     * @author Andreas Gohr <andi@splitbrain.org>
293     */
294    function php($text) {
295        global $conf;
296        if($conf['phpok']){
297            ob_start();
298            eval($text);
299            $this->doc .= ob_get_contents();
300            ob_end_clean();
301        }else{
302            $this->file($text);
303        }
304    }
305
306    /**
307     * Insert HTML if allowed
308     *
309     * @author Andreas Gohr <andi@splitbrain.org>
310     */
311    function html($text) {
312        global $conf;
313        if($conf['htmlok']){
314          $this->doc .= $text;
315        }else{
316          $this->file($text);
317        }
318    }
319
320    function preformatted($text) {
321        $this->doc .= '<pre class="code">' . $this->_xmlEntities($text) . '</pre>'. DOKU_LF;
322    }
323
324    function file($text) {
325        $this->doc .= '<pre class="file">' . $this->_xmlEntities($text). '</pre>'. DOKU_LF;
326    }
327
328    function quote_open() {
329        $this->doc .= '<blockquote>'.DOKU_LF;
330    }
331
332    function quote_close() {
333        $this->doc .= '</blockquote>'.DOKU_LF;
334    }
335
336    /**
337     * Callback for code text
338     *
339     * Uses GeSHi to highlight language syntax
340     *
341     * @author Andreas Gohr <andi@splitbrain.org>
342     */
343    function code($text, $language = NULL) {
344        global $conf;
345
346        if ( is_null($language) ) {
347            $this->preformatted($text);
348        } else {
349            //strip leading blank line
350            $text = preg_replace('/^\s*?\n/','',$text);
351            // Handle with Geshi here
352            require_once(DOKU_INC . 'inc/geshi.php');
353            $geshi = new GeSHi($text, strtolower($language), DOKU_INC . 'inc/geshi');
354            $geshi->set_encoding('utf-8');
355            $geshi->enable_classes();
356            $geshi->set_header_type(GESHI_HEADER_PRE);
357            $geshi->set_overall_class('code');
358            $geshi->set_link_target($conf['target']['extern']);
359
360            $text = $geshi->parse_code();
361            $this->doc .= $text;
362        }
363    }
364
365    function acronym($acronym) {
366
367        if ( array_key_exists($acronym, $this->acronyms) ) {
368
369            $title = $this->_xmlEntities($this->acronyms[$acronym]);
370
371            $this->doc .= '<acronym title="'.$title
372                .'">'.$this->_xmlEntities($acronym).'</acronym>';
373
374        } else {
375            $this->doc .= $this->_xmlEntities($acronym);
376        }
377    }
378
379    /**
380    */
381    function smiley($smiley) {
382        if ( array_key_exists($smiley, $this->smileys) ) {
383            $title = $this->_xmlEntities($this->smileys[$smiley]);
384            $this->doc .= '<img src="'.DOKU_BASE.'smileys/'.$this->smileys[$smiley].
385                '" align="middle" alt="'.
386                    $this->_xmlEntities($smiley).'" />';
387        } else {
388            $this->doc .= $this->_xmlEntities($smiley);
389        }
390    }
391
392    /**
393    * not used
394    function wordblock($word) {
395        if ( array_key_exists($word, $this->badwords) ) {
396            $this->doc .= '** BLEEP **';
397        } else {
398            $this->doc .= $this->_xmlEntities($word);
399        }
400    }
401    */
402
403    function entity($entity) {
404        if ( array_key_exists($entity, $this->entities) ) {
405            $this->doc .= $this->entities[$entity];
406        } else {
407            $this->doc .= $this->_xmlEntities($entity);
408        }
409    }
410
411    function multiplyentity($x, $y) {
412        $this->doc .= "$x&times;$y";
413    }
414
415    function singlequoteopening() {
416        $this->doc .= "&lsquo;";
417    }
418
419    function singlequoteclosing() {
420        $this->doc .= "&rsquo;";
421    }
422
423    function doublequoteopening() {
424        $this->doc .= "&ldquo;";
425    }
426
427    function doublequoteclosing() {
428        $this->doc .= "&rdquo;";
429    }
430
431    /**
432    */
433    function camelcaselink($link) {
434      $this->internallink($link,$link);
435    }
436
437
438    function locallink($hash, $name = NULL){
439        global $ID;
440        $name  = $this->_getLinkTitle($name, $hash, $isImage);
441        $hash  = $this->_headerToLink($hash);
442        $title = $ID.' &crarr;';
443        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
444        $this->doc .= $name;
445        $this->doc .= '</a>';
446    }
447
448    /**
449     * Render an internal Wiki Link
450     *
451     * $search and $returnonly are not for the renderer but are used
452     * elsewhere - no need to implement them in other renderers
453     *
454     * @author Andreas Gohr <andi@splitbrain.org>
455     */
456    function internallink($id, $name = NULL, $search=NULL,$returnonly=false) {
457        global $conf;
458        global $ID;
459        // default name is based on $id as given
460        $default = $this->_simpleTitle($id);
461        // now first resolve and clean up the $id
462        resolve_pageid(getNS($ID),$id,$exists);
463        $name = $this->_getLinkTitle($name, $default, $isImage, $id);
464        if ( !$isImage ) {
465            if ( $exists ) {
466                $class='wikilink1';
467            } else {
468                $class='wikilink2';
469            }
470        } else {
471            $class='media';
472        }
473
474        //keep hash anchor
475        list($id,$hash) = split('#',$id,2);
476
477        //prepare for formating
478        $link['target'] = $conf['target']['wiki'];
479        $link['style']  = '';
480        $link['pre']    = '';
481        $link['suf']    = '';
482        // highlight link to current page
483        if ($id == $ID) {
484            $link['pre']    = '<span class="curid">';
485            $link['suf']    = '</span>';
486        }
487        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
488        $link['class']  = $class;
489        $link['url']    = wl($id);
490        $link['name']   = $name;
491        $link['title']  = $id;
492        //add search string
493        if($search){
494            ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&amp;s=';
495            $link['url'] .= urlencode($search);
496        }
497
498        //keep hash
499        if($hash) $link['url'].='#'.$hash;
500
501        //output formatted
502        if($returnonly){
503            return $this->_formatLink($link);
504        }else{
505            $this->doc .= $this->_formatLink($link);
506        }
507    }
508
509    function externallink($url, $name = NULL) {
510        global $conf;
511
512        $name = $this->_getLinkTitle($name, $url, $isImage);
513
514        // add protocol on simple short URLs
515        if(substr($url,0,3) == 'ftp' && (substr($url,0,6) != 'ftp://')) $url = 'ftp://'.$url;
516        if(substr($url,0,3) == 'www') $url = 'http://'.$url;
517
518        if ( !$isImage ) {
519            $class='urlextern';
520        } else {
521            $class='media';
522        }
523
524        //prepare for formating
525        $link['target'] = $conf['target']['extern'];
526        $link['style']  = '';
527        $link['pre']    = '';
528        $link['suf']    = '';
529        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
530        $link['class']  = $class;
531        $link['url']    = $url;
532        $link['name']   = $name;
533        $link['title']  = $this->_xmlEntities($url);
534        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
535
536        //output formatted
537        $this->doc .= $this->_formatLink($link);
538    }
539
540    /**
541    */
542    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
543        global $conf;
544
545        $link = array();
546        $link['target'] = $conf['target']['interwiki'];
547        $link['pre']    = '';
548        $link['suf']    = '';
549        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
550        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
551
552        if ( !$isImage ) {
553            $link['class'] = 'interwiki';
554        } else {
555            $link['class'] = 'media';
556        }
557
558        //get interwiki URL
559        if ( isset($this->interwiki[$wikiName]) ) {
560            $url = $this->interwiki[$wikiName];
561        } else {
562            // Default to Google I'm feeling lucky
563            $url = 'http://www.google.com/search?q={URL}&amp;btnI=lucky';
564            $wikiName = 'go';
565        }
566
567        if(!$isImage){
568            //if ico exists set additional style
569            if(@file_exists(DOKU_INC.'interwiki/'.$wikiName.'.png')){
570                $link['style']='background-image: url('.DOKU_BASE.'interwiki/'.$wikiName.'.png)';
571            }elseif(@file_exists(DOKU_INC.'interwiki/'.$wikiName.'.gif')){
572                $link['style']='background-image: url('.DOKU_BASE.'interwiki/'.$wikiName.'.gif)';
573            }
574        }
575
576        //do we stay at the same server? Use local target
577        if( strpos($url,DOKU_URL) === 0 ){
578            $link['target'] = $conf['target']['wiki'];
579        }
580
581        //replace placeholder
582        if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){
583            //use placeholders
584            $url = str_replace('{URL}',urlencode($wikiUri),$url);
585            $url = str_replace('{NAME}',$wikiUri,$url);
586            $parsed = parse_url($wikiUri);
587            if(!$parsed['port']) $parsed['port'] = 80;
588            $url = str_replace('{SCHEME}',$parsed['scheme'],$url);
589            $url = str_replace('{HOST}',$parsed['host'],$url);
590            $url = str_replace('{PORT}',$parsed['port'],$url);
591            $url = str_replace('{PATH}',$parsed['path'],$url);
592            $url = str_replace('{QUERY}',$parsed['query'],$url);
593            $link['url'] = $url;
594        }else{
595            //default
596            $link['url'] = $url.urlencode($wikiUri);
597        }
598
599        $link['title'] = htmlspecialchars($link['url']);
600
601        //output formatted
602        $this->doc .= $this->_formatLink($link);
603    }
604
605    /**
606     */
607    function windowssharelink($url, $name = NULL) {
608        global $conf;
609        global $lang;
610        //simple setup
611        $link['target'] = $conf['target']['windows'];
612        $link['pre']    = '';
613        $link['suf']   = '';
614        $link['style']  = '';
615        //Display error on browsers other than IE
616        $link['more'] = 'onclick="if(document.all == null){alert(\''.
617                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).
618                        '\');}" onkeypress="if(document.all == null){alert(\''.
619                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).'\');}"';
620
621        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
622        if ( !$isImage ) {
623            $link['class'] = 'windows';
624        } else {
625            $link['class'] = 'media';
626        }
627
628
629        $link['title'] = $this->_xmlEntities($url);
630        $url = str_replace('\\','/',$url);
631        $url = 'file:///'.$url;
632        $link['url'] = $url;
633
634        //output formatted
635        $this->doc .= $this->_formatLink($link);
636    }
637
638    function emaillink($address, $name = NULL) {
639        global $conf;
640        //simple setup
641        $link = array();
642        $link['target'] = '';
643        $link['pre']    = '';
644        $link['suf']   = '';
645        $link['style']  = '';
646        $link['more']   = '';
647
648        //we just test for image here - we need to encode the title our self
649        $this->_getLinkTitle($name, $address, $isImage);
650        if ( !$isImage ) {
651            $link['class']='mail';
652        } else {
653            $link['class']='media';
654        }
655
656        //shields up
657        if($conf['mailguard']=='visible'){
658            //the mail name gets some visible encoding
659            $address = str_replace('@',' [at] ',$address);
660            $address = str_replace('.',' [dot] ',$address);
661            $address = str_replace('-',' [dash] ',$address);
662
663            $title   = $this->_xmlEntities($address);
664            if(empty($name)){
665                $name = $this->_xmlEntities($address);
666            }else{
667                $name = $this->_xmlEntities($name);
668            }
669        }elseif($conf['mailguard']=='hex'){
670            //encode every char to a hex entity
671            for ($x=0; $x < strlen($address); $x++) {
672                $encode .= '&#x' . bin2hex($address[$x]).';';
673            }
674            $address = $encode;
675            $title   = $encode;
676            if(empty($name)){
677                $name = $encode;
678            }else{
679                $name = $this->_xmlEntities($name);
680            }
681        }else{
682            //keep address as is
683            $title   = $this->_xmlEntities($address);
684            if(empty($name)){
685                $name = $this->_xmlEntities($address);
686            }else{
687                $name = $this->_xmlEntities($name);
688            }
689        }
690
691        $link['url']   = 'mailto:'.$address;
692        $link['name']  = $name;
693        $link['title'] = $title;
694
695        //output formatted
696        $this->doc .= $this->_formatLink($link);
697    }
698
699    /**
700     * @todo don't add link for flash
701     */
702    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
703                            $height=NULL, $cache=NULL) {
704        global $conf;
705        global $ID;
706        resolve_mediaid(getNS($ID),$src, $exists);
707
708        $link = array();
709        $link['class']  = 'media';
710        $link['style']  = '';
711        $link['pre']    = '';
712        $link['suf']    = '';
713        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
714        $link['target'] = $conf['target']['media'];
715
716        $link['title']  = $this->_xmlEntities($src);
717        $link['url']    = DOKU_BASE.'fetch.php?cache='.$cache.'&amp;media='.urlencode($src);
718        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
719
720
721        //output formatted
722        $this->doc .= $this->_formatLink($link);
723    }
724
725    /**
726     * @todo don't add link for flash
727     */
728    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
729                            $height=NULL, $cache=NULL) {
730        global $conf;
731
732        $link = array();
733        $link['class']  = 'media';
734        $link['style']  = '';
735        $link['pre']    = '';
736        $link['suf']    = '';
737        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
738        $link['target'] = $conf['target']['media'];
739
740        $link['title']  = $this->_xmlEntities($src);
741        $link['url']    = DOKU_BASE.'fetch.php?cache='.$cache.'&amp;media='.urlencode($src);
742        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
743
744
745        //output formatted
746        $this->doc .= $this->_formatLink($link);
747    }
748
749    /**
750     * Renders an RSS feed using Magpie
751     *
752     * @author Andreas Gohr <andi@splitbrain.org>
753     */
754    function rss ($url){
755        global $lang;
756        define('MAGPIE_CACHE_ON', false); //we do our own caching
757        define('MAGPIE_DIR', DOKU_INC.'inc/magpie/');
758        define('MAGPIE_OUTPUT_ENCODING','UTF-8'); //return all feeds as UTF-8
759        require_once(MAGPIE_DIR.'/rss_fetch.inc');
760
761        //disable warning while fetching
762        $elvl = error_reporting(E_ERROR);
763        $rss  = fetch_rss($url);
764        error_reporting($elvl);
765
766        $this->doc .= '<ul class="rss">';
767        if($rss){
768            foreach ($rss->items as $item ) {
769                $this->doc .= '<li>';
770                $this->externallink($item['link'],$item['title']);
771                $this->doc .= '</li>';
772            }
773        }else{
774            $this->doc .= '<li>';
775            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
776            $this->externallink($url);
777            $this->doc .= '</li>';
778        }
779        $this->doc .= '</ul>';
780    }
781
782    // $numrows not yet implemented
783    function table_open($maxcols = NULL, $numrows = NULL){
784        $this->doc .= '<table class="inline">'.DOKU_LF;
785    }
786
787    function table_close(){
788        $this->doc .= '</table>'.DOKU_LF.'<br />'.DOKU_LF;
789    }
790
791    function tablerow_open(){
792        $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
793    }
794
795    function tablerow_close(){
796        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
797    }
798
799    function tableheader_open($colspan = 1, $align = NULL){
800        $this->doc .= '<th';
801        if ( !is_null($align) ) {
802            $this->doc .= ' class="'.$align.'align"';
803        }
804        if ( $colspan > 1 ) {
805            $this->doc .= ' colspan="'.$colspan.'"';
806        }
807        $this->doc .= '>';
808    }
809
810    function tableheader_close(){
811        $this->doc .= '</th>';
812    }
813
814    function tablecell_open($colspan = 1, $align = NULL){
815        $this->doc .= '<td';
816        if ( !is_null($align) ) {
817            $this->doc .= ' class="'.$align.'align"';
818        }
819        if ( $colspan > 1 ) {
820            $this->doc .= ' colspan="'.$colspan.'"';
821        }
822        $this->doc .= '>';
823    }
824
825    function tablecell_close(){
826        $this->doc .= '</td>';
827    }
828
829    //----------------------------------------------------------
830    // Utils
831
832    /**
833     * Build a link
834     *
835     * Assembles all parts defined in $link returns HTML for the link
836     *
837     * @author Andreas Gohr <andi@splitbrain.org>
838     */
839    function _formatLink($link){
840        //make sure the url is XHTML compliant (skip mailto)
841        if(substr($link['url'],0,7) != 'mailto:'){
842            $link['url'] = str_replace('&','&amp;',$link['url']);
843            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
844        }
845        //remove double encodings in titles
846        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
847
848        $ret  = '';
849        $ret .= $link['pre'];
850        $ret .= '<a href="'.$link['url'].'"';
851        if($link['class'])  $ret .= ' class="'.$link['class'].'"';
852        if($link['target']) $ret .= ' target="'.$link['target'].'"';
853        if($link['title'])  $ret .= ' title="'.$link['title'].'"';
854        if($link['style'])  $ret .= ' style="'.$link['style'].'"';
855        if($link['more'])   $ret .= ' '.$link['more'];
856        $ret .= '>';
857        $ret .= $link['name'];
858        $ret .= '</a>';
859        $ret .= $link['suf'];
860        return $ret;
861    }
862
863    /**
864     * Removes any Namespace from the given name but keeps
865     * casing and special chars
866     *
867     * @author Andreas Gohr <andi@splitbrain.org>
868     */
869    function _simpleTitle($name){
870        global $conf;
871
872        if($conf['useslash']){
873            $nssep = '[:;/]';
874        }else{
875            $nssep = '[:;]';
876        }
877        $name = preg_replace('!.*'.$nssep.'!','',$name);
878        //if there is a hash we use the ancor name only
879        $name = preg_replace('!.*#!','',$name);
880        return $name;
881    }
882
883    /**
884     * Renders internal and external media
885     *
886     * @author Andreas Gohr <andi@splitbrain.org>
887     */
888    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
889                      $height=NULL, $cache=NULL) {
890
891        $ret = '';
892
893        list($ext,$mime) = mimetype($src);
894        if(substr($mime,0,5) == 'image'){
895            //add image tag
896            $ret .= '<img src="'.DOKU_BASE.'fetch.php?w='.$width.'&amp;h='.$height.
897                    '&amp;cache='.$cache.'&amp;media='.urlencode($src).'"';
898
899            $ret .= ' class="media'.$align.'"';
900
901            if (!is_null($title)) {
902                $ret .= ' title="'.$this->_xmlEntities($title).'"';
903                $ret .= ' alt="'.$this->_xmlEntities($title).'"';
904            }else{
905                $ret .= ' alt=""';
906            }
907
908            if ( !is_null($width) )
909                $ret .= ' width="'.$this->_xmlEntities($width).'"';
910
911            if ( !is_null($height) )
912                $ret .= ' height="'.$this->_xmlEntities($height).'"';
913
914            $ret .= ' />';
915
916        }elseif($mime == 'application/x-shockwave-flash'){
917            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
918                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
919            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
920            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
921            $ret .= '>'.DOKU_LF;
922            $ret .= '<param name="movie" value="'.DOKU_BASE.'fetch.php?media='.urlencode($src).'" />'.DOKU_LF;
923            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
924            $ret .= '<embed src="'.DOKU_BASE.'fetch.php?media='.urlencode($src).'"'.
925                    ' quality="high"';
926            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
927            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
928            $ret .= ' type="application/x-shockwave-flash"'.
929                    ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF;
930            $ret .= '</object>'.DOKU_LF;
931
932        }elseif(!is_null($title)){
933            // well at least we have a title to display
934            $ret .= $this->_xmlEntities($title);
935        }else{
936            // just show the source
937            $ret .= $this->_xmlEntities($src);
938        }
939
940        return $ret;
941    }
942
943    function _xmlEntities($string) {
944        return htmlspecialchars($string);
945    }
946
947    function _headerToLink($title) {
948        return str_replace(':','',cleanID($title));
949    }
950
951    /**
952     * Adds code for section editing button
953     *
954     * This is just aplaceholder and gets replace by the button if
955     * section editing is allowed
956     *
957     * @author Andreas Gohr <andi@splitbrain.org>
958     */
959    function _secedit($f, $t){
960        $this->doc .= '<!-- SECTION ['.$f.'-'.$t.'] -->';
961    }
962
963    /**
964     * Construct a title and handle images in titles
965     *
966     * @author Harry Fuecks <hfuecks@gmail.com>
967     */
968    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
969        global $conf;
970
971        $isImage = FALSE;
972        if ( is_null($title) ) {
973            if ($conf['useheading'] && $id) {
974                $heading = p_get_first_heading($id);
975                if ($heading) {
976                    return $this->_xmlEntities($heading);
977                }
978            }
979            return $this->_xmlEntities($default);
980        } else if ( is_string($title) ) {
981            return $this->_xmlEntities($title);
982        } else if ( is_array($title) ) {
983            $isImage = TRUE;
984            return $this->_imageTitle($title);
985        }
986    }
987
988    /**
989     * Returns an HTML code for images used in link titles
990     *
991     * @todo Resolve namespace on internal images
992     * @author Andreas Gohr <andi@splitbrain.org>
993     */
994    function _imageTitle($img) {
995        return $this->_media($img['src'],
996                              $img['title'],
997                              $img['align'],
998                              $img['width'],
999                              $img['height'],
1000                              $img['cache']);
1001    }
1002}
1003
1004//Setup VIM: ex: et ts=4 enc=utf-8 :
1005