Skip to content

Commit e1c76b1

Browse files
committed
Improve new directive handling
1 parent b8863d3 commit e1c76b1

4 files changed

Lines changed: 28 additions & 11 deletions

File tree

packages/guides-restructured-text/src/RestructuredText/Compiler/Passes/DirectiveProcessPass.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,6 @@ public function supports(Node $node): bool
8181

8282
public function getPriority(): int
8383
{
84-
return PHP_INT_MAX;
84+
return 100;
8585
}
8686
}

packages/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,16 @@ final protected function processSub(
5151

5252
public function createNode(DirectiveNode $directiveNode): Node|null
5353
{
54+
$children = $directiveNode->getChildren();
55+
if ($directiveNode->getDirective()->getDataNode() !== null) {
56+
array_unshift($children, new ParagraphNode([$directiveNode->getDirective()->getDataNode()]));
57+
}
58+
5459
return new AdmonitionNode(
5560
$directiveNode->getDirective()->getName(),
56-
$directiveNode->getDirective()->getDataNode(),
61+
null,
5762
$this->text,
58-
$directiveNode->getChildren(),
63+
$children,
5964
);
6065
}
6166
}

packages/guides-restructured-text/src/RestructuredText/Parser/Productions/DirectiveRule.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,22 @@ public function apply(BlockContext $blockContext, CompoundNode|null $on = null):
9595
$buffer = $this->collectDirectiveContents($documentIterator);
9696

9797
if ($this->startingRule !== null && $directiveHandler->isUpgraded()) {
98-
return $this->startingRule->apply(
98+
$node = $this->startingRule->apply(
9999
new BlockContext($blockContext->getDocumentParserContext(), $buffer->getLinesString(), true, $documentIterator->key()),
100100
new DirectiveNode($directive),
101101
);
102+
103+
if ($node === null) {
104+
return null;
105+
}
106+
107+
if ($directive->getVariable() === '') {
108+
return $node;
109+
}
110+
111+
$blockContext->getDocumentParserContext()->getDocument()->addVariable($directive->getVariable(), $node);
112+
113+
return null;
102114
}
103115

104116
// Processing the Directive, the handler is responsible for adding the right Nodes to the document.

packages/guides/src/Compiler/NodeTransformers/VariableInlineNodeTransformer.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -48,15 +48,15 @@ public function leaveNode(Node $node, CompilerContextInterface $compilerContext)
4848
$nodeReplacement ??= $compilerContext->getProjectNode()->getVariable($node->getValue(), null);
4949

5050
if ($nodeReplacement instanceof Node) {
51-
$node->setChild($nodeReplacement);
52-
} else {
53-
$this->logger->warning(
54-
'No replacement was found for variable |' . $node->getValue() . '|',
55-
$compilerContext->getLoggerInformation(),
56-
);
57-
$node->setChild(new PlainTextInlineNode('|' . $node->getValue() . '|'));
51+
return $nodeReplacement;
5852
}
5953

54+
$this->logger->warning(
55+
'No replacement was found for variable |' . $node->getValue() . '|',
56+
$compilerContext->getLoggerInformation(),
57+
);
58+
$node->setChild(new PlainTextInlineNode('|' . $node->getValue() . '|'));
59+
6060
return $node;
6161
}
6262

0 commit comments

Comments
 (0)