xref: /dokuwiki/inc/parser/xhtml.php (revision 60b9901b5ad71f2968a75700cc8d30454f51800a)
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        global $conf;
340        if($conf['phpok']){
341            ob_start();
342            eval($text);
343            $this->doc .= ob_get_contents();
344            ob_end_clean();
345        }else{
346            $this->file($text);
347        }
348    }
349
350    /**
351     * Insert HTML if allowed
352     *
353     * @author Andreas Gohr <andi@splitbrain.org>
354     */
355    function html($text) {
356        global $conf;
357        if($conf['htmlok']){
358          $this->doc .= $text;
359        }else{
360          $this->file($text);
361        }
362    }
363
364    function preformatted($text) {
365        $this->doc .= '<pre class="code">' . $this->_xmlEntities($text) . '</pre>'. DOKU_LF;
366    }
367
368    function file($text) {
369        $this->doc .= '<pre class="file">' . $this->_xmlEntities($text). '</pre>'. DOKU_LF;
370    }
371
372    function quote_open() {
373        $this->doc .= '<blockquote><div class="no">'.DOKU_LF;
374    }
375
376    function quote_close() {
377        $this->doc .= '</div></blockquote>'.DOKU_LF;
378    }
379
380    /**
381     * Callback for code text
382     *
383     * Uses GeSHi to highlight language syntax
384     *
385     * @author Andreas Gohr <andi@splitbrain.org>
386     */
387    function code($text, $language = NULL) {
388        global $conf;
389
390        if ( is_null($language) ) {
391            $this->preformatted($text);
392        } else {
393            //strip leading and trailing blank line
394            $text = preg_replace('/^\s*?\n/','',$text);
395            $text = preg_replace('/\s*?\n$/','',$text);
396            $this->doc .= p_xhtml_cached_geshi($text, $language);
397        }
398    }
399
400    function acronym($acronym) {
401
402        if ( array_key_exists($acronym, $this->acronyms) ) {
403
404            $title = $this->_xmlEntities($this->acronyms[$acronym]);
405
406            $this->doc .= '<acronym title="'.$title
407                .'">'.$this->_xmlEntities($acronym).'</acronym>';
408
409        } else {
410            $this->doc .= $this->_xmlEntities($acronym);
411        }
412    }
413
414    function smiley($smiley) {
415        if ( array_key_exists($smiley, $this->smileys) ) {
416            $title = $this->_xmlEntities($this->smileys[$smiley]);
417            $this->doc .= '<img src="'.DOKU_BASE.'lib/images/smileys/'.$this->smileys[$smiley].
418                '" class="middle" alt="'.
419                    $this->_xmlEntities($smiley).'" />';
420        } else {
421            $this->doc .= $this->_xmlEntities($smiley);
422        }
423    }
424
425    /*
426    * not used
427    function wordblock($word) {
428        if ( array_key_exists($word, $this->badwords) ) {
429            $this->doc .= '** BLEEP **';
430        } else {
431            $this->doc .= $this->_xmlEntities($word);
432        }
433    }
434    */
435
436    function entity($entity) {
437        if ( array_key_exists($entity, $this->entities) ) {
438            $this->doc .= $this->entities[$entity];
439        } else {
440            $this->doc .= $this->_xmlEntities($entity);
441        }
442    }
443
444    function multiplyentity($x, $y) {
445        $this->doc .= "$x&times;$y";
446    }
447
448    function singlequoteopening() {
449        global $lang;
450        $this->doc .= $lang['singlequoteopening'];
451    }
452
453    function singlequoteclosing() {
454        global $lang;
455        $this->doc .= $lang['singlequoteclosing'];
456    }
457
458    function apostrophe() {
459        global $lang;
460        $this->doc .= $lang['singlequoteclosing'];
461    }
462
463    function doublequoteopening() {
464        global $lang;
465        $this->doc .= $lang['doublequoteopening'];
466    }
467
468    function doublequoteclosing() {
469        global $lang;
470        $this->doc .= $lang['doublequoteclosing'];
471    }
472
473    /**
474    */
475    function camelcaselink($link) {
476      $this->internallink($link,$link);
477    }
478
479
480    function locallink($hash, $name = NULL){
481        global $ID;
482        $name  = $this->_getLinkTitle($name, $hash, $isImage);
483        $hash  = $this->_headerToLink($hash);
484        $title = $ID.' &crarr;';
485        $this->doc .= '<a href="#'.$hash.'" title="'.$title.'" class="wikilink1">';
486        $this->doc .= $name;
487        $this->doc .= '</a>';
488    }
489
490    /**
491     * Render an internal Wiki Link
492     *
493     * $search and $returnonly are not for the renderer but are used
494     * elsewhere - no need to implement them in other renderers
495     *
496     * @author Andreas Gohr <andi@splitbrain.org>
497     */
498    function internallink($id, $name = NULL, $search=NULL,$returnonly=false) {
499        global $conf;
500        global $ID;
501        // default name is based on $id as given
502        $default = $this->_simpleTitle($id);
503        // now first resolve and clean up the $id
504        resolve_pageid(getNS($ID),$id,$exists);
505        $name = $this->_getLinkTitle($name, $default, $isImage, $id);
506        if ( !$isImage ) {
507            if ( $exists ) {
508                $class='wikilink1';
509            } else {
510                $class='wikilink2';
511            }
512        } else {
513            $class='media';
514        }
515
516        //keep hash anchor
517        list($id,$hash) = explode('#',$id,2);
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                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).
626                        '\');}" onkeypress="if(document.all == null){alert(\''.
627                        $this->_xmlEntities($lang['nosmblinks'],ENT_QUOTES).'\');}"';
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            $this->doc .= '</div></li>';
822        }
823        $this->doc .= '</ul>';
824    }
825
826    // $numrows not yet implemented
827    function table_open($maxcols = NULL, $numrows = NULL){
828        $this->doc .= '<table class="inline">'.DOKU_LF;
829    }
830
831    function table_close(){
832        $this->doc .= '</table>'.DOKU_LF;
833    }
834
835    function tablerow_open(){
836        $this->doc .= DOKU_TAB . '<tr>' . DOKU_LF . DOKU_TAB . DOKU_TAB;
837    }
838
839    function tablerow_close(){
840        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
841    }
842
843    function tableheader_open($colspan = 1, $align = NULL){
844        $this->doc .= '<th';
845        if ( !is_null($align) ) {
846            $this->doc .= ' class="'.$align.'align"';
847        }
848        if ( $colspan > 1 ) {
849            $this->doc .= ' colspan="'.$colspan.'"';
850        }
851        $this->doc .= '>';
852    }
853
854    function tableheader_close(){
855        $this->doc .= '</th>';
856    }
857
858    function tablecell_open($colspan = 1, $align = NULL){
859        $this->doc .= '<td';
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 tablecell_close(){
870        $this->doc .= '</td>';
871    }
872
873    //----------------------------------------------------------
874    // Utils
875
876    /**
877     * Build a link
878     *
879     * Assembles all parts defined in $link returns HTML for the link
880     *
881     * @author Andreas Gohr <andi@splitbrain.org>
882     */
883    function _formatLink($link){
884        //make sure the url is XHTML compliant (skip mailto)
885        if(substr($link['url'],0,7) != 'mailto:'){
886            $link['url'] = str_replace('&','&amp;',$link['url']);
887            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
888        }
889        //remove double encodings in titles
890        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
891
892        // be sure there are no bad chars in url or title
893        // (we can't do this for name because it can contain an img tag)
894        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
895        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
896
897        $ret  = '';
898        $ret .= $link['pre'];
899        $ret .= '<a href="'.$link['url'].'"';
900        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
901        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
902        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
903        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
904        if(!empty($link['more']))   $ret .= ' '.$link['more'];
905        $ret .= '>';
906        $ret .= $link['name'];
907        $ret .= '</a>';
908        $ret .= $link['suf'];
909        return $ret;
910    }
911
912    /**
913     * Renders internal and external media
914     *
915     * @author Andreas Gohr <andi@splitbrain.org>
916     */
917    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
918                      $height=NULL, $cache=NULL) {
919
920        $ret = '';
921
922        list($ext,$mime) = mimetype($src);
923        if(substr($mime,0,5) == 'image'){
924            //add image tag
925            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
926            $ret .= ' class="media'.$align.'"';
927
928            if (!is_null($title)) {
929                $ret .= ' title="'.$this->_xmlEntities($title).'"';
930                $ret .= ' alt="'.$this->_xmlEntities($title).'"';
931            }elseif($ext == 'jpg' || $ext == 'jpeg'){
932                //try to use the caption from IPTC/EXIF
933                require_once(DOKU_INC.'inc/JpegMeta.php');
934                $jpeg =& new JpegMeta(mediaFN($src));
935                if($jpeg !== false) $cap = $jpeg->getTitle();
936                if($cap){
937                    $ret .= ' title="'.$this->_xmlEntities($cap).'"';
938                    $ret .= ' alt="'.$this->_xmlEntities($cap).'"';
939                }else{
940                    $ret .= ' alt=""';
941                }
942            }else{
943                $ret .= ' alt=""';
944            }
945
946            if ( !is_null($width) )
947                $ret .= ' width="'.$this->_xmlEntities($width).'"';
948
949            if ( !is_null($height) )
950                $ret .= ' height="'.$this->_xmlEntities($height).'"';
951
952            $ret .= ' />';
953
954        }elseif($mime == 'application/x-shockwave-flash'){
955            $ret .= '<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"'.
956                    ' codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"';
957            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
958            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
959            $ret .= '>'.DOKU_LF;
960            $ret .= '<param name="movie" value="'.ml($src).'" />'.DOKU_LF;
961            $ret .= '<param name="quality" value="high" />'.DOKU_LF;
962            $ret .= '<embed src="'.ml($src).'"'.
963                    ' quality="high"';
964            if ( !is_null($width) ) $ret .= ' width="'.$this->_xmlEntities($width).'"';
965            if ( !is_null($height) ) $ret .= ' height="'.$this->_xmlEntities($height).'"';
966            $ret .= ' type="application/x-shockwave-flash"'.
967                    ' pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>'.DOKU_LF;
968            $ret .= '</object>'.DOKU_LF;
969
970        }elseif($title){
971            // well at least we have a title to display
972            $ret .= $this->_xmlEntities($title);
973        }else{
974            // just show the sourcename
975            $ret .= $this->_xmlEntities(basename(noNS($src)));
976        }
977
978        return $ret;
979    }
980
981    function _xmlEntities($string) {
982        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
983    }
984
985    /**
986     * Creates a linkid from a headline
987     *
988     * @param string  $title   The headline title
989     * @param boolean $create  Create a new unique ID?
990     * @author Andreas Gohr <andi@splitbrain.org>
991     */
992    function _headerToLink($title,$create=false) {
993        $title = str_replace(':','',cleanID($title));
994        $title = ltrim($title,'0123456789._-');
995        if(empty($title)) $title='section';
996
997        if($create){
998            // make sure tiles are unique
999            $num = '';
1000            while(in_array($title.$num,$this->headers)){
1001                ($num) ? $num++ : $num = 1;
1002            }
1003            $title = $title.$num;
1004            $this->headers[] = $title;
1005        }
1006
1007        return $title;
1008    }
1009
1010    /**
1011     * Construct a title and handle images in titles
1012     *
1013     * @author Harry Fuecks <hfuecks@gmail.com>
1014     */
1015    function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
1016        global $conf;
1017
1018        $isImage = false;
1019        if ( is_null($title) ) {
1020            if ($conf['useheading'] && $id) {
1021                $heading = p_get_first_heading($id,true);
1022                if ($heading) {
1023                    return $this->_xmlEntities($heading);
1024                }
1025            }
1026            return $this->_xmlEntities($default);
1027        } else if ( is_string($title) ) {
1028            return $this->_xmlEntities($title);
1029        } else if ( is_array($title) ) {
1030            $isImage = true;
1031            return $this->_imageTitle($title);
1032        }
1033    }
1034
1035    /**
1036     * Returns an HTML code for images used in link titles
1037     *
1038     * @todo Resolve namespace on internal images
1039     * @author Andreas Gohr <andi@splitbrain.org>
1040     */
1041    function _imageTitle($img) {
1042        return $this->_media($img['src'],
1043                              $img['title'],
1044                              $img['align'],
1045                              $img['width'],
1046                              $img['height'],
1047                              $img['cache']);
1048    }
1049}
1050
1051//Setup VIM: ex: et ts=4 enc=utf-8 :
1052