Skip to content

Commit e6d6b4e

Browse files
committed
refactor(highlighter): implement reset(), refactor visit()
1 parent aa65e14 commit e6d6b4e

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

src/Highlighter.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,17 @@ public function __toString(): string
2525

2626
public function highlight(string $code = null): string
2727
{
28-
$this->out = '';
29-
3028
$this->parse($code);
3129

32-
return $this->out;
30+
return \trim($this->out, "\n") . "\n";
31+
}
32+
33+
protected function reset()
34+
{
35+
$this->out = '';
3336
}
3437

35-
protected function visit(\DOMElement $el)
38+
protected function visit(\DOMNode $el)
3639
{
3740
static $formats = [
3841
'comment' => "\033[0;34;40m%s\033[0m",
@@ -41,8 +44,9 @@ protected function visit(\DOMElement $el)
4144
'string' => "\033[0;33;40m%s\033[0m",
4245
];
4346

47+
$type = $el instanceof \DOMElement ? $el->getAttribute('data-type') : 'raw';
4448
$text = \str_replace(['&nbsp;', '&lt;', '&gt;'], [' ', '<', '>'], $el->textContent);
4549

46-
$this->out .= \sprintf($formats[$el->getAttribute('data-type')] ?? '%s', $text);
50+
$this->out .= \sprintf($formats[$type] ?? '%s', $text);
4751
}
4852
}

0 commit comments

Comments
 (0)