Subiendo proyecto completo sin restricciones de git ignore

This commit is contained in:
Jose Sanchez
2023-08-17 11:44:02 -04:00
parent a0d4f5ba3b
commit 20f1c60600
19921 changed files with 2509159 additions and 45 deletions

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents\Node;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
final class TableOfContents extends ListBlock
{
}

View File

@@ -0,0 +1,20 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents\Node;
use League\CommonMark\Node\Block\AbstractBlock;
final class TableOfContentsPlaceholder extends AbstractBlock
{
}

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
final class AsIsNormalizerStrategy implements NormalizerStrategyInterface
{
/** @psalm-readonly-allow-private-mutation */
private ListBlock $parentListBlock;
/** @psalm-readonly-allow-private-mutation */
private int $parentLevel = 1;
/** @psalm-readonly-allow-private-mutation */
private ?ListItem $lastListItem = null;
public function __construct(TableOfContents $toc)
{
$this->parentListBlock = $toc;
}
public function addItem(int $level, ListItem $listItemToAdd): void
{
while ($level > $this->parentLevel) {
// Descend downwards, creating new ListBlocks if needed, until we reach the correct depth
if ($this->lastListItem === null) {
$this->lastListItem = new ListItem($this->parentListBlock->getListData());
$this->parentListBlock->appendChild($this->lastListItem);
}
$newListBlock = new ListBlock($this->parentListBlock->getListData());
$newListBlock->setStartLine($listItemToAdd->getStartLine());
$newListBlock->setEndLine($listItemToAdd->getEndLine());
$this->lastListItem->appendChild($newListBlock);
$this->parentListBlock = $newListBlock;
$this->lastListItem = null;
$this->parentLevel++;
}
while ($level < $this->parentLevel) {
// Search upwards for the previous parent list block
$search = $this->parentListBlock;
while ($search = $search->parent()) {
if ($search instanceof ListBlock) {
$this->parentListBlock = $search;
break;
}
}
$this->parentLevel--;
}
$this->parentListBlock->appendChild($listItemToAdd);
$this->lastListItem = $listItemToAdd;
}
}

View File

@@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
final class FlatNormalizerStrategy implements NormalizerStrategyInterface
{
/** @psalm-readonly */
private TableOfContents $toc;
public function __construct(TableOfContents $toc)
{
$this->toc = $toc;
}
public function addItem(int $level, ListItem $listItemToAdd): void
{
$this->toc->appendChild($listItemToAdd);
}
}

View File

@@ -0,0 +1,21 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
interface NormalizerStrategyInterface
{
public function addItem(int $level, ListItem $listItemToAdd): void;
}

View File

@@ -0,0 +1,67 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents\Normalizer;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
final class RelativeNormalizerStrategy implements NormalizerStrategyInterface
{
/** @psalm-readonly */
private TableOfContents $toc;
/**
* @var array<int, ListItem>
*
* @psalm-readonly-allow-private-mutation
*/
private array $listItemStack = [];
public function __construct(TableOfContents $toc)
{
$this->toc = $toc;
}
public function addItem(int $level, ListItem $listItemToAdd): void
{
$previousLevel = \array_key_last($this->listItemStack);
// Pop the stack if we're too deep
while ($previousLevel !== null && $level < $previousLevel) {
\array_pop($this->listItemStack);
$previousLevel = \array_key_last($this->listItemStack);
}
$lastListItem = \end($this->listItemStack);
// Need to go one level deeper? Add that level
if ($lastListItem !== false && $level > $previousLevel) {
$targetListBlock = new ListBlock($lastListItem->getListData());
$targetListBlock->setStartLine($listItemToAdd->getStartLine());
$targetListBlock->setEndLine($listItemToAdd->getEndLine());
$lastListItem->appendChild($targetListBlock);
// Otherwise we're at the right level
// If there's no stack we're adding this item directly to the TOC element
} elseif ($lastListItem === false) {
$targetListBlock = $this->toc;
// Otherwise add it to the last list item
} else {
$targetListBlock = $lastListItem->parent();
}
$targetListBlock->appendChild($listItemToAdd);
$this->listItemStack[$level] = $listItemToAdd;
}
}

View File

