xref: /dokuwiki/inc/parser/xhtml.php (revision 10bd69fc459dde7a3022736e9a9c39bdb52e81ef)
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',fullpath(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 .= '<sup><a href="#fnt__'.$id.'" id="fn__'.$id.'" name="fn__'.$id.'" class="fn_bot">';
64                    $this->doc .= $id.')</a></sup> '.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 .= ', <sup><a href="#fnt__'.($ref+1).'" id="fn__'.($ref+1).'" name="fn__'.($ref+1).'" class="fn_bot">';
73                        $this->doc .= ($ref+1).')</a></sup> '.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 .= '<sup><a href="#fn__'.$id.'" name="fnt__'.$id.'" id="fnt__'.$id.'" class="fn_top">'.$id.')</a></sup>';
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        global $conf;
309
310        if($conf['phpok']){
311          ob_start();
312          eval($text);
313          $this->doc .= ob_get_contents();
314          ob_end_clean();
315        } else {
316          $this->code($text, 'php');
317        }
318    }
319
320    function phpblock($text) {
321        $this->php($text);
322    }
323
324    /**
325     * Insert HTML if allowed
326     *
327     * @author Andreas Gohr <andi@splitbrain.org>
328     */
329    function html($text) {
330        global $conf;
331
332        if($conf['htmlok']){
333          $this->doc .= $text;
334        } else {
335          $this->code($text, 'html4strict');
336        }
337    }
338
339    function htmlblock($text) {
340        $this->html($text);
341    }
342
343    function preformatted($text) {
344        $this->doc .= '<pre class="code">' . $this->_xmlEntities($text) . '</pre>'. DOKU_LF;
345    }
346
347    function file($text) {
348        $this->doc .= '<pre class="file">' . $this->_xmlEntities($text). '</pre>'. DOKU_LF;
349    }
350
351    function quote_open() {
352        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
353    }
354
355    function quote_close() {
356        $this->doc .= '</div></blockquote>'.DOKU_LF;
357    }
358
359    /**
360     * Callback for code text
361     *
362     * Uses GeSHi to highlight language syntax
363     *
364     * @author Andreas Gohr <andi@splitbrain.org>
365     */
366    function code($text, $language = NULL) {
367        global $conf;
368
369        if ( is_null($language) ) {
370            $this->preformatted($text);
371        } else {
372            //strip leading and trailing blank line
373            $text = preg_replace('/^\s*?\n/','',$text);
374            $text = preg_replace('/\s*?\n$/','',$text);
375            $this->doc .= p_xhtml_cached_geshi($text, $language);
376        }
377    }
378
379    function acronym($acronym) {
380
381        if ( array_key_exists($acronym, $this->acronyms) ) {
382
383            $title = $this->_xmlEntities($this->acronyms[$acronym]);
384
385            $this->doc .= '<acronym title="'.$title
386                .'">'.$this->_xmlEntities($acronym).'</acronym>';
387
388        } else {
389            $this->doc .= $this->_xmlEntities($acronym);
390        }
391    }
392
393    function smiley($smiley) {
394        if ( array_key_exists($smiley, $this->smileys) ) {
395            $title = $this->_xmlEntities($this->smileys[$smiley]);
396            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
397                '" class="middle" alt="'.
398                    $this->_xmlEntities($smiley).'" />';
399        } else {
400            $this->doc .= $this->_xmlEntities($smiley);
401        }
402    }
403
404    /*
405    * not used
406    function wordblock($word) {
407        if ( array_key_exists($word, $this->badwords) ) {
408            $this->doc .= '** BLEEP **';
409        } else {
410            $this->doc .= $this->_xmlEntities($word);
411        }
412    }
413    */
414
415    function entity($entity) {
416        if ( array_key_exists($entity, $this->entities) ) {
417            $this->doc .= $this->entities[$entity];
418        } else {
419            $this->doc .= $this->_xmlEntities($entity);
420        }
421    }
422
423    function multiplyentity($x, $y) {
424        $this->doc .= "$x&times;$y";
425    }
426
427    function singlequoteopening() {
428        global $lang;
429        $this->doc .= $lang['singlequoteopening'];
430    }
431
432    function singlequoteclosing() {
433        global $lang;
434        $this->doc .= $lang['singlequoteclosing'];
435    }
436
437    function apostrophe() {
438        global $lang;
439        $this->doc .= $lang['apostrophe'];
440    }
441
442    function doublequoteopening() {
443        global $lang;
444        $this->doc .= $lang['doublequoteopening'];
445    }
446
447    function doublequoteclosing() {
448        global $lang;
449        $this->doc .= $lang['doublequoteclosing'];
450    }
451
452    /**
453    */
454    function camelcaselink($link) {
455      $this->internallink($link,$link);
456    }
457
458
459    function locallink($hash, $name = NULL){
460        global $ID;
461        $name  = $this->_getLinkTitle($name, $hash, $isImage);
462        $hash  = $this->_headerToLink($hash);
463        $title = $ID.' &crarr;';
464        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
465        $this->doc .= $name;
466        $this->doc .= '</a>';
467    }
468
469    /**
470     * Render an internal Wiki Link
471     *
472     * $search and $returnonly are not for the renderer but are used
473     * elsewhere - no need to implement them in other renderers
474     *
475     * @author Andreas Gohr <andi@splitbrain.org>
476     */
477    function internallink($id, $name = NULL, $search=NULL,$returnonly=false) {
478        global $conf;
479        global $ID;
480        // default name is based on $id as given
481        $default = $this->_simpleTitle($id);
482
483        // now first resolve and clean up the $id
484        resolve_pageid(getNS($ID),$id,$exists);
485        $name = $this->_getLinkTitle($name, $default, $isImage, $id);
486        if ( !$isImage ) {
487            if ( $exists ) {
488                $class='wikilink1';
489            } else {
490                $class='wikilink2';
491                $link['rel']='nofollow';
492            }
493        } else {
494            $class='media';
495        }
496
497        //keep hash anchor
498        list($id,$hash) = explode('#',$id,2);
499        if(!empty($hash)) $hash = $this->_headerToLink($hash);
500
501        //prepare for formating
502        $link['target'] = $conf['target']['wiki'];
503        $link['style']  = '';
504        $link['pre']    = '';
505        $link['suf']    = '';
506        // highlight link to current page
507        if ($id == $ID) {
508            $link['pre']    = '<span class="curid">';
509            $link['suf']    = '</span>';
510        }
511        $link['more']   = '';
512        $link['class']  = $class;
513        $link['url']    = wl($id);
514        $link['name']   = $name;
515        $link['title']  = $id;
516        //add search string
517        if($search){
518            ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&amp;s=';
519            $link['url'] .= rawurlencode($search);
520        }
521
522        //keep hash
523        if($hash) $link['url'].='#'.$hash;
524
525        //output formatted
526        if($returnonly){
527            return $this->_formatLink($link);
528        }else{
529            $this->doc .= $this->_formatLink($link);
530        }
531    }
532
533    function externallink($url, $name = NULL) {
534        global $conf;
535
536        $name = $this->_getLinkTitle($name, $url, $isImage);
537
538        if ( !$isImage ) {
539            $class='urlextern';
540        } else {
541            $class='media';
542        }
543
544        //prepare for formating
545        $link['target'] = $conf['target']['extern'];
546        $link['style']  = '';
547        $link['pre']    = '';
548        $link['suf']    = '';
549        $link['more']   = '';
550        $link['class']  = $class;
551        $link['url']    = $url;
552
553        $link['name']   = $name;
554        $link['title']  = $this->_xmlEntities($url);
555        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
556
557        //output formatted
558        $this->doc .= $this->_formatLink($link);
559    }
560
561    /**
562    */
563    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
564        global $conf;
565
566        $link = array();
567        $link['target'] = $conf['target']['interwiki'];
568        $link['pre']    = '';
569        $link['suf']    = '';
570        $link['more']   = '';
571        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
572
573        //get interwiki URL
574        $url = $this-> _resolveInterWiki($wikiName,$wikiUri);
575
576        if ( !$isImage ) {
577            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName);
578            $link['class'] = "interwiki iw_$class";
579        } else {
580            $link['class'] = 'media';
581        }
582
583        //do we stay at the same server? Use local target
584        if( strpos($url,DOKU_URL) === 0 ){
585            $link['target'] = $conf['target']['wiki'];
586        }
587
588        $link['url'] = $url;
589        $link['title'] = htmlspecialchars($link['url']);
590
591        //output formatted
592        $this->doc .= $this->_formatLink($link);
593    }
594
595    /**
596     */
597    function windowssharelink($url, $name = NULL) {
598        global $conf;
599        global $lang;
600        //simple setup
601        $link['target'] = $conf['target']['windows'];
602        $link['pre']    = '';
603        $link['suf']   = '';
604        $link['style']  = '';
605        //Display error on browsers other than IE
606        $link['more'] = 'onclick="if(document.all == null){alert(\''.
607                        str_replace('\\\\n','\\n',addslashes($lang['nosmblinks'])).
608                        '\');}" onkeypress="if(document.all == null){alert(\''.
609                        str_replace('\\\\n','\\n',addslashes($lang['nosmblinks'])).'\');}"';
610
611        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
612        if ( !$isImage ) {
613            $link['class'] = 'windows';
614        } else {
615            $link['class'] = 'media';
616        }
617
618
619        $link['title'] = $this->_xmlEntities($url);
620        $url = str_replace('\\','/',$url);
621        $url = 'file:///'.$url;
622        $link['url'] = $url;
623
624        //output formatted
625        $this->doc .= $this->_formatLink($link);
626    }
627
628    function emaillink($address, $name = NULL) {
629        global $conf;
630        //simple setup
631        $link = array();
632        $link['target'] = '';
633        $link['pre']    = '';
634        $link['suf']   = '';
635        $link['style']  = '';
636        $link['more']   = '';
637
638        $name = $this->_getLinkTitle($name, '', $isImage);
639        if ( !$isImage ) {
640            $link['class']='mail JSnocheck';
641        } else {
642            $link['class']='media JSnocheck';
643        }
644
645        $address = $this->_xmlEntities($address);
646        $address = obfuscate($address);
647        $title   = $address;
648
649        if(empty($name)){
650            $name = $address;
651        }
652#elseif($isImage{
653#            $name = $this->_xmlEntities($name);
654#        }
655
656        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
657
658        $link['url']   = 'mailto:'.$address;
659        $link['name']  = $name;
660        $link['title'] = $title;
661
662        //output formatted
663        $this->doc .= $this->_formatLink($link);
664    }
665
666    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
667                            $height=NULL, $cache=NULL, $linking=NULL) {
668        global $conf;
669        global $ID;
670        resolve_mediaid(getNS($ID),$src, $exists);
671
672        $link = array();
673        $link['class']  = 'media';
674        $link['style']  = '';
675        $link['pre']    = '';
676        $link['suf']    = '';
677        $link['more']   = '';
678        $link['target'] = $conf['target']['media'];
679        $noLink = false;
680
681        $link['title']  = $this->_xmlEntities($src);
682        list($ext,$mime) = mimetype($src);
683        if(substr($mime,0,5) == 'image'){
684             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
685         }elseif($mime == 'application/x-shockwave-flash'){
686             // don't link flash movies
687             $noLink = true;
688         }else{
689             // add file icons
690             $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
691             $link['class'] .= ' mediafile mf_'.$class;
692             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
693         }
694         $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
695
696         //output formatted
697         if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
698         else $this->doc .= $this->_formatLink($link);
699    }
700
701    /**
702     * @todo don't add link for flash
703     */
704    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
705                            $height=NULL, $cache=NULL, $linking=NULL) {
706        global $conf;
707
708        $link = array();
709        $link['class']  = 'media';
710        $link['style']  = '';
711        $link['pre']    = '';
712        $link['suf']    = '';
713        $link['more']   = '';
714        $link['target'] = $conf['target']['media'];
715
716        $link['title']  = $this->_xmlEntities($src);
717        $link['url']    = ml($src,array('cache'=>$cache));
718        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
719        $noLink = false;
720
721        list($ext,$mime) = mimetype($src);
722        if(substr($mime,0,5) == 'image'){
723             // link only jpeg images
724             // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
725        }elseif($mime == 'application/x-shockwave-flash'){
726             // don't link flash movies
727             $noLink = true;
728        }else{
729             // add file icons
730             $link['class'] .= ' mediafile mf_'.$ext;
731         }
732
733        //output formatted
734        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
735        else $this->doc .= $this->_formatLink($link);
736    }
737
738    /**
739     * Renders an RSS feed
740     *
741     * @author Andreas Gohr <andi@splitbrain.org>
742     */
743    function rss ($url,$params){
744        global $lang;
745        global $conf;
746
747        require_once(DOKU_INC.'inc/FeedParser.php');
748        $feed = new FeedParser();
749        $feed->set_feed_url($url);
750
751        //disable warning while fetching
752        if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); }
753        $rc = $feed->init();
754        if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); }
755
756        //decide on start and end
757        if($params['reverse']){
758            $mod = -1;
759            $start = $feed->get_item_quantity()-1;
760            $end   = $start - ($params['max']);
761            $end   = ($end < -1) ? -1 : $end;
762        }else{
763            $mod   = 1;
764            $start = 0;
765            $end   = $feed->get_item_quantity();
766            $end   = ($end > $params['max']) ? $params['max'] : $end;;
767        }
768
769        $this->doc .= '<ul class="rss">';
770        if($rc){
771            for ($x = $start; $x != $end; $x += $mod) {
772                $item = $feed->get_item($x);
773                $this->doc .= '<li><div class="li">';
774                $this->externallink($item->get_permalink(),
775                                    $item->get_title());
776                if($params['author']){
777                    $author = $item->get_author(0);
778                    if($author){
779                        $name = $author->get_name();
780                        if(!$name) $name = $author->get_email();
781                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
782                    }
783                }
784                if($params['date']){
785                    $this->doc .= ' ('.$item->get_date($conf['dformat']).')';
786                }
787                if($params['details']){
788                    $this->doc .= '<div class="detail">';
789                    if($conf['htmlok']){
790                        $this->doc .= $item->get_description();
791                    }else{
792                        $this->doc .= strip_tags($item->get_description());
793                    }
794                    $this->doc .= '</div>';
795                }
796
797                $this->doc .= '</div></li>';
798            }
799        }else{
800            $this->doc .= '<li><div class="li">';
801            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
802            $this->externallink($url);
803            if($conf['allowdebug']){
804                $this->doc .= '<!--'.hsc($feed->error).'-->';
805            }
806            $this->doc .= '</div></li>';
807        }
808        $this->doc .= '</ul>';
809    }
810
811    // $numrows not yet implemented
812    function table_open($maxcols = NULL, $numrows = NULL){
813        $this->doc .= '<table class="inline">'.DOKU_LF;
814    }
815
816    function table_close(){
817        $this->doc .= '</table>'.DOKU_LF;
818    }
819
820    function tablerow_open(){
821        $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
822    }
823
824    function tablerow_close(){
825        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
826    }
827
828    function tableheader_open($colspan = 1, $align = NULL){
829        $this->doc .= '<th';
830        if ( !is_null($align) ) {
831            $this->doc .= ' class="'.$align.'align"';
832        }
833        if ( $colspan > 1 ) {
834            $this->doc .= ' colspan="'.$colspan.'"';
835        }
836        $this->doc .= '>';
837    }
838
839    function tableheader_close(){
840        $this->doc .= '</th>';
841    }
842
843    function tablecell_open($colspan = 1, $align = NULL){
844        $this->doc .= '<td';
845        if ( !is_null($align) ) {
846            $this->doc .= ' class="'.$align.'align"';
847        }
848        if ( $colspan > 1 ) {
849            $this->doc .= ' colspan="'.$colspan.'"';
850        }
851        $this->doc .= '>';
852    }
853
854    function tablecell_close(){
855        $this->doc .= '</td>';
856    }
857
858    //----------------------------------------------------------
859    // Utils
860
861    /**
862     * Build a link
863     *
864     * Assembles all parts defined in $link returns HTML for the link
865     *
866     * @author Andreas Gohr <andi@splitbrain.org>
867     */
868    function _formatLink($link){
869        //make sure the url is XHTML compliant (skip mailto)
870        if(substr($link['url'],0,7) != 'mailto:'){
871            $link['url'] = str_replace('&','&amp;',$link['url']);
872            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
873        }
874        //remove double encodings in titles
875        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
876
877        // be sure there are no bad chars in url or title
878        // (we can't do this for name because it can contain an img tag)
879        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
880        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
881
882        $ret  = '';
883        $ret .= $link['pre'];
884        $ret .= '<a href="'.$link['url'].'"';
885        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
886        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
887        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
888        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
889        if(!empty($link['rel']))    $ret .= ' rel="'.$link['rel'].'"';
890        if(!empty($link['more']))   $ret .= ' '.$link['more'];
891        $ret .= '>';
892        $ret .= $link['name'];
893        $ret .= '</a>';
894        $ret .= $link['suf'];
895        return $ret;
896    }
897
898    /**
899     * Renders internal and external media
900     *
901     * @author Andreas Gohr <andi@splitbrain.org>
902     */
903    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
904                      $height=NULL, $cache=NULL) {
905
906        $ret = '';
907
908        list($ext,$mime) = mimetype($src);
909        if(substr($mime,0,5) == 'image'){
910            //add image tag
911            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
912            $ret .= ' class="media'.$align.'"';
913
914            // make left/right alignment for no-CSS view work (feeds)
915            if($align == 'right') $ret .= ' align="right"';
916            if($align == 'left')  $ret .= ' align="left"';
917
918            if (!is_null($title)) {
919                $ret .= ' title="'.$this->_xmlEntities($title).'"';
920                $ret .= ' alt="'.$this->_xmlEntities($title).'"';
921            }elseif($ext == 'jpg' || $ext == 'jpeg'){
922                //try to use the caption from IPTC/EXIF
923                require_once(DOKU_INC.'inc/JpegMeta.php');
924                $jpeg =& new JpegMeta(mediaFN($src));
925                if($jpeg !== false) $cap = $jpeg->getTitle();
926                if($cap){
927                    $ret .= ' title="'.$this->_xmlEntities($cap).'"';
928                    $ret .= ' alt="'.$this->_xmlEntities($cap).'"';
929                }else{
930                    $ret .= ' alt=""';
931                }
932            }else{
933                $ret .= ' alt=""';
934            }
935
936            if ( !is_null($width) )
937                $ret .= ' width="'.$this->_xmlEntities($width).'"';
938
939            if ( !is_null($height) )
940                $ret .= ' height="'.$this->_xmlEntities($height).'"';
941
942            $ret .= ' />';
943
944        }elseif($mime == 'application/x-shockwave-flash'){
945            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
946                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
947            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
948            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
949            $ret .= '>'.DOKU_LF;
950            $ret .= '<param name="movie" value="'.ml($src).'" />'.DOKU_LF;
951            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
952            $ret .= '<embed src="'.ml($src).'"'.
953                    ' quality="high"';
954            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
955            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
956            $ret .= ' type="application/x-shockwave-flash"'.
957                    ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF;
958            $ret .= '</object>'.DOKU_LF;
959
960        }elseif($title){
961            // well at least we have a title to display
962            $ret .= $this->_xmlEntities($title);
963        }else{
964            // just show the sourcename
965            $ret .= $this->_xmlEntities(basename(noNS($src)));
966        }
967
968        return $ret;
969    }
970
971    function _xmlEntities($string) {
972        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
973    }
974
975    /**
976     * Creates a linkid from a headline
977     *
978     * @param string  $title   The headline title
979     * @param boolean $create  Create a new unique ID?
980     * @author Andreas Gohr <andi@splitbrain.org>
981     */
982    function _headerToLink($title,$create=false) {
983        $title = str_replace(':','',cleanID($title));
984        $title = ltrim($title,'0123456789._-');
985        if(empty($title)) $title='section';
986
987        if($create){
988            // make sure tiles are unique
989            $num = '';
990            while(in_array($title.$num,$this->headers)){
991                ($num) ? $num++ : $num = 1;
992            }
993            $title = $title.$num;
994            $this->headers[] = $title;
995        }
996
997        return $title;
998    }
999
1000    /**
1001     * Construct a title and handle images in titles
1002     *
1003     * @author Harry Fuecks <hfuecks@gmail.com>
1004     */
1005    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
1006        global $conf;
1007
1008        $isImage = false;
1009        if ( is_null($title) ) {
1010            if ($conf['useheading'] && $id) {
1011                $heading = p_get_first_heading($id,true);
1012                if ($heading) {
1013                    return $this->_xmlEntities($heading);
1014                }
1015            }
1016            return $this->_xmlEntities($default);
1017        } else if ( is_string($title) ) {
1018            return $this->_xmlEntities($title);
1019        } else if ( is_array($title) ) {
1020            $isImage = true;
1021            return $this->_imageTitle($title);
1022        }
1023    }
1024
1025    /**
1026     * Returns an HTML code for images used in link titles
1027     *
1028     * @todo Resolve namespace on internal images
1029     * @author Andreas Gohr <andi@splitbrain.org>
1030     */
1031    function _imageTitle($img) {
1032        return $this->_media($img['src'],
1033                              $img['title'],
1034                              $img['align'],
1035                              $img['width'],
1036                              $img['height'],
1037                              $img['cache']);
1038    }
1039}
1040
1041//Setup VIM: ex: et ts=4 enc=utf-8 :
1042