xref: /dokuwiki/inc/parser/xhtml.php (revision b038bf62450c4c0aceb9f2cf7d4e24335d6e7bdf)
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
563        $params = '';
564        $parts = explode('?', $id, 2);
565        if (count($parts) === 2) {
566            $id = $parts[0];
567            $params = $parts[1];
568        }
569
570        // default name is based on $id as given
571        $default = $this->_simpleTitle($id);
572
573        // now first resolve and clean up the $id
574        resolve_pageid(getNS($ID),$id,$exists);
575        $name = $this->_getLinkTitle($name, $default, $isImage, $id, $linktype);
576        if ( !$isImage ) {
577            if ( $exists ) {
578                $class='wikilink1';
579            } else {
580                $class='wikilink2';
581                $link['rel']='nofollow';
582            }
583        } else {
584            $class='media';
585        }
586
587        //keep hash anchor
588        list($id,$hash) = explode('#',$id,2);
589        if(!empty($hash)) $hash = $this->_headerToLink($hash);
590
591        //prepare for formating
592        $link['target'] = $conf['target']['wiki'];
593        $link['style']  = '';
594        $link['pre']    = '';
595        $link['suf']    = '';
596        // highlight link to current page
597        if ($id == $ID) {
598            $link['pre']    = '<span class="curid">';
599            $link['suf']    = '</span>';
600        }
601        $link['more']   = '';
602        $link['class']  = $class;
603        $link['url']    = wl($id, $params);
604        $link['name']   = $name;
605        $link['title']  = $id;
606        //add search string
607        if($search){
608            ($conf['userewrite']) ? $link['url'].='?' : $link['url'].='&amp;';
609            if(is_array($search)){
610                $search = array_map('rawurlencode',$search);
611                $link['url'] .= 's[]='.join('&amp;s[]=',$search);
612            }else{
613                $link['url'] .= 's='.rawurlencode($search);
614            }
615        }
616
617        //keep hash
618        if($hash) $link['url'].='#'.$hash;
619
620        //output formatted
621        if($returnonly){
622            return $this->_formatLink($link);
623        }else{
624            $this->doc .= $this->_formatLink($link);
625        }
626    }
627
628    function externallink($url, $name = NULL) {
629        global $conf;
630
631        $name = $this->_getLinkTitle($name, $url, $isImage);
632
633        if ( !$isImage ) {
634            $class='urlextern';
635        } else {
636            $class='media';
637        }
638
639        //prepare for formating
640        $link['target'] = $conf['target']['extern'];
641        $link['style']  = '';
642        $link['pre']    = '';
643        $link['suf']    = '';
644        $link['more']   = '';
645        $link['class']  = $class;
646        $link['url']    = $url;
647
648        $link['name']   = $name;
649        $link['title']  = $this->_xmlEntities($url);
650        if($conf['relnofollow']) $link['more'] .= ' rel="nofollow"';
651
652        //output formatted
653        $this->doc .= $this->_formatLink($link);
654    }
655
656    /**
657    */
658    function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
659        global $conf;
660
661        $link = array();
662        $link['target'] = $conf['target']['interwiki'];
663        $link['pre']    = '';
664        $link['suf']    = '';
665        $link['more']   = '';
666        $link['name']   = $this->_getLinkTitle($name, $wikiUri, $isImage);
667
668        //get interwiki URL
669        $url = $this->_resolveInterWiki($wikiName,$wikiUri);
670
671        if ( !$isImage ) {
672            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$wikiName);
673            $link['class'] = "interwiki iw_$class";
674        } else {
675            $link['class'] = 'media';
676        }
677
678        //do we stay at the same server? Use local target
679        if( strpos($url,DOKU_URL) === 0 ){
680            $link['target'] = $conf['target']['wiki'];
681        }
682
683        $link['url'] = $url;
684        $link['title'] = htmlspecialchars($link['url']);
685
686        //output formatted
687        $this->doc .= $this->_formatLink($link);
688    }
689
690    /**
691     */
692    function windowssharelink($url, $name = NULL) {
693        global $conf;
694        global $lang;
695        //simple setup
696        $link['target'] = $conf['target']['windows'];
697        $link['pre']    = '';
698        $link['suf']   = '';
699        $link['style']  = '';
700
701        $link['name'] = $this->_getLinkTitle($name, $url, $isImage);
702        if ( !$isImage ) {
703            $link['class'] = 'windows';
704        } else {
705            $link['class'] = 'media';
706        }
707
708
709        $link['title'] = $this->_xmlEntities($url);
710        $url = str_replace('\\','/',$url);
711        $url = 'file:///'.$url;
712        $link['url'] = $url;
713
714        //output formatted
715        $this->doc .= $this->_formatLink($link);
716    }
717
718    function emaillink($address, $name = NULL) {
719        global $conf;
720        //simple setup
721        $link = array();
722        $link['target'] = '';
723        $link['pre']    = '';
724        $link['suf']   = '';
725        $link['style']  = '';
726        $link['more']   = '';
727
728        $name = $this->_getLinkTitle($name, '', $isImage);
729        if ( !$isImage ) {
730            $link['class']='mail JSnocheck';
731        } else {
732            $link['class']='media JSnocheck';
733        }
734
735        $address = $this->_xmlEntities($address);
736        $address = obfuscate($address);
737        $title   = $address;
738
739        if(empty($name)){
740            $name = $address;
741        }
742
743        if($conf['mailguard'] == 'visible') $address = rawurlencode($address);
744
745        $link['url']   = 'mailto:'.$address;
746        $link['name']  = $name;
747        $link['title'] = $title;
748
749        //output formatted
750        $this->doc .= $this->_formatLink($link);
751    }
752
753    function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
754                            $height=NULL, $cache=NULL, $linking=NULL) {
755        global $ID;
756        list($src,$hash) = explode('#',$src,2);
757        resolve_mediaid(getNS($ID),$src, $exists);
758
759        $noLink = false;
760        $render = ($linking == 'linkonly') ? false : true;
761        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
762
763        list($ext,$mime,$dl) = mimetype($src,false);
764        if(substr($mime,0,5) == 'image' && $render){
765            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),($linking=='direct'));
766        }elseif($mime == 'application/x-shockwave-flash' && $render){
767            // don't link flash movies
768            $noLink = true;
769        }else{
770            // add file icons
771            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
772            $link['class'] .= ' mediafile mf_'.$class;
773            $link['url'] = ml($src,array('id'=>$ID,'cache'=>$cache),true);
774        }
775
776        if($hash) $link['url'] .= '#'.$hash;
777
778        //markup non existing files
779        if (!$exists)
780          $link['class'] .= ' wikilink2';
781
782        //output formatted
783        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
784        else $this->doc .= $this->_formatLink($link);
785    }
786
787    function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
788                            $height=NULL, $cache=NULL, $linking=NULL) {
789        list($src,$hash) = explode('#',$src,2);
790        $noLink = false;
791        $render = ($linking == 'linkonly') ? false : true;
792        $link = $this->_getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render);
793
794        $link['url']    = ml($src,array('cache'=>$cache));
795
796        list($ext,$mime,$dl) = mimetype($src,false);
797        if(substr($mime,0,5) == 'image' && $render){
798            // link only jpeg images
799            // if ($ext != 'jpg' && $ext != 'jpeg') $noLink = true;
800        }elseif($mime == 'application/x-shockwave-flash' && $render){
801            // don't link flash movies
802            $noLink = true;
803        }else{
804            // add file icons
805            $class = preg_replace('/[^_\-a-z0-9]+/i','_',$ext);
806            $link['class'] .= ' mediafile mf_'.$class;
807        }
808
809        if($hash) $link['url'] .= '#'.$hash;
810
811        //output formatted
812        if ($linking == 'nolink' || $noLink) $this->doc .= $link['name'];
813        else $this->doc .= $this->_formatLink($link);
814    }
815
816    /**
817     * Renders an RSS feed
818     *
819     * @author Andreas Gohr <andi@splitbrain.org>
820     */
821    function rss ($url,$params){
822        global $lang;
823        global $conf;
824
825        require_once(DOKU_INC.'inc/FeedParser.php');
826        $feed = new FeedParser();
827        $feed->set_feed_url($url);
828
829        //disable warning while fetching
830        if (!defined('DOKU_E_LEVEL')) { $elvl = error_reporting(E_ERROR); }
831        $rc = $feed->init();
832        if (!defined('DOKU_E_LEVEL')) { error_reporting($elvl); }
833
834        //decide on start and end
835        if($params['reverse']){
836            $mod = -1;
837            $start = $feed->get_item_quantity()-1;
838            $end   = $start - ($params['max']);
839            $end   = ($end < -1) ? -1 : $end;
840        }else{
841            $mod   = 1;
842            $start = 0;
843            $end   = $feed->get_item_quantity();
844            $end   = ($end > $params['max']) ? $params['max'] : $end;;
845        }
846
847        $this->doc .= '<ul class="rss">';
848        if($rc){
849            for ($x = $start; $x != $end; $x += $mod) {
850                $item = $feed->get_item($x);
851                $this->doc .= '<li><div class="li">';
852                // support feeds without links
853                $lnkurl = $item->get_permalink();
854                if($lnkurl){
855                    // title is escaped by SimplePie, we unescape here because it
856                    // is escaped again in externallink() FS#1705
857                    $this->externallink($item->get_permalink(),
858                                        htmlspecialchars_decode($item->get_title()));
859                }else{
860                    $this->doc .= ' '.$item->get_title();
861                }
862                if($params['author']){
863                    $author = $item->get_author(0);
864                    if($author){
865                        $name = $author->get_name();
866                        if(!$name) $name = $author->get_email();
867                        if($name) $this->doc .= ' '.$lang['by'].' '.$name;
868                    }
869                }
870                if($params['date']){
871                    $this->doc .= ' ('.$item->get_local_date($conf['dformat']).')';
872                }
873                if($params['details']){
874                    $this->doc .= '<div class="detail">';
875                    if($conf['htmlok']){
876                        $this->doc .= $item->get_description();
877                    }else{
878                        $this->doc .= strip_tags($item->get_description());
879                    }
880                    $this->doc .= '</div>';
881                }
882
883                $this->doc .= '</div></li>';
884            }
885        }else{
886            $this->doc .= '<li><div class="li">';
887            $this->doc .= '<em>'.$lang['rssfailed'].'</em>';
888            $this->externallink($url);
889            if($conf['allowdebug']){
890                $this->doc .= '<!--'.hsc($feed->error).'-->';
891            }
892            $this->doc .= '</div></li>';
893        }
894        $this->doc .= '</ul>';
895    }
896
897    // $numrows not yet implemented
898    function table_open($maxcols = NULL, $numrows = NULL, $pos){
899        global $lang;
900        // initialize the row counter used for classes
901        $this->_counter['row_counter'] = 0;
902        $this->doc .= '<table class="inline ' . $this->startSectionEdit($pos, 'table') . '">'.DOKU_LF;
903    }
904
905    function table_close($pos){
906        $this->doc .= '</table>'.DOKU_LF;
907        $this->finishSectionEdit($pos);
908    }
909
910    function tablerow_open(){
911        // initialize the cell counter used for classes
912        $this->_counter['cell_counter'] = 0;
913        $class = 'row' . $this->_counter['row_counter']++;
914        $this->doc .= DOKU_TAB . '<tr class="'.$class.'">' . DOKU_LF . DOKU_TAB . DOKU_TAB;
915    }
916
917    function tablerow_close(){
918        $this->doc .= DOKU_LF . DOKU_TAB . '</tr>' . DOKU_LF;
919    }
920
921    function tableheader_open($colspan = 1, $align = NULL, $rowspan = 1){
922        $class = 'class="col' . $this->_counter['cell_counter']++;
923        if ( !is_null($align) ) {
924            $class .= ' '.$align.'align';
925        }
926        $class .= '"';
927        $this->doc .= '<th ' . $class;
928        if ( $colspan > 1 ) {
929            $this->_counter['cell_counter'] += $colspan-1;
930            $this->doc .= ' colspan="'.$colspan.'"';
931        }
932        if ( $rowspan > 1 ) {
933            $this->doc .= ' rowspan="'.$rowspan.'"';
934        }
935        $this->doc .= '>';
936    }
937
938    function tableheader_close(){
939        $this->doc .= '</th>';
940    }
941
942    function tablecell_open($colspan = 1, $align = NULL, $rowspan = 1){
943        $class = 'class="col' . $this->_counter['cell_counter']++;
944        if ( !is_null($align) ) {
945            $class .= ' '.$align.'align';
946        }
947        $class .= '"';
948        $this->doc .= '<td '.$class;
949        if ( $colspan > 1 ) {
950            $this->_counter['cell_counter'] += $colspan-1;
951            $this->doc .= ' colspan="'.$colspan.'"';
952        }
953        if ( $rowspan > 1 ) {
954            $this->doc .= ' rowspan="'.$rowspan.'"';
955        }
956        $this->doc .= '>';
957    }
958
959    function tablecell_close(){
960        $this->doc .= '</td>';
961    }
962
963    //----------------------------------------------------------
964    // Utils
965
966    /**
967     * Build a link
968     *
969     * Assembles all parts defined in $link returns HTML for the link
970     *
971     * @author Andreas Gohr <andi@splitbrain.org>
972     */
973    function _formatLink($link){
974        //make sure the url is XHTML compliant (skip mailto)
975        if(substr($link['url'],0,7) != 'mailto:'){
976            $link['url'] = str_replace('&','&amp;',$link['url']);
977            $link['url'] = str_replace('&amp;amp;','&amp;',$link['url']);
978        }
979        //remove double encodings in titles
980        $link['title'] = str_replace('&amp;amp;','&amp;',$link['title']);
981
982        // be sure there are no bad chars in url or title
983        // (we can't do this for name because it can contain an img tag)
984        $link['url']   = strtr($link['url'],array('>'=>'%3E','<'=>'%3C','"'=>'%22'));
985        $link['title'] = strtr($link['title'],array('>'=>'&gt;','<'=>'&lt;','"'=>'&quot;'));
986
987        $ret  = '';
988        $ret .= $link['pre'];
989        $ret .= '<a href="'.$link['url'].'"';
990        if(!empty($link['class']))  $ret .= ' class="'.$link['class'].'"';
991        if(!empty($link['target'])) $ret .= ' target="'.$link['target'].'"';
992        if(!empty($link['title']))  $ret .= ' title="'.$link['title'].'"';
993        if(!empty($link['style']))  $ret .= ' style="'.$link['style'].'"';
994        if(!empty($link['rel']))    $ret .= ' rel="'.$link['rel'].'"';
995        if(!empty($link['more']))   $ret .= ' '.$link['more'];
996        $ret .= '>';
997        $ret .= $link['name'];
998        $ret .= '</a>';
999        $ret .= $link['suf'];
1000        return $ret;
1001    }
1002
1003    /**
1004     * Renders internal and external media
1005     *
1006     * @author Andreas Gohr <andi@splitbrain.org>
1007     */
1008    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
1009                      $height=NULL, $cache=NULL, $render = true) {
1010
1011        $ret = '';
1012
1013        list($ext,$mime,$dl) = mimetype($src);
1014        if(substr($mime,0,5) == 'image'){
1015            // first get the $title
1016            if (!is_null($title)) {
1017                $title  = $this->_xmlEntities($title);
1018            }elseif($ext == 'jpg' || $ext == 'jpeg'){
1019                //try to use the caption from IPTC/EXIF
1020                require_once(DOKU_INC.'inc/JpegMeta.php');
1021                $jpeg =new JpegMeta(mediaFN($src));
1022                if($jpeg !== false) $cap = $jpeg->getTitle();
1023                if($cap){
1024                    $title = $this->_xmlEntities($cap);
1025                }
1026            }
1027            if (!$render) {
1028                // if the picture is not supposed to be rendered
1029                // return the title of the picture
1030                if (!$title) {
1031                    // just show the sourcename
1032                    $title = $this->_xmlEntities(basename(noNS($src)));
1033                }
1034                return $title;
1035            }
1036            //add image tag
1037            $ret .= '<img src="'.ml($src,array('w'=>$width,'h'=>$height,'cache'=>$cache)).'"';
1038            $ret .= ' class="media'.$align.'"';
1039
1040            // make left/right alignment for no-CSS view work (feeds)
1041            if($align == 'right') $ret .= ' align="right"';
1042            if($align == 'left')  $ret .= ' align="left"';
1043
1044            if ($title) {
1045                $ret .= ' title="' . $title . '"';
1046                $ret .= ' alt="'   . $title .'"';
1047            }else{
1048                $ret .= ' alt=""';
1049            }
1050
1051            if ( !is_null($width) )
1052                $ret .= ' width="'.$this->_xmlEntities($width).'"';
1053
1054            if ( !is_null($height) )
1055                $ret .= ' height="'.$this->_xmlEntities($height).'"';
1056
1057            $ret .= ' />';
1058
1059        }elseif($mime == 'application/x-shockwave-flash'){
1060            if (!$render) {
1061                // if the flash is not supposed to be rendered
1062                // return the title of the flash
1063                if (!$title) {
1064                    // just show the sourcename
1065                    $title = basename(noNS($src));
1066                }
1067                return $this->_xmlEntities($title);
1068            }
1069
1070            $att = array();
1071            $att['class'] = "media$align";
1072            if($align == 'right') $att['align'] = 'right';
1073            if($align == 'left')  $att['align'] = 'left';
1074            $ret .= html_flashobject(ml($src,array('cache'=>$cache),true,'&'),$width,$height,
1075                                     array('quality' => 'high'),
1076                                     null,
1077                                     $att,
1078                                     $this->_xmlEntities($title));
1079        }elseif($title){
1080            // well at least we have a title to display
1081            $ret .= $this->_xmlEntities($title);
1082        }else{
1083            // just show the sourcename
1084            $ret .= $this->_xmlEntities(basename(noNS($src)));
1085        }
1086
1087        return $ret;
1088    }
1089
1090    function _xmlEntities($string) {
1091        return htmlspecialchars($string,ENT_QUOTES,'UTF-8');
1092    }
1093
1094    /**
1095     * Creates a linkid from a headline
1096     *
1097     * @param string  $title   The headline title
1098     * @param boolean $create  Create a new unique ID?
1099     * @author Andreas Gohr <andi@splitbrain.org>
1100     */
1101    function _headerToLink($title,$create=false) {
1102        if($create){
1103            return sectionID($title,$this->headers);
1104        }else{
1105            $check = false;
1106            return sectionID($title,$check);
1107        }
1108    }
1109
1110    /**
1111     * Construct a title and handle images in titles
1112     *
1113     * @author Harry Fuecks <hfuecks@gmail.com>
1114     */
1115    function _getLinkTitle($title, $default, & $isImage, $id=NULL, $linktype='content') {
1116        global $conf;
1117
1118        $isImage = false;
1119        if ( is_array($title) ) {
1120            $isImage = true;
1121            return $this->_imageTitle($title);
1122        } elseif ( is_null($title) || trim($title)=='') {
1123            if (useHeading($linktype) && $id) {
1124                $heading = p_get_first_heading($id,true);
1125                if ($heading) {
1126                    return $this->_xmlEntities($heading);
1127                }
1128            }
1129            return $this->_xmlEntities($default);
1130        } else {
1131            return $this->_xmlEntities($title);
1132        }
1133    }
1134
1135    /**
1136     * Returns an HTML code for images used in link titles
1137     *
1138     * @todo Resolve namespace on internal images
1139     * @author Andreas Gohr <andi@splitbrain.org>
1140     */
1141    function _imageTitle($img) {
1142        global $ID;
1143
1144        // some fixes on $img['src']
1145        // see internalmedia() and externalmedia()
1146        list($img['src'],$hash) = explode('#',$img['src'],2);
1147        if ($img['type'] == 'internalmedia') {
1148            resolve_mediaid(getNS($ID),$img['src'],$exists);
1149        }
1150
1151        return $this->_media($img['src'],
1152                              $img['title'],
1153                              $img['align'],
1154                              $img['width'],
1155                              $img['height'],
1156                              $img['cache']);
1157    }
1158
1159    /**
1160     * _getMediaLinkConf is a helperfunction to internalmedia() and externalmedia()
1161     * which returns a basic link to a media.
1162     *
1163     * @author Pierre Spring <pierre.spring@liip.ch>
1164     * @param string $src
1165     * @param string $title
1166     * @param string $align
1167     * @param string $width
1168     * @param string $height
1169     * @param string $cache
1170     * @param string $render
1171     * @access protected
1172     * @return array
1173     */
1174    function _getMediaLinkConf($src, $title, $align, $width, $height, $cache, $render)
1175    {
1176        global $conf;
1177
1178        $link = array();
1179        $link['class']  = 'media';
1180        $link['style']  = '';
1181        $link['pre']    = '';
1182        $link['suf']    = '';
1183        $link['more']   = '';
1184        $link['target'] = $conf['target']['media'];
1185        $link['title']  = $this->_xmlEntities($src);
1186        $link['name']   = $this->_media($src, $title, $align, $width, $height, $cache, $render);
1187
1188        return $link;
1189    }
1190
1191
1192}
1193
1194//Setup VIM: ex: et ts=4 enc=utf-8 :
1195