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