Total Complexity | 100 |
Total Lines | 680 |
Duplicated Lines | 0 % |
Changes | 17 | ||
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() |
||
106 | { |
||
107 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
108 | $this->nodestack->drop('paragraph'); |
||
109 | } |
||
110 | $this->nodestack->addTop(new Node('blockquote')); |
||
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() |
||
164 | { |
||
165 | |||
166 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
167 | $this->nodestack->drop('paragraph'); |
||
168 | } |
||
169 | $this->nodestack->drop('list_item'); |
||
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() |
||
190 | { |
||
191 | $this->nodestack->addTop(new Node('table_row')); |
||
192 | $this->colcount = 0; |
||
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->colcount += $colspan; |
||
206 | |||
207 | $node = new Node('table_cell'); |
||
208 | $node->attr('is_header', false); |
||
209 | $node->attr('colspan', $colspan); |
||
210 | $node->attr('rowspan', $rowspan); |
||
211 | $node->attr('align', $rowspan); |
||
212 | $this->nodestack->addTop($node); |
||
213 | } |
||
214 | |||
215 | /** @inheritdoc */ |
||
216 | function tablecell_close() |
||
217 | { |
||
218 | $this->nodestack->drop('table_cell'); |
||
219 | } |
||
220 | |||
221 | /** @inheritDoc */ |
||
222 | function tableheader_open($colspan = 1, $align = null, $rowspan = 1) |
||
223 | { |
||
224 | $this->colcount += $colspan; |
||
225 | |||
226 | $node = new Node('table_cell'); |
||
227 | $node->attr('is_header', true); |
||
228 | $node->attr('colspan', $colspan); |
||
229 | $node->attr('rowspan', $rowspan); |
||
230 | $node->attr('align', $rowspan); |
||
231 | $this->nodestack->addTop($node); |
||
232 | } |
||
233 | |||
234 | /** @inheritdoc */ |
||
235 | function tableheader_close() |
||
236 | { |
||
237 | $this->nodestack->drop('table_cell'); |
||
238 | } |
||
239 | |||
240 | #endregion table |
||
241 | |||
242 | /** @inheritDoc */ |
||
243 | function header($text, $level, $pos) |
||
244 | { |
||
245 | $node = new Node('heading'); |
||
246 | $node->attr('level', $level); |
||
247 | |||
248 | $tnode = new Node('text'); |
||
249 | $tnode->setText($text); |
||
250 | $node->addChild($tnode); |
||
251 | |||
252 | $this->nodestack->add($node); |
||
253 | } |
||
254 | |||
255 | /** @inheritDoc */ |
||
256 | function cdata($text) |
||
257 | { |
||
258 | if ($text === '') { |
||
259 | return; |
||
260 | } |
||
261 | |||
262 | $parentNode = $this->nodestack->current()->getType(); |
||
263 | |||
264 | if (in_array($parentNode, ['paragraph', 'footnote'])) { |
||
265 | $text = str_replace("\n", ' ', $text); |
||
266 | } |
||
267 | |||
268 | if ($parentNode === 'list_item') { |
||
269 | $node = new Node('paragraph'); |
||
270 | $this->nodestack->addTop($node); |
||
271 | } |
||
272 | |||
273 | if ($parentNode === 'blockquote') { |
||
274 | $node = new Node('paragraph'); |
||
275 | $this->nodestack->addTop($node); |
||
276 | } |
||
277 | |||
278 | if ($parentNode === 'doc') { |
||
279 | $node = new Node('paragraph'); |
||
280 | $this->nodestack->addTop($node); |
||
281 | } |
||
282 | |||
283 | $node = new Node('text'); |
||
284 | $node->setText($text); |
||
285 | foreach (array_keys($this->marks) as $mark) { |
||
286 | $node->addMark(new Mark($mark)); |
||
287 | } |
||
288 | $this->nodestack->add($node); |
||
289 | } |
||
290 | |||
291 | public function preformatted($text) |
||
292 | { |
||
293 | $this->clearBlock(); |
||
294 | $node = new Node('preformatted'); |
||
295 | $this->nodestack->addTop($node); |
||
296 | $this->cdata($text); |
||
297 | $this->nodestack->drop('preformatted'); |
||
298 | } |
||
299 | |||
300 | public function code($text, $lang = null, $file = null) |
||
310 | } |
||
311 | |||
312 | public function file($text, $lang = null, $file = null) |
||
313 | { |
||
314 | $this->code($text, $lang, $file); |
||
315 | } |
||
316 | |||
317 | public function html($text) |
||
318 | { |
||
319 | $node = new Node('html_inline'); |
||
320 | $node->attr('class', 'html_inline'); |
||
321 | $this->nodestack->addTop($node); |
||
322 | $this->cdata(str_replace("\n", ' ', $text)); |
||
323 | $this->nodestack->drop('html_inline'); |
||
324 | } |
||
325 | |||
326 | public function htmlblock($text) |
||
327 | { |
||
328 | $this->clearBlock(); |
||
329 | $node = new Node('html_block'); |
||
330 | $node->attr('class', 'html_block'); |
||
331 | $this->nodestack->addTop($node); |
||
332 | $this->cdata(trim($text, "\n")); |
||
333 | $this->nodestack->drop('html_block'); |
||
334 | } |
||
335 | |||
336 | public function php($text) |
||
337 | { |
||
338 | $node = new Node('php_inline'); |
||
339 | $node->attr('class', 'php_inline'); |
||
340 | $this->nodestack->addTop($node); |
||
341 | $this->cdata(str_replace("\n", ' ', $text)); |
||
342 | $this->nodestack->drop('php_inline'); |
||
343 | } |
||
344 | |||
345 | public function phpblock($text) |
||
346 | { |
||
347 | $this->clearBlock(); |
||
348 | $node = new Node('php_block'); |
||
349 | $node->attr('class', 'php_block'); |
||
350 | $this->nodestack->addTop($node); |
||
351 | $this->cdata(trim($text, "\n")); |
||
352 | $this->nodestack->drop('php_block'); |
||
353 | } |
||
354 | |||
355 | /** |
||
356 | * @inheritDoc |
||
357 | */ |
||
358 | function rss($url, $params) |
||
359 | { |
||
360 | $this->clearBlock(); |
||
361 | $node = new Node('rss'); |
||
362 | $node->attr('url', hsc($url)); |
||
363 | $node->attr('max', $params['max']); |
||
364 | $node->attr('reverse', (bool)$params['reverse']); |
||
365 | $node->attr('author', (bool)$params['author']); |
||
366 | $node->attr('date', (bool)$params['date']); |
||
367 | $node->attr('details', (bool)$params['details']); |
||
368 | |||
369 | if ($params['refresh'] % 86400 === 0) { |
||
370 | $refresh = $params['refresh']/86400 . 'd'; |
||
371 | } else if ($params['refresh'] % 3600 === 0) { |
||
372 | $refresh = $params['refresh']/3600 . 'h'; |
||
373 | } else { |
||
374 | $refresh = $params['refresh']/60 . 'm'; |
||
375 | } |
||
376 | |||
377 | $node->attr('refresh', trim($refresh)); |
||
378 | $this->nodestack->add($node); |
||
379 | } |
||
380 | |||
381 | |||
382 | function footnote_open() |
||
383 | { |
||
384 | $footnoteNode = new Node('footnote'); |
||
385 | $this->nodestack->addTop($footnoteNode); |
||
386 | $this->nodestackBackup[] = $this->nodestack; |
||
387 | $this->nodestack = new NodeStack(); |
||
388 | |||
389 | } |
||
390 | |||
391 | function footnote_close() |
||
392 | { |
||
393 | $json = json_encode($this->nodestack->doc()); |
||
394 | $this->nodestack = array_pop($this->nodestackBackup); |
||
395 | $this->nodestack->current()->attr('contentJSON', $json); |
||
396 | $this->nodestack->drop('footnote'); |
||
397 | } |
||
398 | |||
399 | /** |
||
400 | * @inheritDoc |
||
401 | */ |
||
402 | function internalmedia( |
||
403 | $src, |
||
404 | $title = null, |
||
405 | $align = null, |
||
406 | $width = null, |
||
407 | $height = null, |
||
408 | $cache = null, |
||
409 | $linking = null |
||
410 | ) { |
||
411 | |||
412 | // FIXME how do we handle non-images, e.g. pdfs or audio? |
||
413 | \dokuwiki\plugin\prosemirror\parser\ImageNode::render( |
||
414 | $this, |
||
415 | $src, |
||
416 | $title, |
||
417 | $align, |
||
418 | $width, |
||
419 | $height, |
||
420 | $cache, |
||
421 | $linking |
||
422 | ); |
||
423 | } |
||
424 | |||
425 | /** |
||
426 | * @inheritDoc |
||
427 | */ |
||
428 | function externalmedia( |
||
429 | $src, |
||
430 | $title = null, |
||
431 | $align = null, |
||
432 | $width = null, |
||
433 | $height = null, |
||
434 | $cache = null, |
||
435 | $linking = null |
||
436 | ) { |
||
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 | public function locallink($hash, $name = null) |
||
451 | { |
||
452 | \dokuwiki\plugin\prosemirror\parser\LocalLinkNode::render($this, $hash, $name); |
||
453 | } |
||
454 | |||
455 | /** |
||
456 | * @inheritDoc |
||
457 | */ |
||
458 | public function internallink($id, $name = null) |
||
459 | { |
||
460 | \dokuwiki\plugin\prosemirror\parser\InternalLinkNode::render($this, $id, $name); |
||
461 | } |
||
462 | |||
463 | public function externallink($link, $title = null) |
||
464 | { |
||
465 | \dokuwiki\plugin\prosemirror\parser\ExternalLinkNode::render($this, $link, $title); |
||
466 | } |
||
467 | |||
468 | public function interwikilink($link, $title = null, $wikiName, $wikiUri) |
||
469 | { |
||
470 | \dokuwiki\plugin\prosemirror\parser\InterwikiLinkNode::render($this, $title, $wikiName, $wikiUri); |
||
471 | } |
||
472 | |||
473 | public function emaillink($address, $name = null) |
||
474 | { |
||
475 | \dokuwiki\plugin\prosemirror\parser\EmailLinkNode::render($this, $address, $name); |
||
476 | } |
||
477 | |||
478 | public function windowssharelink($link, $title = null) |
||
479 | { |
||
480 | \dokuwiki\plugin\prosemirror\parser\WindowsShareLinkNode::render($this, $link, $title); |
||
481 | } |
||
482 | |||
483 | /** @inheritDoc */ |
||
484 | function linebreak() |
||
485 | { |
||
486 | $this->nodestack->add(new Node('hard_break')); |
||
487 | } |
||
488 | |||
489 | /** @inheritDoc */ |
||
490 | function hr() |
||
491 | { |
||
492 | $this->nodestack->add(new Node('horizontal_rule')); |
||
493 | } |
||
494 | |||
495 | function plugin($name, $data, $state = '', $match = '') |
||
496 | { |
||
497 | if (empty($match)) { |
||
498 | return; |
||
499 | } |
||
500 | $eventData = [ |
||
501 | 'name' => $name, |
||
502 | 'data' => $data, |
||
503 | 'state' => $state, |
||
504 | 'match' => $match, |
||
505 | 'renderer' => $this, |
||
506 | ]; |
||
507 | $event = new Doku_Event('PROSEMIRROR_RENDER_PLUGIN', $eventData); |
||
508 | if ($event->advise_before()) { |
||
509 | if ($this->nodestack->current()->getType() === 'paragraph') { |
||
510 | $nodetype = 'dwplugin_inline'; |
||
511 | } else { |
||
512 | $nodetype = 'dwplugin_block'; |
||
513 | } |
||
514 | $node = new Node($nodetype); |
||
515 | $node->attr('class', 'dwplugin'); |
||
516 | $node->attr('data-pluginname', $name); |
||
517 | $this->nodestack->addTop($node); |
||
518 | $this->cdata($match); |
||
519 | $this->nodestack->drop($nodetype); |
||
520 | } |
||
521 | } |
||
522 | |||
523 | function smiley($smiley) |
||
524 | { |
||
525 | if(array_key_exists($smiley, $this->smileys)) { |
||
526 | $node = new Node('smiley'); |
||
527 | $node->attr('icon', $this->smileys[$smiley]); |
||
528 | $node->attr('syntax', $smiley); |
||
529 | $this->nodestack->add($node); |
||
530 | } else { |
||
531 | $this->cdata($smiley); |
||
532 | } |
||
533 | } |
||
534 | |||
535 | #region elements with no special WYSIWYG representation |
||
536 | |||
537 | /** @inheritDoc */ |
||
538 | function entity($entity) |
||
539 | { |
||
540 | $this->cdata($entity); // FIXME should we handle them special? |
||
541 | } |
||
542 | |||
543 | /** @inheritDoc */ |
||
544 | function multiplyentity($x, $y) |
||
545 | { |
||
546 | $this->cdata($x . 'x' . $y); |
||
547 | } |
||
548 | |||
549 | /** @inheritDoc */ |
||
550 | function acronym($acronym) |
||
551 | { |
||
552 | $this->cdata($acronym); |
||
553 | } |
||
554 | |||
555 | /** @inheritDoc */ |
||
556 | function apostrophe() |
||
557 | { |
||
558 | $this->cdata("'"); |
||
559 | } |
||
560 | |||
561 | /** @inheritDoc */ |
||
562 | function singlequoteopening() |
||
563 | { |
||
564 | $this->cdata("'"); |
||
565 | } |
||
566 | |||
567 | /** @inheritDoc */ |
||
568 | function singlequoteclosing() |
||
569 | { |
||
570 | $this->cdata("'"); |
||
571 | } |
||
572 | |||
573 | /** @inheritDoc */ |
||
574 | function doublequoteopening() |
||
575 | { |
||
576 | $this->cdata('"'); |
||
577 | } |
||
578 | |||
579 | /** @inheritDoc */ |
||
580 | function doublequoteclosing() |
||
581 | { |
||
582 | $this->cdata('"'); |
||
583 | } |
||
584 | |||
585 | /** @inheritDoc */ |
||
586 | function camelcaselink($link) |
||
587 | { |
||
588 | $this->cdata($link); // FIXME should/could we decorate it? |
||
589 | } |
||
590 | |||
591 | #endregion |
||
592 | |||
593 | #region formatter marks |
||
594 | |||
595 | /** @inheritDoc */ |
||
596 | function strong_open() |
||
597 | { |
||
598 | $this->marks['strong'] = 1; |
||
599 | } |
||
600 | |||
601 | /** @inheritDoc */ |
||
602 | function strong_close() |
||
603 | { |
||
604 | if (isset($this->marks['strong'])) { |
||
605 | unset($this->marks['strong']); |
||
606 | } |
||
607 | } |
||
608 | |||
609 | /** @inheritDoc */ |
||
610 | function emphasis_open() |
||
611 | { |
||
612 | $this->marks['em'] = 1; |
||
613 | } |
||
614 | |||
615 | /** @inheritDoc */ |
||
616 | function emphasis_close() |
||
617 | { |
||
618 | if (isset($this->marks['em'])) { |
||
619 | unset($this->marks['em']); |
||
620 | } |
||
621 | } |
||
622 | |||
623 | /** @inheritdoc */ |
||
624 | function subscript_open() |
||
625 | { |
||
626 | $this->marks['subscript'] = 1; |
||
627 | } |
||
628 | |||
629 | /** @inheritDoc */ |
||
630 | function subscript_close() |
||
631 | { |
||
632 | if (isset($this->marks['subscript'])) { |
||
633 | unset($this->marks['subscript']); |
||
634 | } |
||
635 | } |
||
636 | |||
637 | /** @inheritdoc */ |
||
638 | function superscript_open() |
||
639 | { |
||
640 | $this->marks['superscript'] = 1; |
||
641 | } |
||
642 | |||
643 | /** @inheritDoc */ |
||
644 | function superscript_close() |
||
645 | { |
||
646 | if (isset($this->marks['superscript'])) { |
||
647 | unset($this->marks['superscript']); |
||
648 | } |
||
649 | } |
||
650 | |||
651 | /** @inheritDoc */ |
||
652 | function monospace_open() |
||
653 | { |
||
654 | $this->marks['code'] = 1; |
||
655 | } |
||
656 | |||
657 | /** @inheritDoc */ |
||
658 | function monospace_close() |
||
659 | { |
||
660 | if (isset($this->marks['code'])) { |
||
661 | unset($this->marks['code']); |
||
662 | } |
||
663 | } |
||
664 | |||
665 | /** @inheritDoc */ |
||
666 | function deleted_open() |
||
667 | { |
||
668 | $this->marks['deleted'] = 1; |
||
669 | } |
||
670 | |||
671 | /** @inheritDoc */ |
||
672 | function deleted_close() |
||
673 | { |
||
674 | if (isset($this->marks['deleted'])) { |
||
675 | unset($this->marks['deleted']); |
||
676 | } |
||
677 | } |
||
678 | |||
679 | /** @inheritDoc */ |
||
680 | function underline_open() |
||
683 | } |
||
684 | |||
685 | /** @inheritDoc */ |
||
686 | function underline_close() |
||
687 | { |
||
688 | if (isset($this->marks['underline'])) { |
||
689 | unset($this->marks['underline']); |
||
690 | } |
||
691 | } |
||
692 | |||
693 | |||
694 | /** @inheritDoc */ |
||
695 | function unformatted($text) |
||
696 | { |
||
697 | $this->marks['unformatted'] = 1; |
||
698 | parent::unformatted($text); |
||
699 | unset($this->marks['unformatted']); |
||
700 | } |
||
701 | |||
702 | |||
703 | #endregion formatter marks |
||
704 | } |
||
705 |