Primo Committ

This commit is contained in:
paoloar77
2024-05-07 12:17:25 +02:00
commit e73d0e5113
7204 changed files with 884387 additions and 0 deletions

View File

@@ -0,0 +1,33 @@
<?php
/*
* 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\HeadingPermalink;
use League\CommonMark\Inline\Element\AbstractInline;
/**
* Represents an anchor link within a heading
*/
final class HeadingPermalink extends AbstractInline
{
/** @var string */
private $slug;
public function __construct(string $slug)
{
$this->slug = $slug;
}
public function getSlug(): string
{
return $this->slug;
}
}

View File

@@ -0,0 +1,28 @@
<?php
/*
* 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\HeadingPermalink;
use League\CommonMark\ConfigurableEnvironmentInterface;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Extension\ExtensionInterface;
/**
* Extension which automatically anchor links to heading elements
*/
final class HeadingPermalinkExtension implements ExtensionInterface
{
public function register(ConfigurableEnvironmentInterface $environment)
{
$environment->addEventListener(DocumentParsedEvent::class, new HeadingPermalinkProcessor(), -100);
$environment->addInlineRenderer(HeadingPermalink::class, new HeadingPermalinkRenderer());
}
}

View File

@@ -0,0 +1,147 @@
<?php
/*
* 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\HeadingPermalink;
use League\CommonMark\Block\Element\Document;
use League\CommonMark\Block\Element\Heading;
use League\CommonMark\Event\DocumentParsedEvent;
use League\CommonMark\Exception\InvalidOptionException;
use League\CommonMark\Extension\HeadingPermalink\Slug\SlugGeneratorInterface as DeprecatedSlugGeneratorInterface;
use League\CommonMark\Inline\Element\AbstractStringContainer;
use League\CommonMark\Node\Node;
use League\CommonMark\Normalizer\SlugNormalizer;
use League\CommonMark\Normalizer\TextNormalizerInterface;
use League\CommonMark\Util\ConfigurationAwareInterface;
use League\CommonMark\Util\ConfigurationInterface;
/**
* Searches the Document for Heading elements and adds HeadingPermalinks to each one
*/
final class HeadingPermalinkProcessor implements ConfigurationAwareInterface
{
const INSERT_BEFORE = 'before';
const INSERT_AFTER = 'after';
/** @var TextNormalizerInterface|DeprecatedSlugGeneratorInterface */
private $slugNormalizer;
/** @var ConfigurationInterface */
private $config;
/**
* @param TextNormalizerInterface|DeprecatedSlugGeneratorInterface|null $slugNormalizer
*/
public function __construct($slugNormalizer = null)
{
if ($slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
@trigger_error(sprintf('Passing a %s into the %s constructor is deprecated; use a %s instead', DeprecatedSlugGeneratorInterface::class, self::class, TextNormalizerInterface::class), E_USER_DEPRECATED);
}
$this->slugNormalizer = $slugNormalizer ?? new SlugNormalizer();
}
public function setConfiguration(ConfigurationInterface $configuration)
{
$this->config = $configuration;
}
public function __invoke(DocumentParsedEvent $e): void
{
$this->useNormalizerFromConfigurationIfProvided();
$walker = $e->getDocument()->walker();
while ($event = $walker->next()) {
$node = $event->getNode();
if ($node instanceof Heading && $event->isEntering()) {
$this->addHeadingLink($node, $e->getDocument());
}
}
}
private function useNormalizerFromConfigurationIfProvided(): void
{
$generator = $this->config->get('heading_permalink/slug_normalizer');
if ($generator === null) {
return;
}
if (!($generator instanceof DeprecatedSlugGeneratorInterface || $generator instanceof TextNormalizerInterface)) {
throw new InvalidOptionException('The heading_permalink/slug_normalizer option must be an instance of ' . TextNormalizerInterface::class);
}
$this->slugNormalizer = $generator;
}
private function addHeadingLink(Heading $heading, Document $document): void
{
$text = $this->getChildText($heading);
if ($this->slugNormalizer instanceof DeprecatedSlugGeneratorInterface) {
$slug = $this->slugNormalizer->createSlug($text);
} else {
$slug = $this->slugNormalizer->normalize($text, $heading);
}
$slug = $this->ensureUnique($slug, $document);
$headingLinkAnchor = new HeadingPermalink($slug);
switch ($this->config->get('heading_permalink/insert', 'before')) {
case self::INSERT_BEFORE:
$heading->prependChild($headingLinkAnchor);
return;
case self::INSERT_AFTER:
$heading->appendChild($headingLinkAnchor);
return;
default:
throw new \RuntimeException("Invalid configuration value for heading_permalink/insert; expected 'before' or 'after'");
}
}
/**
* @deprecated Not needed in 2.0
*/
private function getChildText(Node $node): string
{
$text = '';
$walker = $node->walker();
while ($event = $walker->next()) {
if ($event->isEntering() && (($child = $event->getNode()) instanceof AbstractStringContainer)) {
$text .= $child->getContent();
}
}
return $text;
}
private function ensureUnique(string $proposed, Document $document): string
{
// Quick path, it's a unique ID
if (!isset($document->data['heading_ids'][$proposed])) {
$document->data['heading_ids'][$proposed] = true;
return $proposed;
}
$extension = 0;
do {
++$extension;
} while (isset($document->data['heading_ids']["$proposed-$extension"]));
$document->data['heading_ids']["$proposed-$extension"] = true;
return "$proposed-$extension";
}
}

