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