1 | <?php |
||
21 | class FencedCode extends AbstractStringContainerBlock |
||
22 | { |
||
23 | /** |
||
24 | * @var string |
||
25 | */ |
||
26 | protected $info; |
||
27 | |||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $length; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | protected $char; |
||
37 | |||
38 | /** |
||
39 | * @var int |
||
40 | */ |
||
41 | protected $offset; |
||
42 | |||
43 | /** |
||
44 | * @param int $length |
||
45 | * @param string $char |
||
46 | * @param int $offset |
||
47 | */ |
||
48 | 132 | public function __construct(int $length, string $char, int $offset) |
|
56 | |||
57 | /** |
||
58 | * @return string |
||
59 | */ |
||
60 | 3 | public function getInfo(): string |
|
61 | { |
||
62 | 3 | return $this->info; |
|
63 | } |
||
64 | |||
65 | /** |
||
66 | * @return string[] |
||
67 | */ |
||
68 | 114 | public function getInfoWords(): array |
|
69 | { |
||
70 | 114 | return \preg_split('/\s+/', $this->info) ?: []; |
|
71 | } |
||
72 | |||
73 | /** |
||
74 | * @return string |
||
75 | */ |
||
76 | 111 | public function getChar(): string |
|
80 | |||
81 | /** |
||
82 | * @param string $char |
||
83 | * |
||
84 | * @return $this |
||
85 | */ |
||
86 | 3 | public function setChar(string $char): self |
|
87 | { |
||
88 | 3 | $this->char = $char; |
|
89 | |||
90 | 3 | return $this; |
|
91 | } |
||
92 | |||
93 | /** |
||
94 | * @return int |
||
95 | */ |
||
96 | 96 | public function getLength(): int |
|
100 | |||
101 | /** |
||
102 | * @param int $length |
||
103 | * |
||
104 | * @return $this |
||
105 | */ |
||
106 | 90 | public function setLength(int $length): self |
|
112 | |||
113 | /** |
||
114 | * @return int |
||
115 | */ |
||
116 | 6 | public function getOffset(): int |
|
117 | { |
||
118 | 6 | return $this->offset; |
|
119 | } |
||
120 | |||
121 | /** |
||
122 | * @param int $offset |
||
123 | * |
||
124 | * @return $this |
||
125 | */ |
||
126 | 3 | public function setOffset(int $offset): self |
|
127 | { |
||
128 | 3 | $this->offset = $offset; |
|
129 | |||
130 | 3 | return $this; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * Returns true if this block can contain the given block as a child node |
||
135 | * |
||
136 | * @param AbstractBlock $block |
||
137 | * |
||
138 | * @return bool |
||
139 | */ |
||
140 | 3 | public function canContain(AbstractBlock $block): bool |
|
141 | { |
||
142 | 3 | return false; |
|
143 | } |
||
144 | |||
145 | /** |
||
146 | * Whether this is a code block |
||
147 | * |
||
148 | * @return bool |
||
149 | */ |
||
150 | 102 | public function isCode(): bool |
|
154 | |||
155 | 99 | public function matchesNextLine(Cursor $cursor): bool |
|
170 | |||
171 | 114 | public function finalize(ContextInterface $context, int $endLineNumber) |
|
184 | |||
185 | /** |
||
186 | * @param ContextInterface $context |
||
187 | * @param Cursor $cursor |
||
188 | */ |
||
189 | 105 | public function handleRemainingContents(ContextInterface $context, Cursor $cursor) |
|
190 | { |
||
191 | /** @var self $container */ |
||
192 | 105 | $container = $context->getContainer(); |
|
193 | |||
194 | // check for closing code fence |
||
195 | 105 | if ($cursor->getIndent() <= 3 && $cursor->getNextNonSpaceCharacter() === $container->getChar()) { |
|
196 | 93 | $match = RegexHelper::matchAll('/^(?:`{3,}|~{3,})(?= *$)/', $cursor->getLine(), $cursor->getNextNonSpacePosition()); |
|
197 | 93 | if ($match !== null && \strlen($match[0]) >= $container->getLength()) { |
|
198 | // don't add closing fence to container; instead, close it: |
||
199 | 87 | $this->setLength(-1); // -1 means we've passed closer |
|
200 | |||
201 | 87 | return; |
|
202 | } |
||
203 | } |
||
204 | |||
205 | 105 | $container->addLine($cursor->getRemainder()); |
|
206 | 105 | } |
|
207 | |||
208 | /** |
||
209 | * @param Cursor $cursor |
||
210 | * @param int $currentLineNumber |
||
211 | * |
||
212 | * @return bool |
||
213 | */ |
||
214 | 105 | public function shouldLastLineBeBlank(Cursor $cursor, int $currentLineNumber): bool |
|
218 | } |
||
219 |