1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This is part of the webuni/commonmark-attributes-extension package. |
5
|
|
|
* |
6
|
|
|
* (c) Martin Hasoň <[email protected]> |
7
|
|
|
* (c) Webuni s.r.o. <[email protected]> |
8
|
|
|
* |
9
|
|
|
* For the full copyright and license information, please view the LICENSE |
10
|
|
|
* file that was distributed with this source code. |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Webuni\CommonMark\AttributesExtension; |
14
|
|
|
|
15
|
|
|
use League\CommonMark\Block\Element\AbstractBlock; |
16
|
|
|
use League\CommonMark\Block\Element\Document; |
17
|
|
|
use League\CommonMark\Block\Element\ListBlock; |
18
|
|
|
use League\CommonMark\Block\Element\ListItem; |
19
|
|
|
use League\CommonMark\DocumentProcessorInterface; |
20
|
|
|
use League\CommonMark\Node\Node; |
21
|
|
|
|
22
|
|
|
class AttributesProcessor implements DocumentProcessorInterface |
23
|
|
|
{ |
24
|
|
|
const DIRECTION_PREFIX = 'prefix'; |
25
|
|
|
const DIRECTION_SUFFIX = 'suffix'; |
26
|
|
|
|
27
|
|
|
public function processDocument(Document $document) |
28
|
|
|
{ |
29
|
|
|
$walker = $document->walker(); |
30
|
|
|
while ($event = $walker->next()) { |
31
|
|
|
$node = $event->getNode(); |
32
|
|
|
|
33
|
|
|
if ($event->isEntering() || !$node instanceof Attributes) { |
34
|
|
|
continue; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
list($target, $direction) = $this->findTargetAndDirection($node); |
38
|
|
|
|
39
|
|
|
if ($target) { |
40
|
|
View Code Duplication |
if (($parent = $target->parent()) instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) { |
|
|
|
|
41
|
|
|
$target = $parent; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
if ($direction === self::DIRECTION_SUFFIX) { |
45
|
|
|
$attributes = AttributesUtils::merge($target, $node->getAttributes()); |
46
|
|
|
} else { |
47
|
|
|
$attributes = AttributesUtils::merge($node->getAttributes(), $target); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
$target->data['attributes'] = $attributes; |
|
|
|
|
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
if ($node instanceof AbstractBlock && $node->endsWithBlankLine() && $node->next() && $node->previous()) { |
54
|
|
|
$node->previous()->setLastLineBlank(true); |
|
|
|
|
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$node->detach(); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
private function findTargetAndDirection(Node $node) |
62
|
|
|
{ |
63
|
|
|
$target = null; |
64
|
|
|
$direction = null; |
65
|
|
|
$previous = $next = $node; |
66
|
|
|
while (true) { |
67
|
|
|
$previous = $this->getPrevious($previous); |
68
|
|
|
$next = $this->getNext($next); |
69
|
|
|
|
70
|
|
|
if ($previous === null && $next === null) { |
71
|
|
|
$target = $node->parent(); |
72
|
|
|
$direction = self::DIRECTION_SUFFIX; |
73
|
|
|
break; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
if ($previous !== null && !$previous instanceof Attributes) { |
|
|
|
|
77
|
|
|
$target = $previous; |
78
|
|
|
$direction = self::DIRECTION_SUFFIX; |
79
|
|
|
break; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
View Code Duplication |
if ($next !== null && !$next instanceof Attributes) { |
|
|
|
|
83
|
|
|
$target = $next; |
84
|
|
|
$direction = self::DIRECTION_PREFIX; |
85
|
|
|
break; |
86
|
|
|
} |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
return [$target, $direction]; |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
private function getPrevious(Node $node = null) |
93
|
|
|
{ |
94
|
|
|
$previous = $node instanceof Node ? $node->previous() : null; |
95
|
|
|
|
96
|
|
|
if ($previous instanceof AbstractBlock && $previous->endsWithBlankLine()) { |
97
|
|
|
$previous = null; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
return $previous; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
private function getNext(Node $node = null) |
104
|
|
|
{ |
105
|
|
|
if ($node instanceof Node) { |
106
|
|
|
return $node instanceof AbstractBlock && $node->endsWithBlankLine() ? null : $node->next(); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.