xref: /dokuwiki/inc/parser/xhtml.php (revision 96cda5bf14b9eeeb072a6446a684055a0b58e895)
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 .= '<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                $link['rel']='nofollow';
480            }
481        } else {
482            $class='media';
483        }
484
485        //keep hash anchor
486        list($id,$hash) = explode('#',$id,2);
487        if(!empty($hash)) $hash = $this->_headerToLink($hash);
488
489        //prepare for formating
490        $link['target'] = $conf['target']['wiki'];
491        $link['style']  = '';
492        $link['pre']    = '';
493        $link['suf']    = '';
494        // highlight link to current page
495        if ($id == $ID) {
496            $link['pre']    = '<span class="curid">';
497            $link['suf']    = '</span>';
498        }
499        $link['more']   = '';
500        $link['class']  = $class;
501        $link['url']    = wl($id);
502        $link['name']   = $name;
503        $link['title']  = $id;
504        //add search string
505        if($search){
506            ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&amp;s=';
507            $link['url'] .= rawurlencode($search);
508        }
509
510        //keep hash
511        if($hash) $link['url'].='#'.$hash;
512
513        //output formatted
514        if($returnonly){
515            return $this->_formatLink($link);
516        }else{
517            $this->doc .= $this->_formatLink($link);
518        }
519    }
520
521    function externallink($url, $name = NULL) {
522        global $conf;
523
524        $name = $this->_getLinkTitle($name, $url, $isImage);
525
526        if ( !$isImage ) {
527            $class='urlextern';
528        } else {
529            $class='media';
530        }
531
532        //prepare for formating
533        $link['target'] = $conf['target']['extern'];
534        $link['style']  = '';
535        $link['pre']    = '';
536        $link['suf']    = '';
537        $link['more']   = '';
538        $link['class']  = $class;
539        $link['url']    = $url;
540
541        $link['name']   = $name;
542        $link['title']  = $this->_xmlEntities($url);
543        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
544
545        //output formatted
546        $this->doc .= $this->_formatLink($link);
547    }
548
549    /**
550    */
551    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
552        global $conf;
553
554        $link = array();
555        $link['target'] = $conf['target']['interwiki'];
556        $link['pre']    = '';
557        $link['suf']    = '';
558        $link['more']   = '';
559        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
560
561        //get interwiki URL
562        $url = $this-> _resolveInterWiki($wikiName,$wikiUri);
563
564        if ( !$isImage ) {
565            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName);
566            $link['class'] = "interwiki iw_$class";
567        } else {
568            $link['class'] = 'media';
569        }
570
571        //do we stay at the same server? Use local target
572        if( strpos($url,DOKU_URL) === 0 ){
573            $link['target'] = $conf['target']['wiki'];
574        }
575
576        $link['url'] = $url;
577        $link['title'] = htmlspecialchars($link['url']);
578
579        //output formatted
580        $this->doc .= $this->_formatLink($link);
581    }
582
583    /**
584     */
585    function windowssharelink($url, $name = NULL) {
586        global $conf;
587        global $lang;
588        //simple setup
589        $link['target'] = $conf['target']['windows'];
590        $link['pre']    = '';
591        $link['suf']   = '';
592        $link['style']  = '';
593        //Display error on browsers other than IE
594        $link['more'] = 'onclick="if(document.all == null){alert(\''.
595                        str_replace('\\\\n','\\n',addslashes($lang['nosmblinks'])).
596                        '\');}" onkeypress="if(document.all == null){alert(\''.
597                        str_replace('\\\\n','\\n',addslashes($lang['nosmblinks'])).'\');}"';
598
599        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
600        if ( !$isImage ) {
601            $link['class'] = 'windows';
602        } else {
603            $link['class'] = 'media';
604        }
605
606
607        $link['title'] = $this->_xmlEntities($url);
608        $url = str_replace('\\','/',$url);
609        $url = 'file:///'.$url;
610        $link['url'] = $url;
611
612        //output formatted
613        $this->doc .= $this->_formatLink($link);
614    }
615
616    function emaillink($address, $name = NULL) {
617        global $conf;
618        //simple setup
619        $link = array();
620        $link['target'] = '';
621        $link['pre']    = '';
622        $link['suf']   = '';
623        $link['style']  = '';
624        $link['more']   = '';
625
626        $name = $this->_getLinkTitle($name, '', $isImage);
627        if ( !$isImage ) {
628            $link['class']='mail JSnocheck';
629        } else {
630            $link['class']='media JSnocheck';
631        }
632
633        $address = $this->_xmlEntities($address);
634        $address = obfuscate($address);
635        $title   = $address;
636
637        if(empty($name)){
638            $name = $address;
639        }
640#elseif($isImage{
641#            $name = $this->_xmlEntities($name);
642#        }
643
644        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
645
646        $link['url']   = 'mailto:'.$address;
647        $link['name']  = $name;
648        $link['title'] = $title;
649
650        //output formatted
651        $this->doc .= $this->_formatLink($link);
652    }
653
654    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
655                            $height=NULL, $cache=NULL, $linking=NULL) {
656        global $conf;
657        global $ID;
658        resolve_mediaid(getNS($ID),$src, $exists);
659
660        $link = array();
661        $link['class']  = 'media';
662        $link['style']  = '';
663        $link['pre']    = '';
664        $link['suf']    = '';
665        $link['more']   = '';
666        $link['target'] = $conf['target']['media'];
667        $noLink = false;
668
669        $link['title']  = $this->_xmlEntities($src);
670        list($ext,$mime) = mimetype($src);
671        if(substr($mime,0,5) == 'image'){
672             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
673         }elseif($mime == 'application/x-shockwave-flash'){
674             // don't link flash movies
675             $noLink = true;
676         }else{
677             // add file icons
678             $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
679             $link['class'] .= ' mediafile mf_'.$class;
680             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
681         }
682         $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
683
684         //output formatted
685         if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
686         else $this->doc .= $this->_formatLink($link);
687    }
688
689    /**
690     * @todo don't add link for flash
691     */
692    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
693                            $height=NULL, $cache=NULL, $linking=NULL) {
694        global $conf;
695
696        $link = array();
697        $link['class']  = 'media';
698        $link['style']  = '';
699        $link['pre']    = '';
700        $link['suf']    = '';
701        $link['more']   = '';
702        $link['target'] = $conf['target']['media'];
703
704        $link['title']  = $this->_xmlEntities($src);
705        $link['url']    = ml($src,array('cache'=>$cache));
706        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
707        $noLink = false;
708
709        list($ext,$mime) = mimetype($src);
710        if(substr($mime,0,5) == 'image'){
711             // link only jpeg images
712             // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
713        }elseif($mime == 'application/x-shockwave-flash'){
714             // don't link flash movies
715             $noLink = true;
716        }else{
717             // add file icons
718             $link['class'] .= ' mediafile mf_'.$ext;
719         }
720
721        //output formatted
722        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
723        else $this->doc .= $this->_formatLink($link);
724    }
725
726    /**
727     * Renders an RSS feed
728     *
729     * @author Andreas Gohr <andi@splitbrain.org>
730     */
731    function rss ($url,$params){
732        global $lang;
733        global $conf;
734
735        require_once(DOKU_INC.'inc/FeedParser.php');
736        $feed = new FeedParser();
737        $feed->set_feed_url($url);
738
739        //disable warning while fetching
740        if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); }
741        $rc = $feed->init();
742        if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); }
743
744        //decide on start and end
745        if($params['reverse']){
746            $mod = -1;
747            $start = $feed->get_item_quantity()-1;
748            $end   = $start - ($params['max']);
749            $end   = ($end < -1) ? -1 : $end;
750        }else{
751            $mod   = 1;
752            $start = 0;
753            $end   = $feed->get_item_quantity();
754            $end   = ($end > $params['max']) ? $params['max'] : $end;;
755        }
756
757        $this->doc .= '<ul class="rss">';
758        if($rc){
759            for ($x = $start; $x != $end; $x += $mod) {
760                $item = $feed->get_item($x);
761                $this->doc .= '<li><div class="li">';
762                $this->externallink($item->get_permalink(),
763                                    $item->get_title());
764                if($params['author']){
765                    $author = $item->get_author(0);
766                    if($author){
767                        $name = $author->get_name();
768                        if(!$name) $name = $author->get_email();
769                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
770                    }
771                }
772                if($params['date']){
773                    $this->doc .= ' ('.$item->get_date($conf['dformat']).')';
774                }
775                if($params['details']){
776                    $this->doc .= '<div class="detail">';
777                    if($conf['htmlok']){
778                        $this->doc .= $item->get_description();
779                    }else{
780                        $this->doc .= strip_tags($item->get_description());
781                    }
782                    $this->doc .= '</div>';
783                }
784
785                $this->doc .= '</div></li>';
786            }
787        }else{
788            $this->doc .= '<li><div class="li">';
789            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
790            $this->externallink($url);
791            if($conf['allowdebug']){
792                $this->doc .= '<!--'.hsc($feed->error).'-->';
793            }
794            $this->doc .= '</div></li>';
795        }
796        $this->doc .= '</ul>';
797    }
798
799    // $numrows not yet implemented
800    function table_open($maxcols = NULL, $numrows = NULL){
801        $this->doc .= '<table class="inline">'.DOKU_LF;
802    }
803
804    function table_close(){
805        $this->doc .= '</table>'.DOKU_LF;
806    }
807
808    function tablerow_open(){
809        $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
810    }
811
812    function tablerow_close(){
813        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
814    }
815
816    function tableheader_open($colspan = 1, $align = NULL){
817        $this->doc .= '<th';
818        if ( !is_null($align) ) {
819            $this->doc .= ' class="'.$align.'align"';
820        }
821        if ( $colspan > 1 ) {
822            $this->doc .= ' colspan="'.$colspan.'"';
823        }
824        $this->doc .= '>';
825    }
826
827    function tableheader_close(){
828        $this->doc .= '</th>';
829    }
830
831    function tablecell_open($colspan = 1, $align = NULL){
832        $this->doc .= '<td';
833        if ( !is_null($align) ) {
834            $this->doc .= ' class="'.$align.'align"';
835        }
836        if ( $colspan > 1 ) {
837            $this->doc .= ' colspan="'.$colspan.'"';
838        }
839        $this->doc .= '>';
840    }
841
842    function tablecell_close(){
843        $this->doc .= '</td>';
844    }
845
846    //----------------------------------------------------------
847    // Utils
848
849    /**
850     * Build a link
851     *
852     * Assembles all parts defined in $link returns HTML for the link
853     *
854     * @author Andreas Gohr <andi@splitbrain.org>
855     */
856    function _formatLink($link){
857        //make sure the url is XHTML compliant (skip mailto)
858        if(substr($link['url'],0,7) != 'mailto:'){
859            $link['url'] = str_replace('&','&amp;',$link['url']);
860            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
861        }
862        //remove double encodings in titles
863        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
864
865        // be sure there are no bad chars in url or title
866        // (we can't do this for name because it can contain an img tag)
867        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
868        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
869
870        $ret  = '';
871        $ret .= $link['pre'];
872        $ret .= '<a href="'.$link['url'].'"';
873        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
874        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
875        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
876        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
877        if(!empty($link['rel']))    $ret .= ' rel="'.$link['rel'].'"';
878        if(!empty($link['more']))   $ret .= ' '.$link['more'];
879        $ret .= '>';
880        $ret .= $link['name'];
881        $ret .= '</a>';
882        $ret .= $link['suf'];
883        return $ret;
884    }
885
886    /**
887     * Renders internal and external media
888     *
889     * @author Andreas Gohr <andi@splitbrain.org>
890     */
891    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
892                      $height=NULL, $cache=NULL) {
893
894        $ret = '';
895
896        list($ext,$mime) = mimetype($src);
897        if(substr($mime,0,5) == 'image'){
898            //add image tag
899            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
900            $ret .= ' class="media'.$align.'"';
901
902            // make left/right alignment for no-CSS view work (feeds)
903            if($align == 'right') $ret .= ' align="right"';
904            if($align == 'left')  $ret .= ' align="left"';
905
906            if (!is_null($title)) {
907                $ret .= ' title="'.$this->_xmlEntities($title).'"';
908                $ret .= ' alt="'.$this->_xmlEntities($title).'"';
909            }elseif($ext == 'jpg' || $ext == 'jpeg'){
910                //try to use the caption from IPTC/EXIF
911                require_once(DOKU_INC.'inc/JpegMeta.php');
912                $jpeg =& new JpegMeta(mediaFN($src));
913                if($jpeg !== false) $cap = $jpeg->getTitle();
914                if($cap){
915                    $ret .= ' title="'.$this->_xmlEntities($cap).'"';
916                    $ret .= ' alt="'.$this->_xmlEntities($cap).'"';
917                }else{
918                    $ret .= ' alt=""';
919                }
920            }else{
921                $ret .= ' alt=""';
922            }
923
924            if ( !is_null($width) )
925                $ret .= ' width="'.$this->_xmlEntities($width).'"';
926
927            if ( !is_null($height) )
928                $ret .= ' height="'.$this->_xmlEntities($height).'"';
929
930            $ret .= ' />';
931
932        }elseif($mime == 'application/x-shockwave-flash'){
933            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
934                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
935            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
936            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
937            $ret .= '>'.DOKU_LF;
938            $ret .= '<param name="movie" value="'.ml($src).'" />'.DOKU_LF;
939            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
940            $ret .= '<embed src="'.ml($src).'"'.
941                    ' quality="high"';
942            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
943            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
944            $ret .= ' type="application/x-shockwave-flash"'.
945                    ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF;
946            $ret .= '</object>'.DOKU_LF;
947
948        }elseif($title){
949            // well at least we have a title to display
950            $ret .= $this->_xmlEntities($title);
951        }else{
952            // just show the sourcename
953            $ret .= $this->_xmlEntities(basename(noNS($src)));
954        }
955
956        return $ret;
957    }
958
959    function _xmlEntities($string) {
960        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
961    }
962
963    /**
964     * Creates a linkid from a headline
965     *
966     * @param string  $title   The headline title
967     * @param boolean $create  Create a new unique ID?
968     * @author Andreas Gohr <andi@splitbrain.org>
969     */
970    function _headerToLink($title,$create=false) {
971        $title = str_replace(':','',cleanID($title));
972        $title = ltrim($title,'0123456789._-');
973        if(empty($title)) $title='section';
974
975        if($create){
976            // make sure tiles are unique
977            $num = '';
978            while(in_array($title.$num,$this->headers)){
979                ($num) ? $num++ : $num = 1;
980            }
981            $title = $title.$num;
982            $this->headers[] = $title;
983        }
984
985        return $title;
986    }
987
988    /**
989     * Construct a title and handle images in titles
990     *
991     * @author Harry Fuecks <hfuecks@gmail.com>
992     */
993    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
994        global $conf;
995
996        $isImage = false;
997        if ( is_null($title) ) {
998            if ($conf['useheading'] && $id) {
999                $heading = p_get_first_heading($id,true);
1000                if ($heading) {
1001                    return $this->_xmlEntities($heading);
1002                }
1003            }
1004            return $this->_xmlEntities($default);
1005        } else if ( is_string($title) ) {
1006            return $this->_xmlEntities($title);
1007        } else if ( is_array($title) ) {
1008            $isImage = true;
1009            return $this->_imageTitle($title);
1010        }
1011    }
1012
1013    /**
1014     * Returns an HTML code for images used in link titles
1015     *
1016     * @todo Resolve namespace on internal images
1017     * @author Andreas Gohr <andi@splitbrain.org>
1018     */
1019    function _imageTitle($img) {
1020        return $this->_media($img['src'],
1021                              $img['title'],
1022                              $img['align'],
1023                              $img['width'],
1024                              $img['height'],
1025                              $img['cache']);
1026    }
1027}
1028
1029//Setup VIM: ex: et ts=4 enc=utf-8 :
1030