xref: /dokuwiki/inc/parser/xhtml.php (revision 37e34a5ef6c436d6cf1525ba3643b1597cadc168)
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, $search=NULL) {
392        global $conf;
393        global $ID;
394
395        $name = $this->_getLinkTitle($name, $this->_simpleTitle($id), $isImage, $id);
396        resolve_pageid(getNS($ID),$id,$exists);
397
398        if ( !$isImage ) {
399            if ( $exists ) {
400                $class='wikilink1';
401            } else {
402                $class='wikilink2';
403            }
404        } else {
405            $class='media';
406        }
407
408        //prepare for formating
409        $link['target'] = $conf['target']['wiki'];
410        $link['style']  = '';
411        $link['pre']    = '';
412        $link['suf']    = '';
413        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
414        $link['class']  = $class;
415        $link['url']    = wl($id);
416        $link['name']   = $name;
417        $link['title']  = $id;
418
419        //add search string
420        if($search){
421            ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&amp;s=';
422            $link['url'] .= urlencode($search);
423        }
424
425        //output formatted
426        echo $this->_formatLink($link);
427    }
428
429    function externallink($url, $name = NULL) {
430        global $conf;
431
432        $name = $this->_getLinkTitle($name, $url, $isImage);
433
434        if ( !$isImage ) {
435            $class='urlextern';
436        } else {
437            $class='media';
438        }
439
440        //prepare for formating
441        $link['target'] = $conf['target']['extern'];
442        $link['style']  = '';
443        $link['pre']    = '';
444        $link['suf']    = '';
445        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
446        $link['class']  = $class;
447        $link['url']    = $url;
448        $link['name']   = $name;
449        $link['title']  = $this->_xmlEntities($url);
450        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
451
452        //output formatted
453        echo $this->_formatLink($link);
454    }
455
456    /**
457    */
458    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
459        global $conf;
460
461        $link = array();
462        $link['target'] = $conf['target']['interwiki'];
463        $link['pre']    = '';
464        $link['suf']    = '';
465        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
466        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
467
468        if ( !$isImage ) {
469            $link['class'] = 'interwiki';
470        } else {
471            $link['class'] = 'media';
472        }
473
474        //get interwiki URL
475        if ( isset($this->interwiki[$wikiName]) ) {
476            $url = $this->interwiki[$wikiName];
477        } else {
478            // Default to Google I'm feeling lucky
479            $url = 'http://www.google.com/search?q={URL}&amp;btnI=lucky';
480            $wikiName = 'go';
481        }
482
483        if(!$isImage){
484            //if ico exists set additional style
485            if(@file_exists(DOKU_INC.'interwiki/'.$wikiName.'.png')){
486                $link['style']='background: transparent url('.DOKU_BASE.'interwiki/'.$wikiName.'.png) 0px 1px no-repeat;';
487            }elseif(@file_exists(DOKU_INC.'interwiki/'.$wikiName.'.gif')){
488                $link['style']='background: transparent url('.DOKU_BASE.'interwiki/'.$wikiName.'.gif) 0px 1px no-repeat;';
489            }
490        }
491
492        //do we stay at the same server? Use local target
493        if( strpos($url,DOKU_URL) === 0 ){
494            $link['target'] = $conf['target']['wiki'];
495        }
496
497        //replace placeholder
498        if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){
499            //use placeholders
500            $url = str_replace('{URL}',urlencode($wikiUri),$url);
501            $url = str_replace('{NAME}',$wikiUri,$url);
502            $parsed = parse_url($wikiUri);
503            if(!$parsed['port']) $parsed['port'] = 80;
504            $url = str_replace('{SCHEME}',$parsed['scheme'],$url);
505            $url = str_replace('{HOST}',$parsed['host'],$url);
506            $url = str_replace('{PORT}',$parsed['port'],$url);
507            $url = str_replace('{PATH}',$parsed['path'],$url);
508            $url = str_replace('{QUERY}',$parsed['query'],$url);
509            $link['url'] = $url;
510        }else{
511            //default
512            $link['url'] = $url.urlencode($wikiUri);
513        }
514
515        $link['title'] = htmlspecialchars($link['url']);
516
517        //output formatted
518        echo $this->_formatLink($link);
519    }
520
521    /*
522     * @deprecated not used!!!
523     * @TODO Correct the CSS class for files? (not windows)
524     * @TODO Remove hard coded URL to splitbrain.org
525    function filelink($link, $title = NULL) {
526        echo '<a';
527
528        $title = $this->_getLinkTitle($title, $link, $isImage);
529
530        if ( !$isImage ) {
531            echo ' class="windows"';
532        } else {
533            echo ' class="media"';
534        }
535
536        echo ' href="'.$this->_xmlEntities($link).'"';
537
538        echo ' style="background: transparent url(http://wiki.splitbrain.org/images/windows.gif) 0px 1px no-repeat;"';
539
540        echo ' onclick="return svchk()" onkeypress="return svchk()">';
541
542        echo $title;
543
544        echo '</a>';
545    }
546    */
547
548    /**
549    */
550    function windowssharelink($url, $name = NULL) {
551        global $conf;
552        global $lang;
553        //simple setup
554        $link['target'] = $conf['target']['windows'];
555        $link['pre']    = '';
556        $link['suf']   = '';
557        $link['style']  = '';
558        //Display error on browsers other than IE
559        $link['more'] = 'onclick="if(document.all == null){alert(\''.
560                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).
561                        '\');}" onkeypress="if(document.all == null){alert(\''.
562                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).'\');}"';
563
564        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
565        if ( !$isImage ) {
566            $link['class'] = 'windows';
567        } else {
568            $link['class'] = 'media';
569        }
570
571
572        $link['title'] = $this->_xmlEntities($url);
573        $url = str_replace('\\','/',$url);
574        $url = 'file:///'.$url;
575        $link['url'] = $url;
576
577        //output formatted
578        echo $this->_formatLink($link);
579    }
580
581    function emaillink($address, $name = NULL) {
582        global $conf;
583        //simple setup
584        $link = array();
585        $link['target'] = '';
586        $link['pre']    = '';
587        $link['suf']   = '';
588        $link['style']  = '';
589        $link['more']   = '';
590
591        //we just test for image here - we need to encode the title our self
592        $this->_getLinkTitle($name, $address, $isImage);
593        if ( !$isImage ) {
594            $link['class']='mail';
595        } else {
596            $link['class']='media';
597        }
598
599        //shields up
600        if($conf['mailguard']=='visible'){
601            //the mail name gets some visible encoding
602            $address = str_replace('@',' [at] ',$address);
603            $address = str_replace('.',' [dot] ',$address);
604            $address = str_replace('-',' [dash] ',$address);
605
606            $title   = $this->_xmlEntities($address);
607            if(empty($name)){
608                $name = $this->_xmlEntities($address);
609            }else{
610                $name = $this->_xmlEntities($name);
611            }
612        }elseif($conf['mailguard']=='hex'){
613            //encode every char to a hex entity
614            for ($x=0; $x < strlen($address); $x++) {
615                $encode .= '&#x' . bin2hex($address[$x]).';';
616            }
617            $address = $encode;
618            $title   = $encode;
619            if(empty($name)){
620                $name = $encode;
621            }else{
622                $name = $this->_xmlEntities($name);
623            }
624        }else{
625            //keep address as is
626            $title   = $this->_xmlEntities($address);
627            if(empty($name)){
628                $name = $this->_xmlEntities($address);
629            }else{
630                $name = $this->_xmlEntities($name);
631            }
632        }
633
634        $link['url']   = 'mailto:'.$address;
635        $link['name']  = $name;
636        $link['title'] = $title;
637
638        //output formatted
639        echo $this->_formatLink($link);
640    }
641
642    /**
643     * @todo don't add link for flash
644     */
645    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
646                            $height=NULL, $cache=NULL) {
647        global $conf;
648        global $ID;
649        resolve_mediaid(getNS($ID),$src, $exists);
650
651        $link = array();
652        $link['class']  = 'media';
653        $link['style']  = '';
654        $link['pre']    = '';
655        $link['suf']    = '';
656        $link['more']   = 'onclick="return svchk()" onkeypress="return svchk()"';
657        $link['target'] = $conf['target']['media'];
658
659        $link['title']  = $this->_xmlEntities($src);
660        $link['url']    = DOKU_BASE.'fetch.php?cache='.$cache.'&amp;media='.urlencode($src);
661        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
662
663
664        //output formatted
665        echo $this->_formatLink($link);
666    }
667
668    /**
669     * @todo don't add link for flash
670     */
671    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
672                            $height=NULL, $cache=NULL) {
673        global $conf;
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        echo $this->_formatLink($link);
690    }
691
692    /**
693     * Renders an RSS feed using magpie
694     *
695     * @author Andreas Gohr <andi@splitbrain.org>
696     */
697    function rss ($url){
698        global $lang;
699        define('MAGPIE_CACHE_ON', false); //we do our own caching
700        define('MAGPIE_DIR', DOKU_INC.'inc/magpie/');
701        define('MAGPIE_OUTPUT_ENCODING','UTF-8'); //return all feeds as UTF-8
702        require_once(MAGPIE_DIR.'/rss_fetch.inc');
703
704        //disable warning while fetching
705        $elvl = error_reporting(E_ERROR);
706        $rss  = fetch_rss($url);
707        error_reporting($elvl);
708
709        print '<ul class="rss">';
710        if($rss){
711            foreach ($rss->items as $item ) {
712                print '<li>';
713                $this->externallink($item['link'],$item['title']);
714                print '</li>';
715            }
716        }else{
717            print '<li>';
718            print '<em>'.$lang['rssfailed'].'</em>';
719            $this->externallink($url);
720            print '</li>';
721        }
722        print '</ul>';
723    }
724
725    /**
726     * Renders internal and external media
727     *
728     * @author Andreas Gohr <andi@splitbrain.org>
729     * @todo   handle center align
730     * @todo   move to bottom
731     */
732    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
733                      $height=NULL, $cache=NULL) {
734
735        $ret = '';
736
737        list($ext,$mime) = mimetype($src);
738        if(substr($mime,0,5) == 'image'){
739            //add image tag
740            $ret .= '<img src="'.DOKU_BASE.'fetch.php?w='.$width.'&amp;h='.$height.
741                    '&amp;cache='.$cache.'&amp;media='.urlencode($src).'"';
742
743            $ret .= ' class="media'.$align.'"';
744
745            if (!is_null($title))
746                $ret .= ' title="'.$this->_xmlEntities($title).'"';
747
748            if ( !is_null($width) )
749                $ret .= ' width="'.$this->_xmlEntities($width).'"';
750
751            if ( !is_null($height) )
752                $ret .= ' height="'.$this->_xmlEntities($height).'"';
753
754            $ret .= ' />';
755
756        }elseif($mime == 'application/x-shockwave-flash'){
757            //FIXME default to a higher flash version?
758
759            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
760                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=5,0,0,0"';
761            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
762            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
763            $ret .= '>'.DOKU_LF;
764            $ret .= '<param name="movie" value="'.DOKU_BASE.'fetch.php?media='.urlencode($src).'" />'.DOKU_LF;
765            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
766            $ret .= '<embed src="'.DOKU_BASE.'fetch.php?media='.urlencode($src).'"'.
767                    ' quality="high" bgcolor="#000000"';
768            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
769            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
770            $ret .= ' type="application/x-shockwave-flash"'.
771                    ' pluginspage="http://www.macromedia.com/shockwave/download/index.cgi'.
772                    '?P1_Prod_Version=ShockwaveFlash"></embed>'.DOKU_LF;
773            $ret .= '</object>'.DOKU_LF;
774
775        }elseif(!is_null($title)){
776            // well at least we have a title to display
777            $ret .= $this->_xmlEntities($title);
778        }else{
779            // just show the source
780            $ret .= $this->_xmlEntities($src);
781        }
782
783        return $ret;
784    }
785
786    // $numrows not yet implemented
787    function table_open($maxcols = NULL, $numrows = NULL){
788        echo '<table class="inline">'.DOKU_LF;
789    }
790
791    function table_close(){
792        echo '</table>'.DOKU_LF.'<br />'.DOKU_LF;
793    }
794
795    function tablerow_open(){
796        echo DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
797    }
798
799    function tablerow_close(){
800        echo DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
801    }
802
803    function tableheader_open($colspan = 1, $align = NULL){
804        echo '<th';
805        if ( !is_null($align) ) {
806            echo ' class="'.$align.'align"';
807        }
808        if ( $colspan > 1 ) {
809            echo ' colspan="'.$colspan.'"';
810        }
811        echo '>';
812    }
813
814    function tableheader_close(){
815        echo '</th>';
816    }
817
818    function tablecell_open($colspan = 1, $align = NULL){
819        echo '<td';
820        if ( !is_null($align) ) {
821            echo ' class="'.$align.'align"';
822        }
823        if ( $colspan > 1 ) {
824            echo ' colspan="'.$colspan.'"';
825        }
826        echo '>';
827    }
828
829    function tablecell_close(){
830        echo '</td>';
831    }
832
833    //----------------------------------------------------------
834    // Utils
835
836    /**
837     * Assembles all parts defined by the link formater below
838     * Returns HTML for the link
839     *
840     * @author Andreas Gohr <andi@splitbrain.org>
841     */
842    function _formatLink($link){
843        //make sure the url is XHTML compliant (skip mailto)
844        if(substr($link['url'],0,7) != 'mailto:'){
845            $link['url'] = str_replace('&','&amp;',$link['url']);
846            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
847        }
848        //remove double encodings in titles
849        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
850
851        $ret  = '';
852        $ret .= $link['pre'];
853        $ret .= '<a href="'.$link['url'].'"';
854        if($link['class'])  $ret .= ' class="'.$link['class'].'"';
855        if($link['target']) $ret .= ' target="'.$link['target'].'"';
856        if($link['title'])  $ret .= ' title="'.$link['title'].'"';
857        if($link['style'])  $ret .= ' style="'.$link['style'].'"';
858        if($link['more'])   $ret .= ' '.$link['more'];
859        $ret .= '>';
860        $ret .= $link['name'];
861        $ret .= '</a>';
862        $ret .= $link['suf'];
863        return $ret;
864    }
865
866    /**
867     * Removes any Namespace from the given name but keeps
868     * casing and special chars
869     *
870     * @author Andreas Gohr <andi@splitbrain.org>
871     */
872    function _simpleTitle($name){
873        global $conf;
874        if($conf['useslash']){
875            $nssep = '[:;/]';
876        }else{
877            $nssep = '[:;]';
878        }
879        return preg_replace('!.*'.$nssep.'!','',$name);
880    }
881
882
883    function _newFootnoteId() {
884        static $id = 1;
885        return $id++;
886    }
887
888    function _xmlEntities($string) {
889        return htmlspecialchars($string);
890    }
891
892    /**
893    * @TODO Tuning needed - e.g. utf8 strtolower ?
894    */
895    function _headerToLink($title) {
896        return preg_replace('/\W/','_',trim($title));
897    }
898
899    /**
900     * Adds code for section editing button
901     */
902    function _secedit($f, $t){
903        print '<!-- SECTION ['.$f.'-'.$t.'] -->';
904    }
905
906    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
907        global $conf;
908
909        $isImage = FALSE;
910
911        if ( is_null($title) ) {
912	  if ($conf['useheading'] && $id) {
913	    $heading = p_get_first_heading($id);
914	    if ($heading) {
915	      return $this->_xmlEntities($heading);
916	    }
917	  }
918	  return $this->_xmlEntities($default);
919
920        } else if ( is_string($title) ) {
921
922            return $this->_xmlEntities($title);
923
924        } else if ( is_array($title) ) {
925
926            $isImage = TRUE;
927            return $this->_imageTitle($title);
928
929        }
930    }
931
932    /**
933     * @TODO Resolve namespace on internal images
934     */
935    function _imageTitle($img) {
936
937        //FIXME resolve internal links
938
939        return $this->_media($img['src'],
940                              $img['title'],
941                              $img['align'],
942                              $img['width'],
943                              $img['height'],
944                              $img['cache']);
945
946/*
947        if ( $img['type'] == 'internalmedia' ) {
948
949            // Resolve here...
950            if ( strpos($img['src'],':') ) {
951                $src = explode(':',$img['src']);
952                $src = $src[1];
953            } else {
954                $src = $img['src'];
955            }
956
957            $imgStr = '<img class="media" src="http://wiki.splitbrain.org/media/wiki/'.$this->_xmlEntities($src).'"';
958
959        } else {
960
961            $imgStr = '<img class="media" src="'.$this->_xmlEntities($img['src']).'"';
962
963        }
964
965        if ( !is_null($img['title']) ) {
966            $imgStr .= ' alt="'.$this->_xmlEntities($img['title']).'"';
967        } else {
968            $imgStr .= ' alt=""';
969        }
970
971        if ( !is_null($img['align']) ) {
972            $imgStr .= ' align="'.$img['align'].'"';
973        }
974
975        if ( !is_null($img['width']) ) {
976            $imgStr .= ' width="'.$this->_xmlEntities($img['width']).'"';
977        }
978
979        if ( !is_null($img['height']) ) {
980            $imgStr .= ' height="'.$this->_xmlEntities($img['height']).'"';
981        }
982
983        $imgStr .= '/>';
984
985        return $imgStr;
986*/
987    }
988}
989
990/**
991* Test whether there's an image to display with this interwiki link
992*/
993function interwikiImgExists($name) {
994
995    static $exists = array();
996
997    if ( array_key_exists($name,$exists) ) {
998        return $exists[$name];
999    }
1000
1001    if( @file_exists( DOKU. 'interwiki/'.$name.'.png') ) {
1002        $exists[$name] = 'png';
1003    } else if ( @file_exists( DOKU . 'interwiki/'.$name.'.gif') ) {
1004        $exists[$name] = 'gif';
1005    } else {
1006        $exists[$name] = FALSE;
1007    }
1008
1009    return $exists[$name];
1010}
1011
1012/**
1013 * For determining whether to use CSS class "wikilink1" or "wikilink2"
1014 * @todo use configinstead of DOKU_DATA
1015 * @deprecated -> resolve_pagename should be used
1016 */
1017function wikiPageExists($name) {
1018msg("deprecated wikiPageExists called",-1);
1019    static $pages = array();
1020
1021    if ( array_key_exists($name,$pages) ) {
1022        return $pages[$name];
1023    }
1024
1025    $file = str_replace(':','/',$name).'.txt';
1026
1027    if ( @file_exists( DOKU_DATA . $file ) ) {
1028        $pages[$name] = TRUE;
1029    } else {
1030        $pages[$name] = FALSE;
1031    }
1032
1033    return $pages[$name];
1034}
1035
1036
1037//Setup VIM: ex: et ts=4 enc=utf-8 :
1038