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