xref: /plugin/dw2pdf/renderer.php (revision 57a1b6f9c7920e3f5997e179597ea717ff928fde)
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
63        //only add items within global configured levels (doesn't check the pdf toc settings)
64        $this->toc_additem($hid, $text, $level);
65
66        $check = false;
67        $pid = sectionID($ID, $check);
68        $hid = $pid . '__' . $hid;
69
70            // add PDF bookmark
71        $bookmark = '';
72        $bmlevel = $this->actioninstance->getExportConfig('maxbookmarks');
73        if($bmlevel && $bmlevel >= $level){
74            // PDF readers choke on invalid nested levels
75
76            if ($this->lastheadlevel == -1)
77            	$this->lastheadlevel = $level;
78
79            $step = $level - $this->lastheadlevel;
80
81            if ($step > 0)
82            	$this->current_bookmark_level += 1;
83            else if ($step <0)  {
84            	$this->current_bookmark_level -= 1;
85                if ($this->current_bookmark_level < 0)
86                    $this->current_bookmark_level = 0;
87            }
88
89            $this->lastheadlevel = $level;
90
91            $bookmark = '<bookmark content="'.$this->_xmlEntities($text).'" level="'.($this->current_bookmark_level).'" />';
92        }
93
94        // print header
95        $this->doc .= DOKU_LF."<h$level>$bookmark";
96        $this->doc .= "<a name=\"$hid\">";
97        $this->doc .= $this->_xmlEntities($text);
98        $this->doc .= "</a>";
99        $this->doc .= "</h$level>".DOKU_LF;
100    }
101
102    /**
103     * Render a page local link
104     *
105     * @param string $hash hash link identifier
106     * @param string $name name for the link
107     *
108     * // modified copy of parent function
109     * @see Doku_Renderer_xhtml::locallink
110     */
111    function locallink($hash, $name = null, $returnonly = false) {
112        global $ID;
113        $name  = $this->_getLinkTitle($name, $hash, $isImage);
114        $hash  = $this->_headerToLink($hash);
115        $title = $ID.' ↵';
116
117        $check = false;
118        $pid = sectionID($ID, $check);
119
120        $this->doc .= '<a href="#'. $pid . '__' . $hash.'" title="'.$title.'" class="wikilink1">';
121        $this->doc .= $name;
122        $this->doc .= '</a>';
123    }
124
125    /**
126     * Wrap centered media in a div to center it
127     */
128    function _media ($src, $title=NULL, $align=NULL, $width=NULL,
129                      $height=NULL, $cache=NULL, $render = true) {
130
131        $out = '';
132        if($align == 'center'){
133            $out .= '<div align="center" style="text-align: center">';
134        }
135
136        $out .= parent::_media ($src, $title, $align, $width, $height, $cache, $render);
137
138        if($align == 'center'){
139            $out .= '</div>';
140        }
141
142        return $out;
143    }
144
145    /**
146     * hover info makes no sense in PDFs, so drop acronyms
147     */
148    function acronym($acronym) {
149        $this->doc .= $this->_xmlEntities($acronym);
150    }
151
152
153    /**
154     * reformat links if needed
155     */
156
157    function _formatLink($link){
158
159        // for internal links contains the title the pageid
160        if(in_array($link['title'], $this->actioninstance->getExportedPages())) {
161            list(/* $url */, $hash) = explode('#', $link['url'], 2);
162
163            $check = false;
164            $pid = sectionID($link['title'], $check);
165            $link['url'] = "#" . $pid . '__' . $hash;
166        }
167
168
169        // prefix interwiki links with interwiki icon
170        if($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/',$link['class'],$m)){
171            if(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.png')){
172                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.png';
173            }elseif(file_exists(DOKU_INC.'lib/images/interwiki/'.$m[1].'.gif')){
174                $img = DOKU_BASE.'lib/images/interwiki/'.$m[1].'.gif';
175            }else{
176                $img = DOKU_BASE.'lib/images/interwiki.png';
177            }
178
179            $link['name'] = '<img src="'.$img.'" width="16" height="16" style="vertical-align: center" class="'.$link['class'].'" />'.$link['name'];
180        }
181        return parent::_formatLink($link);
182    }
183
184    /**
185     * no obfuscation for email addresses
186     */
187    function emaillink($address, $name = NULL, $returnonly = false) {
188        global $conf;
189        $old = $conf['mailguard'];
190        $conf['mailguard'] = 'none';
191        parent::emaillink($address, $name, $returnonly);
192        $conf['mailguard'] = $old;
193    }
194
195}
196
197