|
13 | 13 |
|
14 | 14 | namespace Ahc\CliSyntax; |
15 | 15 |
|
16 | | -class Highlighter |
| 16 | +class Highlighter extends Pretty |
17 | 17 | { |
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 = ''; |
28 | 20 |
|
29 | 21 | public function __toString(): string |
30 | 22 | { |
31 | 23 | return $this->highlight(); |
32 | 24 | } |
33 | 25 |
|
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 | | - |
43 | 26 | public function highlight(string $code = null): string |
44 | 27 | { |
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 = ''; |
58 | 29 |
|
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); |
62 | 31 |
|
63 | | - static::$configured = true; |
| 32 | + return $this->out; |
64 | 33 | } |
65 | 34 |
|
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([' ', '<', '>'], [' ', '<', '>'], $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) |
93 | 36 | { |
94 | 37 | static $formats = [ |
95 | | - 'comment' => "\033[1;30;40m%s\033[0m", |
| 38 | + 'comment' => "\033[0;34;40m%s\033[0m", |
96 | 39 | '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", |
98 | 41 | 'string' => "\033[0;33;40m%s\033[0m", |
99 | 42 | ]; |
100 | 43 |
|
101 | | - return \sprintf($formats[$type] ?? '%s', $text); |
| 44 | + $text = \str_replace([' ', '<', '>'], [' ', '<', '>'], $el->textContent); |
| 45 | + |
| 46 | + $this->out .= \sprintf($formats[$el->getAttribute('data-type')] ?? '%s', $text); |
102 | 47 | } |
103 | 48 | } |
0 commit comments