Completed
Push — master ( a3e52e...3da711 )
by Josh
01:34
created

Blocks::getBlockMarks()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2019 The s9e Authors
6
* @license   http://www.opensource.org/licenses/mit-license.php The MIT License
7
*/
8
namespace s9e\TextFormatter\Plugins\Litedown\Parser\Passes;
9
10
use s9e\TextFormatter\Parser as Rules;
11
12
class Blocks extends AbstractPass
13
{
14
	/**
15
	* @var array
16
	*/
17
	protected $setextLines = [];
18
19
	/**
20
	* {@inheritdoc}
21
	*/
22 123
	public function parse()
23
	{
24 123
		$this->matchSetextLines();
25
26 123
		$blocks       = [];
27 123
		$blocksCnt    = 0;
28 123
		$codeFence    = null;
29 123
		$codeIndent   = 4;
30 123
		$codeTag      = null;
31 123
		$lineIsEmpty  = true;
32 123
		$lists        = [];
33 123
		$listsCnt     = 0;
34 123
		$newContext   = false;
35 123
		$textBoundary = 0;
36
37 123
		$regexp = '/^(?:(?=[-*+\\d \\t>`~#_])((?: {0,3}>(?:(?!!)|!(?![^\\n>]*?!<)) ?)+)?([ \\t]+)?(\\* *\\* *\\*[* ]*$|- *- *-[- ]*$|_ *_ *_[_ ]*$|=+$)?((?:[-*+]|\\d+\\.)[ \\t]+(?=\\S))?[ \\t]*(#{1,6}[ \\t]+|```+[^`\\n]*$|~~~+[^~\\n]*$)?)?/m';
38 123
		preg_match_all($regexp, $this->text, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
39
40 123
		foreach ($matches as $m)
41
		{
42 123
			$blockDepth = 0;
43 123
			$blockMarks = [];
44 123
			$ignoreLen  = 0;
45 123
			$matchLen   = strlen($m[0][0]);
46 123
			$matchPos   = $m[0][1];
47
48
			// If the last line was empty then this is not a continuation, and vice-versa
49 123
			$continuation = !$lineIsEmpty;
50
51
			// Capture the position of the end of the line and determine whether the line is empty
52 123
			$lfPos       = $this->text->indexOf("\n", $matchPos);
53 123
			$lineIsEmpty = ($lfPos === $matchPos + $matchLen && empty($m[3][0]) && empty($m[4][0]) && empty($m[5][0]));
54
55
			// If the line is empty and it's the first empty line then we break current paragraph.
56 123
			$breakParagraph = ($lineIsEmpty && $continuation);
57
58
			// Count block marks
59 123
			if (!empty($m[1][0]))
60
			{
61 37
				$blockMarks = $this->getBlockMarks($m[1][0]);
62 37
				$blockDepth = count($blockMarks);
63 37
				$ignoreLen  = strlen($m[1][0]);
64 37
				if (isset($codeTag) && $codeTag->hasAttribute('blockDepth'))
65
				{
66 5
					$blockDepth = min($blockDepth, $codeTag->getAttribute('blockDepth'));
67 5
					$ignoreLen  = $this->computeBlockIgnoreLen($m[1][0], $blockDepth);
68
				}
69
70
				// Overwrite block markup
71 37
				$this->text->overwrite($matchPos, $ignoreLen);
72
			}
73
74
			// Close supernumerary blocks
75 123
			if ($blockDepth < $blocksCnt && !$continuation)
76
			{
77 36
				$newContext = true;
78
				do
79
				{
80 36
					$startTag = array_pop($blocks);
81 36
					$this->parser->addEndTag($startTag->getName(), $textBoundary, 0)
82 36
					             ->pairWith($startTag);
83
				}
84 36
				while ($blockDepth < --$blocksCnt);
85
			}
86
87
			// Open new blocks
88 123
			if ($blockDepth > $blocksCnt && !$lineIsEmpty)
89
			{
90 36
				$newContext = true;
91
				do
92
				{
93 36
					$tagName  = ($blockMarks[$blocksCnt] === '>!') ? 'SPOILER' : 'QUOTE';
94 36
					$blocks[] = $this->parser->addStartTag($tagName, $matchPos, 0, -999);
95
				}
96 36
				while ($blockDepth > ++$blocksCnt);
97
			}
98
99
			// Compute the width of the indentation
100 123
			$indentWidth = 0;
101 123
			$indentPos   = 0;
102 123
			if (!empty($m[2][0]) && !$codeFence)
103
			{
104 36
				$indentStr = $m[2][0];
105 36
				$indentLen = strlen($indentStr);
106
				do
107
				{
108 36
					if ($indentStr[$indentPos] === ' ')
109
					{
110 34
						++$indentWidth;
111
					}
112
					else
113
					{
114 4
						$indentWidth = ($indentWidth + 4) & ~3;
115
					}
116
				}
117 36
				while (++$indentPos < $indentLen && $indentWidth < $codeIndent);
118
			}
119
120
			// Test whether we're out of a code block
121 123
			if (isset($codeTag) && !$codeFence && $indentWidth < $codeIndent && !$lineIsEmpty)
122
			{
123 16
				$newContext = true;
124
			}
125
126 123
			if ($newContext)
127
			{
128 49
				$newContext = false;
129
130
				// Close the code block if applicable
131 49
				if (isset($codeTag))
132
				{
133 16
					if ($textBoundary > $codeTag->getPos())
134
					{
135
						// Overwrite the whole block
136 14
						$this->text->overwrite($codeTag->getPos(), $textBoundary - $codeTag->getPos());
137
138 14
						$endTag = $this->parser->addEndTag('CODE', $textBoundary, 0, -1);
139 14
						$endTag->pairWith($codeTag);
140
					}
141
					else
142
					{
143
						// The code block is empty
144 2
						$codeTag->invalidate();
145
					}
146
147 16
					$codeTag = null;
148 16
					$codeFence = null;
149
				}
150
151
				// Close all the lists
152 49
				foreach ($lists as $list)
153
				{
154 2
					$this->closeList($list, $textBoundary);
155
				}
156 49
				$lists    = [];
157 49
				$listsCnt = 0;
158
159
				// Mark the block boundary
160 49
				if ($matchPos)
161
				{
162 49
					$this->text->markBoundary($matchPos - 1);
163
				}
164
			}
165
166 123
			if ($indentWidth >= $codeIndent)
167
			{
168 17
				if (isset($codeTag) || !$continuation)
169
				{
170
					// Adjust the amount of text being ignored
171 16
					$ignoreLen += $indentPos;
172
173 16
					if (!isset($codeTag))
174
					{
175
						// Create code block
176 16
						$codeTag = $this->parser->addStartTag('CODE', $matchPos + $ignoreLen, 0, -999);
177
					}
178
179
					// Clear the captures to prevent any further processing
180 17
					$m = [];
181
				}
182
			}
183
			else
184
			{
185 123
				$hasListItem = !empty($m[4][0]);
186
187 123
				if (!$indentWidth && !$continuation && !$hasListItem)
188
				{
189
					// Start of a new context
190 123
					$listIndex = -1;
191
				}
192 121
				elseif ($continuation && !$hasListItem)
193
				{
194
					// Continuation of current list item or paragraph
195 121
					$listIndex = $listsCnt - 1;
196
				}
197 30
				elseif (!$listsCnt)
198
				{
199
					// We're not inside of a list already, we can start one if there's a list item
200 30
					$listIndex = ($hasListItem) ? 0 : -1;
201
				}
202
				else
203
				{
204
					// We're inside of a list but we need to compute the depth
205 21
					$listIndex = 0;
206 21
					while ($listIndex < $listsCnt && $indentWidth > $lists[$listIndex]['maxIndent'])
207
					{
208 7
						++$listIndex;
209
					}
210
				}
211
212
				// Close deeper lists
213 123
				while ($listIndex < $listsCnt - 1)
214
				{
215 27
					$this->closeList(array_pop($lists), $textBoundary);
216 27
					--$listsCnt;
217
				}
218
219
				// If there's no list item at current index, we'll need to either create one or
220
				// drop down to previous index, in which case we have to adjust maxIndent
221 123
				if ($listIndex === $listsCnt && !$hasListItem)
222
				{
223 2
					--$listIndex;
224
				}
225
226 123
				if ($hasListItem && $listIndex >= 0)
227
				{
228 28
					$breakParagraph = true;
229
230
					// Compute the position and amount of text consumed by the item tag
231 28
					$tagPos = $matchPos + $ignoreLen + $indentPos;
232 28
					$tagLen = strlen($m[4][0]);
233
234
					// Create a LI tag that consumes its markup
235 28
					$itemTag = $this->parser->addStartTag('LI', $tagPos, $tagLen);
236
237
					// Overwrite the markup
238 28
					$this->text->overwrite($tagPos, $tagLen);
239
240
					// If the list index is within current lists count it means this is not a new
241
					// list and we have to close the last item. Otherwise, it's a new list that we
242
					// have to create
243 28
					if ($listIndex < $listsCnt)
244
					{
245 20
						$this->parser->addEndTag('LI', $textBoundary, 0)
246 20
						             ->pairWith($lists[$listIndex]['itemTag']);
247
248
						// Record the item in the list
249 20
						$lists[$listIndex]['itemTag']    = $itemTag;
250 20
						$lists[$listIndex]['itemTags'][] = $itemTag;
251
					}
252
					else
253
					{
254 28
						++$listsCnt;
255
256 28
						if ($listIndex)
257
						{
258 6
							$minIndent = $lists[$listIndex - 1]['maxIndent'] + 1;
259 6
							$maxIndent = max($minIndent, $listIndex * 4);
260
						}
261
						else
262
						{
263 28
							$minIndent = 0;
264 28
							$maxIndent = $indentWidth;
265
						}
266
267
						// Create a 0-width LIST tag right before the item tag LI
268 28
						$listTag = $this->parser->addStartTag('LIST', $tagPos, 0);
269
270
						// Test whether the list item ends with a dot, as in "1."
271 28
						if (strpos($m[4][0], '.') !== false)
272
						{
273 10
							$listTag->setAttribute('type', 'decimal');
274
275 10
							$start = (int) $m[4][0];
276 10
							if ($start !== 1)
277
							{
278 2
								$listTag->setAttribute('start', $start);
279
							}
280
						}
281
282
						// Record the new list depth
283 28
						$lists[] = [
284 28
							'listTag'   => $listTag,
285 28
							'itemTag'   => $itemTag,
286 28
							'itemTags'  => [$itemTag],
287 28
							'minIndent' => $minIndent,
288 28
							'maxIndent' => $maxIndent,
289
							'tight'     => true
290
						];
291
					}
292
				}
293
294
				// If we're in a list, on a non-empty line preceded with a blank line...
295 123
				if ($listsCnt && !$continuation && !$lineIsEmpty)
296
				{
297
					// ...and this is not the first item of the list...
298 23
					if (count($lists[0]['itemTags']) > 1 || !$hasListItem)
299
					{
300
						// ...every list that is currently open becomes loose
301 6
						foreach ($lists as &$list)
302
						{
303 6
							$list['tight'] = false;
304
						}
305 6
						unset($list);
306
					}
307
				}
308
309 123
				$codeIndent = ($listsCnt + 1) * 4;
310
			}
311
312 123
			if (isset($m[5]))
313
			{
314
				// Headers
315 37
				if ($m[5][0][0] === '#')
316
				{
317 17
					$startLen = strlen($m[5][0]);
318 17
					$startPos = $matchPos + $matchLen - $startLen;
319 17
					$endLen   = $this->getAtxHeaderEndTagLen($matchPos + $matchLen, $lfPos);
320 17
					$endPos   = $lfPos - $endLen;
321
322 17
					$this->parser->addTagPair('H' . strspn($m[5][0], '#', 0, 6), $startPos, $startLen, $endPos, $endLen);
323
324
					// Mark the start and the end of the header as boundaries
325 17
					$this->text->markBoundary($startPos);
326 17
					$this->text->markBoundary($lfPos);
327
328 17
					if ($continuation)
329
					{
330 17
						$breakParagraph = true;
331
					}
332
				}
333
				// Code fence
334 20
				elseif ($m[5][0][0] === '`' || $m[5][0][0] === '~')
335
				{
336 20
					$tagPos = $matchPos + $ignoreLen;
337 20
					$tagLen = $lfPos - $tagPos;
338
339 20
					if (isset($codeTag) && $m[5][0] === $codeFence)
340
					{
341 20
						$endTag = $this->parser->addEndTag('CODE', $tagPos, $tagLen, -1);
342 20
						$endTag->pairWith($codeTag);
343
344 20
						$this->parser->addIgnoreTag($textBoundary, $tagPos - $textBoundary);
345
346
						// Overwrite the whole block
347 20
						$this->text->overwrite($codeTag->getPos(), $tagPos + $tagLen - $codeTag->getPos());
348 20
						$codeTag = null;
349 20
						$codeFence = null;
350
					}
351 20
					elseif (!isset($codeTag))
352
					{
353
						// Create code block
354 20
						$codeTag   = $this->parser->addStartTag('CODE', $tagPos, $tagLen);
355 20
						$codeFence = substr($m[5][0], 0, strspn($m[5][0], '`~'));
356 20
						$codeTag->setAttribute('blockDepth', $blockDepth);
357
358
						// Ignore the next character, which should be a newline
359 20
						$this->parser->addIgnoreTag($tagPos + $tagLen, 1);
360
361
						// Add the language if present, e.g. ```php
362 20
						$lang = trim(trim($m[5][0], '`~'));
363 20
						if ($lang !== '')
364
						{
365 37
							$codeTag->setAttribute('lang', $lang);
366
						}
367
					}
368
				}
369
			}
370 123
			elseif (!empty($m[3][0]) && !$listsCnt && $this->text->charAt($matchPos + $matchLen) !== "\x17")
371
			{
372
				// Horizontal rule
373 9
				$this->parser->addSelfClosingTag('HR', $matchPos + $ignoreLen, $matchLen - $ignoreLen);
374 9
				$breakParagraph = true;
375
376
				// Mark the end of the line as a boundary
377 9
				$this->text->markBoundary($lfPos);
378
			}
379 123
			elseif (isset($this->setextLines[$lfPos]) && $this->setextLines[$lfPos]['blockDepth'] === $blockDepth && !$lineIsEmpty && !$listsCnt && !isset($codeTag))
380
			{
381
				// Setext-style header
382 11
				$this->parser->addTagPair(
383 11
					$this->setextLines[$lfPos]['tagName'],
384 11
					$matchPos + $ignoreLen,
385 11
					0,
386 11
					$this->setextLines[$lfPos]['endPos'],
387 11
					$this->setextLines[$lfPos]['endLen']
388
				);
389
390
				// Mark the end of the Setext line
391 11
				$this->text->markBoundary($this->setextLines[$lfPos]['endPos'] + $this->setextLines[$lfPos]['endLen']);
392
			}
393
394 123
			if ($breakParagraph)
395
			{
396 121
				$this->parser->addParagraphBreak($textBoundary);
397 121
				$this->text->markBoundary($textBoundary);
398
			}
399
400 123
			if (!$lineIsEmpty)
401
			{
402 123
				$textBoundary = $lfPos;
403
			}
404
405 123
			if ($ignoreLen)
406
			{
407 49
				$this->parser->addIgnoreTag($matchPos, $ignoreLen, 1000);
408
			}
409
		}
410
	}
411
412
	/**
413
	* Close a list at given offset
414
	*
415
	* @param  array   $list
416
	* @param  integer $textBoundary
417
	* @return void
418
	*/
419 28
	protected function closeList(array $list, $textBoundary)
420
	{
421 28
		$this->parser->addEndTag('LIST', $textBoundary, 0)->pairWith($list['listTag']);
422 28
		$this->parser->addEndTag('LI',   $textBoundary, 0)->pairWith($list['itemTag']);
423
424 28
		if ($list['tight'])
425
		{
426 25
			foreach ($list['itemTags'] as $itemTag)
427
			{
428 25
				$itemTag->removeFlags(Rules::RULE_CREATE_PARAGRAPHS);
429
			}
430
		}
431
	}
432
433
	/**
434
	* Compute the amount of text to ignore at the start of a block line
435
	*
436
	* @param  string  $str           Original block markup
437
	* @param  integer $maxBlockDepth Maximum block depth
438
	* @return integer                Number of characters to ignore
439
	*/
440 5
	protected function computeBlockIgnoreLen($str, $maxBlockDepth)
441
	{
442 5
		$remaining = $str;
443 5
		while (--$maxBlockDepth >= 0)
444
		{
445 4
			$remaining = preg_replace('/^ *>!? ?/', '', $remaining);
446
		}
447
448 5
		return strlen($str) - strlen($remaining);
449
	}
450
451
	/**
452
	* Return the length of the markup at the end of an ATX header
453
	*
454
	* @param  integer $startPos Start of the header's text
455
	* @param  integer $endPos   End of the header's text
456
	* @return integer
457
	*/
458 17
	protected function getAtxHeaderEndTagLen($startPos, $endPos)
459
	{
460 17
		$content = substr($this->text, $startPos, $endPos - $startPos);
461 17
		preg_match('/[ \\t]*#*[ \\t]*$/', $content, $m);
462
463 17
		return strlen($m[0]);
464
	}
465
466
	/**
467
	* Capture and return block marks from given string
468
	*
469
	* @param  string   $str Block markup, composed of ">", "!" and whitespace
470
	* @return string[]
471
	*/
472 37
	protected function getBlockMarks($str)
473
	{
474 37
		preg_match_all('(>!?)', $str, $m);
475
476 37
		return $m[0];
477
	}
478
479
	/**
480
	* Capture and store lines that contain a Setext-tyle header
481
	*
482
	* @return void
483
	*/
484 123
	protected function matchSetextLines()
485
	{
486 123
		if ($this->text->indexOf('-') === false && $this->text->indexOf('=') === false)
487
		{
488 93
			return;
489
		}
490
491
		// Capture the any series of - or = alone on a line, optionally preceded with the
492
		// angle brackets notation used in block markup
493 30
		$regexp = '/^(?=[-=>])(?:>!? ?)*(?=[-=])(?:-+|=+) *$/m';
494 30
		if (!preg_match_all($regexp, $this->text, $matches, PREG_OFFSET_CAPTURE))
495
		{
496 11
			return;
497
		}
498
499 19
		foreach ($matches[0] as list($match, $matchPos))
500
		{
501
			// Compute the position of the end tag. We start on the LF character before the
502
			// match and keep rewinding until we find a non-space character
503 19
			$endPos = $matchPos - 1;
504 19
			while ($endPos > 0 && $this->text->charAt($endPos - 1) === ' ')
505
			{
506 4
				--$endPos;
507
			}
508
509
			// Store at the offset of the LF character
510 19
			$this->setextLines[$matchPos - 1] = [
511 19
				'endLen'     => $matchPos + strlen($match) - $endPos,
512 19
				'endPos'     => $endPos,
513 19
				'blockDepth' => substr_count($match, '>'),
514 19
				'tagName'    => ($match[0] === '=') ? 'H1' : 'H2'
515
			];
516
		}
517
	}
518
}