-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathsyntax.php
413 lines (388 loc) · 16 KB
/
syntax.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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
<?php
/**
* ODT Plugin: Exports to ODT
*
* @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
* @author Andreas Gohr <andi@splitbrain.org>
* @author Aurelien Bompard <aurelien@bompard.org>
*/
// must be run within Dokuwiki
if(!defined('DOKU_INC')) die();
/**
* Class syntax_plugin_odt
*
* @package DokuWiki\Syntax
*/
class syntax_plugin_odt extends DokuWiki_Syntax_Plugin {
protected $config = NULL;
/**
* What kind of syntax are we?
*/
public function getType() {
return 'substition';
}
/**
* What about paragraphs?
*/
public function getPType() {
return 'normal';
}
/**
* Where to sort in?
*/
public function getSort() {
return 319; // Before image detection, which uses {{...}} and is 320
}
/**
* Connect pattern to lexer
*
* @param string $mode
*/
public function connectTo($mode) {
$this->Lexer->addSpecialPattern('~~ODT~~', $mode, 'plugin_odt');
$this->Lexer->addSpecialPattern('{{odt>.+?}}', $mode, 'plugin_odt');
}
/**
* Handler to prepare matched data for the rendering process
*
* @param string $match The text matched by the patterns
* @param int $state The lexer state for the match
* @param int $pos The character position of the matched text
* @param Doku_Handler $handler The Doku_Handler object
* @return bool|array Return an array with all data you want to use in render, false don't add an instruction
*/
public function handle($match, $state, $pos, Doku_Handler $handler) {
// Export button
if($match == '~~ODT~~') {
return array();
}
// Extended info
$match = substr($match, 6, -2); //strip markup
$extinfo = explode(':', $match);
$info_type = $extinfo[0];
if(count($extinfo) < 2) { // no value
$info_value = '';
} elseif(count($extinfo) == 2) {
$info_value = $extinfo[1];
} else { // value may contain colons
$info_value = implode(array_slice($extinfo, 1), ':');
}
return array($info_type, $info_value, $pos);
}
/**
* Handles the actual output creation.
*
* @param string $format output format being rendered
* @param Doku_Renderer $renderer the current renderer object
* @param array $data data created by handler()
* @return boolean rendered correctly? (however, returned value is not used at the moment)
*/
public function render($format, Doku_Renderer $renderer, $data) {
global $ID, $REV;
if(!$data) { // Export button
if($format != 'xhtml') return false;
$renderer->doc .= '<a href="' . exportlink($ID, 'odt', ($REV != '' ? 'rev=' . $REV : '')) . '" title="' . $this->getLang('view') . '">';
$renderer->doc .= '<img src="' . DOKU_BASE . 'lib/plugins/odt/odt.png" align="right" alt="' . $this->getLang('view') . '" width="48" height="48" />';
$renderer->doc .= '</a>';
return true;
} else { // Extended info
// Load config helper if not done yet
if ( !isset($this->config) ) {
$this->config = plugin_load('helper', 'odt_config');
}
list($info_type, $info_value, $pos) = $data;
// Handle config setting 'firsttemplatedefinitionwins'.
// It returns from render() if a template was already set,
// This functionality needs the usecounter plugin: https://www.dokuwiki.org/plugin:usecounter
$usecounter_helper = plugin_load('helper','usecounter');
if ($usecounter_helper && $this->config->getParam('firsttemplatedefinitionwins')) {
if ($info_type == "template" || $info_type == "templatepage" || $info_type == "odt_template" || $info_type == "css_template") {
if ($usecounter_helper && $usecounter_helper->amountOfUses('ODT>template') > 0) {
// here we leave the function, since a previous usage of the template setting has been registered before
return true;
} else {
// here we decide to go on
if ($usecounter_helper) {
$usecounter_helper->incUsageOf('ODT>template');
}
}
}
}
// If it is a config option store it in the meta data
// and set the config parameter in the renderer.
if ( $this->config->isParam($info_type) ) {
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$renderer->setConfigParam($info_type, $info_value);
} elseif($format == 'metadata') {
if ($this->config->addingToMetaIsAllowed($info_type, $pos)) {
/** @var Doku_Renderer_metadata $renderer */
$renderer->meta['relation']['odt'][$info_type] = $info_value;
}
}
}
// Do some more work for the tags which are not just a config parameter setter
switch($info_type)
{
case 'toc': // Insert TOC in exported ODT file
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$renderer->render_index('toc', $info_value);
} elseif($format == 'metadata') {
/** @var Doku_Renderer_metadata $renderer */
$renderer->meta['relation']['odt']['toc'] = $info_value;
} elseif($format == 'xhtml') {
$this->insert_index_preview ($renderer, 'toc');
}
break;
case 'chapter-index': // Insert chapter index in exported ODT file
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$renderer->render_index('chapter', $info_value);
} elseif($format == 'xhtml') {
$this->insert_index_preview ($renderer, 'chapter');
}
break;
case 'disablelinks': // Disable creating links and only show the text instead
if($format == 'odt') {
$renderer->disable_links();
}
break;
case 'enablelinks': // Re-enable creating links
if($format == 'odt') {
$renderer->enable_links();
}
break;
case 'page':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$params = explode(',', $info_value);
$format = trim ($params [0]);
$orientation = trim ($params [1]);
for ( $index = 2 ; $index < 6 ; $index++ ) {
if ( empty($params [$index]) ) {
$params [$index] = 2;
}
}
$renderer->setPageFormat($format, $orientation, $params [2], $params [3], $params [4], $params [5]);
}
break;
case 'format':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$format = trim ($info_value);
$renderer->setPageFormat($format);
}
break;
case 'orientation':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$orientation = trim ($info_value);
$renderer->setPageFormat(NULL,$orientation);
}
break;
case 'margin_top':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$margin = trim ($info_value);
$renderer->setPageFormat(NULL,NULL,$margin);
}
break;
case 'margin_right':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$margin = trim ($info_value);
$renderer->setPageFormat(NULL,NULL,NULL,$margin);
}
break;
case 'margin_bottom':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$margin = trim ($info_value);
$renderer->setPageFormat(NULL,NULL,NULL,NULL,$margin);
}
break;
case 'margin_left':
if($format == 'odt') {
/** @var renderer_plugin_odt_page $renderer */
$margin = trim ($info_value);
$renderer->setPageFormat(NULL,NULL,NULL,NULL,NULL,$margin);
}
break;
case 'templatepage': // Take wiki page content as additional CSS input
if($format == 'odt' || $format == 'xhtml' ) {
if ($this->check_templatepage ($info_value, $format) == true &&
$format == 'odt' ) {
/** @var renderer_plugin_odt_page $renderer */
$renderer->read_templatepage($info_value);
}
}
break;
case 'frame-open': // Insert/Open ODT frame
if($format == 'odt' ) {
/** @var renderer_plugin_odt_page $renderer */
$this->frame_open($renderer, $info_value);
}
break;
case 'frame-close': // Close ODT frame
if($format == 'odt' ) {
/** @var renderer_plugin_odt_page $renderer */
$this->frame_close($renderer);
}
break;
}
}
return false;
}
/**
* Insert a browser preview for an index.
*
* @param Doku_Renderer $renderer The current renderer
* @param string $type The index type ('toc' or 'chapter)'
*/
function insert_index_preview ($renderer, $type='toc') {
if ($this->config->getParam ('index_in_browser') == 'hide') {
return;
}
switch ($type) {
case 'toc':
$msg = $this->getLang('toc_msg');
$reminder = $this->getLang('update_toc_msg');
break;
case 'chapter':
$msg = $this->getLang('chapter_msg');
$reminder = $this->getLang('update_chapter_msg');
break;
}
$renderer->doc .= '<p class="index_preview_odt">';
$renderer->doc .= '<span id="text" class="index_preview_odt">'.$msg.'</span><br>';
$renderer->doc .= '<span id="reminder" class="index_preview_odt">'.$reminder.'</span>';
$renderer->doc .= '</p>';
}
/**
* Checl existance of the template page and display error
* message in case of xhtml rendering.
*
* @param string $pagename The page to check
* @param string $format The render format ('xhtml' or 'odt')
*/
protected function check_templatepage ($pagename, $format) {
$exists = false;
if (empty($pagename)) {
if ($format == 'xhtml') {
msg(sprintf("No page specified!", html_wikilink($pagename)), -1);
}
return (false);
}
resolve_pageid($INFO['namespace'], $pagename, $exists);
if(!$exists) {
if ($format == 'xhtml') {
msg(sprintf("Page not found!", html_wikilink($pagename)), -1);
}
return (false);
}
return (true);
}
/**
* Open a frame with a text box.
*
* @param Doku_Renderer $renderer The current renderer object
* @param string $params Parameters for the frame
*/
protected function frame_open ($renderer, $params) {
// Get inline CSS for ODT frame
$odt_css = '';
if ( preg_match('/odt-css="[^"]+";/', $params, $matches) === 1 ) {
$quote = strpos ($matches [0], '"');
$temp = substr ($matches [0], $quote+1);
$temp = trim ($temp, '";');
$odt_css = $temp.';';
}
$odt_css_id = '';
if ( preg_match('/odt-css-id="[^"]+";/', $params, $matches) === 1 ) {
$quote = strpos ($matches [0], '"');
$temp = substr ($matches [0], $quote+1);
$temp = trim ($temp, '";');
$odt_css_id = $temp;
}
$properties = array();
$renderer->getODTPropertiesNew ($properties, NULL, 'id="'.$odt_css_id.'" style="'.$odt_css.'"');
if (empty($properties ['page'])) {
$properties ['anchor-type'] = 'page';
}
if (empty($properties ['wrap'])) {
$properties ['wrap'] = 'run-through';
}
if (empty($properties ['number-wrapped-paragraphs'])) {
$properties ['number-wrapped-paragraphs'] = 'no-limit';
}
if (empty($properties ['vertical-pos'])) {
$properties ['vertical-pos'] = 'from-top';
}
if (empty($properties ['vertical-rel'])) {
$properties ['vertical-rel'] = 'page';
}
if (empty($properties ['horizontal-pos'])) {
$properties ['horizontal-pos'] = 'from-left';
}
if (empty($properties ['horizontal-rel'])) {
$properties ['horizontal-rel'] = 'page';
}
if (empty($properties ['wrap-influence-on-position'])) {
$properties ['wrap-influence-on-position'] = 'once-concurrent';
}
if (empty($properties ['flow-with-text'])) {
$properties ['flow-with-text'] = 'false';
}
if (empty($properties ['margin-top'])) {
$properties ['margin-top'] = '0cm';
}
if (empty($properties ['margin-right'])) {
$properties ['margin-right'] = '0cm';
}
if (empty($properties ['margin-bottom'])) {
$properties ['margin-bottom'] = '0cm';
}
if (empty($properties ['margin-left'])) {
$properties ['margin-left'] = '0cm';
}
if (empty($properties ['padding-top'])) {
$properties ['padding-top'] = '0cm';
}
if (empty($properties ['padding-right'])) {
$properties ['padding-right'] = '0cm';
}
if (empty($properties ['padding-bottom'])) {
$properties ['padding-bottom'] = '0cm';
}
if (empty($properties ['padding-left'])) {
$properties ['padding-left'] = '0cm';
}
if (empty($properties ['border-top'])) {
$properties ['border-top'] = 'none';
}
if (empty($properties ['border-right'])) {
$properties ['border-right'] = 'none';
}
if (empty($properties ['border-bottom'])) {
$properties ['border-bottom'] = 'none';
}
if (empty($properties ['border-left'])) {
$properties ['border-left'] = 'none';
}
if (empty($properties ['horizontal-align'])) {
$properties ['horizontal-align'] = 'left';
}
$renderer->_odtOpenTextBoxUseProperties ($properties);
$renderer->p_open();
}
/**
* Close a frame with a text box.
*
* @param Doku_Renderer $renderer The current renderer object
*/
protected function frame_close ($renderer) {
$renderer->p_close();
$renderer->_odtCloseTextBox ();
}
}