xref: /plugin/dw2pdf/renderer.php (revision 475fd24805412d7ce8255493e88dc2cb930eed56)
1<?php
2/**
3 * DokuWiki Plugin dw2pdf (Renderer Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <gohr@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) die();
11
12if (!defined('DOKU_LF')) define('DOKU_LF', "\n");
13if (!defined('DOKU_TAB')) define('DOKU_TAB', "\t");
14require_once DOKU_INC.'inc/parser/xhtml.php';
15
16class renderer_plugin_dw2pdf extends Doku_Renderer_xhtml {
17
18    private $lastheadlevel = -1;
19    private $current_bookmark_level = 0;
20
21    /**
22     * Make available as XHTML replacement renderer
23     */
24    public function canRender($format){
25        if($format == 'xhtml') return true;
26        return false;
27    }
28
29    /**
30     * Simplified header printing with PDF bookmarks
31     */
32    function header($text, $level, $pos) {
33        if(!$text) return; //skip empty headlines
34
35        $hid = $this->_headerToLink($text,true);
36
37        // add PDF bookmark
38        $bmlevel = $this->getConf('maxbookmarks');
39        if($bmlevel && $bmlevel >= $level){
40            // PDF readers choke on invalid nested levels
41
42            if ($this->lastheadlevel == -1)
43            	$this->lastheadlevel = $level;
44
45            $step = $level - $this->lastheadlevel;
46
47            if ($step > 0)
48            	$this->current_bookmark_level += 1;
49            else if ($step <0)  {
50            	$this->current_bookmark_level -= 1;
51                if ($this->current_bookmark_level < 0)
52                    $this->current_bookmark_level = 0;
53            }
54
55            $this->lastheadlevel = $level;
56
57            $this->doc .= '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($this->current_bookmark_level).'" />';
58        }
59
60        // print header
61        $this->doc .= DOKU_LF."<h$level>";
62        $this->doc .= "<a name=\"$hid\">";
63        $this->doc .= $this->_xmlEntities($text);
64        $this->doc .= "</a>";
65        $this->doc .= "</h$level>".DOKU_LF;
66    }
67
68    /**
69     * Wrap centered media in a div to center it
70     */
71    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
72                      $height=NULL, $cache=NULL, $render = true) {
73
74        $out = '';
75        if($align == 'center'){
76            $out .= '<div align="center" style="text-align: center">';
77        }
78
79        $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);
80
81        if($align == 'center'){
82            $out .= '</div>';
83        }
84
85        return $out;
86    }
87
88    /**
89     * hover info makes no sense in PDFs, so drop acronyms
90     */
91    function acronym($acronym) {
92        $this->doc .= $this->_xmlEntities($acronym);
93    }
94
95
96    /**
97     * reformat links if needed
98     */
99
100    function _formatLink($link){
101        // prefix interwiki links with interwiki icon
102        if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){
103            if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){
104                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png';
105            }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){
106                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif';
107            }else{
108                $img = DOKU_BASE.'lib/images/interwiki.png';
109            }
110
111            $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" class="'.$link['class'].'" />'.$link['name'];
112        }
113        return parent::_formatLink($link);
114    }
115
116    /**
117     * no obfuscation for email addresses
118     */
119    function emaillink($address, $name = NULL) {
120        global $conf;
121        $old = $conf['mailguard'];
122        $conf['mailguard'] = 'none';
123        parent::emaillink($address, $name);
124        $conf['mailguard'] = $old;
125    }
126
127}
128
129