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