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