Skip to content

Commit 352050f

Browse files
committed
Inline more utility calls and remove wrapper methods
- Replace $this->isBlankLine() with IndentationHelper::isBlankLine() - Replace $this->parseInlineAttributes() with AttributeParser::parse() - Replace $this->parseAttributeStringToArray() with AttributeParser::parse() - Remove the now-unused wrapper methods BlockParser reduced from 2,225 to 2,200 lines.
1 parent 9a9ab2d commit 352050f

File tree

1 file changed

+27
-52
lines changed

1 file changed

+27
-52
lines changed

src/Parser/BlockParser.php

Lines changed: 27 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ protected function extractReferences(array $lines): void
326326

327327
// Check for attributes that may precede a reference definition
328328
if (preg_match('/^\{([^}]+)\}\s*$/', $line, $attrMatches)) {
329-
$pendingAttrs = $this->parseInlineAttributes($attrMatches[1]);
329+
$pendingAttrs = AttributeParser::parse($attrMatches[1]);
330330
$i++;
331331

332332
continue;
@@ -342,7 +342,7 @@ protected function extractReferences(array $lines): void
342342
$j = $i + 1;
343343
while ($j < $count) {
344344
$nextLine = $lines[$j];
345-
if ($this->isBlankLine($nextLine)) {
345+
if (IndentationHelper::isBlankLine($nextLine)) {
346346
break;
347347
}
348348
// Check if next line starts a new reference definition
@@ -368,24 +368,14 @@ protected function extractReferences(array $lines): void
368368
}
369369

370370
// Non-reference line, clear any pending attributes
371-
if (!$this->isBlankLine($line)) {
371+
if (!IndentationHelper::isBlankLine($line)) {
372372
$pendingAttrs = [];
373373
}
374374

375375
$i++;
376376
}
377377
}
378378

379-
/**
380-
* Parse inline attributes from a string like ".class #id title=foo"
381-
*
382-
* @return array<string, mixed>
383-
*/
384-
protected function parseInlineAttributes(string $attrStr): array
385-
{
386-
return AttributeParser::parse($attrStr);
387-
}
388-
389379
/**
390380
* Extract footnote definitions from the document
391381
*
@@ -416,7 +406,7 @@ protected function extractFootnotes(array $lines): void
416406
$hasContent = false;
417407
while ($j < $count) {
418408
$nextLine = $lines[$j];
419-
if ($this->isBlankLine($nextLine)) {
409+
if (IndentationHelper::isBlankLine($nextLine)) {
420410
// Add blank line to preserve structure
421411
$contentLines[] = '';
422412
$j++;
@@ -524,7 +514,7 @@ protected function extractHeadingReferences(array $lines): void
524514
}
525515
} else {
526516
// Non-heading, non-attribute line - clear pending ID
527-
if (!$this->isBlankLine($line)) {
517+
if (!IndentationHelper::isBlankLine($line)) {
528518
$pendingId = null;
529519
}
530520
}
@@ -545,7 +535,7 @@ protected function parseBlocks(Node $parent, array $lines, int $indent): void
545535
$line = $lines[$i];
546536

547537
// Skip blank lines
548-
if ($this->isBlankLine($line)) {
538+
if (IndentationHelper::isBlankLine($line)) {
549539
$i++;
550540

551541
continue;
@@ -648,7 +638,7 @@ protected function tryParseBlockAttributes(array $lines, int $start): ?int
648638
// (they were already applied during extractReferences)
649639
$count = count($lines);
650640
$nextIdx = $start + 1;
651-
while ($nextIdx < $count && $this->isBlankLine($lines[$nextIdx])) {
641+
while ($nextIdx < $count && IndentationHelper::isBlankLine($lines[$nextIdx])) {
652642
$nextIdx++;
653643
}
654644
if ($nextIdx < $count && preg_match('/^\[([^\]]+)\]:/', $lines[$nextIdx])) {
@@ -706,16 +696,6 @@ protected function parseAttributeString(string $attrStr): void
706696
$this->pendingAttributes = AttributeParser::parseAndMerge($this->pendingAttributes, $attrStr);
707697
}
708698

709-
/**
710-
* Parse attribute string and return as array (without affecting pendingAttributes)
711-
*
712-
* @return array<string, string>
713-
*/
714-
protected function parseAttributeStringToArray(string $attrStr): array
715-
{
716-
return AttributeParser::parse($attrStr);
717-
}
718-
719699
/**
720700
* Apply pending attributes to a node and clear them
721701
*/
@@ -1040,7 +1020,7 @@ protected function tryParseHeading(Node $parent, array $lines, int $start): ?int
10401020
$nextLine = $lines[$i];
10411021

10421022
// Empty line ends the heading
1043-
if ($this->isBlankLine($nextLine)) {
1023+
if (IndentationHelper::isBlankLine($nextLine)) {
10441024
break;
10451025
}
10461026

@@ -1145,7 +1125,7 @@ protected function tryParseBlockQuote(Node $parent, array $lines, int $start): ?
11451125
while ($i < $count) {
11461126
$currentLine = $lines[$i];
11471127

1148-
if ($this->isBlankLine($currentLine)) {
1128+
if (IndentationHelper::isBlankLine($currentLine)) {
11491129
break;
11501130
}
11511131

@@ -1199,7 +1179,7 @@ protected function tryParseDefinitionList(Node $parent, array $lines, int $start
11991179
$defLine = $lines[$start + 1];
12001180

12011181
// Term must not start with special characters
1202-
if (preg_match('/^[>#\-*+\d`:|]/', $termLine) || $this->isBlankLine($termLine)) {
1182+
if (preg_match('/^[>#\-*+\d`:|]/', $termLine) || IndentationHelper::isBlankLine($termLine)) {
12031183
return null;
12041184
}
12051185

@@ -1216,7 +1196,7 @@ protected function tryParseDefinitionList(Node $parent, array $lines, int $start
12161196
$currentLine = $lines[$i];
12171197

12181198
// Skip blank lines between items
1219-
if ($this->isBlankLine($currentLine)) {
1199+
if (IndentationHelper::isBlankLine($currentLine)) {
12201200
$i++;
12211201

12221202
continue;
@@ -1243,7 +1223,7 @@ protected function tryParseDefinitionList(Node $parent, array $lines, int $start
12431223
$i++;
12441224
while ($i < $count) {
12451225
$contLine = $lines[$i];
1246-
if ($this->isBlankLine($contLine)) {
1226+
if (IndentationHelper::isBlankLine($contLine)) {
12471227
break;
12481228
}
12491229
if (preg_match('/^\s+(.+)$/', $contLine, $contMatch)) {
@@ -1332,7 +1312,7 @@ protected function tryParseList(Node $parent, array $lines, int $start): ?int
13321312
$currentLine = $lines[$i];
13331313

13341314
// Skip blank lines, track them for tight/loose determination
1335-
if ($this->isBlankLine($currentLine)) {
1315+
if (IndentationHelper::isBlankLine($currentLine)) {
13361316
$lastItemHadBlankAfter = true;
13371317
$i++;
13381318

@@ -1368,7 +1348,7 @@ protected function tryParseList(Node $parent, array $lines, int $start): ?int
13681348
$sawBlankLine = false;
13691349
while ($i < $count) {
13701350
$subLine = $lines[$i];
1371-
if ($this->isBlankLine($subLine)) {
1351+
if (IndentationHelper::isBlankLine($subLine)) {
13721352
$subLines[] = '';
13731353
$sawBlankLine = true;
13741354
$i++;
@@ -1462,7 +1442,7 @@ protected function tryParseList(Node $parent, array $lines, int $start): ?int
14621442
while ($i < $count) {
14631443
$nextLine = $lines[$i];
14641444

1465-
if ($this->isBlankLine($nextLine)) {
1445+
if (IndentationHelper::isBlankLine($nextLine)) {
14661446
break;
14671447
}
14681448

@@ -1522,7 +1502,7 @@ protected function tryParseList(Node $parent, array $lines, int $start): ?int
15221502
preg_match('/^\{([^{}]+)\}\s*$/', $trimmedAttrLine, $attrMatch) &&
15231503
IndentationHelper::getLeadingSpaces($potentialAttrLine) >= $contentIndent
15241504
) {
1525-
$itemAttributes = $this->parseAttributeStringToArray($attrMatch[1]);
1505+
$itemAttributes = AttributeParser::parse($attrMatch[1]);
15261506
$i++;
15271507
}
15281508
}
@@ -1547,7 +1527,7 @@ protected function tryParseList(Node $parent, array $lines, int $start): ?int
15471527
$subLines = [];
15481528
while ($i < $count) {
15491529
$subLine = $lines[$i];
1550-
if ($this->isBlankLine($subLine)) {
1530+
if (IndentationHelper::isBlankLine($subLine)) {
15511531
break;
15521532
}
15531533
$lineIndent = IndentationHelper::getLeadingSpaces($subLine);
@@ -1602,7 +1582,7 @@ protected function tryParseDjotDefinitionList(Node $parent, array $lines, int $s
16021582
$line = $lines[$i];
16031583

16041584
// Skip blank lines
1605-
if ($this->isBlankLine($line)) {
1585+
if (IndentationHelper::isBlankLine($line)) {
16061586
$i++;
16071587

16081588
continue;
@@ -1621,7 +1601,7 @@ protected function tryParseDjotDefinitionList(Node $parent, array $lines, int $s
16211601
$termLine = $lines[$i];
16221602

16231603
// Skip blank lines between terms
1624-
if ($this->isBlankLine($termLine)) {
1604+
if (IndentationHelper::isBlankLine($termLine)) {
16251605
$i++;
16261606

16271607
continue;
@@ -1652,7 +1632,7 @@ protected function tryParseDjotDefinitionList(Node $parent, array $lines, int $s
16521632
// Collect continuation lines for term (before blank line, single-space indent)
16531633
while ($i < $count) {
16541634
$nextLine = $lines[$i];
1655-
if ($this->isBlankLine($nextLine)) {
1635+
if (IndentationHelper::isBlankLine($nextLine)) {
16561636
break;
16571637
}
16581638
// Single space continuation is part of term
@@ -1668,7 +1648,7 @@ protected function tryParseDjotDefinitionList(Node $parent, array $lines, int $s
16681648

16691649
// Check if next non-blank line is another term or definition content
16701650
$peekIdx = $i;
1671-
while ($peekIdx < $count && $this->isBlankLine($lines[$peekIdx])) {
1651+
while ($peekIdx < $count && IndentationHelper::isBlankLine($lines[$peekIdx])) {
16721652
$peekIdx++;
16731653
}
16741654

@@ -1698,7 +1678,7 @@ protected function tryParseDjotDefinitionList(Node $parent, array $lines, int $s
16981678
while ($i < $count) {
16991679
$defLine = $lines[$i];
17001680

1701-
if ($this->isBlankLine($defLine)) {
1681+
if (IndentationHelper::isBlankLine($defLine)) {
17021682
$defLines[] = '';
17031683
$i++;
17041684

@@ -1902,7 +1882,7 @@ protected function tryParseTable(Node $parent, array $lines, int $start): ?int
19021882

19031883
// Check for caption: ^ Caption text (can have blank line before it)
19041884
$captionStart = $i;
1905-
if ($captionStart < $count && $this->isBlankLine($lines[$captionStart])) {
1885+
if ($captionStart < $count && IndentationHelper::isBlankLine($lines[$captionStart])) {
19061886
$captionStart++;
19071887
}
19081888

@@ -1914,7 +1894,7 @@ protected function tryParseTable(Node $parent, array $lines, int $start): ?int
19141894
// Caption can continue on non-blank lines that don't start a new block
19151895
while ($captionStart < $count) {
19161896
$nextLine = $lines[$captionStart];
1917-
if ($this->isBlankLine($nextLine)) {
1897+
if (IndentationHelper::isBlankLine($nextLine)) {
19181898
break;
19191899
}
19201900
// Stop at block-level elements
@@ -1964,7 +1944,7 @@ protected function tryParseFootnoteDefinition(array $lines, int $start): ?int
19641944

19651945
while ($i < $count) {
19661946
$nextLine = $lines[$i];
1967-
if ($this->isBlankLine($nextLine)) {
1947+
if (IndentationHelper::isBlankLine($nextLine)) {
19681948
$i++;
19691949

19701950
continue;
@@ -2000,7 +1980,7 @@ protected function tryParseReferenceDefinition(array $lines, int $start): ?int
20001980

20011981
while ($i < $count) {
20021982
$nextLine = $lines[$i];
2003-
if ($this->isBlankLine($nextLine)) {
1983+
if (IndentationHelper::isBlankLine($nextLine)) {
20041984
break;
20051985
}
20061986
// Check if next line starts a new reference definition
@@ -2040,7 +2020,7 @@ protected function tryParseParagraph(Node $parent, array $lines, int $start): in
20402020
// This handles cases like: text{a=x\n# not-a-heading
20412021
$hasUnclosedBrace = $this->hasUnclosedBrace($content);
20422022

2043-
if ($this->isBlankLine($nextLine)) {
2023+
if (IndentationHelper::isBlankLine($nextLine)) {
20442024
break;
20452025
}
20462026

@@ -2072,11 +2052,6 @@ protected function appendToLastParagraph(Node $parent, string $content, int $lin
20722052
}
20732053
}
20742054

2075-
protected function isBlankLine(string $line): bool
2076-
{
2077-
return trim($line) === '';
2078-
}
2079-
20802055
protected function startsNewBlock(string $line): bool
20812056
{
20822057
// In significantNewlines mode, block elements can interrupt paragraphs

0 commit comments

Comments
 (0)