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