|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of phpDocumentor. |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + * |
| 11 | + * @link https://phpdoc.org |
| 12 | + */ |
| 13 | + |
| 14 | +namespace phpDocumentor\Guides\RestructuredText\Parser\Productions; |
| 15 | + |
| 16 | +use phpDocumentor\Guides\Nodes\CompoundNode; |
| 17 | +use phpDocumentor\Guides\Nodes\Inline\InlineNode; |
| 18 | +use phpDocumentor\Guides\Nodes\Inline\NewlineInlineNode; |
| 19 | +use phpDocumentor\Guides\Nodes\Node; |
| 20 | +use phpDocumentor\Guides\RestructuredText\Nodes\ContainerNode; |
| 21 | +use phpDocumentor\Guides\RestructuredText\Parser\BlockContext; |
| 22 | +use phpDocumentor\Guides\RestructuredText\Parser\Buffer; |
| 23 | +use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator; |
| 24 | +use phpDocumentor\Guides\RestructuredText\Parser\UnindentStrategy; |
| 25 | + |
| 26 | +use function ltrim; |
| 27 | +use function mb_strlen; |
| 28 | +use function str_starts_with; |
| 29 | +use function substr; |
| 30 | + |
| 31 | +/** @implements Rule<ContainerNode> */ |
| 32 | +final class LineBlockRule implements Rule |
| 33 | +{ |
| 34 | + public function __construct( |
| 35 | + private InlineMarkupRule $inlineMarkupRule, |
| 36 | + ) { |
| 37 | + } |
| 38 | + |
| 39 | + public function applies(BlockContext $blockContext): bool |
| 40 | + { |
| 41 | + return str_starts_with($blockContext->getDocumentIterator()->current(), '| ') |
| 42 | + && $blockContext->getDocumentIterator()->getNextLine() !== null |
| 43 | + && str_starts_with($blockContext->getDocumentIterator()->getNextLine(), '| '); |
| 44 | + } |
| 45 | + |
| 46 | + public function apply(BlockContext $blockContext, CompoundNode|null $on = null): Node|null |
| 47 | + { |
| 48 | + return $this->createLineBlock($blockContext); |
| 49 | + } |
| 50 | + |
| 51 | + private function collectContentLines(BlockContext $blockContext): Buffer |
| 52 | + { |
| 53 | + $buffer = new Buffer( |
| 54 | + [ |
| 55 | + substr($blockContext->getDocumentIterator()->current(), 2), |
| 56 | + ], |
| 57 | + UnindentStrategy::NONE, |
| 58 | + ); |
| 59 | + |
| 60 | + $blockContext->getDocumentIterator()->next(); |
| 61 | + |
| 62 | + while ( |
| 63 | + $blockContext->getDocumentIterator()->valid() |
| 64 | + && LinesIterator::isEmptyLine($blockContext->getDocumentIterator()->current()) === false |
| 65 | + && str_starts_with($blockContext->getDocumentIterator()->current(), '|') === false |
| 66 | + ) { |
| 67 | + $buffer->push($blockContext->getDocumentIterator()->current()); |
| 68 | + $blockContext->getDocumentIterator()->next(); |
| 69 | + } |
| 70 | + |
| 71 | + return $buffer; |
| 72 | + } |
| 73 | + |
| 74 | + /** @return CompoundNode<InlineNode> */ |
| 75 | + private function createLine(BlockContext $blockContext, Buffer $buffer): CompoundNode |
| 76 | + { |
| 77 | + $line = $this->inlineMarkupRule->apply(new BlockContext( |
| 78 | + $blockContext->getDocumentParserContext(), |
| 79 | + $buffer->getLinesString(), |
| 80 | + true, |
| 81 | + )); |
| 82 | + |
| 83 | + if ($line->getChildren() === []) { |
| 84 | + $line->addChildNode(new NewlineInlineNode()); |
| 85 | + } |
| 86 | + |
| 87 | + return $line; |
| 88 | + } |
| 89 | + |
| 90 | + private function createLineBlock(BlockContext $blockContext, int $indent = 0): ContainerNode |
| 91 | + { |
| 92 | + $lineBlock = new ContainerNode(); |
| 93 | + $lineBlock->setClasses(['line-block']); |
| 94 | + |
| 95 | + while ( |
| 96 | + $blockContext->getDocumentIterator()->valid() |
| 97 | + && LinesIterator::isEmptyLine($blockContext->getDocumentIterator()->current()) === false |
| 98 | + && ($indent === 0 || LinesIterator::isIndented(substr($blockContext->getDocumentIterator()->current(), 2), $indent)) |
| 99 | + ) { |
| 100 | + if (LinesIterator::isIndented(substr($blockContext->getDocumentIterator()->current(), 2), $indent + 1)) { |
| 101 | + $line = substr($blockContext->getDocumentIterator()->current(), 2); |
| 102 | + $lineBlock->addChildNode( |
| 103 | + $this->createLineBlock($blockContext, mb_strlen($line) - mb_strlen(ltrim($line))), |
| 104 | + ); |
| 105 | + continue; |
| 106 | + } |
| 107 | + |
| 108 | + $child = new ContainerNode(); |
| 109 | + $child->setClasses(['line']); |
| 110 | + $buffer = $this->collectContentLines($blockContext); |
| 111 | + $child->addChildNode($this->createLine($blockContext, $buffer)); |
| 112 | + $lineBlock->addChildNode($child); |
| 113 | + } |
| 114 | + |
| 115 | + return $lineBlock; |
| 116 | + } |
| 117 | +} |
0 commit comments