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