xref: /plugin/dw2pdf/renderer.php (revision 02f9a447ac91ae961961e8bb8f6ca67bb3d2bcd5)
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
12/**
13 * Render xhtml suitable as input for mpdf library
14 */
15class renderer_plugin_dw2pdf extends Doku_Renderer_xhtml {
16
17    private $lastheadlevel = -1;
18    private $current_bookmark_level = 0;
19    /**
20     * Stores action instance
21     *
22     * @var action_plugin_dw2pdf
23     */
24    private $actioninstance = null;
25
26    /**
27     * load action plugin instance
28     */
29    public function __construct() {
30        $this->actioninstance = plugin_load('action', 'dw2pdf');
31    }
32
33    public function document_start() {
34        global $ID;
35
36        parent::document_start();
37
38        //ancher for rewritten links to included pages
39        $check = false;
40        $pid = sectionID($ID, $check);
41
42        $this->doc .= "<a name=\"{$pid}__\">";
43        $this->doc .= "</a>";
44    }
45
46    /**
47     * Make available as XHTML replacement renderer
48     */
49    public function canRender($format){
50        if($format == 'xhtml') return true;
51        return false;
52    }
53
54    /**
55     * Simplified header printing with PDF bookmarks
56     */
57    function header($text, $level, $pos) {
58        if(!$text) return; //skip empty headlines
59        global $ID;
60
61        $hid = $this->_headerToLink($text, true);
62        $check = false;
63        $pid = sectionID($ID, $check);
64        $hid = $pid . '__' . $hid;
65
66            // add PDF bookmark
67        $bookmark = '';
68        $bmlevel = $this->actioninstance->getExportConfig('maxbookmarks');
69        if($bmlevel && $bmlevel >= $level){
70            // PDF readers choke on invalid nested levels
71
72            if ($this->lastheadlevel == -1)
73            	$this->lastheadlevel = $level;
74
75            $step = $level - $this->lastheadlevel;
76
77            if ($step > 0)
78            	$this->current_bookmark_level += 1;
79            else if ($step <0)  {
80            	$this->current_bookmark_level -= 1;
81                if ($this->current_bookmark_level < 0)
82                    $this->current_bookmark_level = 0;
83            }
84
85            $this->lastheadlevel = $level;
86
87            $bookmark = '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($this->current_bookmark_level).'" />';
88        }
89
90        // print header
91        $this->doc .= DOKU_LF."<h$level>$bookmark";
92        $this->doc .= "<a name=\"$hid\">";
93        $this->doc .= $this->_xmlEntities($text);
94        $this->doc .= "</a>";
95        $this->doc .= "</h$level>".DOKU_LF;
96    }
97
98    /**
99     * Render a page local link
100     *
101     * @param string $hash hash link identifier
102     * @param string $name name for the link
103     *
104     * // modified copy of parent function
105     * @see Doku_Renderer_xhtml::locallink
106     */
107    function locallink($hash, $name = null) {
108        global $ID;
109        $name  = $this->_getLinkTitle($name, $hash, $isImage);
110        $hash  = $this->_headerToLink($hash);
111        $title = $ID.' ↵';
112
113        $check = false;
114        $pid = sectionID($ID, $check);
115
116        $this->doc .= '<a href="#'. $pid . '__' . $hash.'" title="'.$title.'" class="wikilink1">';
117        $this->doc .= $name;
118        $this->doc .= '</a>';
119    }
120
121    /**
122     * Wrap centered media in a div to center it
123     */
124    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
125                      $height=NULL, $cache=NULL, $render = true) {
126
127        $out = '';
128        if($align == 'center'){
129            $out .= '<div align="center" style="text-align: center">';
130        }
131
132        $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);
133
134        if($align == 'center'){
135            $out .= '</div>';
136        }
137
138        return $out;
139    }
140
141    /**
142     * hover info makes no sense in PDFs, so drop acronyms
143     */
144    function acronym($acronym) {
145        $this->doc .= $this->_xmlEntities($acronym);
146    }
147
148
149    /**
150     * reformat links if needed
151     */
152
153    function _formatLink($link){
154
155        // for internal links contains the title the pageid
156        if(in_array($link['title'], $this->actioninstance->getExportedPages())) {
157            list(/* $url */, $hash) = explode('#', $link['url'], 2);
158
159            $check = false;
160            $pid = sectionID($link['title'], $check);
161            $link['url'] = "#" . $pid . '__' . $hash;
162        }
163
164
165        // prefix interwiki links with interwiki icon
166        if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){
167            if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){
168                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png';
169            }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){
170                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif';
171            }else{
172                $img = DOKU_BASE.'lib/images/interwiki.png';
173            }
174
175            $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" class="'.$link['class'].'" />'.$link['name'];
176        }
177        return parent::_formatLink($link);
178    }
179
180    /**
181     * no obfuscation for email addresses
182     */
183    function emaillink($address, $name = NULL) {
184        global $conf;
185        $old = $conf['mailguard'];
186        $conf['mailguard'] = 'none';
187        parent::emaillink($address, $name);
188        $conf['mailguard'] = $old;
189    }
190
191}
192
193