-
Notifications
You must be signed in to change notification settings - Fork 68
/
renderer.php
292 lines (253 loc) · 8.23 KB
/
renderer.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
<?php
// phpcs:disable: PSR1.Methods.CamelCapsMethodName.NotCamelCaps
// phpcs:disable: PSR2.Methods.MethodDeclaration.Underscore
/**
* DokuWiki Plugin dw2pdf (Renderer Component)
* Render xhtml suitable as input for mpdf library
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <gohr@cosmocode.de>
*/
class renderer_plugin_dw2pdf extends Doku_Renderer_xhtml
{
private $lastHeaderLevel = -1;
private $originalHeaderLevel = 0;
private $difference = 0;
private static $header_count = [];
private static $previous_level = 0;
/**
* Stores action instance
*
* @var action_plugin_dw2pdf
*/
private $actioninstance;
/**
* load action plugin instance
*/
public function __construct()
{
$this->actioninstance = plugin_load('action', 'dw2pdf');
}
public function document_start()
{
global $ID;
parent::document_start();
//ancher for rewritten links to included pages
$check = false;
$pid = sectionID($ID, $check);
$this->doc .= "<a name=\"{$pid}__\">";
$this->doc .= "</a>";
self::$header_count[1] = $this->actioninstance->getCurrentBookChapter();
}
/**
* Make available as XHTML replacement renderer
*
* @param $format
* @return bool
*/
public function canRender($format)
{
if ($format == 'xhtml') {
return true;
}
return false;
}
/**
* Simplified header printing with PDF bookmarks
*
* @param string $text
* @param int $level from 1 (highest) to 6 (lowest)
* @param int $pos
*/
public function header($text, $level, $pos, $returnonly = false)
{
//skip empty headlines
if (!$text) {
return;
}
global $ID;
$hid = $this->_headerToLink($text, true);
//only add items within global configured levels (doesn't check the pdf toc settings)
$this->toc_additem($hid, $text, $level);
$check = false;
$pid = sectionID($ID, $check);
$hid = $pid . '__' . $hid;
// retrieve numbered headings option
$isnumberedheadings = $this->actioninstance->getExportConfig('headernumber');
$header_prefix = "";
if ($isnumberedheadings) {
if ($level > 0) {
if (self::$previous_level > $level) {
for ($i = $level + 1; $i <= self::$previous_level; $i++) {
self::$header_count[$i] = 0;
}
}
}
self::$header_count[$level]++;
// $header_prefix = "";
for ($i = 1; $i <= $level; $i++) {
$header_prefix .= self::$header_count[$i] . ".";
}
}
// add PDF bookmark
$bookmark = '';
$maxbookmarklevel = $this->actioninstance->getExportConfig('maxbookmarks');
// 0: off, 1-6: show down to this level
if ($maxbookmarklevel && $maxbookmarklevel >= $level) {
$bookmarklevel = $this->calculateBookmarklevel($level);
$bookmark = sprintf(
'<bookmark content="%s %s" level="%d" />',
$header_prefix,
$this->_xmlEntities($text),
$bookmarklevel
);
}
// print header
$this->doc .= DOKU_LF . "<h$level>$bookmark";
$this->doc .= $header_prefix . "<a name=\"$hid\">";
$this->doc .= $this->_xmlEntities($text);
$this->doc .= "</a>";
$this->doc .= "</h$level>" . DOKU_LF;
self::$previous_level = $level;
}
/**
* Bookmark levels might increase maximal +1 per level.
* (note: levels start at 1, bookmarklevels at 0)
*
* @param int $level 1 (highest) to 6 (lowest)
* @return int
*/
protected function calculateBookmarklevel($level)
{
if ($this->lastHeaderLevel == -1) {
$this->lastHeaderLevel = $level;
}
$step = $level - $this->lastHeaderLevel;
if ($step > 1) {
$this->difference += $step - 1;
}
if ($step < 0) {
$this->difference = min($this->difference, $level - $this->originalHeaderLevel);
$this->difference = max($this->difference, 0);
}
$bookmarklevel = $level - $this->difference;
if ($step > 1) {
$this->originalHeaderLevel = $bookmarklevel;
}
$this->lastHeaderLevel = $level;
return $bookmarklevel - 1; //zero indexed
}
/**
* Render a page local link
*
* // modified copy of parent function
*
* @param string $hash hash link identifier
* @param string $name name for the link
* @param bool $returnonly
* @return string|void
*
* @see Doku_Renderer_xhtml::locallink
*/
public function locallink($hash, $name = null, $returnonly = false)
{
global $ID;
$name = $this->_getLinkTitle($name, $hash, $isImage);
$hash = $this->_headerToLink($hash);
$title = $ID . ' ↵';
$check = false;
$pid = sectionID($ID, $check);
$this->doc .= '<a href="#' . $pid . '__' . $hash . '" title="' . $title . '" class="wikilink1">';
$this->doc .= $name;
$this->doc .= '</a>';
}
/**
* Wrap centered media in a div to center it
*
* @param string $src media ID
* @param string $title descriptive text
* @param string $align left|center|right
* @param int $width width of media in pixel
* @param int $height height of media in pixel
* @param string $cache cache|recache|nocache
* @param bool $render should the media be embedded inline or just linked
* @return string
*/
public function _media(
$src,
$title = null,
$align = null,
$width = null,
$height = null,
$cache = null,
$render = true
) {
$out = '';
if ($align == 'center') {
$out .= '<div align="center" style="text-align: center">';
}
$out .= parent::_media($src, $title, $align, $width, $height, $cache, $render);
if ($align == 'center') {
$out .= '</div>';
}
return $out;
}
/**
* hover info makes no sense in PDFs, so drop acronyms
*
* @param string $acronym
*/
public function acronym($acronym)
{
$this->doc .= $this->_xmlEntities($acronym);
}
/**
* reformat links if needed
*
* @param array $link
* @return string
*/
public function _formatLink($link)
{
// for internal links contains the title the pageid
if (in_array($link['title'], $this->actioninstance->getExportedPages())) {
[/* url */, $hash] = sexplode('#', $link['url'], 2, '');
$check = false;
$pid = sectionID($link['title'], $check);
$link['url'] = "#" . $pid . '__' . $hash;
}
// prefix interwiki links with interwiki icon
if ($link['name'][0] != '<' && preg_match('/\binterwiki iw_(.\w+)\b/', $link['class'], $m)) {
if (file_exists(DOKU_INC . 'lib/images/interwiki/' . $m[1] . '.png')) {
$img = DOKU_BASE . 'lib/images/interwiki/' . $m[1] . '.png';
} elseif (file_exists(DOKU_INC . 'lib/images/interwiki/' . $m[1] . '.gif')) {
$img = DOKU_BASE . 'lib/images/interwiki/' . $m[1] . '.gif';
} else {
$img = DOKU_BASE . 'lib/images/interwiki.png';
}
$link['name'] = sprintf(
'<img src="%s" width="16" height="16" style="vertical-align: middle" class="%s" />%s',
$img,
$link['class'],
$link['name']
);
}
return parent::_formatLink($link);
}
/**
* no obfuscation for email addresses
*
* @param string $address
* @param null $name
* @param bool $returnonly
* @return string|void
*/
public function emaillink($address, $name = null, $returnonly = false)
{
global $conf;
$old = $conf['mailguard'];
$conf['mailguard'] = 'none';
parent::emaillink($address, $name, $returnonly);
$conf['mailguard'] = $old;
}
}