@@ -0,0 +1,106 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\NodeIterator;
use League\Config\ConfigurationAwareInterface;
use League\Config\ConfigurationInterface;
use League\Config\Exception\InvalidConfigurationException;
final class TableOfContentsBuilder implements ConfigurationAwareInterface
{
public const POSITION_TOP = 'top';
public const POSITION_BEFORE_HEADINGS = 'before-headings';
public const POSITION_PLACEHOLDER = 'placeholder';
/** @psalm-readonly-allow-private-mutation */
private ConfigurationInterface $config;
public function onDocumentParsed(DocumentParsedEvent $event): void
{
$document = $event->getDocument();
$generator = new TableOfContentsGenerator(
(string) $this->config->get('table_of_contents/style'),
(string) $this->config->get('table_of_contents/normalize'),
(int) $this->config->get('table_of_contents/min_heading_level'),
(int) $this->config->get('table_of_contents/max_heading_level'),
(string) $this->config->get('heading_permalink/fragment_prefix'),
);
$toc = $generator->generate($document);
if ($toc === null) {
// No linkable headers exist, so no TOC could be generated
return;
}
// Add custom CSS class(es), if defined
$class = $this->config->get('table_of_contents/html_class');
if ($class !== null) {
$toc->data->append('attributes/class', $class);
}
// Add the TOC to the Document
$position = $this->config->get('table_of_contents/position');
if ($position === self::POSITION_TOP) {
$document->prependChild($toc);
} elseif ($position === self::POSITION_BEFORE_HEADINGS) {
$this->insertBeforeFirstLinkedHeading($document, $toc);
} elseif ($position === self::POSITION_PLACEHOLDER) {
$this->replacePlaceholders($document, $toc);
} else {
throw InvalidConfigurationException::forConfigOption('table_of_contents/position', $position);
}
}
private function insertBeforeFirstLinkedHeading(Document $document, TableOfContents $toc): void
{
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
if (! $node instanceof Heading) {
continue;
}
foreach ($node->children() as $child) {
if ($child instanceof HeadingPermalink) {
$node->insertBefore($toc);
return;
}
}
}
}
private function replacePlaceholders(Document $document, TableOfContents $toc): void
{
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
// Add the block once we find a placeholder
if (! $node instanceof TableOfContentsPlaceholder) {
continue;
}
$node->replaceWith(clone $toc);
}
}
public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->config = $configuration;
}
}

View File

@@ -0,0 +1,53 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Environment\EnvironmentBuilderInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Extension\CommonMark\Renderer\Block\ListBlockRenderer;
use League\CommonMark\Extension\ConfigurableExtensionInterface;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
use League\Config\ConfigurationBuilderInterface;
use Nette\Schema\Expect;
final class TableOfContentsExtension implements ConfigurableExtensionInterface
{
public function configureSchema(ConfigurationBuilderInterface $builder): void
{
$builder->addSchema('table_of_contents', Expect::structure([
'position' => Expect::anyOf(TableOfContentsBuilder::POSITION_BEFORE_HEADINGS, TableOfContentsBuilder::POSITION_PLACEHOLDER, TableOfContentsBuilder::POSITION_TOP)->default(TableOfContentsBuilder::POSITION_TOP),
'style' => Expect::anyOf(ListBlock::TYPE_BULLET, ListBlock::TYPE_ORDERED)->default(ListBlock::TYPE_BULLET),
'normalize' => Expect::anyOf(TableOfContentsGenerator::NORMALIZE_RELATIVE, TableOfContentsGenerator::NORMALIZE_FLAT, TableOfContentsGenerator::NORMALIZE_DISABLED)->default(TableOfContentsGenerator::NORMALIZE_RELATIVE),
'min_heading_level' => Expect::int()->min(1)->max(6)->default(1),
'max_heading_level' => Expect::int()->min(1)->max(6)->default(6),
'html_class' => Expect::string()->default('table-of-contents'),
'placeholder' => Expect::anyOf(Expect::string(), Expect::null())->default(null),
]));
}
public function register(EnvironmentBuilderInterface $environment): void
{
$environment->addRenderer(TableOfContents::class, new TableOfContentsRenderer(new ListBlockRenderer()));
$environment->addEventListener(DocumentParsedEvent::class, [new TableOfContentsBuilder(), 'onDocumentParsed'], -150);
// phpcs:ignore SlevomatCodingStandard.ControlStructures.EarlyExit.EarlyExitNotUsed
if ($environment->getConfiguration()->get('table_of_contents/position') === TableOfContentsBuilder::POSITION_PLACEHOLDER) {
$environment->addBlockStartParser(TableOfContentsPlaceholderParser::blockStartParser(), 200);
// If a placeholder cannot be replaced with a TOC element this renderer will ensure the parser won't error out
$environment->addRenderer(TableOfContentsPlaceholder::class, new TableOfContentsPlaceholderRenderer());
}
}
}

View File

