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