Skip to content

Commit 772c3d7

Browse files
committed
refactor: extend Pretty, cleanup redundant
1 parent 494c4a0 commit 772c3d7

File tree

1 file changed

+12
-67
lines changed

1 file changed

+12
-67
lines changed

src/Highlighter.php

+12-67
Original file line numberDiff line numberDiff line change
@@ -13,91 +13,36 @@
1313

1414
namespace Ahc\CliSyntax;
1515

16-
class Highlighter
16+
class Highlighter extends Pretty
1717
{
18-
/** @var string The PHP code. */
19-
protected $code;
20-
21-
/** @var bool Indicates if it has been already configured. */
22-
protected static $configured;
23-
24-
public function __construct(string $code = null)
25-
{
26-
$this->code = $code ?? '';
27-
}
18+
/** @var string Output string */
19+
protected $out = '';
2820

2921
public function __toString(): string
3022
{
3123
return $this->highlight();
3224
}
3325

34-
public static function for(string $file): self
35-
{
36-
if (!\is_file($file)) {
37-
throw new \InvalidArgumentException('The given file doesnot exist or is unreadable.');
38-
}
39-
40-
return new static(\file_get_contents($file));
41-
}
42-
4326
public function highlight(string $code = null): string
4427
{
45-
static::configure();
46-
47-
$html = \highlight_string($code ?? $this->code, true);
48-
$html = \str_replace(['<br />', '<br/>', '<br>'], "\n", $html);
49-
50-
return $this->parse($html);
51-
}
52-
53-
public function configure()
54-
{
55-
if (static::$configured) {
56-
return;
57-
}
28+
$this->out = '';
5829

59-
foreach (['comment', 'default', 'html', 'keyword', 'string'] as $type) {
60-
\ini_set("highlight.$type", \ini_get("highlight.$type") . \sprintf('" data-type="%s', $type));
61-
}
30+
$this->parse($code);
6231

63-
static::$configured = true;
32+
return $this->out;
6433
}
6534

66-
protected function parse(string $html): string
67-
{
68-
$str = '';
69-
$dom = new \DOMDocument;
70-
71-
$dom->loadHTML($html);
72-
foreach ($dom->getElementsByTagName('span') as $el) {
73-
$str .= $this->visit($el);
74-
}
75-
76-
return $str;
77-
}
78-
79-
protected function visit(\DOMElement $el): string
80-
{
81-
if ('html' === $type = $el->getAttribute('data-type')) {
82-
return '';
83-
}
84-
85-
$text = $el->textContent;
86-
87-
$text = \str_replace(['&nbsp;', '&lt;', '&gt;'], [' ', '<', '>'], $text);
88-
89-
return $this->format($text, $type);
90-
}
91-
92-
protected function format(string $text, string $type): string
35+
protected function visit(\DOMElement $el)
9336
{
9437
static $formats = [
95-
'comment' => "\033[1;30;40m%s\033[0m",
38+
'comment' => "\033[0;34;40m%s\033[0m",
9639
'default' => "\033[0;32;40m%s\033[0m",
97-
'keyword' => "\033[0;36;40m%s\033[0m",
40+
'keyword' => "\033[0;31;40m%s\033[0m",
9841
'string' => "\033[0;33;40m%s\033[0m",
9942
];
10043

101-
return \sprintf($formats[$type] ?? '%s', $text);
44+
$text = \str_replace(['&nbsp;', '&lt;', '&gt;'], [' ', '<', '>'], $el->textContent);
45+
46+
$this->out .= \sprintf($formats[$el->getAttribute('data-type')] ?? '%s', $text);
10247
}
10348
}

0 commit comments

Comments
 (0)