View File

@@ -0,0 +1,72 @@
<?php
/*
* 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\HeadingPermalink;
use League\CommonMark\ElementRendererInterface;
use League\CommonMark\HtmlElement;
use League\CommonMark\Inline\Element\AbstractInline;
use League\CommonMark\Inline\Renderer\InlineRendererInterface;
use League\CommonMark\Util\ConfigurationAwareInterface;
use League\CommonMark\Util\ConfigurationInterface;
/**
* Renders the HeadingPermalink elements
*/
final class HeadingPermalinkRenderer implements InlineRendererInterface, ConfigurationAwareInterface
{
/** @deprecated */
const DEFAULT_INNER_CONTENTS = '<svg class="heading-permalink-icon" viewBox="0 0 16 16" version="1.1" width="16" height="16" aria-hidden="true"><path fill-rule="evenodd" d="M4 9h1v1H4c-1.5 0-3-1.69-3-3.5S2.55 3 4 3h4c1.45 0 3 1.69 3 3.5 0 1.41-.91 2.72-2 3.25V8.59c.58-.45 1-1.27 1-2.09C10 5.22 8.98 4 8 4H4c-.98 0-2 1.22-2 2.5S3 9 4 9zm9-3h-1v1h1c1 0 2 1.22 2 2.5S13.98 12 13 12H9c-.98 0-2-1.22-2-2.5 0-.83.42-1.64 1-2.09V6.25c-1.09.53-2 1.84-2 3.25C6 11.31 7.55 13 9 13h4c1.45 0 3-1.69 3-3.5S14.5 6 13 6z"></path></svg>';
const DEFAULT_SYMBOL = '¶';
/** @var ConfigurationInterface */
private $config;
public function setConfiguration(ConfigurationInterface $configuration)
{
$this->config = $configuration;
}
public function render(AbstractInline $inline, ElementRendererInterface $htmlRenderer)
{
if (!$inline instanceof HeadingPermalink) {
throw new \InvalidArgumentException('Incompatible inline type: ' . \get_class($inline));
}
$slug = $inline->getSlug();
$idPrefix = (string) $this->config->get('heading_permalink/id_prefix', 'user-content');
if ($idPrefix !== '') {
$idPrefix .= '-';
}
$attrs = [
'id' => $idPrefix . $slug,
'href' => '#' . $slug,
'name' => $slug,
'class' => $this->config->get('heading_permalink/html_class', 'heading-permalink'),
'aria-hidden' => 'true',
'title' => $this->config->get('heading_permalink/title', 'Permalink'),
];
$innerContents = $this->config->get('heading_permalink/inner_contents');
if ($innerContents !== null) {
@trigger_error(sprintf('The %s config option is deprecated; use %s instead', 'inner_contents', 'symbol'), E_USER_DEPRECATED);
return new HtmlElement('a', $attrs, $innerContents, false);
}
$symbol = $this->config->get('heading_permalink/symbol', self::DEFAULT_SYMBOL);
return new HtmlElement('a', $attrs, \htmlspecialchars($symbol), false);
}
}

View File

@@ -0,0 +1,38 @@
<?php
/*
* 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\HeadingPermalink\Slug;
use League\CommonMark\Normalizer\SlugNormalizer;
@trigger_error(sprintf('%s is deprecated; use %s instead', DefaultSlugGenerator::class, SlugNormalizer::class), E_USER_DEPRECATED);
/**
* Creates URL-friendly strings
*
* @deprecated Use League\CommonMark\Normalizer\SlugNormalizer instead
*/
final class DefaultSlugGenerator implements SlugGeneratorInterface
{
public function createSlug(string $input): string
{
// Trim whitespace
$slug = \trim($input);
// Convert to lowercase
$slug = \mb_strtolower($slug);
// Try replacing whitespace with a dash
$slug = \preg_replace('/\s+/u', '-', $slug) ?? $slug;
// Try removing characters other than letters, numbers, and marks.
$slug = \preg_replace('/[^\p{L}\p{Nd}\p{Nl}\p{M}-]+/u', '', $slug) ?? $slug;
return $slug;
}
}

View File

@@ -0,0 +1,31 @@
<?php
/*
* 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\HeadingPermalink\Slug;
use League\CommonMark\Normalizer\TextNormalizerInterface;
@trigger_error(sprintf('%s is deprecated; use %s instead', SlugGeneratorInterface::class, TextNormalizerInterface::class), E_USER_DEPRECATED);
/**
* @deprecated Use League\CommonMark\Normalizer\TextNormalizerInterface instead
*/
interface SlugGeneratorInterface
{
/**
* Create a URL-friendly slug based on the given input string
*
* @param string $input
*
* @return string
*/
public function createSlug(string $input): string;
}