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