diff --git a/packages/guides-markdown/src/Markdown/Parsers/BlockQuoteParser.php b/packages/guides-markdown/src/Markdown/Parsers/BlockQuoteParser.php index 384446595..aec3e201d 100644 --- a/packages/guides-markdown/src/Markdown/Parsers/BlockQuoteParser.php +++ b/packages/guides-markdown/src/Markdown/Parsers/BlockQuoteParser.php @@ -18,14 +18,21 @@ use League\CommonMark\Node\NodeWalker; use League\CommonMark\Node\NodeWalkerEvent; use phpDocumentor\Guides\MarkupLanguageParser; +use phpDocumentor\Guides\Nodes\AdmonitionNode; +use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode; +use phpDocumentor\Guides\Nodes\InlineCompoundNode; use phpDocumentor\Guides\Nodes\Node; +use phpDocumentor\Guides\Nodes\ParagraphNode; use phpDocumentor\Guides\Nodes\QuoteNode; use Psr\Log\LoggerInterface; use RuntimeException; +use function array_shift; +use function count; use function sprintf; +use function trim; -/** @extends AbstractBlockParser */ +/** @extends AbstractBlockParser */ final class BlockQuoteParser extends AbstractBlockParser { /** @param iterable> $subParsers */ @@ -35,7 +42,7 @@ public function __construct( ) { } - public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMarkNode $current): QuoteNode + public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMarkNode $current): Node { $content = []; @@ -55,6 +62,58 @@ public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMa // leaving the heading node if ($commonMarkNode instanceof BlockQuote) { + if (count($content) > 0 && $content[0] instanceof ParagraphNode && ($content[0]->getValue()[0]) instanceof InlineCompoundNode) { + $paragraphContent = $content[0]->getValue()[0]->getValue(); + if (count($paragraphContent) > 0 && $paragraphContent[0] instanceof PlainTextInlineNode) { + $text = trim($paragraphContent[0]->getValue()); + $newParagraphContent = $paragraphContent; + array_shift($newParagraphContent); + switch ($text) { + case '[!NOTE]': + return new AdmonitionNode( + 'note', + new InlineCompoundNode([new PlainTextInlineNode('Note')]), + 'Note', + $newParagraphContent, + ); + + case '[!TIP]': + return new AdmonitionNode( + 'tip', + new InlineCompoundNode([new PlainTextInlineNode('Tip')]), + 'Tip', + $newParagraphContent, + ); + + case '[!IMPORTANT]': + return new AdmonitionNode( + 'important', + new InlineCompoundNode([new PlainTextInlineNode('Important')]), + 'Important', + $newParagraphContent, + ); + + case '[!WARNING]': + return new AdmonitionNode( + 'warning', + new InlineCompoundNode([new PlainTextInlineNode('Warning')]), + 'Warning', + $newParagraphContent, + ); + + case '[!CAUTION]': + return new AdmonitionNode( + 'caution', + new InlineCompoundNode([new PlainTextInlineNode('Caution')]), + 'Caution', + $newParagraphContent, + ); + } + } + + $content[0] = new ParagraphNode([new InlineCompoundNode($paragraphContent)]); + } + return new QuoteNode($content); } diff --git a/packages/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php b/packages/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php index d4d36d35b..cb5deca86 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php +++ b/packages/guides-restructured-text/src/RestructuredText/Directives/AbstractAdmonitionDirective.php @@ -13,9 +13,9 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\Nodes\AdmonitionNode; use phpDocumentor\Guides\Nodes\CollectionNode; use phpDocumentor\Guides\Nodes\Node; -use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode; use phpDocumentor\Guides\RestructuredText\Parser\BlockContext; use phpDocumentor\Guides\RestructuredText\Parser\Directive; use phpDocumentor\Guides\RestructuredText\Parser\Productions\Rule; diff --git a/packages/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php b/packages/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php index 7f3a44a28..300e5557f 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php +++ b/packages/guides-restructured-text/src/RestructuredText/Directives/AdmonitionDirective.php @@ -13,9 +13,9 @@ namespace phpDocumentor\Guides\RestructuredText\Directives; +use phpDocumentor\Guides\Nodes\AdmonitionNode; use phpDocumentor\Guides\Nodes\CollectionNode; use phpDocumentor\Guides\Nodes\Node; -use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode; use phpDocumentor\Guides\RestructuredText\Parser\BlockContext; use phpDocumentor\Guides\RestructuredText\Parser\Directive; diff --git a/packages/guides-restructured-text/src/RestructuredText/NodeRenderers/Html/AdmonitionNodeRenderer.php b/packages/guides-restructured-text/src/RestructuredText/NodeRenderers/Html/AdmonitionNodeRenderer.php index b1aa25fee..3a671282c 100644 --- a/packages/guides-restructured-text/src/RestructuredText/NodeRenderers/Html/AdmonitionNodeRenderer.php +++ b/packages/guides-restructured-text/src/RestructuredText/NodeRenderers/Html/AdmonitionNodeRenderer.php @@ -13,47 +13,6 @@ namespace phpDocumentor\Guides\RestructuredText\NodeRenderers\Html; -use InvalidArgumentException; -use phpDocumentor\Guides\NodeRenderers\NodeRenderer; -use phpDocumentor\Guides\Nodes\Node; -use phpDocumentor\Guides\RenderContext; -use phpDocumentor\Guides\RestructuredText\Nodes\AdmonitionNode; -use phpDocumentor\Guides\TemplateRenderer; - -use function implode; -use function is_a; - -/** @implements NodeRenderer */ -final class AdmonitionNodeRenderer implements NodeRenderer +final class AdmonitionNodeRenderer extends \phpDocumentor\Guides\NodeRenderers\Html\AdmonitionNodeRenderer { - public function __construct(private readonly TemplateRenderer $renderer) - { - } - - public function supports(string $nodeFqcn): bool - { - return $nodeFqcn === AdmonitionNode::class || is_a($nodeFqcn, AdmonitionNode::class, true); - } - - public function render(Node $node, RenderContext $renderContext): string - { - if ($node instanceof AdmonitionNode === false) { - throw new InvalidArgumentException('Node must be an instance of ' . AdmonitionNode::class); - } - - $classes = $node->getClasses(); - - return $this->renderer->renderTemplate( - $renderContext, - 'body/admonition.html.twig', - [ - 'name' => $node->getName(), - 'text' => $node->getText(), - 'title' => $node->getTitle(), - 'isTitled' => $node->isTitled(), - 'class' => implode(' ', $classes), - 'node' => $node->getValue(), - ], - ); - } } diff --git a/packages/guides-restructured-text/src/RestructuredText/Nodes/AdmonitionNode.php b/packages/guides-restructured-text/src/RestructuredText/Nodes/AdmonitionNode.php index e6072572c..cff1bae95 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Nodes/AdmonitionNode.php +++ b/packages/guides-restructured-text/src/RestructuredText/Nodes/AdmonitionNode.php @@ -13,36 +13,6 @@ namespace phpDocumentor\Guides\RestructuredText\Nodes; -use phpDocumentor\Guides\Nodes\CompoundNode; -use phpDocumentor\Guides\Nodes\InlineCompoundNode; -use phpDocumentor\Guides\Nodes\Node; - -/** @extends CompoundNode */ -final class AdmonitionNode extends CompoundNode +class AdmonitionNode extends \phpDocumentor\Guides\Nodes\AdmonitionNode { - /** @param Node[] $value */ - public function __construct(private readonly string $name, private readonly InlineCompoundNode|null $title, private readonly string $text, array $value, private readonly bool $isTitled = false) - { - parent::__construct($value); - } - - public function getName(): string - { - return $this->name; - } - - public function getTitle(): InlineCompoundNode|null - { - return $this->title; - } - - public function getText(): string - { - return $this->text; - } - - public function isTitled(): bool - { - return $this->isTitled; - } } diff --git a/packages/guides-theme-bootstrap/resources/template/body/admonition.html.twig b/packages/guides-theme-bootstrap/resources/template/body/admonition.html.twig index 2d7ecfcdf..6c1b94fbd 100644 --- a/packages/guides-theme-bootstrap/resources/template/body/admonition.html.twig +++ b/packages/guides-theme-bootstrap/resources/template/body/admonition.html.twig @@ -1,11 +1,13 @@ - +{% if name == 'caution' %} + {% include "body/admonitions/caution.html.twig" %} +{% endif %} {% if name == 'important' %} {% include "body/admonitions/important.html.twig" %} {% endif %} {% if name == 'note' %} {% include "body/admonitions/note.html.twig" %} {% endif %} -{% if name == 'warning' or name == 'caution' %} +{% if name == 'warning' %} {% include "body/admonitions/warning.html.twig" %} {% endif %} {% if name == 'tip' or name == 'hint' %} diff --git a/packages/guides-theme-bootstrap/resources/template/body/admonitions/caution.html.twig b/packages/guides-theme-bootstrap/resources/template/body/admonitions/caution.html.twig new file mode 100644 index 000000000..fe7b30d11 --- /dev/null +++ b/packages/guides-theme-bootstrap/resources/template/body/admonitions/caution.html.twig @@ -0,0 +1,4 @@ + diff --git a/packages/guides-theme-bootstrap/resources/template/body/admonitions/important.html.twig b/packages/guides-theme-bootstrap/resources/template/body/admonitions/important.html.twig index dd0705323..30301fde7 100644 --- a/packages/guides-theme-bootstrap/resources/template/body/admonitions/important.html.twig +++ b/packages/guides-theme-bootstrap/resources/template/body/admonitions/important.html.twig @@ -1,4 +1,4 @@ -