@@ -0,0 +1,168 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Extension\CommonMark\Node\Block\Heading;
use League\CommonMark\Extension\CommonMark\Node\Block\ListBlock;
use League\CommonMark\Extension\CommonMark\Node\Block\ListData;
use League\CommonMark\Extension\CommonMark\Node\Block\ListItem;
use League\CommonMark\Extension\CommonMark\Node\Inline\Link;
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalink;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
use League\CommonMark\Extension\TableOfContents\Normalizer\AsIsNormalizerStrategy;
use League\CommonMark\Extension\TableOfContents\Normalizer\FlatNormalizerStrategy;
use League\CommonMark\Extension\TableOfContents\Normalizer\NormalizerStrategyInterface;
use League\CommonMark\Extension\TableOfContents\Normalizer\RelativeNormalizerStrategy;
use League\CommonMark\Node\Block\Document;
use League\CommonMark\Node\NodeIterator;
use League\CommonMark\Node\RawMarkupContainerInterface;
use League\CommonMark\Node\StringContainerHelper;
use League\Config\Exception\InvalidConfigurationException;
final class TableOfContentsGenerator implements TableOfContentsGeneratorInterface
{
public const STYLE_BULLET = ListBlock::TYPE_BULLET;
public const STYLE_ORDERED = ListBlock::TYPE_ORDERED;
public const NORMALIZE_DISABLED = 'as-is';
public const NORMALIZE_RELATIVE = 'relative';
public const NORMALIZE_FLAT = 'flat';
/** @psalm-readonly */
private string $style;
/** @psalm-readonly */
private string $normalizationStrategy;
/** @psalm-readonly */
private int $minHeadingLevel;
/** @psalm-readonly */
private int $maxHeadingLevel;
/** @psalm-readonly */
private string $fragmentPrefix;
public function __construct(string $style, string $normalizationStrategy, int $minHeadingLevel, int $maxHeadingLevel, string $fragmentPrefix)
{
$this->style = $style;
$this->normalizationStrategy = $normalizationStrategy;
$this->minHeadingLevel = $minHeadingLevel;
$this->maxHeadingLevel = $maxHeadingLevel;
$this->fragmentPrefix = $fragmentPrefix;
if ($fragmentPrefix !== '') {
$this->fragmentPrefix .= '-';
}
}
public function generate(Document $document): ?TableOfContents
{
$toc = $this->createToc($document);
$normalizer = $this->getNormalizer($toc);
$firstHeading = null;
foreach ($this->getHeadingLinks($document) as $headingLink) {
$heading = $headingLink->parent();
// Make sure this is actually tied to a heading
if (! $heading instanceof Heading) {
continue;
}
// Skip any headings outside the configured min/max levels
if ($heading->getLevel() < $this->minHeadingLevel || $heading->getLevel() > $this->maxHeadingLevel) {
continue;
}
// Keep track of the first heading we see - we might need this later
$firstHeading ??= $heading;
// Keep track of the start and end lines
$toc->setStartLine($firstHeading->getStartLine());
$toc->setEndLine($heading->getEndLine());
// Create the new link
$link = new Link('#' . $this->fragmentPrefix . $headingLink->getSlug(), StringContainerHelper::getChildText($heading, [RawMarkupContainerInterface::class]));
$listItem = new ListItem($toc->getListData());
$listItem->setStartLine($heading->getStartLine());
$listItem->setEndLine($heading->getEndLine());
$listItem->appendChild($link);
// Add it to the correct place
$normalizer->addItem($heading->getLevel(), $listItem);
}
// Don't add the TOC if no headings were present
if (! $toc->hasChildren() || $firstHeading === null) {
return null;
}
return $toc;
}
private function createToc(Document $document): TableOfContents
{
$listData = new ListData();
if ($this->style === self::STYLE_BULLET) {
$listData->type = ListBlock::TYPE_BULLET;
} elseif ($this->style === self::STYLE_ORDERED) {
$listData->type = ListBlock::TYPE_ORDERED;
} else {
throw new InvalidConfigurationException(\sprintf('Invalid table of contents list style: "%s"', $this->style));
}
$toc = new TableOfContents($listData);
$toc->setStartLine($document->getStartLine());
$toc->setEndLine($document->getEndLine());
return $toc;
}
/**
* @return iterable<HeadingPermalink>
*/
private function getHeadingLinks(Document $document): iterable
{
foreach ($document->iterator(NodeIterator::FLAG_BLOCKS_ONLY) as $node) {
if (! $node instanceof Heading) {
continue;
}
foreach ($node->children() as $child) {
if ($child instanceof HeadingPermalink) {
yield $child;
}
}
}
}
private function getNormalizer(TableOfContents $toc): NormalizerStrategyInterface
{
switch ($this->normalizationStrategy) {
case self::NORMALIZE_DISABLED:
return new AsIsNormalizerStrategy($toc);
case self::NORMALIZE_RELATIVE:
return new RelativeNormalizerStrategy($toc);
case self::NORMALIZE_FLAT:
return new FlatNormalizerStrategy($toc);
default:
throw new InvalidConfigurationException(\sprintf('Invalid table of contents normalization strategy: "%s"', $this->normalizationStrategy));
}
}
}

