vendor and env first commit
This commit is contained in:
+26
@@ -0,0 +1,26 @@
|
||||
<?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\FrontMatter\Data;
|
||||
|
||||
use League\CommonMark\Extension\FrontMatter\Exception\InvalidFrontMatterException;
|
||||
|
||||
interface FrontMatterDataParserInterface
|
||||
{
|
||||
/**
|
||||
* @return mixed|null The parsed data (which may be null, if the input represents a null value)
|
||||
*
|
||||
* @throws InvalidFrontMatterException if parsing fails
|
||||
*/
|
||||
public function parse(string $frontMatter);
|
||||
}
|
||||
+47
@@ -0,0 +1,47 @@
|
||||
<?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\FrontMatter\Data;
|
||||
|
||||
use League\CommonMark\Exception\MissingDependencyException;
|
||||
use League\CommonMark\Extension\FrontMatter\Exception\InvalidFrontMatterException;
|
||||
|
||||
final class LibYamlFrontMatterParser implements FrontMatterDataParserInterface
|
||||
{
|
||||
public static function capable(): ?LibYamlFrontMatterParser
|
||||
{
|
||||
if (! \extension_loaded('yaml')) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return new LibYamlFrontMatterParser();
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function parse(string $frontMatter)
|
||||
{
|
||||
if (! \extension_loaded('yaml')) {
|
||||
throw new MissingDependencyException('Failed to parse yaml: "ext-yaml" extension is missing');
|
||||
}
|
||||
|
||||
$result = @\yaml_parse($frontMatter);
|
||||
|
||||
if ($result === false) {
|
||||
throw new InvalidFrontMatterException('Failed to parse front matter');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
}
|
||||
+39
@@ -0,0 +1,39 @@
|
||||
<?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\FrontMatter\Data;
|
||||
|
||||
use League\CommonMark\Exception\MissingDependencyException;
|
||||
use League\CommonMark\Extension\FrontMatter\Exception\InvalidFrontMatterException;
|
||||
use Symfony\Component\Yaml\Exception\ParseException;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
final class SymfonyYamlFrontMatterParser implements FrontMatterDataParserInterface
|
||||
{
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function parse(string $frontMatter)
|
||||
{
|
||||
if (! \class_exists(Yaml::class)) {
|
||||
throw new MissingDependencyException('Failed to parse yaml: "symfony/yaml" library is missing');
|
||||
}
|
||||
|
||||
try {
|
||||
/** @psalm-suppress ReservedWord */
|
||||
return Yaml::parse($frontMatter);
|
||||
} catch (ParseException $ex) {
|
||||
throw InvalidFrontMatterException::wrap($ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
Vendored
+24
@@ -0,0 +1,24 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter\Exception;
|
||||
|
||||
use League\CommonMark\Exception\CommonMarkException;
|
||||
|
||||
class InvalidFrontMatterException extends \RuntimeException implements CommonMarkException
|
||||
{
|
||||
public static function wrap(\Throwable $t): self
|
||||
{
|
||||
return new InvalidFrontMatterException('Failed to parse front matter: ' . $t->getMessage(), 0, $t);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
<?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\FrontMatter;
|
||||
|
||||
use League\CommonMark\Environment\EnvironmentBuilderInterface;
|
||||
use League\CommonMark\Event\DocumentPreParsedEvent;
|
||||
use League\CommonMark\Event\DocumentRenderedEvent;
|
||||
use League\CommonMark\Extension\ExtensionInterface;
|
||||
use League\CommonMark\Extension\FrontMatter\Data\FrontMatterDataParserInterface;
|
||||
use League\CommonMark\Extension\FrontMatter\Data\LibYamlFrontMatterParser;
|
||||
use League\CommonMark\Extension\FrontMatter\Data\SymfonyYamlFrontMatterParser;
|
||||
use League\CommonMark\Extension\FrontMatter\Listener\FrontMatterPostRenderListener;
|
||||
use League\CommonMark\Extension\FrontMatter\Listener\FrontMatterPreParser;
|
||||
|
||||
final class FrontMatterExtension implements ExtensionInterface
|
||||
{
|
||||
/** @psalm-readonly */
|
||||
private FrontMatterParserInterface $frontMatterParser;
|
||||
|
||||
public function __construct(?FrontMatterDataParserInterface $dataParser = null)
|
||||
{
|
||||
$this->frontMatterParser = new FrontMatterParser($dataParser ?? LibYamlFrontMatterParser::capable() ?? new SymfonyYamlFrontMatterParser());
|
||||
}
|
||||
|
||||
public function getFrontMatterParser(): FrontMatterParserInterface
|
||||
{
|
||||
return $this->frontMatterParser;
|
||||
}
|
||||
|
||||
public function register(EnvironmentBuilderInterface $environment): void
|
||||
{
|
||||
$environment->addEventListener(DocumentPreParsedEvent::class, new FrontMatterPreParser($this->frontMatterParser));
|
||||
$environment->addEventListener(DocumentRenderedEvent::class, new FrontMatterPostRenderListener(), -500);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
<?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\FrontMatter;
|
||||
|
||||
use League\CommonMark\Extension\FrontMatter\Data\FrontMatterDataParserInterface;
|
||||
use League\CommonMark\Extension\FrontMatter\Exception\InvalidFrontMatterException;
|
||||
use League\CommonMark\Extension\FrontMatter\Input\MarkdownInputWithFrontMatter;
|
||||
use League\CommonMark\Parser\Cursor;
|
||||
|
||||
final class FrontMatterParser implements FrontMatterParserInterface
|
||||
{
|
||||
/** @psalm-readonly */
|
||||
private FrontMatterDataParserInterface $frontMatterParser;
|
||||
|
||||
private const REGEX_FRONT_MATTER = '/^---\\R.*?\\R---\\R/s';
|
||||
|
||||
public function __construct(FrontMatterDataParserInterface $frontMatterParser)
|
||||
{
|
||||
$this->frontMatterParser = $frontMatterParser;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws InvalidFrontMatterException if the front matter cannot be parsed
|
||||
*/
|
||||
public function parse(string $markdownContent): MarkdownInputWithFrontMatter
|
||||
{
|
||||
$cursor = new Cursor($markdownContent);
|
||||
|
||||
// Locate the front matter
|
||||
$frontMatter = $cursor->match(self::REGEX_FRONT_MATTER);
|
||||
if ($frontMatter === null) {
|
||||
return new MarkdownInputWithFrontMatter($markdownContent);
|
||||
}
|
||||
|
||||
// Trim the last line (ending ---s and newline)
|
||||
$frontMatter = \preg_replace('/---\R$/', '', $frontMatter);
|
||||
if ($frontMatter === null) {
|
||||
return new MarkdownInputWithFrontMatter($markdownContent);
|
||||
}
|
||||
|
||||
// Parse the resulting YAML data
|
||||
$data = $this->frontMatterParser->parse($frontMatter);
|
||||
|
||||
// Advance through any remaining newlines which separated the front matter from the Markdown text
|
||||
$trailingNewlines = $cursor->match('/^\R+/');
|
||||
|
||||
// Calculate how many lines the Markdown is offset from the front matter by counting the number of newlines
|
||||
// Don't forget to add 1 because we stripped one out when trimming the trailing delims
|
||||
$lineOffset = \preg_match_all('/\R/', $frontMatter . $trailingNewlines) + 1;
|
||||
|
||||
return new MarkdownInputWithFrontMatter($cursor->getRemainder(), $lineOffset, $data);
|
||||
}
|
||||
}
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter;
|
||||
|
||||
use League\CommonMark\Extension\FrontMatter\Input\MarkdownInputWithFrontMatter;
|
||||
|
||||
interface FrontMatterParserInterface
|
||||
{
|
||||
public function parse(string $markdownContent): MarkdownInputWithFrontMatter;
|
||||
}
|
||||
+22
@@ -0,0 +1,22 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter;
|
||||
|
||||
interface FrontMatterProviderInterface
|
||||
{
|
||||
/**
|
||||
* @return mixed|null
|
||||
*/
|
||||
public function getFrontMatter();
|
||||
}
|
||||
+43
@@ -0,0 +1,43 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter\Input;
|
||||
|
||||
use League\CommonMark\Extension\FrontMatter\FrontMatterProviderInterface;
|
||||
use League\CommonMark\Input\MarkdownInput;
|
||||
|
||||
final class MarkdownInputWithFrontMatter extends MarkdownInput implements FrontMatterProviderInterface
|
||||
{
|
||||
/** @var mixed|null */
|
||||
private $frontMatter;
|
||||
|
||||
/**
|
||||
* @param string $content Markdown content without the raw front matter
|
||||
* @param int $lineOffset Line offset (based on number of front matter lines removed)
|
||||
* @param mixed|null $frontMatter Parsed front matter
|
||||
*/
|
||||
public function __construct(string $content, int $lineOffset = 0, $frontMatter = null)
|
||||
{
|
||||
parent::__construct($content, $lineOffset);
|
||||
|
||||
$this->frontMatter = $frontMatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getFrontMatter()
|
||||
{
|
||||
return $this->frontMatter;
|
||||
}
|
||||
}
|
||||
Vendored
+35
@@ -0,0 +1,35 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter\Listener;
|
||||
|
||||
use League\CommonMark\Event\DocumentRenderedEvent;
|
||||
use League\CommonMark\Extension\FrontMatter\Output\RenderedContentWithFrontMatter;
|
||||
|
||||
final class FrontMatterPostRenderListener
|
||||
{
|
||||
public function __invoke(DocumentRenderedEvent $event): void
|
||||
{
|
||||
if ($event->getOutput()->getDocument()->data->get('front_matter', null) === null) {
|
||||
return;
|
||||
}
|
||||
|
||||
$frontMatter = $event->getOutput()->getDocument()->data->get('front_matter');
|
||||
|
||||
$event->replaceOutput(new RenderedContentWithFrontMatter(
|
||||
$event->getOutput()->getDocument(),
|
||||
$event->getOutput()->getContent(),
|
||||
$frontMatter
|
||||
));
|
||||
}
|
||||
}
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter\Listener;
|
||||
|
||||
use League\CommonMark\Event\DocumentPreParsedEvent;
|
||||
use League\CommonMark\Extension\FrontMatter\FrontMatterParserInterface;
|
||||
|
||||
final class FrontMatterPreParser
|
||||
{
|
||||
private FrontMatterParserInterface $parser;
|
||||
|
||||
public function __construct(FrontMatterParserInterface $parser)
|
||||
{
|
||||
$this->parser = $parser;
|
||||
}
|
||||
|
||||
public function __invoke(DocumentPreParsedEvent $event): void
|
||||
{
|
||||
$content = $event->getMarkdown()->getContent();
|
||||
|
||||
$parsed = $this->parser->parse($content);
|
||||
|
||||
$event->getDocument()->data->set('front_matter', $parsed->getFrontMatter());
|
||||
$event->replaceMarkdown($parsed);
|
||||
}
|
||||
}
|
||||
Vendored
+51
@@ -0,0 +1,51 @@
|
||||
<?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.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace League\CommonMark\Extension\FrontMatter\Output;
|
||||
|
||||
use League\CommonMark\Extension\FrontMatter\FrontMatterProviderInterface;
|
||||
use League\CommonMark\Node\Block\Document;
|
||||
use League\CommonMark\Output\RenderedContent;
|
||||
|
||||
/**
|
||||
* @psalm-immutable
|
||||
*/
|
||||
final class RenderedContentWithFrontMatter extends RenderedContent implements FrontMatterProviderInterface
|
||||
{
|
||||
/**
|
||||
* @var mixed
|
||||
*
|
||||
* @psalm-readonly
|
||||
*/
|
||||
private $frontMatter;
|
||||
|
||||
/**
|
||||
* @param Document $document The parsed Document object
|
||||
* @param string $content The final HTML
|
||||
* @param mixed|null $frontMatter Any parsed front matter
|
||||
*/
|
||||
public function __construct(Document $document, string $content, $frontMatter)
|
||||
{
|
||||
parent::__construct($document, $content);
|
||||
|
||||
$this->frontMatter = $frontMatter;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public function getFrontMatter()
|
||||
{
|
||||
return $this->frontMatter;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user