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