View File

@@ -0,0 +1,22 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContents;
use League\CommonMark\Node\Block\Document;
interface TableOfContentsGeneratorInterface
{
public function generate(Document $document): ?TableOfContents;
}

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Extension\TableOfContents\Node\TableOfContentsPlaceholder;
use League\CommonMark\Parser\Block\AbstractBlockContinueParser;
use League\CommonMark\Parser\Block\BlockContinue;
use League\CommonMark\Parser\Block\BlockContinueParserInterface;
use League\CommonMark\Parser\Block\BlockStart;
use League\CommonMark\Parser\Block\BlockStartParserInterface;
use League\CommonMark\Parser\Cursor;
use League\CommonMark\Parser\MarkdownParserStateInterface;
use League\Config\ConfigurationAwareInterface;
use League\Config\ConfigurationInterface;
final class TableOfContentsPlaceholderParser extends AbstractBlockContinueParser
{
/** @psalm-readonly */
private TableOfContentsPlaceholder $block;
public function __construct()
{
$this->block = new TableOfContentsPlaceholder();
}
public function getBlock(): TableOfContentsPlaceholder
{
return $this->block;
}
public function tryContinue(Cursor $cursor, BlockContinueParserInterface $activeBlockParser): ?BlockContinue
{
return BlockContinue::none();
}
public static function blockStartParser(): BlockStartParserInterface
{
return new class () implements BlockStartParserInterface, ConfigurationAwareInterface {
/** @psalm-readonly-allow-private-mutation */
private ConfigurationInterface $config;
public function tryStart(Cursor $cursor, MarkdownParserStateInterface $parserState): ?BlockStart
{
$placeholder = $this->config->get('table_of_contents/placeholder');
if ($placeholder === null) {
return BlockStart::none();
}
// The placeholder must be the only thing on the line
if ($cursor->match('/^' . \preg_quote($placeholder, '/') . '$/') === null) {
return BlockStart::none();
}
return BlockStart::of(new TableOfContentsPlaceholderParser())->at($cursor);
}
public function setConfiguration(ConfigurationInterface $configuration): void
{
$this->config = $configuration;
}
};
}
}

View File

@@ -0,0 +1,40 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Xml\XmlNodeRendererInterface;
final class TableOfContentsPlaceholderRenderer implements NodeRendererInterface, XmlNodeRendererInterface
{
public function render(Node $node, ChildNodeRendererInterface $childRenderer): string
{
return '<!-- table of contents -->';
}
public function getXmlTagName(Node $node): string
{
return 'table_of_contents_placeholder';
}
/**
* @return array<string, scalar>
*/
public function getXmlAttributes(Node $node): array
{
return [];
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
/*
* This file is part of the league/commonmark package.
*
* (c) Colin O'Dell <colinodell@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace League\CommonMark\Extension\TableOfContents;
use League\CommonMark\Node\Node;
use League\CommonMark\Renderer\ChildNodeRendererInterface;
use League\CommonMark\Renderer\NodeRendererInterface;
use League\CommonMark\Xml\XmlNodeRendererInterface;
final class TableOfContentsRenderer implements NodeRendererInterface, XmlNodeRendererInterface
{
/** @var NodeRendererInterface&XmlNodeRendererInterface */
private $innerRenderer;
/**
* @psalm-param NodeRendererInterface&XmlNodeRendererInterface $innerRenderer
*
* @phpstan-param NodeRendererInterface&XmlNodeRendererInterface $innerRenderer
*/
public function __construct(NodeRendererInterface $innerRenderer)
{
$this->innerRenderer = $innerRenderer;
}
/**
* {@inheritDoc}
*/
public function render(Node $node, ChildNodeRendererInterface $childRenderer)
{
return $this->innerRenderer->render($node, $childRenderer);
}
public function getXmlTagName(Node $node): string
{
return 'table_of_contents';
}
/**
* @return array<string, scalar>
*/
public function getXmlAttributes(Node $node): array
{
return $this->innerRenderer->getXmlAttributes($node);
}
}