xref: /dokuwiki/inc/parser/xhtml.php (revision da9f31c579b1d16d9a1a27ff627fa4e68a70531f)
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
291        $this->doc .= '<a href="#fn__'.$id.'" name="fnt__'.$id.'" id="fnt__'.$id.'" class="fn_top">'.$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 and trailing blank line
391            $text = preg_replace('/^\s*?\n/','',$text);
392            $text = preg_replace('/\s*?\n$/','',$text);
393            $this->doc .= p_xhtml_cached_geshi($text, $language);
394        }
395    }
396
397    function acronym($acronym) {
398
399        if ( array_key_exists($acronym, $this->acronyms) ) {
400
401            $title = $this->_xmlEntities($this->acronyms[$acronym]);
402
403            $this->doc .= '<acronym title="'.$title
404                .'">'.$this->_xmlEntities($acronym).'</acronym>';
405
406        } else {
407            $this->doc .= $this->_xmlEntities($acronym);
408        }
409    }
410
411    function smiley($smiley) {
412        if ( array_key_exists($smiley, $this->smileys) ) {
413            $title = $this->_xmlEntities($this->smileys[$smiley]);
414            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
415                '" class="middle" alt="'.
416                    $this->_xmlEntities($smiley).'" />';
417        } else {
418            $this->doc .= $this->_xmlEntities($smiley);
419        }
420    }
421
422    /*
423    * not used
424    function wordblock($word) {
425        if ( array_key_exists($word, $this->badwords) ) {
426            $this->doc .= '** BLEEP **';
427        } else {
428            $this->doc .= $this->_xmlEntities($word);
429        }
430    }
431    */
432
433    function entity($entity) {
434        if ( array_key_exists($entity, $this->entities) ) {
435            $this->doc .= $this->entities[$entity];
436        } else {
437            $this->doc .= $this->_xmlEntities($entity);
438        }
439    }
440
441    function multiplyentity($x, $y) {
442        $this->doc .= "$x&times;$y";
443    }
444
445    function singlequoteopening() {
446        $this->doc .= "&lsquo;";
447    }
448
449    function singlequoteclosing() {
450        $this->doc .= "&rsquo;";
451    }
452
453    function doublequoteopening() {
454        $this->doc .= "&ldquo;";
455    }
456
457    function doublequoteclosing() {
458        $this->doc .= "&rdquo;";
459    }
460
461    /**
462    */
463    function camelcaselink($link) {
464      $this->internallink($link,$link);
465    }
466
467
468    function locallink($hash, $name = NULL){
469        global $ID;
470        $name  = $this->_getLinkTitle($name, $hash, $isImage);
471        $hash  = $this->_headerToLink($hash);
472        $title = $ID.' &crarr;';
473        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
474        $this->doc .= $name;
475        $this->doc .= '</a>';
476    }
477
478    /**
479     * Render an internal Wiki Link
480     *
481     * $search and $returnonly are not for the renderer but are used
482     * elsewhere - no need to implement them in other renderers
483     *
484     * @author Andreas Gohr <andi@splitbrain.org>
485     */
486    function internallink($id, $name = NULL, $search=NULL,$returnonly=false) {
487        global $conf;
488        global $ID;
489        // default name is based on $id as given
490        $default = $this->_simpleTitle($id);
491        // now first resolve and clean up the $id
492        resolve_pageid(getNS($ID),$id,$exists);
493        $name = $this->_getLinkTitle($name, $default, $isImage, $id);
494        if ( !$isImage ) {
495            if ( $exists ) {
496                $class='wikilink1';
497            } else {
498                $class='wikilink2';
499            }
500        } else {
501            $class='media';
502        }
503
504        //keep hash anchor
505        list($id,$hash) = explode('#',$id,2);
506
507        //prepare for formating
508        $link['target'] = $conf['target']['wiki'];
509        $link['style']  = '';
510        $link['pre']    = '';
511        $link['suf']    = '';
512        // highlight link to current page
513        if ($id == $ID) {
514            $link['pre']    = '<span class="curid">';
515            $link['suf']    = '</span>';
516        }
517        $link['more']   = '';
518        $link['class']  = $class;
519        $link['url']    = wl($id);
520        $link['name']   = $name;
521        $link['title']  = $id;
522        //add search string
523        if($search){
524            ($conf['userewrite']) ? $link['url'].='?s=' : $link['url'].='&amp;s=';
525            $link['url'] .= rawurlencode($search);
526        }
527
528        //keep hash
529        if($hash) $link['url'].='#'.$hash;
530
531        //output formatted
532        if($returnonly){
533            return $this->_formatLink($link);
534        }else{
535            $this->doc .= $this->_formatLink($link);
536        }
537    }
538
539    function externallink($url, $name = NULL) {
540        global $conf;
541
542        $name = $this->_getLinkTitle($name, $url, $isImage);
543
544        if ( !$isImage ) {
545            $class='urlextern';
546        } else {
547            $class='media';
548        }
549
550        //prepare for formating
551        $link['target'] = $conf['target']['extern'];
552        $link['style']  = '';
553        $link['pre']    = '';
554        $link['suf']    = '';
555        $link['more']   = '';
556        $link['class']  = $class;
557        $link['url']    = $url;
558
559        $link['name']   = $name;
560        $link['title']  = $this->_xmlEntities($url);
561        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
562
563        //output formatted
564        $this->doc .= $this->_formatLink($link);
565    }
566
567    /**
568    */
569    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
570        global $conf;
571
572        $link = array();
573        $link['target'] = $conf['target']['interwiki'];
574        $link['pre']    = '';
575        $link['suf']    = '';
576        $link['more']   = '';
577        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
578
579        //get interwiki URL
580        if ( isset($this->interwiki[$wikiName]) ) {
581            $url = $this->interwiki[$wikiName];
582        } else {
583            // Default to Google I'm feeling lucky
584            $url = 'http://www.google.com/search?q={URL}&amp;btnI=lucky';
585            $wikiName = 'go';
586        }
587
588        if ( !$isImage ) {
589            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName);
590            $link['class'] = "interwiki iw_$class";
591        } else {
592            $link['class'] = 'media';
593        }
594
595        //do we stay at the same server? Use local target
596        if( strpos($url,DOKU_URL) === 0 ){
597            $link['target'] = $conf['target']['wiki'];
598        }
599
600        //split into hash and url part
601        list($wikiUri,$hash) = explode('#',$wikiUri,2);
602
603        //replace placeholder
604        if(preg_match('#\{(URL|NAME|SCHEME|HOST|PORT|PATH|QUERY)\}#',$url)){
605            //use placeholders
606            $url = str_replace('{URL}',rawurlencode($wikiUri),$url);
607            $url = str_replace('{NAME}',$wikiUri,$url);
608            $parsed = parse_url($wikiUri);
609            if(!$parsed['port']) $parsed['port'] = 80;
610            $url = str_replace('{SCHEME}',$parsed['scheme'],$url);
611            $url = str_replace('{HOST}',$parsed['host'],$url);
612            $url = str_replace('{PORT}',$parsed['port'],$url);
613            $url = str_replace('{PATH}',$parsed['path'],$url);
614            $url = str_replace('{QUERY}',$parsed['query'],$url);
615            $link['url'] = $url;
616        }else{
617            //default
618            $link['url'] = $url.rawurlencode($wikiUri);
619        }
620        if($hash) $link['url'] .= '#'.rawurlencode($hash);
621
622        $link['title'] = htmlspecialchars($link['url']);
623
624        //output formatted
625        $this->doc .= $this->_formatLink($link);
626    }
627
628    /**
629     */
630    function windowssharelink($url, $name = NULL) {
631        global $conf;
632        global $lang;
633        //simple setup
634        $link['target'] = $conf['target']['windows'];
635        $link['pre']    = '';
636        $link['suf']   = '';
637        $link['style']  = '';
638        //Display error on browsers other than IE
639        $link['more'] = 'onclick="if(document.all == null){alert(\''.
640                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).
641                        '\');}" onkeypress="if(document.all == null){alert(\''.
642                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).'\');}"';
643
644        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
645        if ( !$isImage ) {
646            $link['class'] = 'windows';
647        } else {
648            $link['class'] = 'media';
649        }
650
651
652        $link['title'] = $this->_xmlEntities($url);
653        $url = str_replace('\\','/',$url);
654        $url = 'file:///'.$url;
655        $link['url'] = $url;
656
657        //output formatted
658        $this->doc .= $this->_formatLink($link);
659    }
660
661    function emaillink($address, $name = NULL) {
662        global $conf;
663        //simple setup
664        $link = array();
665        $link['target'] = '';
666        $link['pre']    = '';
667        $link['suf']   = '';
668        $link['style']  = '';
669        $link['more']   = '';
670
671        $name = $this->_getLinkTitle($name, '', $isImage);
672        if ( !$isImage ) {
673            $link['class']='mail JSnocheck';
674        } else {
675            $link['class']='media JSnocheck';
676        }
677
678        $address = $this->_xmlEntities($address);
679        $address = obfuscate($address);
680        $title   = $address;
681
682        if(empty($name)){
683            $name = $address;
684        }
685#elseif($isImage{
686#            $name = $this->_xmlEntities($name);
687#        }
688
689        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
690
691        $link['url']   = 'mailto:'.$address;
692        $link['name']  = $name;
693        $link['title'] = $title;
694
695        //output formatted
696        $this->doc .= $this->_formatLink($link);
697    }
698
699    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
700                            $height=NULL, $cache=NULL, $linking=NULL) {
701        global $conf;
702        global $ID;
703        resolve_mediaid(getNS($ID),$src, $exists);
704
705        $link = array();
706        $link['class']  = 'media';
707        $link['style']  = '';
708        $link['pre']    = '';
709        $link['suf']    = '';
710        $link['more']   = '';
711        $link['target'] = $conf['target']['media'];
712        $noLink = false;
713
714        $link['title']  = $this->_xmlEntities($src);
715        list($ext,$mime) = mimetype($src);
716        if(substr($mime,0,5) == 'image'){
717             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
718         }elseif($mime == 'application/x-shockwave-flash'){
719             // don't link flash movies
720             $noLink = true;
721         }else{
722             // add file icons
723             $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
724             $link['class'] .= ' mediafile mf_'.$class;
725             $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
726         }
727         $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
728
729         //output formatted
730         if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
731         else $this->doc .= $this->_formatLink($link);
732    }
733
734    /**
735     * @todo don't add link for flash
736     */
737    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
738                            $height=NULL, $cache=NULL, $linking=NULL) {
739        global $conf;
740
741        $link = array();
742        $link['class']  = 'media';
743        $link['style']  = '';
744        $link['pre']    = '';
745        $link['suf']    = '';
746        $link['more']   = '';
747        $link['target'] = $conf['target']['media'];
748
749        $link['title']  = $this->_xmlEntities($src);
750        $link['url']    = ml($src,array('cache'=>$cache));
751        $link['name']   = $this->_media ($src, $title, $align, $width, $height, $cache);
752        $noLink = false;
753
754        list($ext,$mime) = mimetype($src);
755        if(substr($mime,0,5) == 'image'){
756             // link only jpeg images
757             // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
758        }elseif($mime == 'application/x-shockwave-flash'){
759             // don't link flash movies
760             $noLink = true;
761        }else{
762             // add file icons
763             $link['class'] .= ' mediafile mf_'.$ext;
764         }
765
766        //output formatted
767        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
768        else $this->doc .= $this->_formatLink($link);
769    }
770
771    /**
772     * Renders an RSS feed
773     *
774     * @author Andreas Gohr <andi@splitbrain.org>
775     */
776    function rss ($url,$params){
777        global $lang;
778        global $conf;
779
780        require_once(DOKU_INC.'inc/FeedParser.php');
781        $feed = new FeedParser();
782        $feed->feed_url($url);
783
784        //disable warning while fetching
785        if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); }
786        $rc = $feed->init();
787        if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); }
788
789        //decide on start and end
790        if($params['reverse']){
791            $mod = -1;
792            $start = $feed->get_item_quantity()-1;
793            $end   = $start - ($params['max']);
794            $end   = ($end < -1) ? -1 : $end;
795        }else{
796            $mod   = 1;
797            $start = 0;
798            $end   = $feed->get_item_quantity();
799            $end   = ($end > $params['max']) ? $params['max'] : $end;;
800        }
801
802        $this->doc .= '<ul class="rss">';
803        if($rc){
804            for ($x = $start; $x != $end; $x += $mod) {
805                $item = $feed->get_item($x);
806                $this->doc .= '<li><div class="li">';
807                $this->externallink($item->get_permalink(),
808                                    $item->get_title());
809                if($params['author']){
810                    $author = $item->get_author(0);
811                    if($author){
812                        $name = $author->get_name();
813                        if(!$name) $name = $author->get_email();
814                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
815                    }
816                }
817                if($params['date']){
818                    $this->doc .= ' ('.$item->get_date($conf['dformat']).')';
819                }
820                if($params['details']){
821                    $this->doc .= '<div class="detail">';
822                    if($htmlok){
823                        $this->doc .= $item->get_description();
824                    }else{
825                        $this->doc .= strip_tags($item->get_description());
826                    }
827                    $this->doc .= '</div>';
828                }
829
830                $this->doc .= '</div></li>';
831            }
832        }else{
833            $this->doc .= '<li><div class="li">';
834            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
835            $this->externallink($url);
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        $this->doc .= '<table class="inline">'.DOKU_LF;
844    }
845
846    function table_close(){
847        $this->doc .= '</table>'.DOKU_LF;
848    }
849
850    function tablerow_open(){
851        $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
852    }
853
854    function tablerow_close(){
855        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
856    }
857
858    function tableheader_open($colspan = 1, $align = NULL){
859        $this->doc .= '<th';
860        if ( !is_null($align) ) {
861            $this->doc .= ' class="'.$align.'align"';
862        }
863        if ( $colspan > 1 ) {
864            $this->doc .= ' colspan="'.$colspan.'"';
865        }
866        $this->doc .= '>';
867    }
868
869    function tableheader_close(){
870        $this->doc .= '</th>';
871    }
872
873    function tablecell_open($colspan = 1, $align = NULL){
874        $this->doc .= '<td';
875        if ( !is_null($align) ) {
876            $this->doc .= ' class="'.$align.'align"';
877        }
878        if ( $colspan > 1 ) {
879            $this->doc .= ' colspan="'.$colspan.'"';
880        }
881        $this->doc .= '>';
882    }
883
884    function tablecell_close(){
885        $this->doc .= '</td>';
886    }
887
888    //----------------------------------------------------------
889    // Utils
890
891    /**
892     * Build a link
893     *
894     * Assembles all parts defined in $link returns HTML for the link
895     *
896     * @author Andreas Gohr <andi@splitbrain.org>
897     */
898    function _formatLink($link){
899        //make sure the url is XHTML compliant (skip mailto)
900        if(substr($link['url'],0,7) != 'mailto:'){
901            $link['url'] = str_replace('&','&amp;',$link['url']);
902            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
903        }
904        //remove double encodings in titles
905        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
906
907        // be sure there are no bad chars in url or title
908        // (we can't do this for name because it can contain an img tag)
909        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
910        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
911
912        $ret  = '';
913        $ret .= $link['pre'];
914        $ret .= '<a href="'.$link['url'].'"';
915        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
916        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
917        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
918        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
919        if(!empty($link['more']))   $ret .= ' '.$link['more'];
920        $ret .= '>';
921        $ret .= $link['name'];
922        $ret .= '</a>';
923        $ret .= $link['suf'];
924        return $ret;
925    }
926
927    /**
928     * Renders internal and external media
929     *
930     * @author Andreas Gohr <andi@splitbrain.org>
931     */
932    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
933                      $height=NULL, $cache=NULL) {
934
935        $ret = '';
936
937        list($ext,$mime) = mimetype($src);
938        if(substr($mime,0,5) == 'image'){
939            //add image tag
940            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
941            $ret .= ' class="media'.$align.'"';
942
943            if (!is_null($title)) {
944                $ret .= ' title="'.$this->_xmlEntities($title).'"';
945                $ret .= ' alt="'.$this->_xmlEntities($title).'"';
946            }elseif($ext == 'jpg' || $ext == 'jpeg'){
947                //try to use the caption from IPTC/EXIF
948                require_once(DOKU_INC.'inc/JpegMeta.php');
949                $jpeg =& new JpegMeta(mediaFN($src));
950                if($jpeg !== false) $cap = $jpeg->getTitle();
951                if($cap){
952                    $ret .= ' title="'.$this->_xmlEntities($cap).'"';
953                    $ret .= ' alt="'.$this->_xmlEntities($cap).'"';
954                }
955            }else{
956                $ret .= ' alt=""';
957            }
958
959            if ( !is_null($width) )
960                $ret .= ' width="'.$this->_xmlEntities($width).'"';
961
962            if ( !is_null($height) )
963                $ret .= ' height="'.$this->_xmlEntities($height).'"';
964
965            $ret .= ' />';
966
967        }elseif($mime == 'application/x-shockwave-flash'){
968            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
969                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
970            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
971            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
972            $ret .= '>'.DOKU_LF;
973            $ret .= '<param name="movie" value="'.ml($src).'" />'.DOKU_LF;
974            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
975            $ret .= '<embed src="'.ml($src).'"'.
976                    ' quality="high"';
977            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
978            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
979            $ret .= ' type="application/x-shockwave-flash"'.
980                    ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF;
981            $ret .= '</object>'.DOKU_LF;
982
983        }elseif($title){
984            // well at least we have a title to display
985            $ret .= $this->_xmlEntities($title);
986        }else{
987            // just show the sourcename
988            $ret .= $this->_xmlEntities(basename(noNS($src)));
989        }
990
991        return $ret;
992    }
993
994    function _xmlEntities($string) {
995        return htmlspecialchars($string);
996    }
997
998    /**
999     * Creates a linkid from a headline
1000     *
1001     * @param string  $title   The headline title
1002     * @param boolean $create  Create a new unique ID?
1003     * @author Andreas Gohr <andi@splitbrain.org>
1004     */
1005    function _headerToLink($title,$create=false) {
1006        $title = str_replace(':','',cleanID($title));
1007        $title = ltrim($title,'0123456789._-');
1008        if(empty($title)) $title='section';
1009
1010        if($create){
1011            // make sure tiles are unique
1012            $num = '';
1013            while(in_array($title.$num,$this->headers)){
1014                ($num) ? $num++ : $num = 1;
1015            }
1016            $title = $title.$num;
1017            $this->headers[] = $title;
1018        }
1019
1020        return $title;
1021    }
1022
1023    /**
1024     * Construct a title and handle images in titles
1025     *
1026     * @author Harry Fuecks <hfuecks@gmail.com>
1027     */
1028    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
1029        global $conf;
1030
1031        $isImage = false;
1032        if ( is_null($title) ) {
1033            if ($conf['useheading'] && $id) {
1034                $heading = p_get_first_heading($id);
1035                if ($heading) {
1036                    return $this->_xmlEntities($heading);
1037                }
1038            }
1039            return $this->_xmlEntities($default);
1040        } else if ( is_string($title) ) {
1041            return $this->_xmlEntities($title);
1042        } else if ( is_array($title) ) {
1043            $isImage = true;
1044            return $this->_imageTitle($title);
1045        }
1046    }
1047
1048    /**
1049     * Returns an HTML code for images used in link titles
1050     *
1051     * @todo Resolve namespace on internal images
1052     * @author Andreas Gohr <andi@splitbrain.org>
1053     */
1054    function _imageTitle($img) {
1055        return $this->_media($img['src'],
1056                              $img['title'],
1057                              $img['align'],
1058                              $img['width'],
1059                              $img['height'],
1060                              $img['cache']);
1061    }
1062}
1063
1064//Setup VIM: ex: et ts=4 enc=utf-8 :
1065