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