-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.php
444 lines (365 loc) · 12.1 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
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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
<?php
/**
* Renderer for exporting pages as MicroAlg source code
*
* @author Christophe Gragnic <christophegragnic@gmail.com>
* @author Michael Hamann <michael@content-space.de>
* @author Todd Augsburger <todd@rollerorgans.com>
*/
if(!defined('DOKU_INC')) define('DOKU_INC',fullpath(dirname(__FILE__).'/../../').'/');
if ( !defined('DOKU_LF') ) {
define ('DOKU_LF',"\n");
}
if ( !defined('MALG_COM') ) {
define ('MALG_COM', "# ");
}
require_once DOKU_INC . 'inc/parser/renderer.php';
require_once DOKU_INC . 'inc/html.php';
class renderer_plugin_microalg extends Doku_Renderer {
// @access public
var $doc = ''; // will contain the whole document
var $toc = array(); // will contain the Table of Contents
var $footnotes = array();
var $store = '';
var $nSpan = 0;
var $separator = '';
function getFormat(){
return 'microalg';
}
/* Compatibility functions for the xhtml mode */
public function startSectionEdit($start, $type, $title = null) {
}
public function finishSectionEdit($end = null) {
}
//handle plugin rendering
function plugin($name,$data){
$plugin =& plugin_load('syntax',$name);
if($plugin != null){
if(!$plugin->render($this->getFormat(),$this,$data)) {
// probably doesn't support text, so use stripped-down xhtml
$tmpData = $this->doc;
$this->doc = '';
if($plugin->render('xhtml',$this,$data) && ($this->doc != '')) {
$search = array('@<script[^>]*?>.*?</script>@si', // javascript
'@<style[^>]*?>.*?</style>@siU', // style tags
'@<[\/\!]*?[^<>]*?>@si', // HTML tags
'@<![\s\S]*?--[ \t\n\r]*>@', // multi-line comments
'@\s+@' // extra whitespace
);
$this->doc = $tmpData . DOKU_LF . trim(html_entity_decode(preg_replace($search,' ',$this->doc),ENT_QUOTES)) . DOKU_LF;
}
else
$this->doc = $tmpData;
}
}
}
function document_start() {
global $ID;
$this->doc = '';
$this->toc = array();
$this->footnotes = array();
$this->store = '';
$this->nSpan = 0;
$this->separator = '';
$metaheader = array();
$metaheader['Content-Type'] = 'text/plain; charset=utf-8';
if (!array_key_exists('test', $_GET)) $metaheader['Content-Disposition'] = 'attachment; filename="' . $ID . '.malg"';
$meta = array();
$meta['format']['microalg'] = $metaheader;
p_set_metadata($ID,$meta);
}
function document_end() {
if ( count ($this->footnotes) > 0 ) {
$this->doc .= DOKU_LF;
$id = 0;
foreach ( $this->footnotes as $footnote ) {
$id++; // the number of the current footnote
// check its not a placeholder that indicates actual footnote text is elsewhere
if (substr($footnote, 0, 5) != "@@FNT") {
$this->doc .= $id.') ';
// get any other footnotes that use the same markup
$alt = array_keys($this->footnotes, "@@FNT$id");
if (count($alt)) {
foreach ($alt as $ref) {
$this->doc .= ($ref+1).') ';
}
}
$this->doc .= $footnote . DOKU_LF;
}
}
}
// Prepare the TOC
if($this->info['toc'] && is_array($this->toc) && count($this->toc) > 2){
global $TOC;
$TOC = $this->toc;
}
// make sure there are no empty paragraphs
$this->doc = preg_replace('#'.DOKU_LF.'\s*'.DOKU_LF.'\s*'.DOKU_LF.'#',DOKU_LF.DOKU_LF,$this->doc);
}
function header($text, $level, $pos) {
$this->doc .= MALG_COM.$text.DOKU_LF.DOKU_LF;
}
function section_close() {
$this->doc .= DOKU_LF;
}
function cdata($text) {
$this->doc .= preg_replace('/\n/', "\n# " , $text);
}
function p_open() {
$this->doc .= MALG_COM;
}
function p_close() {
$this->doc .= DOKU_LF;
}
function linebreak() {
$this->doc .= DOKU_LF . MALG_COM;
}
function hr() {
$this->doc .= MALG_COM.'--------'.DOKU_LF;
}
/**
* Callback for footnote start syntax
*
* All following content will go to the footnote instead of
* the document. To achieve this the previous rendered content
* is moved to $store and $doc is cleared
*
* @author Andreas Gohr <andi@splitbrain.org>
*/
function footnote_open() {
// move current content to store and record footnote
$this->store = $this->doc;
$this->doc = '';
}
/**
* Callback for footnote end syntax
*
* All rendered content is moved to the $footnotes array and the old
* content is restored from $store again
*
* @author Andreas Gohr
*/
function footnote_close() {
// recover footnote into the stack and restore old content
$footnote = $this->doc;
$this->doc = $this->store;
$this->store = '';
// check to see if this footnote has been seen before
$i = array_search($footnote, $this->footnotes);
if ($i === false) {
// its a new footnote, add it to the $footnotes array
$id = count($this->footnotes)+1;
$this->footnotes[count($this->footnotes)] = $footnote;
} else {
// seen this one before, translate the index to an id and save a placeholder
$i++;
$id = count($this->footnotes)+1;
$this->footnotes[count($this->footnotes)] = "@@FNT".($i);
}
// output the footnote reference and link
$this->doc .= MALG_COM.' '.$id.')';
}
function listo_open() {
$this->doc .= DOKU_LF;
}
function listo_close() {
$this->doc .= DOKU_LF;
}
function listu_open() {
$this->doc .= DOKU_LF;
}
function listu_close() {
$this->doc .= DOKU_LF;
}
function listitem_open($level) {
$this->doc .= MALG_COM.' * ';
}
function listitem_close() {
// nothing
}
function listcontent_close() {
$this->doc .= DOKU_LF;
}
function unformatted($text) {
$this->doc .= $text;
}
function php($text) {
global $conf;
if($conf['phpok']){
ob_start();
eval($text);
$this->html(ob_get_contents());
ob_end_clean();
} else {
$this->cdata($text);
}
}
function phpblock($text) {
$this->doc .= $text;
}
function html($text) {
$this->doc .= strip_tags($text);
}
function htmlblock($text) {
$this->html($text);
}
function preformatted($text) {
$this->doc .= "\n" . preg_replace('/.+/', '# $0' , $text) . "\n\n";
}
function file($text) {
$this->doc .= $text.DOKU_LF;
}
function code($text, $language = NULL) {
$this->preformatted($text);
}
function acronym($acronym) {
if ( array_key_exists($acronym, $this->acronyms) ) {
$title = $this->acronyms[$acronym];
$this->doc .= $acronym.' ('.$title.')';
} else {
$this->doc .= $acronym;
}
}
function smiley($smiley) {
$this->doc .= $smiley;
}
function entity($entity) {
if ( array_key_exists($entity, $this->entities) ) {
$this->doc .= $this->entities[$entity];
} else {
$this->doc .= $entity;
}
}
function multiplyentity($x, $y) {
$this->doc .= $x.'x'.$y;
}
function singlequoteopening() {
global $lang;
$this->doc .= $lang['singlequoteopening'];
}
function singlequoteclosing() {
global $lang;
$this->doc .= $lang['singlequoteclosing'];
}
function apostrophe() {
global $lang;
$this->doc .= $lang['apostrophe'];
}
function doublequoteopening() {
global $lang;
$this->doc .= $lang['doublequoteopening'];
}
function doublequoteclosing() {
global $lang;
$this->doc .= $lang['doublequoteclosing'];
}
function camelcaselink($link) {
$this->internallink($link,$link);
}
function locallink($hash, $name = NULL){
$name = $this->_getLinkTitle($name, $hash, $isImage);
$this->doc .= $name;;
}
function internallink($id, $name = NULL, $search=NULL,$returnonly=false) {
global $ID;
// default name is based on $id as given
$default = $this->_simpleTitle($id);
resolve_pageid(getNS($ID),$id,$exists);
$this->doc .= $this->_getLinkTitle($name, $default, $isImage, $id);
}
function externallink($url, $name = NULL) {
$this->doc .= $this->_getLinkTitle($name, $url, $isImage);
}
function interwikilink($match, $name = NULL, $wikiName, $wikiUri) {
$this->doc .= $this->_getLinkTitle($name, $wikiUri, $isImage);
}
function windowssharelink($url, $name = NULL) {
$this->doc .= $this->_getLinkTitle($name, $url, $isImage);
}
function emaillink($address, $name = NULL) {
$name = $this->_getLinkTitle($name, '', $isImage);
$address = html_entity_decode(obfuscate($address),ENT_QUOTES,'UTF-8');
if(empty($name)){
$name = $address;
}
$this->doc .= $name;
}
function internalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
$height=NULL, $cache=NULL, $linking=NULL) {
$this->doc .= $title;
}
function externalmedia ($src, $title=NULL, $align=NULL, $width=NULL,
$height=NULL, $cache=NULL, $linking=NULL) {
$this->doc .= $title;
}
function table_close(){
$this->doc .= DOKU_LF;
}
function tablerow_open(){
$this->separator = '';
}
function tablerow_close(){
$this->doc .= DOKU_LF;
}
function tableheader_open($colspan = 1, $align = NULL){
$this->tablecell_open();
}
function tableheader_close(){
$this->tablecell_close();
}
function tablecell_open($colspan = 1, $align = NULL){
$this->nSpan = $colspan;
$this->doc .= $this->separator;
$this->separator = ', ';
}
function tablecell_close(){
$this->doc .= str_repeat(',',$this->nSpan-1);
$this->nSpan = 0;
}
/**
* Creates a linkid from a headline
*
* @param string $title The headline title
* @param boolean $create Create a new unique ID?
* @author Andreas Gohr <andi@splitbrain.org>
*/
function _headerToLink($title,$create=false) {
$title = str_replace(':','',cleanID($title));
$title = ltrim($title,'0123456789._-');
if(empty($title)) $title='section';
return $title;
}
function _getLinkTitle($title, $default, & $isImage, $id=NULL) {
global $conf;
$isImage = false;
if ( is_null($title) ) {
if ($conf['useheading'] && $id) {
$heading = p_get_first_heading($id,true);
if ($heading) {
return $heading;
}
}
return $default;
} else if ( is_string($title) ) {
if (!is_null($default) && ($default != $title))
return $default." ".$title;
else
return $title;
} else if ( is_array($title) ) {
if (!is_null($default) && ($default != $title['title']))
return $default." ".$title['title'];
else
return $title['title'];
}
}
function _xmlEntities($string) {
return $string; // nothing to do for text
}
function _formatLink($link){
if(!empty($link['name']))
return $link['name'];
elseif(!empty($link['title']))
return $link['title'];
return $link['url'];
}
}