2 title: PHP commandline highlighter
9 Just chewed up a command line highlighter for php code.
10 It's hacky, but it fulfills my needs.
14 protected $colors = array(
23 protected $encoding = "UTF-8";
28 public function __construct() {
29 if ($files = $this->setup()) {
30 foreach ($files as $file) {
31 $this->highlight(file_get_contents($file), $file);
33 } elseif ($this->select()) {
34 $this->highlight(file_get_contents("php://stdin"), "<STDIN>");
40 public function __destruct() {
41 if ($this->tabstops) {
48 protected function usage() {
51 printf(" %s [-e ENC] [-t TAB] [-f FILE, ...] [-c TOKEN=COLOR, ...]\n", basename($_SERVER["argv"][0]));
53 printf(" -e ENC specify the character set of the highlighted code\n");
54 printf(" -t TAB specify the tab with\n");
55 printf(" -f FILE specify one or more files to highlight, STDIN if omitted\n");
56 printf(" -c TOKEN=COLOR specify the color code for the type of token\n");
57 printf(" possible tokens: %s\n", implode(", ",array_keys($this->colors)));
62 protected function setup() {
64 foreach (getopt("c:e:f:t:h") as $key => $val) {
70 foreach ((array)$val as $col) {
71 list($type, $color) = explode("=", $col);
72 $this->colors[$type] = $color;
76 $this->encoding = current(array_reverse((array)$val));
79 if ($tabstops = (int) current(array_reverse((array)$val))) {
80 $this->tabstops = $tabstops;
84 $files = (array) $val;
89 if (!$this->colwidth = getenv("COLUMNS")) {
90 if (preg_match("/columns (\d+)/", shell_exec("stty -a"), $m)) {
91 $this->colwidth = $m[1];
97 $this->tabstops && $this->tabstops();
98 ini_set("highlight.comment", "comment");
99 ini_set("highlight.default", "default");
100 ini_set("highlight.html", "html");
101 ini_set("highlight.keyword", "keyword");
102 ini_set("highlight.string", "string");
103 $this->formats = array(
108 "/( ){4}/" => "\t",
111 "/<span style=\"color: comment\">/" => $this->color("comment"),
112 "/<span style=\"color: default\">/" => $this->color("default"),
113 "/<span style=\"color: html\">/" => $this->color("html"),
114 "/<span style=\"color: keyword\">/" => $this->color("keyword"),
115 "/<span style=\"color: string\">/" => $this->color("string"),
116 // fix colors before newline
117 #"/((\033\[\d+m)+)([[:punct:][:space:]]*)(\r?\n)/s" => "\$1\$3\$4\$1",
119 "/<code>/" => $this->color("start"),
120 "/<\/code>/" => $this->color("stop", "", PHP_EOL),
125 protected function select() {
126 $r = $w = $e = array();
128 if (stream_select($r, $w, $e, 0, 100) && $r[0]) {
134 protected function tabstops() {
135 $colw = $this->colwidth;
136 $tabs = str_repeat(" ", $this->tabstops) . "\033H";
137 $tabs = str_repeat($tabs, $colw/$this->tabstops);
141 protected function color($type, $prepend = "", $append = "") {
142 return $prepend . "\033[" . implode("m\033[", explode(":", $this->colors[$type])) . "m" . $append;
145 protected function highlight($code, $name) {
146 echo "## Syntax highlighted code of $name:\n";
147 echo html_entity_decode(
149 array_keys($this->formats),
150 array_values($this->formats),
151 highlight_string($code, true)