Skip to content

Instantly share code, notes, and snippets.

@Hywan
Created May 14, 2014 10:53
Show Gist options
  • Save Hywan/b72b59581279f9b68055 to your computer and use it in GitHub Desktop.
Save Hywan/b72b59581279f9b68055 to your computer and use it in GitHub Desktop.
Highlight PHP source code
<?php
require '/usr/local/lib/Hoa/Core/Core.php';
use Hoa\Console;
class Formatter {
protected $_colors = array(
T_ABSTRACT => 'foreground(#5fafd7)',
T_AS => 'foreground(#5fafd7)',
T_BREAK => 'foreground(#5fafd7)',
T_CASE => 'foreground(#5fafd7)',
T_CATCH => 'foreground(#5fafd7)',
T_CLASS => 'foreground(#5fafd7)',
T_CLONE => 'foreground(#5fafd7)',
T_COMMENT => 'foreground(#839496)',
T_CONSTANT_ENCAPSED_STRING => 'foreground(#859900)',
T_CONTINUE => 'foreground(#5fafd7)',
T_DECLARE => 'foreground(#5fafd7)',
T_DEFAULT => 'foreground(#5fafd7)',
T_DIR => 'foreground(#d33682)',
T_DNUMBER => 'foreground(#d33682)',
T_DO => 'foreground(#5fafd7)',
T_DOC_COMMENT => 'foreground(#839496)',
T_ELSE => 'foreground(#5fafd7)',
T_ELSEIF => 'foreground(#5fafd7)',
T_EXIT => 'foreground(#5fafd7)',
T_EXTENDS => 'foreground(#5fafd7)',
T_FILE => 'foreground(#5fafd7)',
T_FILE => 'foreground(#d33682)',
T_FINALLY => 'foreground(#5fafd7)',
T_FOR => 'foreground(#5fafd7)',
T_FOREACH => 'foreground(#5fafd7)',
T_FUNCTION => 'foreground(#5fafd7)',
T_GLOBAL => 'foreground(#5fafd7)',
T_GOTO => 'foreground(#5fafd7)',
T_IF => 'foreground(#5fafd7)',
T_IMPLEMENTS => 'foreground(#5fafd7)',
T_INCLUDE => 'foreground(#5fafd7)',
T_INCLUDE_ONCE => 'foreground(#5fafd7)',
T_LNUMBER => 'foreground(#d33682)',
T_LNUMBER => 'foreground(#d33682)',
T_NAMESPACE => 'foreground(#5fafd7)',
T_NEW => 'foreground(#5fafd7)',
T_OPEN_TAG => 'foreground(#cb4b16)',
T_OPEN_TAG_WITH_ECHO => 'foreground(#cb4b16)',
T_PRIVATE => 'foreground(#5fafd7)',
T_PROTECTED => 'foreground(#5fafd7)',
T_PUBLIC => 'foreground(#5fafd7)',
T_REQUIRE => 'foreground(#5fafd7)',
T_REQUIRE_ONCE => 'foreground(#5fafd7)',
T_RETURN => 'foreground(#5fafd7)',
T_STATIC => 'foreground(#5fafd7)',
T_STRING => 'foreground(#afaf87)',
T_SWITCH => 'foreground(#5fafd7)',
T_THROW => 'foreground(#5fafd7)',
T_TRAIT => 'foreground(#5fafd7)',
T_TRY => 'foreground(#5fafd7)',
T_USE => 'foreground(#5fafd7)',
T_VARIABLE => 'foreground(#d7b05f)',
T_WHILE => 'foreground(#5fafd7)',
T_YIELD => 'foreground(#5fafd7)',
'highlight' => 'foreground(#002b36) background(#eee8d5)',
'*' => 'foreground(#fdf6e3)'
);
protected $_tokens = array();
protected $_higlights = array();
/**
* Foobar.
*/
public function __construct ( $code, Array $colors = array() ) {
$this->_tokens = token_get_all($code);
foreach($colors as $token => $color)
$this->_colors[$token] = $color;
return;
}
public function highlight ( $lines ) {
if(!is_array($lines))
$lines = array($lines);
foreach($lines as $line)
$this->_highlights[$line] = true;
return;
}
/**
* Compute.
*/
protected function compute ( ) {
$out = null;
foreach($this->_tokens as $token) {
if(!is_array($token)) {
$out .= Console\Chrome\Text::colorize(
$token,
$this->_colors['*']
);
continue;
}
list($tokenType, $tokenValue, ) = $token;
$lines = explode("\n", $tokenValue);
$count = count($lines);
foreach($lines as $line) {
if(isset($this->_colors[$tokenType]))
$out .= Console\Chrome\Text::colorize(
$line,
$this->_colors[$tokenType]
);
else
$out .= Console\Chrome\Text::colorize(
$line,
$this->_colors['*']
);
if(0 < --$count)
$out .= "\n";
}
}
$oout = null;
foreach(explode("\n", $out) as $number => $line) {
++$number;
$oout .= Console\Chrome\Text::colorize(
sprintf('%4d', $number),
isset($this->_highlights[$number])
? $this->_colors['highlight']
: 'foreground(#586e75)'
) .
' ' .
$line .
"\n";
}
return $oout;
}
public function __toString ( ) {
return $this->compute();
}
}
$formatter = new Formatter(rtrim(file_get_contents(__FILE__)));
$formatter->highlight(range(138, 150));
echo $formatter;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment