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