Skip to content

Commit 5d3b600

Browse files
authored
Merge pull request #95 from nikophil/refacto/no-extend-from-lib
refacto: don't extend from parser
2 parents 629f0b4 + 54e5d22 commit 5d3b600

File tree

5 files changed

+94
-26
lines changed

5 files changed

+94
-26
lines changed

src/Directive/RstClassDirective.php

+29-1
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,41 @@
22

33
namespace SymfonyDocsBuilder\Directive;
44

5+
use Doctrine\RST\Directives\SubDirective;
56
use Doctrine\RST\HTML\Directives\ClassDirective;
7+
use Doctrine\RST\Nodes\Node;
8+
use Doctrine\RST\Parser;
69

710
/**
811
* Allows you to add custom classes to the next directive.
912
*/
10-
class RstClassDirective extends ClassDirective
13+
class RstClassDirective extends SubDirective
1114
{
15+
private $classDirective;
16+
17+
public function __construct(ClassDirective $classDirective)
18+
{
19+
$this->classDirective = $classDirective;
20+
}
21+
22+
/**
23+
* @param string[] $options
24+
*/
25+
public function processSub(
26+
Parser $parser,
27+
?Node $document,
28+
string $variable,
29+
string $data,
30+
array $options
31+
): ?Node {
32+
return $this->classDirective->processSub($parser, $document, $variable, $data, $options);
33+
}
34+
35+
public function appliesToNonBlockContent(): bool
36+
{
37+
return $this->classDirective->appliesToNonBlockContent();
38+
}
39+
1240
public function getName() : string
1341
{
1442
return 'rst-class';

src/KernelFactory.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace SymfonyDocsBuilder;
1313

1414
use Doctrine\RST\Configuration as RSTParserConfiguration;
15+
use Doctrine\RST\HTML\Directives\ClassDirective;
1516
use Doctrine\RST\Kernel;
1617
use SymfonyDocsBuilder\CI\UrlChecker;
1718
use SymfonyDocsBuilder\Directive as SymfonyDirectives;
@@ -82,7 +83,7 @@ private static function getDirectives(): array
8283
new SymfonyDirectives\IndexDirective(),
8384
new SymfonyDirectives\RoleDirective(),
8485
new SymfonyDirectives\NoteDirective(),
85-
new SymfonyDirectives\RstClassDirective(),
86+
new SymfonyDirectives\RstClassDirective(new ClassDirective()),
8687
new SymfonyDirectives\SeeAlsoDirective(),
8788
new SymfonyDirectives\SidebarDirective(),
8889
new SymfonyDirectives\TipDirective(),

src/Renderers/CodeNodeRenderer.php

-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
use Doctrine\RST\Renderers\NodeRenderer;
1616
use Doctrine\RST\Templates\TemplateRenderer;
1717
use Highlight\Highlighter;
18-
use function Symfony\Component\String\u;
1918

2019
class CodeNodeRenderer implements NodeRenderer
2120
{

src/Renderers/SpanNodeRenderer.php

+61-22
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,34 @@
1414
use Doctrine\RST\Environment;
1515
use Doctrine\RST\HTML\Renderers\SpanNodeRenderer as BaseSpanNodeRenderer;
1616
use Doctrine\RST\Nodes\SpanNode;
17-
use Doctrine\RST\Templates\TemplateRenderer;
17+
use Doctrine\RST\References\ResolvedReference;
18+
use Doctrine\RST\Renderers\SpanNodeRenderer as AbstractSpanNodeRenderer;
1819
use SymfonyDocsBuilder\CI\UrlChecker;
1920
use function Symfony\Component\String\u;
2021

21-
class SpanNodeRenderer extends BaseSpanNodeRenderer
22+
class SpanNodeRenderer extends AbstractSpanNodeRenderer
2223
{
23-
/** @var TemplateRenderer */
24-
private $templateRenderer;
24+
/** @var BaseSpanNodeRenderer */
25+
private $decoratedSpanNodeRenderer;
2526
/** @var UrlChecker|null */
2627
private $urlChecker;
2728

2829
public function __construct(
2930
Environment $environment,
3031
SpanNode $span,
31-
TemplateRenderer $templateRenderer,
32+
BaseSpanNodeRenderer $decoratedSpanNodeRenderer,
3233
?UrlChecker $urlChecker = null
3334
) {
34-
parent::__construct($environment, $span, $templateRenderer);
35+
parent::__construct($environment, $span);
3536

36-
$this->templateRenderer = $templateRenderer;
37+
$this->decoratedSpanNodeRenderer = $decoratedSpanNodeRenderer;
3738
$this->urlChecker = $urlChecker;
3839
}
3940

40-
/**
41-
* @param mixed[] $attributes
42-
*/
41+
/** @inheritDoc */
4342
public function link(?string $url, string $title, array $attributes = []): string
4443
{
45-
$url = (string) $url;
44+
$url = (string)$url;
4645

4746
if (
4847
$this->urlChecker &&
@@ -53,18 +52,25 @@ public function link(?string $url, string $title, array $attributes = []): strin
5352
}
5453

5554
if (!$this->isSafeUrl($url)) {
56-
$attributes['rel'] = 'external noopener noreferrer';
57-
$attributes['target'] = '_blank';
55+
$attributes = $this->addAttributesForUnsafeUrl($attributes);
5856
}
5957

60-
return $this->templateRenderer->render(
61-
'link.html.twig',
62-
[
63-
'url' => $this->environment->generateUrl($url),
64-
'title' => $title,
65-
'attributes' => $attributes,
66-
]
67-
);
58+
return $this->decoratedSpanNodeRenderer->link($url, $title, $attributes);
59+
}
60+
61+
public function reference(ResolvedReference $reference, array $value): string
62+
{
63+
if (!$this->isSafeUrl($reference->getUrl())) {
64+
$reference = new ResolvedReference(
65+
$reference->getFile(),
66+
$reference->getTitle(),
67+
$reference->getUrl(),
68+
$reference->getTitles(),
69+
$this->addAttributesForUnsafeUrl($reference->getAttributes())
70+
);
71+
}
72+
73+
return $this->decoratedSpanNodeRenderer->reference($reference, $value);
6874
}
6975

7076
public function literal(string $text): string
@@ -78,7 +84,32 @@ public function literal(string $text): string
7884
$text = str_replace('\\', '<wbr>\\', $text);
7985
}
8086

81-
return $this->templateRenderer->render('literal.html.twig', ['text' => $text]);
87+
return $this->decoratedSpanNodeRenderer->literal($text);
88+
}
89+
90+
public function emphasis(string $text): string
91+
{
92+
return $this->decoratedSpanNodeRenderer->emphasis($text);
93+
}
94+
95+
public function strongEmphasis(string $text): string
96+
{
97+
return $this->decoratedSpanNodeRenderer->strongEmphasis($text);
98+
}
99+
100+
public function nbsp(): string
101+
{
102+
return $this->decoratedSpanNodeRenderer->nbsp();
103+
}
104+
105+
public function br(): string
106+
{
107+
return $this->decoratedSpanNodeRenderer->br();
108+
}
109+
110+
public function escape(string $span): string
111+
{
112+
return $this->decoratedSpanNodeRenderer->escape($span);
82113
}
83114

84115
private function isExternalUrl($url): bool
@@ -101,4 +132,12 @@ private function isSafeUrl(string $url): bool
101132

102133
return $isSymfonyUrl || $isRelativeUrl;
103134
}
135+
136+
private function addAttributesForUnsafeUrl(array $attributes): array
137+
{
138+
return array_merge(
139+
$attributes,
140+
['rel' => 'external noopener noreferrer', 'target' => '_blank']
141+
);
142+
}
104143
}

src/SymfonyHTMLFormat.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Doctrine\RST\Renderers\NodeRendererFactory;
1919
use Doctrine\RST\Templates\TemplateRenderer;
2020
use SymfonyDocsBuilder\CI\UrlChecker;
21+
use Doctrine\RST\HTML\Renderers\SpanNodeRenderer as BaseSpanNodeRenderer;
2122

2223
/**
2324
* Class SymfonyHTMLFormat.
@@ -67,7 +68,7 @@ function (SpanNode $node) {
6768
return new Renderers\SpanNodeRenderer(
6869
$node->getEnvironment(),
6970
$node,
70-
$this->templateRenderer,
71+
new BaseSpanNodeRenderer($node->getEnvironment(), $node, $this->templateRenderer),
7172
$this->urlChecker
7273
);
7374
}

0 commit comments

Comments
 (0)