Update website

This commit is contained in:
Guilhem Lavaux 2024-11-19 08:02:04 +01:00
parent 4413528994
commit 1d90fbf296
6865 changed files with 1091082 additions and 0 deletions

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Nodes\CallableNode;
final class CallableNodeRenderer implements NodeRenderer
{
/** @var CallableNode */
private $callableNode;
public function __construct(CallableNode $callableNode)
{
$this->callableNode = $callableNode;
}
public function render(): string
{
return $this->callableNode->getCallable()();
}
}

View file

@ -0,0 +1,23 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Nodes\Node;
final class CallableNodeRendererFactory implements NodeRendererFactory
{
/** @var callable */
private $callable;
public function __construct(callable $callable)
{
$this->callable = $callable;
}
public function create(Node $node): NodeRenderer
{
return ($this->callable)($node);
}
}

View file

@ -0,0 +1,33 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Nodes\Node;
final class DefaultNodeRenderer implements NodeRenderer
{
/** @var Node */
private $node;
public function __construct(Node $node)
{
$this->node = $node;
}
public function render(): string
{
$value = $this->node->getValue();
if ($value instanceof Node) {
return $value->render();
}
if ($value === null) {
return '';
}
return $value;
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Nodes\DocumentNode;
final class DocumentNodeRenderer implements NodeRenderer
{
/** @var DocumentNode */
private $document;
public function __construct(DocumentNode $document)
{
$this->document = $document;
}
public function render(): string
{
$document = '';
foreach ($this->document->getNodes() as $node) {
$document .= $node->render() . "\n";
}
return $document;
}
}

View file

@ -0,0 +1,13 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
interface FormatListRenderer
{
public function createElement(string $text, string $prefix): string;
/** @return string[] */
public function createList(bool $ordered): array;
}

View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
interface FullDocumentNodeRenderer
{
public function renderDocument(): string;
}

View file

@ -0,0 +1,10 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
interface NodeRenderer
{
public function render(): string;
}

View file

@ -0,0 +1,12 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Nodes\Node;
interface NodeRendererFactory
{
public function create(Node $node): NodeRenderer;
}

View file

@ -0,0 +1,37 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Nodes\Node;
final class RenderedNode
{
/** @var Node */
private $node;
/** @var string */
private $rendered;
public function __construct(Node $node, string $rendered)
{
$this->node = $node;
$this->rendered = $rendered;
}
public function getNode(): Node
{
return $this->node;
}
public function setRendered(string $rendered): void
{
$this->rendered = $rendered;
}
public function getRendered(): string
{
return $this->rendered;
}
}

View file

@ -0,0 +1,190 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\Environment;
use Doctrine\RST\InvalidLink;
use Doctrine\RST\Nodes\Node;
use Doctrine\RST\Nodes\SpanNode;
use Doctrine\RST\Span\SpanToken;
use InvalidArgumentException;
use function is_string;
use function preg_replace;
use function preg_replace_callback;
use function sprintf;
use function str_replace;
abstract class SpanNodeRenderer implements NodeRenderer, SpanRenderer
{
/** @var Environment */
protected $environment;
/** @var SpanNode */
protected $span;
public function __construct(
Environment $environment,
SpanNode $span
) {
$this->environment = $environment;
$this->span = $span;
}
public function render(): string
{
$value = $this->span->getValue();
$span = $this->renderSyntaxes($value);
$span = $this->renderTokens($span);
return $span;
}
private function renderSyntaxes(string $span): string
{
$span = $this->escape($span);
$span = $this->renderStrongEmphasis($span);
$span = $this->renderEmphasis($span);
$span = $this->renderNbsp($span);
$span = $this->renderVariables($span);
$span = $this->renderBrs($span);
return $span;
}
private function renderStrongEmphasis(string $span): string
{
return (string) preg_replace_callback('/\*\*(.+)\*\*/mUsi', function (array $matches): string {
return $this->strongEmphasis($matches[1]);
}, $span);
}
private function renderEmphasis(string $span): string
{
return (string) preg_replace_callback('/\*(.+)\*/mUsi', function (array $matches): string {
return $this->emphasis($matches[1]);
}, $span);
}
private function renderNbsp(string $span): string
{
return (string) preg_replace('/~/', $this->nbsp(), $span);
}
private function renderVariables(string $span): string
{
return (string) preg_replace_callback('/\|(.+)\|/mUsi', function (array $match): string {
$variable = $this->environment->getVariable($match[1]);
if ($variable === null) {
return '';
}
if ($variable instanceof Node) {
return $variable->render();
}
if (is_string($variable)) {
return $variable;
}
return (string) $variable;
}, $span);
}
private function renderBrs(string $span): string
{
// Adding brs when a space is at the end of a line
return (string) preg_replace('/ \n/', $this->br(), $span);
}
private function renderTokens(string $span): string
{
foreach ($this->span->getTokens() as $token) {
$span = $this->renderToken($token, $span);
}
return $span;
}
private function renderToken(SpanToken $spanToken, string $span): string
{
switch ($spanToken->getType()) {
case SpanToken::TYPE_LITERAL:
return $this->renderLiteral($spanToken, $span);
case SpanToken::TYPE_REFERENCE:
return $this->renderReference($spanToken, $span);
case SpanToken::TYPE_LINK:
return $this->renderLink($spanToken, $span);
}
throw new InvalidArgumentException(sprintf('Unknown token type %s', $spanToken->getType()));
}
private function renderLiteral(SpanToken $spanToken, string $span): string
{
return str_replace(
$spanToken->getId(),
$this->literal($spanToken->get('text')),
$span
);
}
private function renderReference(SpanToken $spanToken, string $span): string
{
$reference = $this->environment->resolve($spanToken->get('section'), $spanToken->get('url'));
if ($reference === null) {
$this->environment->addInvalidLink(new InvalidLink($spanToken->get('url')));
return str_replace($spanToken->getId(), $spanToken->get('text'), $span);
}
$link = $this->reference($reference, $spanToken->getTokenData());
return str_replace($spanToken->getId(), $link, $span);
}
private function renderLink(SpanToken $spanToken, string $span): string
{
$url = $spanToken->get('url');
$link = $spanToken->get('link');
if ($url === '') {
$url = $this->environment->getLink($link);
if ($url === '') {
$metaEntry = $this->environment->getMetaEntry();
if ($metaEntry !== null && $metaEntry->hasTitle($link)) {
// A strangely-complex way to simply get the current relative URL
// For example, if the current page is "reference/page", then
// this would return "page" so the final URL is href="page#some-anchor".
$currentRelativeUrl = $this->environment->relativeUrl('/' . $metaEntry->getUrl());
$url = $currentRelativeUrl . '#' . Environment::slugify($link);
}
}
if ($url === '') {
$this->environment->addInvalidLink(new InvalidLink($link));
return str_replace($spanToken->getId(), $link, $span);
}
}
$link = $this->link($url, $this->renderSyntaxes($link));
return str_replace($spanToken->getId(), $link, $span);
}
}

View file

@ -0,0 +1,28 @@
<?php
declare(strict_types=1);
namespace Doctrine\RST\Renderers;
use Doctrine\RST\References\ResolvedReference;
interface SpanRenderer
{
public function emphasis(string $text): string;
public function strongEmphasis(string $text): string;
public function nbsp(): string;
public function br(): string;
public function literal(string $text): string;
/** @param mixed[] $attributes */
public function link(?string $url, string $title, array $attributes = []): string;
public function escape(string $span): string;
/** @param string[] $value */
public function reference(ResolvedReference $reference, array $value): string;
}