ColorParser.php 1004 B

123456789101112131415161718192021222324252627282930313233343536
  1. <?php
  2. namespace App\Markdown;
  3. use League\CommonMark\Parser\Inline\InlineParserInterface;
  4. use League\CommonMark\Parser\Inline\InlineParserMatch;
  5. use League\CommonMark\Parser\InlineParserContext;
  6. class ColorParser implements InlineParserInterface
  7. {
  8. public function getMatchDefinition(): InlineParserMatch
  9. {
  10. return InlineParserMatch::regex("!([a-z]+):(.+?)!");
  11. }
  12. public function parse(InlineParserContext $inlineContext): bool
  13. {
  14. $matches = $inlineContext->getMatches();
  15. if (count($matches) === 3) {
  16. // La couleur est capturée dans le premier groupe, et le texte dans le second
  17. $color = $matches[1];
  18. $text = $matches[2];
  19. $colorNode = new ColorText($text, $color);
  20. $inlineContext->getContainer()->appendChild($colorNode);
  21. $inlineContext->getCursor()->advanceBy(mb_strlen($matches[0]));
  22. return true;
  23. }
  24. return false;
  25. }
  26. }