xref: /dokuwiki/inc/parser/xhtml.php (revision 3d491f758802dd2376dddd5d001765b940b08743)
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 preformatted($text) {
362        $this->doc .= '<pre class="code">' . trim($this->_xmlEntities($text)) . '</pre>'. DOKU_LF;
363    }
364
365    function file($text) {
366        $this->doc .= '<pre class="file">' . trim($this->_xmlEntities($text)). '</pre>'. DOKU_LF;
367    }
368
369    function quote_open() {
370        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
371    }
372
373    function quote_close() {
374        $this->doc .= '</div></blockquote>'.DOKU_LF;
375    }
376
377    function preformatted($text) {
378        $this->doc .= '<pre class="'.$class.'">' . trim($this->_xmlEntities($text)) . '</pre>'. DOKU_LF;
379    }
380
381    function file($text, $language=null, $filename=null) {
382        $this->_highlight('file',$text,$language,$filename);
383    }
384
385    function code($text, $language=null, $filename=null) {
386        $this->_highlight('code',$text,$language,$filename);
387    }
388
389    /**
390     * Use GeSHi to highlight language syntax in code and file blocks
391     *
392     * @author Andreas Gohr <andi@splitbrain.org>
393     */
394    function _highlight($type, $text, $language=null, $filename=null) {
395        global $conf;
396        global $ID;
397        global $lang;
398
399        if($filename){
400            $this->doc .= '<dl class="'.$type.'">'.DOKU_LF;
401            $this->doc .= '<dt><a href="'.exportlink($ID,'code',array('codeblock'=>$this->_codeblock)).'" title="'.$lang['download'].'">';
402            $this->doc .= hsc($filename);
403            $this->doc .= '</a></dt>'.DOKU_LF.'<dd>';
404        }
405
406        if ( is_null($language) ) {
407            $this->doc .= '<pre class="'.$type.'">'.$this->_xmlEntities($text).'</pre>'.DOKU_LF;
408        } else {
409            $class = 'code'; //we always need the code class to make the syntax highlighting apply
410            if($type != 'code') $class .= ' '.$type;
411
412            $this->doc .= "<pre class=\"$class $language\">".p_xhtml_cached_geshi($text, $language, '').'</pre>'.DOKU_LF;
413        }
414
415        if($filename){
416            $this->doc .= '</dd></dl>'.DOKU_LF;
417        }
418
419        $this->_codeblock++;
420    }
421
422    function acronym($acronym) {
423
424        if ( array_key_exists($acronym, $this->acronyms) ) {
425
426            $title = $this->_xmlEntities($this->acronyms[$acronym]);
427
428            $this->doc .= '<acronym title="'.$title
429                .'">'.$this->_xmlEntities($acronym).'</acronym>';
430
431        } else {
432            $this->doc .= $this->_xmlEntities($acronym);
433        }
434    }
435
436    function smiley($smiley) {
437        if ( array_key_exists($smiley, $this->smileys) ) {
438            $title = $this->_xmlEntities($this->smileys[$smiley]);
439            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
440                '" class="middle" alt="'.
441                    $this->_xmlEntities($smiley).'" />';
442        } else {
443            $this->doc .= $this->_xmlEntities($smiley);
444        }
445    }
446
447    /*
448    * not used
449    function wordblock($word) {
450        if ( array_key_exists($word, $this->badwords) ) {
451            $this->doc .= '** BLEEP **';
452        } else {
453            $this->doc .= $this->_xmlEntities($word);
454        }
455    }
456    */
457
458    function entity($entity) {
459        if ( array_key_exists($entity, $this->entities) ) {
460            $this->doc .= $this->entities[$entity];
461        } else {
462            $this->doc .= $this->_xmlEntities($entity);
463        }
464    }
465
466    function multiplyentity($x, $y) {
467        $this->doc .= "$x&times;$y";
468    }
469
470    function singlequoteopening() {
471        global $lang;
472        $this->doc .= $lang['singlequoteopening'];
473    }
474
475    function singlequoteclosing() {
476        global $lang;
477        $this->doc .= $lang['singlequoteclosing'];
478    }
479
480    function apostrophe() {
481        global $lang;
482        $this->doc .= $lang['apostrophe'];
483    }
484
485    function doublequoteopening() {
486        global $lang;
487        $this->doc .= $lang['doublequoteopening'];
488    }
489
490    function doublequoteclosing() {
491        global $lang;
492        $this->doc .= $lang['doublequoteclosing'];
493    }
494
495    /**
496    */
497    function camelcaselink($link) {
498      $this->internallink($link,$link);
499    }
500
501
502    function locallink($hash, $name = NULL){
503        global $ID;
504        $name  = $this->_getLinkTitle($name, $hash, $isImage);
505        $hash  = $this->_headerToLink($hash);
506        $title = $ID.' &crarr;';
507        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
508        $this->doc .= $name;
509        $this->doc .= '</a>';
510    }
511
512    /**
513     * Render an internal Wiki Link
514     *
515     * $search,$returnonly & $linktype are not for the renderer but are used
516     * elsewhere - no need to implement them in other renderers
517     *
518     * @author Andreas Gohr <andi@splitbrain.org>
519     */
520    function internallink($id, $name = NULL, $search=NULL,$returnonly=false,$linktype='content') {
521        global $conf;
522        global $ID;
523        // default name is based on $id as given
524        $default = $this->_simpleTitle($id);
525
526        // now first resolve and clean up the $id
527        resolve_pageid(getNS($ID),$id,$exists);
528        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
529        if ( !$isImage ) {
530            if ( $exists ) {
531                $class='wikilink1';
532            } else {
533                $class='wikilink2';
534                $link['rel']='nofollow';
535            }
536        } else {
537            $class='media';
538        }
539
540        //keep hash anchor
541        list($id,$hash) = explode('#',$id,2);
542        if(!empty($hash)) $hash = $this->_headerToLink($hash);
543
544        //prepare for formating
545        $link['target'] = $conf['target']['wiki'];
546        $link['style']  = '';
547        $link['pre']    = '';
548        $link['suf']    = '';
549        // highlight link to current page
550        if ($id == $ID) {
551            $link['pre']    = '<span class="curid">';
552            $link['suf']    = '</span>';
553        }
554        $link['more']   = '';
555        $link['class']  = $class;
556        $link['url']    = wl($id);
557        $link['name']   = $name;
558        $link['title']  = $id;
559        //add search string
560        if($search){
561            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
562            if(is_array($search)){
563                $search = array_map('rawurlencode',$search);
564                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
565            }else{
566                $link['url'] .= 's='.rawurlencode($search);
567            }
568        }
569
570        //keep hash
571        if($hash) $link['url'].='#'.$hash;
572
573        //output formatted
574        if($returnonly){
575            return $this->_formatLink($link);
576        }else{
577            $this->doc .= $this->_formatLink($link);
578        }
579    }
580
581    function externallink($url, $name = NULL) {
582        global $conf;
583
584        $name = $this->_getLinkTitle($name, $url, $isImage);
585
586        if ( !$isImage ) {
587            $class='urlextern';
588        } else {
589            $class='media';
590        }
591
592        //prepare for formating
593        $link['target'] = $conf['target']['extern'];
594        $link['style']  = '';
595        $link['pre']    = '';
596        $link['suf']    = '';
597        $link['more']   = '';
598        $link['class']  = $class;
599        $link['url']    = $url;
600
601        $link['name']   = $name;
602        $link['title']  = $this->_xmlEntities($url);
603        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
604
605        //output formatted
606        $this->doc .= $this->_formatLink($link);
607    }
608
609    /**
610    */
611    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
612        global $conf;
613
614        $link = array();
615        $link['target'] = $conf['target']['interwiki'];
616        $link['pre']    = '';
617        $link['suf']    = '';
618        $link['more']   = '';
619        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
620
621        //get interwiki URL
622        $url = $this->_resolveInterWiki($wikiName,$wikiUri);
623
624        if ( !$isImage ) {
625            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName);
626            $link['class'] = "interwiki iw_$class";
627        } else {
628            $link['class'] = 'media';
629        }
630
631        //do we stay at the same server? Use local target
632        if( strpos($url,DOKU_URL) === 0 ){
633            $link['target'] = $conf['target']['wiki'];
634        }
635
636        $link['url'] = $url;
637        $link['title'] = htmlspecialchars($link['url']);
638
639        //output formatted
640        $this->doc .= $this->_formatLink($link);
641    }
642
643    /**
644     */
645    function windowssharelink($url, $name = NULL) {
646        global $conf;
647        global $lang;
648        //simple setup
649        $link['target'] = $conf['target']['windows'];
650        $link['pre']    = '';
651        $link['suf']   = '';
652        $link['style']  = '';
653
654        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
655        if ( !$isImage ) {
656            $link['class'] = 'windows';
657        } else {
658            $link['class'] = 'media';
659        }
660
661
662        $link['title'] = $this->_xmlEntities($url);
663        $url = str_replace('\\','/',$url);
664        $url = 'file:///'.$url;
665        $link['url'] = $url;
666
667        //output formatted
668        $this->doc .= $this->_formatLink($link);
669    }
670
671    function emaillink($address, $name = NULL) {
672        global $conf;
673        //simple setup
674        $link = array();
675        $link['target'] = '';
676        $link['pre']    = '';
677        $link['suf']   = '';
678        $link['style']  = '';
679        $link['more']   = '';
680
681        $name = $this->_getLinkTitle($name, '', $isImage);
682        if ( !$isImage ) {
683            $link['class']='mail JSnocheck';
684        } else {
685            $link['class']='media JSnocheck';
686        }
687
688        $address = $this->_xmlEntities($address);
689        $address = obfuscate($address);
690        $title   = $address;
691
692        if(empty($name)){
693            $name = $address;
694        }
695
696        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
697
698        $link['url']   = 'mailto:'.$address;
699        $link['name']  = $name;
700        $link['title'] = $title;
701
702        //output formatted
703        $this->doc .= $this->_formatLink($link);
704    }
705
706    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
707                            $height=NULL, $cache=NULL, $linking=NULL) {
708        global $ID;
709        list($src,$hash) = explode('#',$src,2);
710        resolve_mediaid(getNS($ID),$src, $exists);
711
712        $noLink = false;
713        $render = ($linking == 'linkonly') ? false : true;
714        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
715
716        list($ext,$mime,$dl) = mimetype($src);
717        if(substr($mime,0,5) == 'image' && $render){
718            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
719        }elseif($mime == 'application/x-shockwave-flash' && $render){
720            // don't link flash movies
721            $noLink = true;
722        }else{
723            // add file icons
724            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
725            $link['class'] .= ' mediafile mf_'.$class;
726            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
727        }
728
729        if($hash) $link['url'] .= '#'.$hash;
730
731        //markup non existing files
732        if (!$exists)
733          $link['class'] .= ' wikilink2';
734
735        //output formatted
736        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
737        else $this->doc .= $this->_formatLink($link);
738    }
739
740    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
741                            $height=NULL, $cache=NULL, $linking=NULL) {
742        list($src,$hash) = explode('#',$src,2);
743        $noLink = false;
744        $render = ($linking == 'linkonly') ? false : true;
745        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
746
747        $link['url']    = ml($src,array('cache'=>$cache));
748
749        list($ext,$mime,$dl) = mimetype($src);
750        if(substr($mime,0,5) == 'image' && $render){
751             // link only jpeg images
752             // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
753        }elseif($mime == 'application/x-shockwave-flash' && $render){
754             // don't link flash movies
755             $noLink = true;
756        }else{
757             // add file icons
758             $link['class'] .= ' mediafile mf_'.$ext;
759        }
760
761        if($hash) $link['url'] .= '#'.$hash;
762
763        //output formatted
764        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
765        else $this->doc .= $this->_formatLink($link);
766    }
767
768    /**
769     * Renders an RSS feed
770     *
771     * @author Andreas Gohr <andi@splitbrain.org>
772     */
773    function rss ($url,$params){
774        global $lang;
775        global $conf;
776
777        require_once(DOKU_INC.'inc/FeedParser.php');
778        $feed = new FeedParser();
779        $feed->set_feed_url($url);
780
781        //disable warning while fetching
782        if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); }
783        $rc = $feed->init();
784        if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); }
785
786        //decide on start and end
787        if($params['reverse']){
788            $mod = -1;
789            $start = $feed->get_item_quantity()-1;
790            $end   = $start - ($params['max']);
791            $end   = ($end < -1) ? -1 : $end;
792        }else{
793            $mod   = 1;
794            $start = 0;
795            $end   = $feed->get_item_quantity();
796            $end   = ($end > $params['max']) ? $params['max'] : $end;;
797        }
798
799        $this->doc .= '<ul class="rss">';
800        if($rc){
801            for ($x = $start; $x != $end; $x += $mod) {
802                $item = $feed->get_item($x);
803                $this->doc .= '<li><div class="li">';
804                // support feeds without links
805                $lnkurl = $item->get_permalink();
806                if($lnkurl){
807                    // title is escaped by SimplePie, we unescape here because it
808                    // is escaped again in externallink() FS#1705
809                    $this->externallink($item->get_permalink(),
810                                        htmlspecialchars_decode($item->get_title()));
811                }else{
812                    $this->doc .= ' '.$item->get_title();
813                }
814                if($params['author']){
815                    $author = $item->get_author(0);
816                    if($author){
817                        $name = $author->get_name();
818                        if(!$name) $name = $author->get_email();
819                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
820                    }
821                }
822                if($params['date']){
823                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
824                }
825                if($params['details']){
826                    $this->doc .= '<div class="detail">';
827                    if($conf['htmlok']){
828                        $this->doc .= $item->get_description();
829                    }else{
830                        $this->doc .= strip_tags($item->get_description());
831                    }
832                    $this->doc .= '</div>';
833                }
834
835                $this->doc .= '</div></li>';
836            }
837        }else{
838            $this->doc .= '<li><div class="li">';
839            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
840            $this->externallink($url);
841            if($conf['allowdebug']){
842                $this->doc .= '<!--'.hsc($feed->error).'-->';
843            }
844            $this->doc .= '</div></li>';
845        }
846        $this->doc .= '</ul>';
847    }
848
849    // $numrows not yet implemented
850    function table_open($maxcols = NULL, $numrows = NULL){
851        // initialize the row counter used for classes
852        $this->_counter['row_counter'] = 0;
853        $this->doc .= '<table class="inline">'.DOKU_LF;
854    }
855
856    function table_close(){
857        $this->doc .= '</table>'.DOKU_LF;
858    }
859
860    function tablerow_open(){
861        // initialize the cell counter used for classes
862        $this->_counter['cell_counter'] = 0;
863        $class = 'row' . $this->_counter['row_counter']++;
864        $this->doc .= DOKU_TAB . '<tr class="'.$class.'">' . DOKU_LF . DOKU_TAB . DOKU_TAB;
865    }
866
867    function tablerow_close(){
868        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
869    }
870
871    function tableheader_open($colspan = 1, $align = NULL){
872        $class = 'class="col' . $this->_counter['cell_counter']++;
873        if ( !is_null($align) ) {
874            $class .= ' '.$align.'align';
875        }
876        $class .= '"';
877        $this->doc .= '<th ' . $class;
878        if ( $colspan > 1 ) {
879            $this->_counter['cell_counter'] += $colspan-1;
880            $this->doc .= ' colspan="'.$colspan.'"';
881        }
882        $this->doc .= '>';
883    }
884
885    function tableheader_close(){
886        $this->doc .= '</th>';
887    }
888
889    function tablecell_open($colspan = 1, $align = NULL){
890        $class = 'class="col' . $this->_counter['cell_counter']++;
891        if ( !is_null($align) ) {
892            $class .= ' '.$align.'align';
893        }
894        $class .= '"';
895        $this->doc .= '<td '.$class;
896        if ( $colspan > 1 ) {
897            $this->_counter['cell_counter'] += $colspan-1;
898            $this->doc .= ' colspan="'.$colspan.'"';
899        }
900        $this->doc .= '>';
901    }
902
903    function tablecell_close(){
904        $this->doc .= '</td>';
905    }
906
907    //----------------------------------------------------------
908    // Utils
909
910    /**
911     * Build a link
912     *
913     * Assembles all parts defined in $link returns HTML for the link
914     *
915     * @author Andreas Gohr <andi@splitbrain.org>
916     */
917    function _formatLink($link){
918        //make sure the url is XHTML compliant (skip mailto)
919        if(substr($link['url'],0,7) != 'mailto:'){
920            $link['url'] = str_replace('&','&amp;',$link['url']);
921            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
922        }
923        //remove double encodings in titles
924        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
925
926        // be sure there are no bad chars in url or title
927        // (we can't do this for name because it can contain an img tag)
928        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
929        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
930
931        $ret  = '';
932        $ret .= $link['pre'];
933        $ret .= '<a href="'.$link['url'].'"';
934        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
935        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
936        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
937        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
938        if(!empty($link['rel']))    $ret .= ' rel="'.$link['rel'].'"';
939        if(!empty($link['more']))   $ret .= ' '.$link['more'];
940        $ret .= '>';
941        $ret .= $link['name'];
942        $ret .= '</a>';
943        $ret .= $link['suf'];
944        return $ret;
945    }
946
947    /**
948     * Renders internal and external media
949     *
950     * @author Andreas Gohr <andi@splitbrain.org>
951     */
952    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
953                      $height=NULL, $cache=NULL, $render = true) {
954
955        $ret = '';
956
957        list($ext,$mime,$dl) = mimetype($src);
958        if(substr($mime,0,5) == 'image'){
959            // first get the $title
960            if (!is_null($title)) {
961                $title  = $this->_xmlEntities($title);
962            }elseif($ext == 'jpg' || $ext == 'jpeg'){
963                //try to use the caption from IPTC/EXIF
964                require_once(DOKU_INC.'inc/JpegMeta.php');
965                $jpeg =& new JpegMeta(mediaFN($src));
966                if($jpeg !== false) $cap = $jpeg->getTitle();
967                if($cap){
968                    $title = $this->_xmlEntities($cap);
969                }
970            }
971            if (!$render) {
972                // if the picture is not supposed to be rendered
973                // return the title of the picture
974                if (!$title) {
975                    // just show the sourcename
976                    $title = $this->_xmlEntities(basename(noNS($src)));
977                }
978                return $title;
979            }
980            //add image tag
981            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
982            $ret .= ' class="media'.$align.'"';
983
984            // make left/right alignment for no-CSS view work (feeds)
985            if($align == 'right') $ret .= ' align="right"';
986            if($align == 'left')  $ret .= ' align="left"';
987
988            if ($title) {
989                $ret .= ' title="' . $title . '"';
990                $ret .= ' alt="'   . $title .'"';
991            }else{
992                $ret .= ' alt=""';
993            }
994
995            if ( !is_null($width) )
996                $ret .= ' width="'.$this->_xmlEntities($width).'"';
997
998            if ( !is_null($height) )
999                $ret .= ' height="'.$this->_xmlEntities($height).'"';
1000
1001            $ret .= ' />';
1002
1003        }elseif($mime == 'application/x-shockwave-flash'){
1004            if (!$render) {
1005                // if the flash is not supposed to be rendered
1006                // return the title of the flash
1007                if (!$title) {
1008                    // just show the sourcename
1009                    $title = basename(noNS($src));
1010                }
1011                return $this->_xmlEntities($title);
1012            }
1013
1014            $att = array();
1015            $att['class'] = "media$align";
1016            if($align == 'right') $att['align'] = 'right';
1017            if($align == 'left')  $att['align'] = 'left';
1018            $ret .= html_flashobject(ml($src,array('cache'=>$cache)),$width,$height,
1019                                     array('quality' => 'high'),
1020                                     null,
1021                                     $att,
1022                                     $this->_xmlEntities($title));
1023        }elseif($title){
1024            // well at least we have a title to display
1025            $ret .= $this->_xmlEntities($title);
1026        }else{
1027            // just show the sourcename
1028            $ret .= $this->_xmlEntities(basename(noNS($src)));
1029        }
1030
1031        return $ret;
1032    }
1033
1034    function _xmlEntities($string) {
1035        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
1036    }
1037
1038    /**
1039     * Creates a linkid from a headline
1040     *
1041     * @param string  $title   The headline title
1042     * @param boolean $create  Create a new unique ID?
1043     * @author Andreas Gohr <andi@splitbrain.org>
1044     */
1045    function _headerToLink($title,$create=false) {
1046        if($create){
1047            return sectionID($title,$this->headers);
1048        }else{
1049            $check = false;
1050            return sectionID($title,$check);
1051        }
1052    }
1053
1054    /**
1055     * Construct a title and handle images in titles
1056     *
1057     * @author Harry Fuecks <hfuecks@gmail.com>
1058     */
1059    function _getLinkTitle($title, $default, & $isImage, $id=NULL, $linktype='content') {
1060        global $conf;
1061
1062        $isImage = false;
1063        if ( is_null($title) || trim($title)=='') {
1064            if (useHeading($linktype) && $id) {
1065                $heading = p_get_first_heading($id,true);
1066                if ($heading) {
1067                    return $this->_xmlEntities($heading);
1068                }
1069            }
1070            return $this->_xmlEntities($default);
1071        } else if ( is_array($title) ) {
1072            $isImage = true;
1073            return $this->_imageTitle($title);
1074        } else {
1075            return $this->_xmlEntities($title);
1076        }
1077    }
1078
1079    /**
1080     * Returns an HTML code for images used in link titles
1081     *
1082     * @todo Resolve namespace on internal images
1083     * @author Andreas Gohr <andi@splitbrain.org>
1084     */
1085    function _imageTitle($img) {
1086        return $this->_media($img['src'],
1087                              $img['title'],
1088                              $img['align'],
1089                              $img['width'],
1090                              $img['height'],
1091                              $img['cache']);
1092    }
1093
1094    /**
1095     * _getMediaLinkConf is a helperfunction to internalmedia() and externalmedia()
1096     * which returns a basic link to a media.
1097     *
1098     * @author Pierre Spring <pierre.spring@liip.ch>
1099     * @param string $src
1100     * @param string $title
1101     * @param string $align
1102     * @param string $width
1103     * @param string $height
1104     * @param string $cache
1105     * @param string $render
1106     * @access protected
1107     * @return array
1108     */
1109    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render)
1110    {
1111        global $conf;
1112
1113        $link = array();
1114        $link['class']  = 'media';
1115        $link['style']  = '';
1116        $link['pre']    = '';
1117        $link['suf']    = '';
1118        $link['more']   = '';
1119        $link['target'] = $conf['target']['media'];
1120        $link['title']  = $this->_xmlEntities($src);
1121        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1122
1123        return $link;
1124    }
1125
1126
1127}
1128
1129//Setup VIM: ex: et ts=4 enc=utf-8 :
1130