Total Complexity | 103 |
Total Lines | 704 |
Duplicated Lines | 0 % |
Changes | 18 | ||
Bugs | 3 | Features | 4 |
Complex classes like renderer_plugin_prosemirror often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use renderer_plugin_prosemirror, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | class renderer_plugin_prosemirror extends Doku_Renderer |
||
21 | { |
||
22 | |||
23 | /** @var NodeStack */ |
||
24 | public $nodestack; |
||
25 | |||
26 | /** @var NodeStack[] */ |
||
27 | protected $nodestackBackup = []; |
||
28 | |||
29 | /** @var array list of currently active formatting marks */ |
||
30 | protected $marks = []; |
||
31 | |||
32 | /** @var int column counter for table handling */ |
||
33 | protected $colcount = 0; |
||
34 | |||
35 | /** |
||
36 | * The format this renderer produces |
||
37 | */ |
||
38 | public function getFormat() |
||
39 | { |
||
40 | return 'prosemirror'; |
||
41 | } |
||
42 | |||
43 | public function addToNodestackTop(Node $node) |
||
44 | { |
||
45 | $this->nodestack->addTop($node); |
||
46 | } |
||
47 | |||
48 | public function addToNodestack(Node $node) |
||
49 | { |
||
50 | $this->nodestack->add($node); |
||
51 | } |
||
52 | |||
53 | public function dropFromNodeStack($nodeType) |
||
54 | { |
||
55 | $this->nodestack->drop($nodeType); |
||
56 | } |
||
57 | |||
58 | public function getCurrentMarks() |
||
59 | { |
||
60 | return $this->marks; |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * If there is a block scope open, close it. |
||
65 | */ |
||
66 | protected function clearBlock() |
||
67 | { |
||
68 | $parentNode = $this->nodestack->current()->getType(); |
||
69 | if (in_array($parentNode, ['paragraph'])) { |
||
70 | $this->nodestack->drop($parentNode); |
||
71 | } |
||
72 | } |
||
73 | |||
74 | // FIXME implement all methods of Doku_Renderer here |
||
75 | |||
76 | /** @inheritDoc */ |
||
77 | function document_start() |
||
78 | { |
||
79 | $this->nodestack = new NodeStack(); |
||
80 | } |
||
81 | |||
82 | /** @inheritDoc */ |
||
83 | function document_end() |
||
84 | { |
||
85 | if ($this->nodestack->isEmpty()) { |
||
86 | $this->p_open(); |
||
87 | $this->p_close(); |
||
88 | } |
||
89 | $this->doc = json_encode($this->nodestack->doc(), JSON_PRETTY_PRINT); |
||
90 | } |
||
91 | |||
92 | /** @inheritDoc */ |
||
93 | function p_open() |
||
94 | { |
||
95 | $this->nodestack->addTop(new Node('paragraph')); |
||
96 | } |
||
97 | |||
98 | /** @inheritdoc */ |
||
99 | function p_close() |
||
100 | { |
||
101 | $this->nodestack->drop('paragraph'); |
||
102 | } |
||
103 | |||
104 | /** @inheritDoc */ |
||
105 | function quote_open() |
||
111 | } |
||
112 | |||
113 | /** @inheritDoc */ |
||
114 | function quote_close() |
||
115 | { |
||
116 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
117 | $this->nodestack->drop('paragraph'); |
||
118 | } |
||
119 | $this->nodestack->drop('blockquote'); |
||
120 | } |
||
121 | |||
122 | #region lists |
||
123 | |||
124 | /** @inheritDoc */ |
||
125 | function listu_open() |
||
126 | { |
||
127 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
128 | $this->nodestack->drop('paragraph'); |
||
129 | } |
||
130 | |||
131 | $this->nodestack->addTop(new Node('bullet_list')); |
||
132 | } |
||
133 | |||
134 | /** @inheritDoc */ |
||
135 | function listu_close() |
||
136 | { |
||
137 | $this->nodestack->drop('bullet_list'); |
||
138 | } |
||
139 | |||
140 | /** @inheritDoc */ |
||
141 | function listo_open() |
||
142 | { |
||
143 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
144 | $this->nodestack->drop('paragraph'); |
||
145 | } |
||
146 | |||
147 | $this->nodestack->addTop(new Node('ordered_list')); |
||
148 | } |
||
149 | |||
150 | /** @inheritDoc */ |
||
151 | function listo_close() |
||
152 | { |
||
153 | $this->nodestack->drop('ordered_list'); |
||
154 | } |
||
155 | |||
156 | /** @inheritDoc */ |
||
157 | function listitem_open($level, $node = false) |
||
158 | { |
||
159 | $this->nodestack->addTop(new Node('list_item')); |
||
160 | } |
||
161 | |||
162 | /** @inheritDoc */ |
||
163 | function listitem_close() |
||
170 | } |
||
171 | |||
172 | #endregion lists |
||
173 | |||
174 | #region table |
||
175 | |||
176 | /** @inheritDoc */ |
||
177 | function table_open($maxcols = null, $numrows = null, $pos = null) |
||
178 | { |
||
179 | $this->nodestack->addTop(new Node('table')); |
||
180 | } |
||
181 | |||
182 | /** @inheritDoc */ |
||
183 | function table_close($pos = null) |
||
184 | { |
||
185 | $this->nodestack->drop('table'); |
||
186 | } |
||
187 | |||
188 | /** @inheritDoc */ |
||
189 | function tablerow_open() |
||
193 | } |
||
194 | |||
195 | /** @inheritDoc */ |
||
196 | function tablerow_close() |
||
197 | { |
||
198 | $node = $this->nodestack->drop('table_row'); |
||
199 | $node->attr('columns', $this->colcount); |
||
200 | } |
||
201 | |||
202 | /** @inheritDoc */ |
||
203 | function tablecell_open($colspan = 1, $align = null, $rowspan = 1) |
||
204 | { |
||
205 | $this->openTableCell('table_cell', $colspan, $align, $rowspan); |
||
206 | } |
||
207 | |||
208 | /** @inheritdoc */ |
||
209 | function tablecell_close() |
||
210 | { |
||
211 | $this->closeTableCell('table_cell'); |
||
212 | } |
||
213 | |||
214 | /** @inheritDoc */ |
||
215 | function tableheader_open($colspan = 1, $align = null, $rowspan = 1) |
||
216 | { |
||
217 | $this->openTableCell('table_header', $colspan, $align, $rowspan); |
||
218 | } |
||
219 | |||
220 | /** @inheritdoc */ |
||
221 | function tableheader_close() |
||
222 | { |
||
223 | $this->closeTableCell('table_header'); |
||
224 | } |
||
225 | |||
226 | /** |
||
227 | * Add a new table cell to the top of the stack |
||
228 | * |
||
229 | * @param string $type either table_cell or table_header |
||
230 | * @param int $colspan |
||
231 | * @param string|null $align either null/left, center or right |
||
232 | * @param int $rowspan |
||
233 | */ |
||
234 | protected function openTableCell($type, $colspan, $align, $rowspan) { |
||
235 | $this->colcount += $colspan; |
||
236 | |||
237 | $node = new Node($type); |
||
238 | $node->attr('colspan', $colspan); |
||
239 | $node->attr('rowspan', $rowspan); |
||
240 | $node->attr('align', $align); |
||
241 | $this->nodestack->addTop($node); |
||
242 | |||
243 | $node = new Node('paragraph'); |
||
244 | $this->nodestack->addTop($node); |
||
245 | } |
||
246 | |||
247 | /** |
||
248 | * Remove a table cell from the top of the stack |
||
249 | * |
||
250 | * @param string $type either table_cell or table_header |
||
251 | */ |
||
252 | protected function closeTableCell($type) |
||
253 | { |
||
254 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
255 | $this->nodestack->drop('paragraph'); |
||
256 | } |
||
257 | |||
258 | $curNode = $this->nodestack->current(); |
||
259 | $curNode->trimContentLeft(); |
||
260 | $curNode->trimContentRight(); |
||
261 | $this->nodestack->drop($type); |
||
262 | } |
||
263 | |||
264 | #endregion table |
||
265 | |||
266 | /** @inheritDoc */ |
||
267 | function header($text, $level, $pos) |
||
268 | { |
||
269 | $node = new Node('heading'); |
||
270 | $node->attr('level', $level); |
||
271 | |||
272 | $tnode = new Node('text'); |
||
273 | $tnode->setText($text); |
||
274 | $node->addChild($tnode); |
||
275 | |||
276 | $this->nodestack->add($node); |
||
277 | } |
||
278 | |||
279 | /** @inheritDoc */ |
||
280 | function cdata($text) |
||
281 | { |
||
282 | if ($text === '') { |
||
283 | return; |
||
284 | } |
||
285 | |||
286 | $parentNode = $this->nodestack->current()->getType(); |
||
287 | |||
288 | if (in_array($parentNode, ['paragraph', 'footnote'])) { |
||
289 | $text = str_replace("\n", ' ', $text); |
||
290 | } |
||
291 | |||
292 | if ($parentNode === 'list_item') { |
||
293 | $node = new Node('paragraph'); |
||
294 | $this->nodestack->addTop($node); |
||
295 | } |
||
296 | |||
297 | if ($parentNode === 'blockquote') { |
||
298 | $node = new Node('paragraph'); |
||
299 | $this->nodestack->addTop($node); |
||
300 | } |
||
301 | |||
302 | if ($parentNode === 'doc') { |
||
303 | $node = new Node('paragraph'); |
||
304 | $this->nodestack->addTop($node); |
||
305 | } |
||
306 | |||
307 | $node = new Node('text'); |
||
308 | $node->setText($text); |
||
309 | foreach (array_keys($this->marks) as $mark) { |
||
310 | $node->addMark(new Mark($mark)); |
||
311 | } |
||
312 | $this->nodestack->add($node); |
||
313 | } |
||
314 | |||
315 | public function preformatted($text) |
||
316 | { |
||
317 | $this->clearBlock(); |
||
318 | $node = new Node('preformatted'); |
||
319 | $this->nodestack->addTop($node); |
||
320 | $this->cdata($text); |
||
321 | $this->nodestack->drop('preformatted'); |
||
322 | } |
||
323 | |||
324 | public function code($text, $lang = null, $file = null) |
||
334 | } |
||
335 | |||
336 | public function file($text, $lang = null, $file = null) |
||
337 | { |
||
338 | $this->code($text, $lang, $file); |
||
339 | } |
||
340 | |||
341 | public function html($text) |
||
342 | { |
||
343 | $node = new Node('html_inline'); |
||
344 | $node->attr('class', 'html_inline'); |
||
345 | $this->nodestack->addTop($node); |
||
346 | $this->cdata(str_replace("\n", ' ', $text)); |
||
347 | $this->nodestack->drop('html_inline'); |
||
348 | } |
||
349 | |||
350 | public function htmlblock($text) |
||
351 | { |
||
352 | $this->clearBlock(); |
||
353 | $node = new Node('html_block'); |
||
354 | $node->attr('class', 'html_block'); |
||
355 | $this->nodestack->addTop($node); |
||
356 | $this->cdata(trim($text, "\n")); |
||
357 | $this->nodestack->drop('html_block'); |
||
358 | } |
||
359 | |||
360 | public function php($text) |
||
361 | { |
||
362 | $node = new Node('php_inline'); |
||
363 | $node->attr('class', 'php_inline'); |
||
364 | $this->nodestack->addTop($node); |
||
365 | $this->cdata(str_replace("\n", ' ', $text)); |
||
366 | $this->nodestack->drop('php_inline'); |
||
367 | } |
||
368 | |||
369 | public function phpblock($text) |
||
370 | { |
||
371 | $this->clearBlock(); |
||
372 | $node = new Node('php_block'); |
||
373 | $node->attr('class', 'php_block'); |
||
374 | $this->nodestack->addTop($node); |
||
375 | $this->cdata(trim($text, "\n")); |
||
376 | $this->nodestack->drop('php_block'); |
||
377 | } |
||
378 | |||
379 | /** |
||
380 | * @inheritDoc |
||
381 | */ |
||
382 | function rss($url, $params) |
||
383 | { |
||
384 | $this->clearBlock(); |
||
385 | $node = new Node('rss'); |
||
386 | $node->attr('url', hsc($url)); |
||
387 | $node->attr('max', $params['max']); |
||
388 | $node->attr('reverse', (bool)$params['reverse']); |
||
389 | $node->attr('author', (bool)$params['author']); |
||
390 | $node->attr('date', (bool)$params['date']); |
||
391 | $node->attr('details', (bool)$params['details']); |
||
392 | |||
393 | if ($params['refresh'] % 86400 === 0) { |
||
394 | $refresh = $params['refresh']/86400 . 'd'; |
||
395 | } else if ($params['refresh'] % 3600 === 0) { |
||
396 | $refresh = $params['refresh']/3600 . 'h'; |
||
397 | } else { |
||
398 | $refresh = $params['refresh']/60 . 'm'; |
||
399 | } |
||
400 | |||
401 | $node->attr('refresh', trim($refresh)); |
||
402 | $this->nodestack->add($node); |
||
403 | } |
||
404 | |||
405 | |||
406 | function footnote_open() |
||
407 | { |
||
408 | $footnoteNode = new Node('footnote'); |
||
409 | $this->nodestack->addTop($footnoteNode); |
||
410 | $this->nodestackBackup[] = $this->nodestack; |
||
411 | $this->nodestack = new NodeStack(); |
||
412 | |||
413 | } |
||
414 | |||
415 | function footnote_close() |
||
416 | { |
||
417 | $json = json_encode($this->nodestack->doc()); |
||
418 | $this->nodestack = array_pop($this->nodestackBackup); |
||
419 | $this->nodestack->current()->attr('contentJSON', $json); |
||
420 | $this->nodestack->drop('footnote'); |
||
421 | } |
||
422 | |||
423 | /** |
||
424 | * @inheritDoc |
||
425 | */ |
||
426 | function internalmedia( |
||
427 | $src, |
||
428 | $title = null, |
||
429 | $align = null, |
||
430 | $width = null, |
||
431 | $height = null, |
||
432 | $cache = null, |
||
433 | $linking = null |
||
434 | ) { |
||
435 | |||
436 | // FIXME how do we handle non-images, e.g. pdfs or audio? |
||
437 | \dokuwiki\plugin\prosemirror\parser\ImageNode::render( |
||
438 | $this, |
||
439 | $src, |
||
440 | $title, |
||
441 | $align, |
||
442 | $width, |
||
443 | $height, |
||
444 | $cache, |
||
445 | $linking |
||
446 | ); |
||
447 | } |
||
448 | |||
449 | /** |
||
450 | * @inheritDoc |
||
451 | */ |
||
452 | function externalmedia( |
||
453 | $src, |
||
454 | $title = null, |
||
455 | $align = null, |
||
456 | $width = null, |
||
457 | $height = null, |
||
458 | $cache = null, |
||
459 | $linking = null |
||
460 | ) { |
||
461 | \dokuwiki\plugin\prosemirror\parser\ImageNode::render( |
||
462 | $this, |
||
463 | $src, |
||
464 | $title, |
||
465 | $align, |
||
466 | $width, |
||
467 | $height, |
||
468 | $cache, |
||
469 | $linking |
||
470 | ); |
||
471 | } |
||
472 | |||
473 | |||
474 | public function locallink($hash, $name = null) |
||
475 | { |
||
476 | \dokuwiki\plugin\prosemirror\parser\LocalLinkNode::render($this, $hash, $name); |
||
477 | } |
||
478 | |||
479 | /** |
||
480 | * @inheritDoc |
||
481 | */ |
||
482 | public function internallink($id, $name = null) |
||
483 | { |
||
484 | \dokuwiki\plugin\prosemirror\parser\InternalLinkNode::render($this, $id, $name); |
||
485 | } |
||
486 | |||
487 | public function externallink($link, $title = null) |
||
488 | { |
||
489 | \dokuwiki\plugin\prosemirror\parser\ExternalLinkNode::render($this, $link, $title); |
||
490 | } |
||
491 | |||
492 | public function interwikilink($link, $title = null, $wikiName, $wikiUri) |
||
493 | { |
||
494 | \dokuwiki\plugin\prosemirror\parser\InterwikiLinkNode::render($this, $title, $wikiName, $wikiUri); |
||
495 | } |
||
496 | |||
497 | public function emaillink($address, $name = null) |
||
498 | { |
||
499 | \dokuwiki\plugin\prosemirror\parser\EmailLinkNode::render($this, $address, $name); |
||
500 | } |
||
501 | |||
502 | public function windowssharelink($link, $title = null) |
||
503 | { |
||
504 | \dokuwiki\plugin\prosemirror\parser\WindowsShareLinkNode::render($this, $link, $title); |
||
505 | } |
||
506 | |||
507 | /** @inheritDoc */ |
||
508 | function linebreak() |
||
509 | { |
||
510 | $this->nodestack->add(new Node('hard_break')); |
||
511 | } |
||
512 | |||
513 | /** @inheritDoc */ |
||
514 | function hr() |
||
515 | { |
||
516 | $this->nodestack->add(new Node('horizontal_rule')); |
||
517 | } |
||
518 | |||
519 | function plugin($name, $data, $state = '', $match = '') |
||
520 | { |
||
521 | if (empty($match)) { |
||
522 | return; |
||
523 | } |
||
524 | $eventData = [ |
||
525 | 'name' => $name, |
||
526 | 'data' => $data, |
||
527 | 'state' => $state, |
||
528 | 'match' => $match, |
||
529 | 'renderer' => $this, |
||
530 | ]; |
||
531 | $event = new Doku_Event('PROSEMIRROR_RENDER_PLUGIN', $eventData); |
||
532 | if ($event->advise_before()) { |
||
533 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
534 | $nodetype = 'dwplugin_inline'; |
||
535 | } else { |
||
536 | $nodetype = 'dwplugin_block'; |
||
537 | } |
||
538 | $node = new Node($nodetype); |
||
539 | $node->attr('class', 'dwplugin'); |
||
540 | $node->attr('data-pluginname', $name); |
||
541 | $this->nodestack->addTop($node); |
||
542 | $this->cdata($match); |
||
543 | $this->nodestack->drop($nodetype); |
||
544 | } |
||
545 | } |
||
546 | |||
547 | function smiley($smiley) |
||
548 | { |
||
549 | if(array_key_exists($smiley, $this->smileys)) { |
||
550 | $node = new Node('smiley'); |
||
551 | $node->attr('icon', $this->smileys[$smiley]); |
||
552 | $node->attr('syntax', $smiley); |
||
553 | $this->nodestack->add($node); |
||
554 | } else { |
||
555 | $this->cdata($smiley); |
||
556 | } |
||
557 | } |
||
558 | |||
559 | #region elements with no special WYSIWYG representation |
||
560 | |||
561 | /** @inheritDoc */ |
||
562 | function entity($entity) |
||
563 | { |
||
564 | $this->cdata($entity); // FIXME should we handle them special? |
||
565 | } |
||
566 | |||
567 | /** @inheritDoc */ |
||
568 | function multiplyentity($x, $y) |
||
569 | { |
||
570 | $this->cdata($x . 'x' . $y); |
||
571 | } |
||
572 | |||
573 | /** @inheritDoc */ |
||
574 | function acronym($acronym) |
||
575 | { |
||
576 | $this->cdata($acronym); |
||
577 | } |
||
578 | |||
579 | /** @inheritDoc */ |
||
580 | function apostrophe() |
||
581 | { |
||
582 | $this->cdata("'"); |
||
583 | } |
||
584 | |||
585 | /** @inheritDoc */ |
||
586 | function singlequoteopening() |
||
587 | { |
||
588 | $this->cdata("'"); |
||
589 | } |
||
590 | |||
591 | /** @inheritDoc */ |
||
592 | function singlequoteclosing() |
||
593 | { |
||
594 | $this->cdata("'"); |
||
595 | } |
||
596 | |||
597 | /** @inheritDoc */ |
||
598 | function doublequoteopening() |
||
599 | { |
||
600 | $this->cdata('"'); |
||
601 | } |
||
602 | |||
603 | /** @inheritDoc */ |
||
604 | function doublequoteclosing() |
||
605 | { |
||
606 | $this->cdata('"'); |
||
607 | } |
||
608 | |||
609 | /** @inheritDoc */ |
||
610 | function camelcaselink($link) |
||
611 | { |
||
612 | $this->cdata($link); // FIXME should/could we decorate it? |
||
613 | } |
||
614 | |||
615 | #endregion |
||
616 | |||
617 | #region formatter marks |
||
618 | |||
619 | /** @inheritDoc */ |
||
620 | function strong_open() |
||
621 | { |
||
622 | $this->marks['strong'] = 1; |
||
623 | } |
||
624 | |||
625 | /** @inheritDoc */ |
||
626 | function strong_close() |
||
627 | { |
||
628 | if (isset($this->marks['strong'])) { |
||
629 | unset($this->marks['strong']); |
||
630 | } |
||
631 | } |
||
632 | |||
633 | /** @inheritDoc */ |
||
634 | function emphasis_open() |
||
635 | { |
||
636 | $this->marks['em'] = 1; |
||
637 | } |
||
638 | |||
639 | /** @inheritDoc */ |
||
640 | function emphasis_close() |
||
641 | { |
||
642 | if (isset($this->marks['em'])) { |
||
643 | unset($this->marks['em']); |
||
644 | } |
||
645 | } |
||
646 | |||
647 | /** @inheritdoc */ |
||
648 | function subscript_open() |
||
649 | { |
||
650 | $this->marks['subscript'] = 1; |
||
651 | } |
||
652 | |||
653 | /** @inheritDoc */ |
||
654 | function subscript_close() |
||
655 | { |
||
656 | if (isset($this->marks['subscript'])) { |
||
657 | unset($this->marks['subscript']); |
||
658 | } |
||
659 | } |
||
660 | |||
661 | /** @inheritdoc */ |
||
662 | function superscript_open() |
||
663 | { |
||
664 | $this->marks['superscript'] = 1; |
||
665 | } |
||
666 | |||
667 | /** @inheritDoc */ |
||
668 | function superscript_close() |
||
669 | { |
||
670 | if (isset($this->marks['superscript'])) { |
||
671 | unset($this->marks['superscript']); |
||
672 | } |
||
673 | } |
||
674 | |||
675 | /** @inheritDoc */ |
||
676 | function monospace_open() |
||
677 | { |
||
678 | $this->marks['code'] = 1; |
||
679 | } |
||
680 | |||
681 | /** @inheritDoc */ |
||
682 | function monospace_close() |
||
683 | { |
||
684 | if (isset($this->marks['code'])) { |
||
685 | unset($this->marks['code']); |
||
686 | } |
||
687 | } |
||
688 | |||
689 | /** @inheritDoc */ |
||
690 | function deleted_open() |
||
691 | { |
||
692 | $this->marks['deleted'] = 1; |
||
693 | } |
||
694 | |||
695 | /** @inheritDoc */ |
||
696 | function deleted_close() |
||
697 | { |
||
698 | if (isset($this->marks['deleted'])) { |
||
699 | unset($this->marks['deleted']); |
||
700 | } |
||
701 | } |
||
702 | |||
703 | /** @inheritDoc */ |
||
704 | function underline_open() |
||
707 | } |
||
708 | |||
709 | /** @inheritDoc */ |
||
710 | function underline_close() |
||
711 | { |
||
712 | if (isset($this->marks['underline'])) { |
||
713 | unset($this->marks['underline']); |
||
714 | } |
||
715 | } |
||
716 | |||
717 | |||
718 | /** @inheritDoc */ |
||
719 | function unformatted($text) |
||
720 | { |
||
721 | $this->marks['unformatted'] = 1; |
||
722 | parent::unformatted($text); |
||
723 | unset($this->marks['unformatted']); |
||
724 | } |
||
725 | |||
726 | |||
727 | #endregion formatter marks |
||
728 | } |
||
729 |