Skip to content

Commit b3691cb

Browse files
committed
[TASK] Have a clone of the Textrole factory in the DocumentParserContext
1 parent bf440be commit b3691cb

File tree

4 files changed

+19
-0
lines changed

4 files changed

+19
-0
lines changed

packages/guides-restructured-text/src/RestructuredText/MarkupLanguageParser.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use phpDocumentor\Guides\ParserContext;
1111
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
1212
use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule;
13+
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRoleFactory;
1314
use RuntimeException;
1415
use Webmozart\Assert\Assert;
1516

@@ -26,6 +27,7 @@ class MarkupLanguageParser implements ParserInterface
2627
/** @param Rule<DocumentNode> $startingRule */
2728
public function __construct(
2829
private readonly Rule $startingRule,
30+
private readonly TextRoleFactory $textRoleFactory,
2931
) {
3032
}
3133

@@ -39,6 +41,7 @@ public function getSubParser(): MarkupLanguageParser
3941
{
4042
return new MarkupLanguageParser(
4143
$this->startingRule,
44+
$this->textRoleFactory,
4245
);
4346
}
4447

@@ -74,6 +77,7 @@ public function parse(ParserContext $parserContext, string $contents): DocumentN
7477
$this->documentParser = new DocumentParserContext(
7578
$contents,
7679
$parserContext,
80+
$this->textRoleFactory,
7781
$this,
7882
);
7983

packages/guides-restructured-text/src/RestructuredText/Parser/DocumentParserContext.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use phpDocumentor\Guides\Nodes\ProjectNode;
1818
use phpDocumentor\Guides\ParserContext;
1919
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
20+
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRoleFactory;
2021
use RuntimeException;
2122

2223
/**
@@ -31,16 +32,21 @@ class DocumentParserContext
3132

3233
private LinesIterator $documentIterator;
3334
private int $currentTitleLevel;
35+
/* Each Document has its own text role factory as text roles can be changed on a per document base
36+
by directives */
37+
private TextRoleFactory $textRoleFactoryForDocument;
3438

3539
/** @var string[] */
3640
private array $titleLetters = [];
3741

3842
public function __construct(
3943
string $content,
4044
private readonly ParserContext $context,
45+
TextRoleFactory $textRoleFactory,
4146
private readonly MarkupLanguageParser $markupLanguageParser,
4247
) {
4348
$this->documentIterator = new LinesIterator();
49+
$this->textRoleFactoryForDocument = clone $textRoleFactory;
4450
$this->documentIterator->load($content);
4551
$this->currentTitleLevel = $context->getInitialHeaderLevel() - 1;
4652
}
@@ -115,4 +121,9 @@ public function withContentsPreserveSpace(string $contents): self
115121

116122
return $that;
117123
}
124+
125+
public function getTextRoleFactoryForDocument(): TextRoleFactory
126+
{
127+
return $this->textRoleFactoryForDocument;
128+
}
118129
}

packages/guides-restructured-text/tests/unit/Parser/Productions/RuleTestCase.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
1414
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
1515
use phpDocumentor\Guides\RestructuredText\Parser\LinesIterator;
16+
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRoleFactory;
1617
use phpDocumentor\Guides\UrlGenerator;
1718
use PHPUnit\Framework\TestCase;
1819

@@ -43,6 +44,7 @@ protected function createContext(string $input): DocumentParserContext
4344
$this->createStub(FilesystemInterface::class),
4445
new UrlGenerator(),
4546
),
47+
$this->createStub(TextRoleFactory::class),
4648
$this->createStub(MarkupLanguageParser::class),
4749
);
4850
}

packages/guides-restructured-text/tests/unit/Parser/Productions/SectionRuleTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
use phpDocumentor\Guides\RestructuredText\MarkupLanguageParser;
2424
use phpDocumentor\Guides\RestructuredText\Parser\DocumentParserContext;
2525
use phpDocumentor\Guides\RestructuredText\Parser\InlineParser;
26+
use phpDocumentor\Guides\RestructuredText\TextRoles\TextRoleFactory;
2627
use phpDocumentor\Guides\UrlGeneratorInterface;
2728

2829
final class SectionRuleTest extends RuleTestCase
@@ -200,6 +201,7 @@ private function getDocumentParserContext(string $content): DocumentParserContex
200201
return new DocumentParserContext(
201202
$content,
202203
$parserContext,
204+
$this->createStub(TextRoleFactory::class),
203205
$this->createStub(MarkupLanguageParser::class),
204206
);
205207
}

0 commit comments

Comments
 (0)