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