|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Hammerstone\Torchlight\Commonmark; |
| 4 | + |
| 5 | +use Hammerstone\Torchlight\Block; |
| 6 | +use Hammerstone\Torchlight\Client; |
| 7 | +use League\CommonMark\Block\Element\AbstractBlock; |
| 8 | +use League\CommonMark\Block\Element\FencedCode; |
| 9 | +use League\CommonMark\Block\Element\IndentedCode; |
| 10 | +use League\CommonMark\Block\Renderer\BlockRendererInterface; |
| 11 | +use League\CommonMark\ConfigurableEnvironmentInterface; |
| 12 | +use League\CommonMark\ElementRendererInterface; |
| 13 | +use League\CommonMark\Event\DocumentParsedEvent; |
| 14 | +use League\CommonMark\Extension\ExtensionInterface; |
| 15 | +use League\CommonMark\Util\Xml; |
| 16 | + |
| 17 | +class TorchlightExtension implements ExtensionInterface, BlockRendererInterface |
| 18 | +{ |
| 19 | + public static $torchlightBlocks = []; |
| 20 | + |
| 21 | + public function register(ConfigurableEnvironmentInterface $environment) |
| 22 | + { |
| 23 | + // We start by walking the document immediately after it's parsed |
| 24 | + // to gather up all the code blocks and send off our requests. |
| 25 | + $environment->addEventListener(DocumentParsedEvent::class, [$this, 'onDocumentParsed']); |
| 26 | + |
| 27 | + |
| 28 | + // After the document is parsed, it's rendered. We register our |
| 29 | + // renderers with a higher priority than the default ones, |
| 30 | + // and we'll fetch the blocks straight from the cache. |
| 31 | + $environment->addBlockRenderer(FencedCode::class, $this, 10); |
| 32 | + $environment->addBlockRenderer(IndentedCode::class, $this, 10); |
| 33 | + |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @param DocumentParsedEvent $event |
| 38 | + */ |
| 39 | + public function onDocumentParsed(DocumentParsedEvent $event) |
| 40 | + { |
| 41 | + $walker = $event->getDocument()->walker(); |
| 42 | + |
| 43 | + while ($event = $walker->next()) { |
| 44 | + $node = $event->getNode(); |
| 45 | + |
| 46 | + // Only look for code nodes, and only process them upon entering. |
| 47 | + if (!$this->isCodeNode($node) || !$event->isEntering()) { |
| 48 | + continue; |
| 49 | + } |
| 50 | + |
| 51 | + $block = $this->makeTorchlightBlock($node); |
| 52 | + |
| 53 | + // Set by hash instead of ID, because we'll be remaking all the |
| 54 | + // blocks in the `render` function so the ID will be different, |
| 55 | + // but the hash will always remain the same. |
| 56 | + static::$torchlightBlocks[$block->hash()] = $block; |
| 57 | + } |
| 58 | + |
| 59 | + // All we need to do is fire the request, which will store |
| 60 | + // the results in the cache. In the render function we |
| 61 | + // use that cached value. |
| 62 | + (new Client)->highlight(static::$torchlightBlocks); |
| 63 | + } |
| 64 | + |
| 65 | + public function render(AbstractBlock $block, ElementRendererInterface $htmlRenderer, $inTightList = false) |
| 66 | + { |
| 67 | + $hash = $this->makeTorchlightBlock($block)->hash(); |
| 68 | + |
| 69 | + if (array_key_exists(static::$torchlightBlocks, $hash)) { |
| 70 | + return static::$torchlightBlocks[$hash]->html; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + protected function makeTorchlightBlock($node) |
| 75 | + { |
| 76 | + return Block::make() |
| 77 | + ->setLanguage($this->getLanguage($node)) |
| 78 | + ->setCode($node->getStringContent()); |
| 79 | + } |
| 80 | + |
| 81 | + protected function isCodeNode($node) |
| 82 | + { |
| 83 | + return $node instanceof FencedCode || $node instanceof IndentedCode; |
| 84 | + } |
| 85 | + |
| 86 | + protected function getLanguage($node) |
| 87 | + { |
| 88 | + if (!$this->isCodeNode($node) || $node instanceof IndentedCode) { |
| 89 | + return null; |
| 90 | + } |
| 91 | + |
| 92 | + $infoWords = $node->getInfoWords(); |
| 93 | + |
| 94 | + if (empty($infoWords) || empty($infoWords[0])) { |
| 95 | + return null; |
| 96 | + } |
| 97 | + |
| 98 | + return Xml::escape($infoWords[0], true); |
| 99 | + } |
| 100 | +} |
0 commit comments