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