Completed
Branch wip/litedown (a8c24a)
by Josh
35:58 queued 23:44
created

Blocks::parse()   F

Complexity

Conditions 79
Paths > 20000

Size

Total Lines 399
Code Lines 184

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 245
CRAP Score 79

Importance

Changes 0
Metric Value
dl 0
loc 399
ccs 245
cts 245
cp 1
rs 2
c 0
b 0
f 0
cc 79
eloc 184
nc 78382081
nop 0
crap 79

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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