forked from cosmocode/dokuwiki-plugin-prosemirror
-
Notifications
You must be signed in to change notification settings - Fork 0
/
renderer.php
742 lines (624 loc) · 17.7 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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
<?php
/**
* DokuWiki Plugin prosemirror (Renderer Component)
*
* @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
* @author Andreas Gohr <[email protected]>
*/
// must be run within Dokuwiki
use dokuwiki\plugin\prosemirror\schema\Mark;
use dokuwiki\plugin\prosemirror\schema\Node;
use dokuwiki\plugin\prosemirror\schema\NodeStack;
if (!defined('DOKU_INC')) {
die();
}
require_once DOKU_INC . 'inc/parser/renderer.php';
class renderer_plugin_prosemirror extends Doku_Renderer
{
/** @var NodeStack */
public $nodestack;
/** @var NodeStack[] */
protected $nodestackBackup = [];
/** @var array list of currently active formatting marks */
protected $marks = [];
/** @var int column counter for table handling */
protected $colcount = 0;
/**
* The format this renderer produces
*/
public function getFormat()
{
return 'prosemirror';
}
public function addToNodestackTop(Node $node)
{
$this->nodestack->addTop($node);
}
public function addToNodestack(Node $node)
{
$this->nodestack->add($node);
}
public function dropFromNodeStack($nodeType)
{
$this->nodestack->drop($nodeType);
}
public function getCurrentMarks()
{
return $this->marks;
}
/**
* If there is a block scope open, close it.
*/
protected function clearBlock()
{
$parentNode = $this->nodestack->current()->getType();
if (in_array($parentNode, ['paragraph'])) {
$this->nodestack->drop($parentNode);
}
}
// FIXME implement all methods of Doku_Renderer here
/** @inheritDoc */
public function document_start()
{
$this->nodestack = new NodeStack();
}
/** @inheritDoc */
public function document_end()
{
if ($this->nodestack->isEmpty()) {
$this->p_open();
$this->p_close();
}
$this->doc = json_encode($this->nodestack->doc(), JSON_PRETTY_PRINT);
}
public function nocache() {
$docNode = $this->nodestack->getDocNode();
$docNode->attr('nocache', true);
}
public function notoc()
{
$docNode = $this->nodestack->getDocNode();
$docNode->attr('notoc', true);
}
/** @inheritDoc */
public function p_open()
{
$this->nodestack->addTop(new Node('paragraph'));
}
/** @inheritdoc */
public function p_close()
{
$this->nodestack->drop('paragraph');
}
/** @inheritDoc */
public function quote_open()
{
if ($this->nodestack->current()->getType() === 'paragraph') {
$this->nodestack->drop('paragraph');
}
$this->nodestack->addTop(new Node('blockquote'));
}
/** @inheritDoc */
public function quote_close()
{
if ($this->nodestack->current()->getType() === 'paragraph') {
$this->nodestack->drop('paragraph');
}
$this->nodestack->drop('blockquote');
}
#region lists
/** @inheritDoc */
public function listu_open()
{
if ($this->nodestack->current()->getType() === 'paragraph') {
$this->nodestack->drop('paragraph');
}
$this->nodestack->addTop(new Node('bullet_list'));
}
/** @inheritDoc */
public function listu_close()
{
$this->nodestack->drop('bullet_list');
}
/** @inheritDoc */
public function listo_open()
{
if ($this->nodestack->current()->getType() === 'paragraph') {
$this->nodestack->drop('paragraph');
}
$this->nodestack->addTop(new Node('ordered_list'));
}
/** @inheritDoc */
public function listo_close()
{
$this->nodestack->drop('ordered_list');
}
/** @inheritDoc */
public function listitem_open($level, $node = false)
{
$this->nodestack->addTop(new Node('list_item'));
$paragraphNode = new Node('paragraph');
$this->nodestack->addTop($paragraphNode);
}
/** @inheritDoc */
public function listitem_close()
{
if ($this->nodestack->current()->getType() === 'paragraph') {
$this->nodestack->drop('paragraph');
}
$this->nodestack->drop('list_item');
}
#endregion lists
#region table
/** @inheritDoc */
public function table_open($maxcols = null, $numrows = null, $pos = null)
{
$this->nodestack->addTop(new Node('table'));
}
/** @inheritDoc */
public function table_close($pos = null)
{
$this->nodestack->drop('table');
}
/** @inheritDoc */
public function tablerow_open()
{
$this->nodestack->addTop(new Node('table_row'));
$this->colcount = 0;
}
/** @inheritDoc */
public function tablerow_close()
{
$node = $this->nodestack->drop('table_row');
$node->attr('columns', $this->colcount);
}
/** @inheritDoc */
public function tablecell_open($colspan = 1, $align = null, $rowspan = 1)
{
$this->openTableCell('table_cell', $colspan, $align, $rowspan);
}
/** @inheritdoc */
public function tablecell_close()
{
$this->closeTableCell('table_cell');
}
/** @inheritDoc */
public function tableheader_open($colspan = 1, $align = null, $rowspan = 1)
{
$this->openTableCell('table_header', $colspan, $align, $rowspan);
}
/** @inheritdoc */
public function tableheader_close()
{
$this->closeTableCell('table_header');
}
/**
* Add a new table cell to the top of the stack
*
* @param string $type either table_cell or table_header
* @param int $colspan
* @param string|null $align either null/left, center or right
* @param int $rowspan
*/
protected function openTableCell($type, $colspan, $align, $rowspan) {
$this->colcount += $colspan;
$node = new Node($type);
$node->attr('colspan', $colspan);
$node->attr('rowspan', $rowspan);
$node->attr('align', $align);
$this->nodestack->addTop($node);
$node = new Node('paragraph');
$this->nodestack->addTop($node);
}
/**
* Remove a table cell from the top of the stack
*
* @param string $type either table_cell or table_header
*/
protected function closeTableCell($type)
{
if ($this->nodestack->current()->getType() === 'paragraph') {
$this->nodestack->drop('paragraph');
}
$curNode = $this->nodestack->current();
$curNode->trimContentLeft();
$curNode->trimContentRight();
$this->nodestack->drop($type);
}
#endregion table
/** @inheritDoc */
public function header($text, $level, $pos)
{
$node = new Node('heading');
$node->attr('level', $level);
$tnode = new Node('text');
$tnode->setText($text);
$node->addChild($tnode);
$this->nodestack->add($node);
}
/** @inheritDoc */
public function cdata($text)
{
if ($text === '') {
return;
}
$parentNode = $this->nodestack->current()->getType();
if (in_array($parentNode, ['paragraph', 'footnote'])) {
$text = str_replace("\n", ' ', $text);
}
if ($parentNode === 'list_item') {
$node = new Node('paragraph');
$this->nodestack->addTop($node);
}
if ($parentNode === 'blockquote') {
$node = new Node('paragraph');
$this->nodestack->addTop($node);
}
if ($parentNode === 'doc') {
$node = new Node('paragraph');
$this->nodestack->addTop($node);
}
$node = new Node('text');
$node->setText($text);
foreach (array_keys($this->marks) as $mark) {
$node->addMark(new Mark($mark));
}
$this->nodestack->add($node);
}
public function preformatted($text)
{
$this->clearBlock();
$node = new Node('preformatted');
$this->nodestack->addTop($node);
$this->cdata($text);
$this->nodestack->drop('preformatted');
}
public function code($text, $lang = null, $file = null)
{
$this->clearBlock();
$node = new Node('code_block');
$node->attr('class', 'code ' . $lang);
$node->attr('data-language', $lang);
$node->attr('data-filename', $file);
$this->nodestack->addTop($node);
$this->cdata(trim($text, "\n"));
$this->nodestack->drop('code_block');
}
public function file($text, $lang = null, $file = null)
{
$this->code($text, $lang, $file);
}
public function html($text)
{
$node = new Node('html_inline');
$node->attr('class', 'html_inline');
$this->nodestack->addTop($node);
$this->cdata(str_replace("\n", ' ', $text));
$this->nodestack->drop('html_inline');
}
public function htmlblock($text)
{
$this->clearBlock();
$node = new Node('html_block');
$node->attr('class', 'html_block');
$this->nodestack->addTop($node);
$this->cdata(trim($text, "\n"));
$this->nodestack->drop('html_block');
}
public function php($text)
{
$node = new Node('php_inline');
$node->attr('class', 'php_inline');
$this->nodestack->addTop($node);
$this->cdata(str_replace("\n", ' ', $text));
$this->nodestack->drop('php_inline');
}
public function phpblock($text)
{
$this->clearBlock();
$node = new Node('php_block');
$node->attr('class', 'php_block');
$this->nodestack->addTop($node);
$this->cdata(trim($text, "\n"));
$this->nodestack->drop('php_block');
}
/**
* @inheritDoc
*/
public function rss($url, $params)
{
$this->clearBlock();
$node = new Node('rss');
$node->attr('url', hsc($url));
$node->attr('max', $params['max']);
$node->attr('reverse', (bool)$params['reverse']);
$node->attr('author', (bool)$params['author']);
$node->attr('date', (bool)$params['date']);
$node->attr('details', (bool)$params['details']);
if ($params['refresh'] % 86400 === 0) {
$refresh = $params['refresh']/86400 . 'd';
} else if ($params['refresh'] % 3600 === 0) {
$refresh = $params['refresh']/3600 . 'h';
} else {
$refresh = $params['refresh']/60 . 'm';
}
$node->attr('refresh', trim($refresh));
$this->nodestack->add($node);
}
public function footnote_open()
{
$footnoteNode = new Node('footnote');
$this->nodestack->addTop($footnoteNode);
$this->nodestackBackup[] = $this->nodestack;
$this->nodestack = new NodeStack();
}
public function footnote_close()
{
$json = json_encode($this->nodestack->doc());
$this->nodestack = array_pop($this->nodestackBackup);
$this->nodestack->current()->attr('contentJSON', $json);
$this->nodestack->drop('footnote');
}
/**
* @inheritDoc
*/
public function internalmedia(
$src,
$title = null,
$align = null,
$width = null,
$height = null,
$cache = null,
$linking = null
) {
// FIXME how do we handle non-images, e.g. pdfs or audio?
\dokuwiki\plugin\prosemirror\parser\ImageNode::render(
$this,
$src,
$title,
$align,
$width,
$height,
$cache,
$linking
);
}
/**
* @inheritDoc
*/
public function externalmedia(
$src,
$title = null,
$align = null,
$width = null,
$height = null,
$cache = null,
$linking = null
) {
\dokuwiki\plugin\prosemirror\parser\ImageNode::render(
$this,
$src,
$title,
$align,
$width,
$height,
$cache,
$linking
);
}
public function locallink($hash, $name = null)
{
\dokuwiki\plugin\prosemirror\parser\LocalLinkNode::render($this, $hash, $name);
}
/**
* @inheritDoc
*/
public function internallink($id, $name = null)
{
\dokuwiki\plugin\prosemirror\parser\InternalLinkNode::render($this, $id, $name);
}
public function externallink($link, $title = null)
{
\dokuwiki\plugin\prosemirror\parser\ExternalLinkNode::render($this, $link, $title);
}
public function interwikilink($link, $title, $wikiName, $wikiUri)
{
\dokuwiki\plugin\prosemirror\parser\InterwikiLinkNode::render($this, $title, $wikiName, $wikiUri);
}
public function emaillink($address, $name = null)
{
\dokuwiki\plugin\prosemirror\parser\EmailLinkNode::render($this, $address, $name);
}
public function windowssharelink($link, $title = null)
{
\dokuwiki\plugin\prosemirror\parser\WindowsShareLinkNode::render($this, $link, $title);
}
/** @inheritDoc */
public function linebreak()
{
$this->nodestack->add(new Node('hard_break'));
}
/** @inheritDoc */
public function hr()
{
$this->nodestack->add(new Node('horizontal_rule'));
}
public function plugin($name, $data, $state = '', $match = '')
{
if (empty($match)) {
return;
}
$eventData = [
'name' => $name,
'data' => $data,
'state' => $state,
'match' => $match,
'renderer' => $this,
];
$event = new Doku_Event('PROSEMIRROR_RENDER_PLUGIN', $eventData);
if ($event->advise_before()) {
if ($this->nodestack->current()->getType() === 'paragraph') {
$nodetype = 'dwplugin_inline';
} else {
$nodetype = 'dwplugin_block';
}
$node = new Node($nodetype);
$node->attr('class', 'dwplugin');
$node->attr('data-pluginname', $name);
$this->nodestack->addTop($node);
$this->cdata($match);
$this->nodestack->drop($nodetype);
}
}
public function smiley($smiley)
{
if(array_key_exists($smiley, $this->smileys)) {
$node = new Node('smiley');
$node->attr('icon', $this->smileys[$smiley]);
$node->attr('syntax', $smiley);
$this->nodestack->add($node);
} else {
$this->cdata($smiley);
}
}
#region elements with no special WYSIWYG representation
/** @inheritDoc */
public function entity($entity)
{
$this->cdata($entity); // FIXME should we handle them special?
}
/** @inheritDoc */
public function multiplyentity($x, $y)
{
$this->cdata($x . 'x' . $y);
}
/** @inheritDoc */
public function acronym($acronym)
{
$this->cdata($acronym);
}
/** @inheritDoc */
public function apostrophe()
{
$this->cdata("'");
}
/** @inheritDoc */
public function singlequoteopening()
{
$this->cdata("'");
}
/** @inheritDoc */
public function singlequoteclosing()
{
$this->cdata("'");
}
/** @inheritDoc */
public function doublequoteopening()
{
$this->cdata('"');
}
/** @inheritDoc */
public function doublequoteclosing()
{
$this->cdata('"');
}
/** @inheritDoc */
public function camelcaselink($link)
{
$this->cdata($link); // FIXME should/could we decorate it?
}
#endregion
#region formatter marks
/** @inheritDoc */
public function strong_open()
{
$this->marks['strong'] = 1;
}
/** @inheritDoc */
public function strong_close()
{
if (isset($this->marks['strong'])) {
unset($this->marks['strong']);
}
}
/** @inheritDoc */
public function emphasis_open()
{
$this->marks['em'] = 1;
}
/** @inheritDoc */
public function emphasis_close()
{
if (isset($this->marks['em'])) {
unset($this->marks['em']);
}
}
/** @inheritdoc */
public function subscript_open()
{
$this->marks['subscript'] = 1;
}
/** @inheritDoc */
public function subscript_close()
{
if (isset($this->marks['subscript'])) {
unset($this->marks['subscript']);
}
}
/** @inheritdoc */
public function superscript_open()
{
$this->marks['superscript'] = 1;
}
/** @inheritDoc */
public function superscript_close()
{
if (isset($this->marks['superscript'])) {
unset($this->marks['superscript']);
}
}
/** @inheritDoc */
public function monospace_open()
{
$this->marks['code'] = 1;
}
/** @inheritDoc */
public function monospace_close()
{
if (isset($this->marks['code'])) {
unset($this->marks['code']);
}
}
/** @inheritDoc */
public function deleted_open()
{
$this->marks['deleted'] = 1;
}
/** @inheritDoc */
public function deleted_close()
{
if (isset($this->marks['deleted'])) {
unset($this->marks['deleted']);
}
}
/** @inheritDoc */
public function underline_open()
{
$this->marks['underline'] = 1;
}
/** @inheritDoc */
public function underline_close()
{
if (isset($this->marks['underline'])) {
unset($this->marks['underline']);
}
}
/** @inheritDoc */
public function unformatted($text)
{
$this->marks['unformatted'] = 1;
parent::unformatted($text);
unset($this->marks['unformatted']);
}
#endregion formatter marks
}