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