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