public function format(Tokens $tokens, $leveled = true) { $source = $tokens->getSource(); $result = ''; $last = 0; $level = 0; /** @var Token $token */ foreach ($tokens as $token) { $pos = StringHelper::positionToLine($source, $token->pos); if ($token->isStart()) { if ($leveled) { $result .= str_repeat(' ', $level); } $result .= Console::styled(['color' => 'yellow'], 'Open ') . '[' . $pos['line'] . ':' . $pos['pos'] . '] ' . Console::styled(['bold' => true], $token->name) . ' ' . Console::styled(['color' => 'blue'], get_class($token)) . ' ' . Console::styled(['color' => 'light green'], '(' . (isset($token->rule->language) ? $token->rule->language->getIdentifier() : 'none') . ')') . PHP_EOL; $level++; $result .= Console::styled(self::getColor($token->name), ($leveled ? str_repeat(' ', $level) : null) . implode(PHP_EOL . ($leveled ? str_repeat(' ', $level) : null), explode(PHP_EOL, substr($source, $token->pos, $token->getLength()))) . PHP_EOL); } else { $level--; if ($leveled) { $result .= str_repeat(' ', $level); } $result .= Console::styled(['color' => 'red'], 'Close ') . '[' . $pos['line'] . ':' . $pos['pos'] . '] ' . Console::styled(['bold' => true], $token->name) . ' ' . Console::styled(['color' => 'blue'], get_class($token)) . PHP_EOL; } $last = $token->pos; } $result .= substr($source, $last) . Console::reset(); return $result; }
public function format(Tokens $tokens) { $source = $tokens->getSource(); $result = ''; $last = 0; /** @var Token $token */ foreach ($tokens as $token) { $result .= htmlspecialchars(substr($source, $last, $token->pos - $last)); $result .= $token->isStart() ? '<span class="' . str_replace('.', ' ', $token->name) . '">' : '</span>'; $last = $token->pos; } $result .= substr($source, $last); return $result; }
public function format(Tokens $tokens) { $source = $tokens->getSource(); $result = ''; $last = 0; /** @var Token $token */ foreach ($tokens as $token) { $result .= substr($source, $last, $token->pos - $last); if (($style = ArrayHelper::resolve($this->_styles, $token->name)) !== null) { $result .= $token->isStart() ? Console::open(is_callable($style) ? $style($token) : $style) : Console::close(); } $last = $token->pos; } $result .= substr($source, $last) . Console::reset(); return $result; }