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