Skip to content

Commit 2faee09

Browse files
committed
clean nodes
1 parent cbe30e6 commit 2faee09

File tree

8 files changed

+51
-77
lines changed

8 files changed

+51
-77
lines changed

_build/src/Nodes/AnchorNode.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class AnchorNode extends Base
88
{
99
public function render(): string
1010
{
11-
return '<span id="'.$this->value.'"></span>';
11+
return sprintf('<span id="%s"></span>', $this->value);
1212
}
1313
}
1414

_build/src/Nodes/ClassTrait.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<?php
2+
3+
namespace SymfonyDocs\Nodes;
4+
5+
trait ClassTrait
6+
{
7+
/** @var string */
8+
private $class;
9+
10+
/**
11+
* @return string
12+
*/
13+
public function getClass(): ?string
14+
{
15+
return $this->class;
16+
}
17+
18+
/**
19+
* @param string $class
20+
*/
21+
public function setClass(?string $class)
22+
{
23+
$this->class = $class;
24+
}
25+
}

_build/src/Nodes/CodeNode.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,7 @@ public function render(): string
5858

5959
$lineNumbers = "";
6060
for ($i = 1; $i <= \count($lines); $i++) {
61-
$iAsString = (string) $i;
62-
if (strlen($iAsString) == 1) {
63-
$iAsString = " ".$iAsString;
64-
}
65-
$lineNumbers .= $iAsString."\n";
61+
$lineNumbers .= str_pad((string) $i, 2, ' ', STR_PAD_LEFT)."\n";
6662
}
6763

6864
$language = $this->getLanguage() ?? 'php';
@@ -85,14 +81,9 @@ public function render(): string
8581
);
8682
}
8783

88-
/**
89-
* @return string[]
90-
*/
9184
private function getLines(string $code): array
9285
{
93-
$lines = preg_split('/\r\n|\r|\n/', $code);
94-
assert(is_array($lines));
95-
86+
$lines = preg_split('/\r\n|\r|\n/', $code);
9687
$reversedLines = array_reverse($lines);
9788

9889
// trim empty lines at the end of the code

_build/src/Nodes/ListNode.php

Lines changed: 1 addition & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66

77
class ListNode extends Base
88
{
9-
/** @var string */
10-
private $class = '';
9+
use ClassTrait;
1110

1211
protected function createElement(string $text, string $prefix): string
1312
{
1413
return sprintf('<li>%s</li>', $text);
1514
}
1615

17-
/**
18-
* @return string[]
19-
*/
2016
protected function createList(bool $ordered): array
2117
{
2218
$keyword = $ordered ? 'ol' : 'ul';
@@ -26,24 +22,4 @@ protected function createList(bool $ordered): array
2622
sprintf('</%s>', $keyword),
2723
];
2824
}
29-
30-
/**
31-
* @return string
32-
*/
33-
public function getClass(): ?string
34-
{
35-
return $this->class;
36-
}
37-
38-
/**
39-
* @param string $class
40-
*
41-
* @return $this
42-
*/
43-
public function setClass(?string $class)
44-
{
45-
$this->class = $class;
46-
47-
return $this;
48-
}
4925
}

_build/src/Nodes/ParagraphNode.php

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,30 +8,9 @@
88

99
class ParagraphNode extends Base
1010
{
11-
/** @var string */
12-
private $class = '';
11+
use ClassTrait;
1312

14-
/**
15-
* @return string
16-
*/
17-
public function getClass(): ?string
18-
{
19-
return $this->class;
20-
}
21-
22-
/**
23-
* @param string $class
24-
*
25-
* @return $this
26-
*/
27-
public function setClass(?string $class)
28-
{
29-
$this->class = $class;
30-
31-
return $this;
32-
}
33-
34-
public function render() : string
13+
public function render(): string
3514
{
3615
if ($this->value instanceof Node) {
3716
$text = trim($this->value->render());
@@ -44,7 +23,7 @@ public function render() : string
4423
return sprintf('<p class="%s">%s</p>', $this->class, $text);
4524
}
4625

47-
return '<p>' . $text . '</p>';
26+
return sprintf('<p>%s</p>', $text);
4827
}
4928

5029
return '';

_build/src/Nodes/SpanNode.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,9 @@
66

77
class SpanNode extends Span
88
{
9-
public function literal(string $text) : string
9+
public function literal(string $text): string
1010
{
11-
return '<code class="notranslate">' . $text . '</code>';
11+
return sprintf('<code class="notranslate">%s</code>', $text);
1212
}
1313

1414
public function link(?string $url, string $title, array $attributes = []): string
@@ -21,6 +21,7 @@ public function link(?string $url, string $title, array $attributes = []): strin
2121
$title = '<em>'.$title.'</em>';
2222
unset($attributes['is-doc']);
2323
}
24+
2425
if (!$attributes) {
2526
$attributes['class'] = sprintf('reference %s', 0 === strpos($url, 'http') ? 'external' : 'internal');
2627
}
@@ -36,6 +37,10 @@ static function ($attribute, $value) {
3637
)
3738
);
3839

39-
return '<a href="'.htmlspecialchars((string) $url).'"'.($htmlAttributes !== '' ? ' '.$htmlAttributes : '').'>'.$title.'</a>';
40+
if ($htmlAttributes) {
41+
$htmlAttributes = ' '.$htmlAttributes;
42+
}
43+
44+
return sprintf('<a href="%s"%s>%s</a>', htmlspecialchars((string) $url), $htmlAttributes, $title);
4045
}
4146
}

_build/src/Nodes/TitleNode.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ public function render(): string
1111
{
1212
$anchor = Environment::slugify((string) $this->value);
1313

14-
return '<h'.$this->level.' id="'.$anchor.'">'.$this->value.'<a class="headerlink" href="#'.$anchor.'" title="Permalink to this headline">¶</a></h'.$this->level.'>';
14+
return sprintf(
15+
'<h%s id="%s">%s<a class="headerlink" href="#%s" title="Permalink to this headline">¶</a></h%s>',
16+
$this->level,
17+
$anchor,
18+
$this->value,
19+
$anchor,
20+
$this->level
21+
);
1522
}
1623
}
1724

_build/src/Nodes/TocNode.php

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,8 @@ public function render(): string
3434
return $html;
3535
}
3636

37-
/**
38-
* @param mixed[]|array $titles
39-
* @param mixed[] $path
40-
*/
41-
private function renderLevel(
42-
?string $url,
43-
array $titles,
44-
int $level = 1,
45-
array $path = []
46-
): string {
37+
private function renderLevel(?string $url, array $titles, int $level = 1, array $path = []): string
38+
{
4739
if (-1 !== $this->depth && $level > $this->depth) {
4840
return '';
4941
}
@@ -63,7 +55,7 @@ private function renderLevel(
6355
$anchor = Environment::slugify($slug);
6456
$target = $url;
6557
if (1 !== $level) {
66-
$target = $url . '#' . $anchor;
58+
$target = $url.'#'.$anchor;
6759
}
6860

6961
if (is_array($title)) {
@@ -83,7 +75,6 @@ private function renderLevel(
8375
}
8476

8577
$html .= '</li>';
86-
8778
}
8879

8980
return $html;

0 commit comments

Comments
 (0)