/** * @param Document $document * * @return void */ public function processDocument(Document $document) { /** @var TableOfContents[] $tocs */ $tocs = []; $headings = []; $walker = $document->walker(); while ($event = $walker->next()) { $node = $event->getNode(); if ($node instanceof TableOfContents && !$event->isEntering()) { $tocs[] = $node; continue; } if (!$node instanceof Heading || !$event->isEntering()) { continue; } $id = $this->addId($node); $headings[] = new Entry($node, $id); } if (count($headings) && (count($tocs) || $this->hasAutoTOC())) { $generated = $this->generate($headings); if (count($tocs)) { foreach ($tocs as $toc) { $toc->appendChild($this->render($generated->getChildren())); } } else { $document->prependChild($this->render($generated->getChildren())); } } }
public function processDocument(Document $document) { $walker = $document->walker(); while ($event = $walker->next()) { $node = $event->getNode(); if ($event->isEntering() || !$node instanceof Attributes) { continue; } list($target, $direction) = $this->findTargetAndDirection($node); if ($target) { if (($parent = $target->parent()) instanceof ListItem && $parent->parent() instanceof ListBlock && $parent->parent()->isTight()) { $target = $parent; } if ($direction === self::DIRECTION_SUFFIX) { $attributes = AttributesUtils::merge($target, $node->getAttributes()); } else { $attributes = AttributesUtils::merge($node->getAttributes(), $target); } $target->data['attributes'] = $attributes; } if ($node instanceof AbstractBlock && $node->endsWithBlankLine() && $node->next() && $node->previous()) { $node->previous()->setLastLineBlank(true); } $node->detach(); } }
/** * Process the CommonMark AST * * @param Document $document CommonMark AST * @return void */ public function processDocument(Document $document) { $walker = $document->walker(); while ($event = $walker->next()) { $node = $event->getNode(); // Process link starts as refers-to relations if ($node instanceof Link && $event->isEntering()) { $this->addRefersToRelation($this->stripFragment($node->getUrl()), empty($node->data['title']) ? null : $node->data['title']); } // Process image starts as embeds relations if ($node instanceof Image && $event->isEntering()) { $this->addEmbedsRelation($this->stripFragment($node->getUrl()), empty($node->data['title']) ? null : $node->data['title']); } } }