-
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TASK] Delegate inline node handling to InlineParsers
- Loading branch information
Showing
13 changed files
with
420 additions
and
142 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
packages/guides-markdown/src/Markdown/Parsers/HeaderParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Markdown\Parsers; | ||
|
||
use League\CommonMark\Extension\CommonMark\Node\Block\Heading; | ||
use League\CommonMark\Node\Node as CommonMarkNode; | ||
use League\CommonMark\Node\NodeWalker; | ||
use League\CommonMark\Node\NodeWalkerEvent; | ||
use phpDocumentor\Guides\Markdown\Parsers\InlineParsers\AbstractInlineParser; | ||
use phpDocumentor\Guides\MarkupLanguageParser; | ||
use phpDocumentor\Guides\Nodes\Inline\InlineNode; | ||
use phpDocumentor\Guides\Nodes\InlineCompoundNode; | ||
use phpDocumentor\Guides\Nodes\Node; | ||
use phpDocumentor\Guides\Nodes\TitleNode; | ||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
use Symfony\Component\String\Slugger\AsciiSlugger; | ||
|
||
use function sprintf; | ||
|
||
/** @extends AbstractBlockParser<TitleNode> */ | ||
final class HeaderParser extends AbstractBlockParser | ||
{ | ||
/** @param iterable<AbstractInlineParser<InlineNode>> $inlineParsers */ | ||
public function __construct( | ||
private readonly iterable $inlineParsers, | ||
private readonly LoggerInterface $logger, | ||
private readonly AsciiSlugger $idGenerator, | ||
) { | ||
} | ||
|
||
public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMarkNode $current): Node | ||
{ | ||
$content = []; | ||
|
||
while ($event = $walker->next()) { | ||
$commonMarkNode = $event->getNode(); | ||
|
||
if ($event->isEntering()) { | ||
foreach ($this->inlineParsers as $subParser) { | ||
if ($subParser->supports($event)) { | ||
$content[] = $subParser->parse($parser, $walker, $commonMarkNode); | ||
break; | ||
} | ||
} | ||
|
||
continue; | ||
} | ||
|
||
// leaving the heading node | ||
if ($commonMarkNode instanceof Heading) { | ||
return new TitleNode( | ||
new InlineCompoundNode($content), | ||
$commonMarkNode->getLevel(), | ||
$this->idGenerator->slug($content[0]->toString() ?? '')->lower()->toString(), | ||
); | ||
} | ||
|
||
$this->logger->warning(sprintf('Header CONTEXT: I am leaving a %s node', $commonMarkNode::class)); | ||
} | ||
|
||
throw new RuntimeException('Unexpected end of NodeWalker'); | ||
} | ||
|
||
public function supports(NodeWalkerEvent $event): bool | ||
{ | ||
return $event->isEntering() && $event->getNode() instanceof Heading; | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
packages/guides-markdown/src/Markdown/Parsers/InlineParsers/AbstractInlineParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Markdown\Parsers\InlineParsers; | ||
|
||
use League\CommonMark\Node\Node as CommonMarkNode; | ||
use League\CommonMark\Node\NodeWalker; | ||
use phpDocumentor\Guides\Markdown\ParserInterface; | ||
use phpDocumentor\Guides\MarkupLanguageParser; | ||
use phpDocumentor\Guides\Nodes\Inline\InlineNode; | ||
|
||
/** | ||
* @template TValue as InlineNode | ||
* @implements ParserInterface<TValue> | ||
*/ | ||
abstract class AbstractInlineParser implements ParserInterface | ||
{ | ||
abstract public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMarkNode $current): InlineNode; | ||
} |
71 changes: 71 additions & 0 deletions
71
packages/guides-markdown/src/Markdown/Parsers/InlineParsers/EmphasisParser.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace phpDocumentor\Guides\Markdown\Parsers\InlineParsers; | ||
|
||
use League\CommonMark\Extension\CommonMark\Node\Inline\Emphasis; | ||
use League\CommonMark\Node\Node as CommonMarkNode; | ||
use League\CommonMark\Node\NodeWalker; | ||
use League\CommonMark\Node\NodeWalkerEvent; | ||
use phpDocumentor\Guides\MarkupLanguageParser; | ||
use phpDocumentor\Guides\Nodes\Inline\EmphasisInlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\InlineNode; | ||
use phpDocumentor\Guides\Nodes\Inline\PlainTextInlineNode; | ||
use Psr\Log\LoggerInterface; | ||
use RuntimeException; | ||
|
||
use function count; | ||
use function sprintf; | ||
use function var_export; | ||
|
||
/** @extends AbstractInlineParser<EmphasisInlineNode> */ | ||
final class EmphasisParser extends AbstractInlineParser | ||
{ | ||
/** @param iterable<AbstractInlineParser<InlineNode>> $inlineParsers */ | ||
public function __construct( | ||
private readonly iterable $inlineParsers, | ||
private readonly LoggerInterface $logger, | ||
) { | ||
} | ||
|
||
public function parse(MarkupLanguageParser $parser, NodeWalker $walker, CommonMarkNode $current): InlineNode | ||
{ | ||
$content = []; | ||
|
||
while ($event = $walker->next()) { | ||
$commonMarkNode = $event->getNode(); | ||
|
||
if ($event->isEntering()) { | ||
foreach ($this->inlineParsers as $subParser) { | ||
if (!$subParser->supports($event)) { | ||
continue; | ||
} | ||
|
||
$content[] = $subParser->parse($parser, $walker, $commonMarkNode); | ||
} | ||
|
||
continue; | ||
} | ||
|
||
if ($commonMarkNode instanceof Emphasis) { | ||
if (count($content) > 0 && $content[0] instanceof PlainTextInlineNode) { | ||
return new EmphasisInlineNode($content[0]->getValue()); | ||
} | ||
|
||
$this->logger->warning(sprintf('Emphasis CONTEXT: Content of emphasis could not be interpreted: %s', var_export($content, true))); | ||
|
||
return new EmphasisInlineNode(''); | ||
} | ||
|
||
$this->logger->warning(sprintf('Emphasis CONTEXT: I am leaving a %s node', $commonMarkNode::class)); | ||
} | ||
|
||
throw new RuntimeException('Unexpected end of NodeWalker'); | ||
} | ||
|
||
public function supports(NodeWalkerEvent $event): bool | ||
{ | ||
return $event->isEntering() && $event->getNode() instanceof Emphasis; | ||
} | ||
} |
Oops, something went wrong.