diff --git a/incubator/guides-markdown/src/Markdown/Parsers/Paragraph.php b/incubator/guides-markdown/src/Markdown/Parsers/Paragraph.php index bd0e9504cf..1305b93583 100644 --- a/incubator/guides-markdown/src/Markdown/Parsers/Paragraph.php +++ b/incubator/guides-markdown/src/Markdown/Parsers/Paragraph.php @@ -21,7 +21,7 @@ final class Paragraph extends AbstractBlock */ public function parse(MarkupLanguageParser $parser, NodeWalker $walker): Nodes\Node { - $context = new ParagraphNode(SpanNode::create($parser, '')); + $context = new ParagraphNode(new SpanNode('', [])); while ($event = $walker->next()) { $node = $event->getNode(); diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php index 53ac9bc23f..ee0cfa701d 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php @@ -14,9 +14,9 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; use phpDocumentor\Guides\Nodes\Node; -use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; abstract class AbstractAdmonitionDirective extends SubDirective { @@ -25,11 +25,13 @@ abstract class AbstractAdmonitionDirective extends SubDirective /** @var string */ private $text; + private SpanParser $spanParser; - public function __construct(string $name, string $text) + public function __construct(string $name, string $text, SpanParser $spanParser) { $this->name = $name; $this->text = $text; + $this->spanParser = $spanParser; } final public function processSub( @@ -42,7 +44,7 @@ final public function processSub( return (new AdmonitionNode( $this->name, $this->text, - $document ?? SpanNode::create($parser, $data) + $document ?? $this->spanParser->parse($data, $parser->getEnvironment()) ))->withOptions($options); } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php index b206c17410..4ff40287c1 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class AdmonitionDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('admonition', 'Admonition'); + parent::__construct('admonition', 'Admonition', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/BestPracticeDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/BestPracticeDirective.php index 69c0594946..409d1b8b70 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/BestPracticeDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/BestPracticeDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class BestPracticeDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('best-practice', 'Best Practice'); + parent::__construct('best-practice', 'Best Practice', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/CautionDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/CautionDirective.php index 4a3cc08f55..71a1505127 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/CautionDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/CautionDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class CautionDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('caution', 'Caution'); + parent::__construct('caution', 'Caution', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/HintDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/HintDirective.php index 783a3b4164..6ea273d35f 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/HintDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/HintDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class HintDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('hint', 'Hint'); + parent::__construct('hint', 'Hint', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/ImportantDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/ImportantDirective.php index 1f08e8be19..dac31ef0a2 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/ImportantDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/ImportantDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class ImportantDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('important', 'Important'); + parent::__construct('important', 'Important', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/NoteDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/NoteDirective.php index 64471b0466..5e9613b681 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/NoteDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/NoteDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class NoteDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('note', 'Note'); + parent::__construct('note', 'Note', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/Replace.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/Replace.php index cd37c17aab..32be512a58 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/Replace.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/Replace.php @@ -5,8 +5,8 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; use phpDocumentor\Guides\Nodes\Node; -use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; /** * The Replace directive will set the variables for the spans @@ -15,6 +15,13 @@ */ class Replace extends Directive { + private SpanParser $spanParser; + + public function __construct(SpanParser $spanParser) + { + $this->spanParser = $spanParser; + } + public function getName(): string { return 'replace'; @@ -29,6 +36,6 @@ public function processNode( string $data, array $options ): Node { - return SpanNode::create($parser, $data); + return $this->spanParser->parse($data, $parser->getEnvironment()); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/SeeAlsoDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/SeeAlsoDirective.php index 716eae3d33..22ba872642 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/SeeAlsoDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/SeeAlsoDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class SeeAlsoDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('seealso', 'See also'); + parent::__construct('seealso', 'See also', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/TipDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/TipDirective.php index d99bca18e6..2f8e13a8c8 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/TipDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/TipDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class TipDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('tip', 'Tip'); + parent::__construct('tip', 'Tip', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Directives/WarningDirective.php b/incubator/guides-restructured-text/src/RestructuredText/Directives/WarningDirective.php index 5de00ea774..8ccf2d2689 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Directives/WarningDirective.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Directives/WarningDirective.php @@ -13,10 +13,12 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; + class WarningDirective extends AbstractAdmonitionDirective { - public function __construct() + public function __construct(SpanParser $spanParser) { - parent::__construct('warning', 'Warning'); + parent::__construct('warning', 'Warning', $spanParser); } } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Parser/LineDataParser.php b/incubator/guides-restructured-text/src/RestructuredText/Parser/LineDataParser.php index 0b21ac04f3..1a6b37d3e2 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Parser/LineDataParser.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Parser/LineDataParser.php @@ -8,8 +8,8 @@ use phpDocumentor\Guides\Nodes\DefinitionLists\DefinitionListTerm; use phpDocumentor\Guides\Nodes\Links\Link; use phpDocumentor\Guides\Nodes\Lists\ListItem; -use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use function array_map; use function count; @@ -23,10 +23,12 @@ class LineDataParser { /** @var MarkupLanguageParser */ private $parser; + private SpanParser $spanParser; - public function __construct(MarkupLanguageParser $parser) + public function __construct(MarkupLanguageParser $parser, SpanParser $spanParser) { $this->parser = $parser; + $this->spanParser = $spanParser; } public function parseLink(string $line): ?Link @@ -167,13 +169,13 @@ public function parseDefinitionList(array $lines): DefinitionList $classifiers = array_map( function (string $classifier) { - return SpanNode::create($this->parser, $classifier); + return $this->spanParser->parse($classifier, $this->parser->getEnvironment()); }, array_map('trim', $parts) ); $definitionListTerm = [ - 'term' => SpanNode::create($this->parser, $term), + 'term' => $this->spanParser->parse($term, $this->parser->getEnvironment()), 'classifiers' => $classifiers, 'definitions' => [], ]; @@ -181,7 +183,10 @@ function (string $classifier) { // last line } elseif ($definitionListTerm !== null && trim($line) === '' && count($lines) - 1 === $key) { if ($currentDefinition !== null) { - $definitionListTerm['definitions'][] = SpanNode::create($this->parser, $currentDefinition); + $definitionListTerm['definitions'][] = $this->spanParser->parse( + $currentDefinition, + $this->parser->getEnvironment() + ); $currentDefinition = null; } @@ -194,7 +199,10 @@ function (string $classifier) { // empty line, start of a new definition for the current term } elseif ($currentDefinition !== null && $definitionListTerm !== null && trim($line) === '') { - $definitionListTerm['definitions'][] = SpanNode::create($this->parser, $currentDefinition); + $definitionListTerm['definitions'][] = $this->spanParser->parse( + $currentDefinition, + $this->parser->getEnvironment() + ); $currentDefinition = null; } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php index 11f126002b..3cde712327 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/DocumentRule.php @@ -14,6 +14,7 @@ use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser; use phpDocumentor\Guides\RestructuredText\Parser\LineDataParser; use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use function array_search; @@ -35,14 +36,15 @@ public function __construct( ) { $this->documentParser = $documentParser; - $lineDataParser = new LineDataParser($parser); + $spanParser = new SpanParser(); + $lineDataParser = new LineDataParser($parser, $spanParser); $literalBlockRule = new LiteralBlockRule(); // TODO: Somehow move this into the top of the instantiation chain so that you can configure which rules // to use when consuming this library $this->productions = [ - new TitleRule($parser, $documentParser), + new TitleRule($parser, $documentParser, $spanParser), new TransitionRule(), // Transition rule must follow Title rule new LinkRule($lineDataParser, $parser), $literalBlockRule, @@ -54,7 +56,7 @@ public function __construct( new TableRule($parser), // For now: ParagraphRule must be last as it is the rule that applies if none other applies. - new ParagraphRule($parser, $documentParser), + new ParagraphRule($parser, $documentParser, $spanParser), ]; } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/ParagraphRule.php b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/ParagraphRule.php index c8dcd1e75b..d01a10b3b9 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/ParagraphRule.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/ParagraphRule.php @@ -15,11 +15,11 @@ use phpDocumentor\Guides\Nodes\Node; use phpDocumentor\Guides\Nodes\ParagraphNode; -use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; use phpDocumentor\Guides\RestructuredText\Parser\Buffer; use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser; use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use function array_pop; use function implode; @@ -36,11 +36,13 @@ final class ParagraphRule implements Rule /** @var DocumentParser */ private $documentParser; + private SpanParser $spanParser; - public function __construct(MarkupLanguageParser $parser, DocumentParser $documentParser) + public function __construct(MarkupLanguageParser $parser, DocumentParser $documentParser, SpanParser $spanParser) { $this->parser = $parser; $this->documentParser = $documentParser; + $this->spanParser = $spanParser; } public function applies(DocumentParser $documentParser): bool @@ -91,7 +93,7 @@ public function apply(LinesIterator $documentIterator, ?Node $on = null): ?Node return null; } - return new ParagraphNode(SpanNode::create($this->parser, $lines)); + return new ParagraphNode($this->spanParser->parse($lines, $this->parser->getEnvironment())); } private function isWhiteline(string $line): bool diff --git a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TableRule.php b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TableRule.php index e3f297afe2..54ea6c5288 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TableRule.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TableRule.php @@ -21,6 +21,7 @@ use phpDocumentor\Guides\RestructuredText\Parser\LineDataParser; use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator; use phpDocumentor\Guides\RestructuredText\Parser\TableParser; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use function trim; @@ -41,7 +42,7 @@ final class TableRule implements Rule public function __construct(MarkupLanguageParser $parser) { $this->parser = $parser; - $this->lineChecker = new LineChecker(new LineDataParser($parser)); + $this->lineChecker = new LineChecker(new LineDataParser($parser, new SpanParser())); $this->tableParser = new TableParser(); } diff --git a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TitleRule.php b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TitleRule.php index ca64eeb674..a52c1ef18e 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TitleRule.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Parser/Productions/TitleRule.php @@ -18,11 +18,11 @@ use phpDocumentor\Guides\Nodes\Node; use phpDocumentor\Guides\Nodes\SectionBeginNode; use phpDocumentor\Guides\Nodes\SectionEndNode; -use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\Nodes\TitleNode; use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser; use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser; use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use function array_search; use function in_array; @@ -77,11 +77,13 @@ final class TitleRule implements Rule /** @var DocumentParser */ private $documentParser; + private SpanParser $spanParser; - public function __construct(MarkupLanguageParser $parser, DocumentParser $documentParser) + public function __construct(MarkupLanguageParser $parser, DocumentParser $documentParser, SpanParser $spanParser) { $this->parser = $parser; $this->documentParser = $documentParser; + $this->spanParser = $spanParser; } public function applies(DocumentParser $documentParser): bool @@ -126,7 +128,7 @@ public function apply(LinesIterator $documentIterator, ?Node $on = null): ?Node $level = $environment->getLevel($letter); $level = $environment->getInitialHeaderLevel() + $level - 1; - $node = new TitleNode(SpanNode::create($this->parser, $title), $level); + $node = new TitleNode($this->spanParser->parse($title, $environment), $level); $this->transitionBetweenSections($node, $on); diff --git a/incubator/guides-restructured-text/src/RestructuredText/Span/SpanParser.php b/incubator/guides-restructured-text/src/RestructuredText/Span/SpanParser.php index 55e35ebd95..05d244d8f6 100644 --- a/incubator/guides-restructured-text/src/RestructuredText/Span/SpanParser.php +++ b/incubator/guides-restructured-text/src/RestructuredText/Span/SpanParser.php @@ -4,11 +4,14 @@ namespace phpDocumentor\Guides\RestructuredText\Span; +use phpDocumentor\Guides\Nodes\SpanNode; use phpDocumentor\Guides\ParserContext; use phpDocumentor\Guides\Span\CrossReferenceNode; use phpDocumentor\Guides\Span\LiteralToken; use phpDocumentor\Guides\Span\SpanToken; +use function implode; +use function is_array; use function mt_rand; use function preg_replace; use function preg_replace_callback; @@ -38,7 +41,17 @@ public function __construct() $this->prefix = mt_rand() . '|' . time(); } - public function process(ParserContext $parserContext, string $span): string + /** @param string|string[] $span */ + public function parse($span, ParserContext $environment): SpanNode + { + if (is_array($span)) { + $span = implode("\n", $span); + } + + return new SpanNode($this->process($environment, $span), $this->getTokens()); + } + + private function process(ParserContext $parserContext, string $span): string { $span = $this->replaceLiterals($span); @@ -55,7 +68,7 @@ public function process(ParserContext $parserContext, string $span): string /** * @return SpanToken[] */ - public function getTokens(): array + private function getTokens(): array { return $this->tokens; } diff --git a/incubator/guides-restructured-text/tests/unit/Parser/Productions/ParagraphRuleTest.php b/incubator/guides-restructured-text/tests/unit/Parser/Productions/ParagraphRuleTest.php index 50eb5656eb..44781d067f 100644 --- a/incubator/guides-restructured-text/tests/unit/Parser/Productions/ParagraphRuleTest.php +++ b/incubator/guides-restructured-text/tests/unit/Parser/Productions/ParagraphRuleTest.php @@ -12,10 +12,14 @@ use phpDocumentor\Guides\RestructuredText\Parser\DocumentParser; use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator; use phpDocumentor\Guides\RestructuredText\Parser\Productions\ParagraphRule; +use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use phpDocumentor\Guides\UrlGenerator; use PHPUnit\Framework\TestCase; +use Prophecy\Argument; use Prophecy\PhpUnit\ProphecyTrait; +use function implode; + final class ParagraphRuleTest extends TestCase { use ProphecyTrait; @@ -50,10 +54,18 @@ public function testParagraphNodeFromLinesIterator( ); $documentParser = $this->prophesize(DocumentParser::class); $documentParser->getDocumentIterator()->willReturn($iterator); + $spanParser = $this->prophesize(SpanParser::class); + $spanParser->parse( + Argument::any(), + Argument::any() + )->will(function ($args) { + return new SpanNode(implode("\n", $args[0])); + }); $rule = new ParagraphRule( $parser->reveal(), - $documentParser->reveal() + $documentParser->reveal(), + $spanParser->reveal() ); self::assertTrue($rule->applies($documentParser->reveal())); @@ -86,7 +98,7 @@ public function paragraphProvider(): array <<spanProcessor->process( - $this->parserContext->reveal(), - 'This text is an example of ``inline literals``.' + $result = $this->spanProcessor->parse( + 'This text is an example of ``inline literals``.', + $this->parserContext->reveal() ); - $token = current($this->spanProcessor->getTokens()); + $token = current($result->getTokens()); - self::assertStringNotContainsString('``inline literals``', $result); + self::assertStringNotContainsString('``inline literals``', $result->getValue()); self::assertInstanceOf(LiteralToken::class, $token); self::assertEquals(SpanToken::TYPE_LITERAL, $token->getType()); self::assertEquals( @@ -51,10 +54,10 @@ public function testInlineLiteralsAreReplacedWithToken(): void /** @dataProvider invalidNotationsProvider */ public function testIncompleteStructuresAreIgnored(string $input): void { - $result = $this->spanProcessor->process($this->parserContext->reveal(), $input); + $result = $this->spanProcessor->parse($input, $this->parserContext->reveal()); - self::assertSame($input, $result); - self::assertCount(0, $this->spanProcessor->getTokens()); + self::assertSame($input, $result->getValue()); + self::assertCount(0, $result->getTokens()); } public function invalidNotationsProvider(): array @@ -76,11 +79,11 @@ public function invalidNotationsProvider(): array */ public function testIncompleteStructureLikeUrlIsReplaced(): void { - $result = $this->spanProcessor->process( - $this->parserContext->reveal(), - 'This text is an example of role:`mis-used`.' + $result = $this->spanProcessor->parse( + 'This text is an example of role:`mis-used`.', + $this->parserContext->reveal() ); - self::assertMatchesRegularExpression('#This text is an example of [a-z0-9]{40}\\.#', $result); + self::assertMatchesRegularExpression('#This text is an example of [a-z0-9]{40}\\.#', $result->getValue()); } /** @dataProvider namedHyperlinkReferenceProvider */ @@ -90,8 +93,8 @@ public function testNamedHyperlinkReferencesAreReplaced( string $text, string $url = '' ): void { - $result = $this->spanProcessor->process($this->parserContext->reveal(), $input); - $token = current($this->spanProcessor->getTokens()); + $result = $this->spanProcessor->parse($input, $this->parserContext->reveal()); + $token = current($result->getTokens()); self::assertInstanceOf(SpanToken::class, $token); self::assertEquals(SpanToken::TYPE_LINK, $token->getType()); @@ -103,7 +106,7 @@ public function testNamedHyperlinkReferencesAreReplaced( ], $token->getTokenData() ); - self::assertRegExp($referenceId, $result); + self::assertRegExp($referenceId, $result->getValue()); if ($url === '') { return; @@ -185,10 +188,10 @@ public function AnonymousHyperlinksProvider(): array public function testInlineInternalTargetsAreReplaced(): void { - $result = $this->spanProcessor->process($this->parserContext->reveal(), 'Some _`internal ref` in text.'); - $token = current($this->spanProcessor->getTokens()); + $result = $this->spanProcessor->parse('Some _`internal ref` in text.', $this->parserContext->reveal()); + $token = current($result->getTokens()); - self::assertStringNotContainsString('_`internal ref`', $result); + self::assertStringNotContainsString('_`internal ref`', $result->getValue()); self::assertInstanceOf(SpanToken::class, $token); self::assertEquals(SpanToken::TYPE_LINK, $token->getType()); self::assertEquals( @@ -204,10 +207,10 @@ public function testInlineInternalTargetsAreReplaced(): void public function testFootNoteReferencesAreReplaced(): void { $this->markTestSkipped('Footnotes are not supported yet'); - $result = $this->spanProcessor->process($this->parserContext->reveal(), 'Please RTFM [1]_.'); - $token = current($this->spanProcessor->getTokens()); + $result = $this->spanProcessor->parse('Please RTFM [1]_.', $this->parserContext->reveal()); + $token = current($result->getTokens()); - self::assertStringNotContainsString('[1]_', $result); + self::assertStringNotContainsString('[1]_', $result->getValue()); self::assertInstanceOf(SpanToken::class, $token); self::assertEquals(SpanToken::TYPE_REFERENCE, $token->getType()); self::assertEquals( @@ -224,11 +227,11 @@ public function testEmailAddressesAreReplacedWithToken(): void { $email = $this->faker()->email; - $result = $this->spanProcessor->process($this->parserContext->reveal(), $email); - $tokens = $this->spanProcessor->getTokens(); + $result = $this->spanProcessor->parse($email, $this->parserContext->reveal()); + $tokens = $result->getTokens(); $token = current($tokens); - self::assertStringNotContainsString($email, $result); + self::assertStringNotContainsString($email, $result->getValue()); self::assertCount(1, $tokens); self::assertSame(SpanToken::TYPE_LINK, $token->getType()); self::assertSame( @@ -245,11 +248,11 @@ public function testInlineUrlsAreReplacedWithToken(): void { $url = $this->faker()->url; - $result = $this->spanProcessor->process($this->parserContext->reveal(), $url); - $tokens = $this->spanProcessor->getTokens(); + $result = $this->spanProcessor->parse($url, $this->parserContext->reveal()); + $tokens = $result->getTokens(); $token = current($tokens); - self::assertStringNotContainsString($url, $result); + self::assertStringNotContainsString($url, $result->getValue()); self::assertCount(1, $tokens); self::assertSame(SpanToken::TYPE_LINK, $token->getType()); self::assertSame( @@ -274,10 +277,10 @@ public function testInterpretedTextIsParsedIntoCrossReferenceNode( ?string $anchor = null, ?string $text = null ): void { - $result = $this->spanProcessor->process($this->parserContext->reveal(), $span); - $token = current($this->spanProcessor->getTokens()); + $result = $this->spanProcessor->parse($span, $this->parserContext->reveal()); + $token = current($result->getTokens()); - self::assertStringNotContainsString($replaced, $result); + self::assertStringNotContainsString($replaced, $result->getValue()); self::assertInstanceOf(CrossReferenceNode::class, $token); self::assertEquals($url, $token->getUrl()); self::assertEquals($role, $token->getRole()); @@ -343,8 +346,8 @@ public function crossReferenceProvider(): array public function testNoReplacementsAreDoneWhenNotNeeded(): void { - $result = $this->spanProcessor->process($this->parserContext->reveal(), 'Raw token'); - self::assertSame('Raw token', $result); - self::assertEmpty($this->spanProcessor->getTokens()); + $result = $this->spanProcessor->parse('Raw token', $this->parserContext->reveal()); + self::assertSame('Raw token', $result->getValue()); + self::assertEmpty($result->getTokens()); } } diff --git a/incubator/guides/src/Nodes/SpanNode.php b/incubator/guides/src/Nodes/SpanNode.php index 5c60ffaf10..4c8b2dec70 100644 --- a/incubator/guides/src/Nodes/SpanNode.php +++ b/incubator/guides/src/Nodes/SpanNode.php @@ -13,35 +13,15 @@ namespace phpDocumentor\Guides\Nodes; -use phpDocumentor\Guides\MarkupLanguageParser; -use phpDocumentor\Guides\RestructuredText\Span\SpanParser; use phpDocumentor\Guides\Span\SpanToken; -use function implode; -use function is_array; - class SpanNode extends Node { /** @var SpanToken[] */ protected $tokens; - /** - * @param string|string[] $span - */ - public static function create(MarkupLanguageParser $parser, $span): self - { - if (is_array($span)) { - $span = implode("\n", $span); - } - - $environment = $parser->getEnvironment(); - $spanProcessor = new SpanParser(); - - return new self($spanProcessor->process($environment, $span), $spanProcessor->getTokens()); - } - /** @param SpanToken[] $tokens */ - public function __construct(string $content, array $tokens) + public function __construct(string $content, array $tokens = []) { parent::__construct($content); $this->tokens = $tokens; diff --git a/incubator/guides/src/Nodes/TableNode.php b/incubator/guides/src/Nodes/TableNode.php index 00603ecc12..ebff8e36a9 100644 --- a/incubator/guides/src/Nodes/TableNode.php +++ b/incubator/guides/src/Nodes/TableNode.php @@ -171,7 +171,8 @@ public function finalize(MarkupLanguageParser $parser, LineChecker $lineChecker) if ($lineChecker->isListLine($lines[0], false)) { $node = $parser->parseFragment($col->getContent())->getNodes()[0]; } else { - $node = SpanNode::create($parser, $col->getContent()); + //TODO: fix this, as we need to parse table contents for links + $node = new SpanNode($col->getContent()); //SpanNode::create($parser, $col->getContent()); } $col->setNode($node); diff --git a/incubator/guides/tests/unit/Nodes/TitleNodeTest.php b/incubator/guides/tests/unit/Nodes/TitleNodeTest.php index 35e341f4d0..f83ee565c4 100644 --- a/incubator/guides/tests/unit/Nodes/TitleNodeTest.php +++ b/incubator/guides/tests/unit/Nodes/TitleNodeTest.php @@ -29,7 +29,7 @@ public function test_it_can_be_created_with_a_title_slug_and_depth(): void $parser = m::mock(MarkupLanguageParser::class); $parser->shouldReceive('getEnvironment')->andReturn($environment); - $titleNode = SpanNode::create($parser, 'Raw String'); + $titleNode = new SpanNode('Raw String'); $node = new TitleNode($titleNode, 1); $node->setTarget('target'); diff --git a/phpcs.xml.dist b/phpcs.xml.dist index 79bc042f59..b646a9477b 100644 --- a/phpcs.xml.dist +++ b/phpcs.xml.dist @@ -66,6 +66,7 @@ + */incubator/guides-restructured-text/tests/unit/** */tests/unit/phpDocumentor/Console/ApplicationTest\.php$ */tests/unit/phpDocumentor/Compiler/Pass/TableOfContentsBuilderTest\.php$