1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 class MTrackSyntaxHighlight {
5 static $schemes = array(
6 '' => 'No syntax highlighting',
7 'wezterm' => "Wez's Terminal",
8 'zenburn' => "Zenburn",
9 'vibrant-ink' => "Vibrant Ink",
11 static $lang_by_ext = array(
31 'json' => 'javascript',
42 static $langs = array(
43 '' => 'No particular file type',
45 'css' => 'CSS (Cascading Style Sheet)',
48 'shell' => 'Shell script',
49 'javascript' => 'Javascript',
50 'vb' => 'Visual Basic',
51 'xml' => 'HTML, XML, XSL',
52 'wiki' => 'Wiki Markup',
53 'diff' => 'Diff/Patch',
56 static function inferFileTypeFromContents($data) {
57 if (preg_match("/vim:.*ft=(\S+)/", $data, $M)) {
60 if (preg_match("/^#!.*env\s+(\S+)/", $data, $M)) {
63 if (preg_match("/^#!\s*(\S+)/", $data, $M)) {
64 return basename($M[1]);
69 static function highlightSource($data, $type = null, $filename = null, $line_numbers = false) {
71 $type = self::inferFileTypeFromContents($data);
72 if ($type === null && $filename !== null) {
73 if (preg_match("/\.([^.]+)$/", $filename, $M)) {
74 $ext = strtolower($M[1]);
75 if (isset(self::$lang_by_ext[$ext])) {
76 $type = self::$lang_by_ext[$ext];
81 if ($type == 'diff') {
82 return mtrack_diff($data);
84 if (strlen($type) && isset(self::$langs[$type])) {
85 require_once dirname(__FILE__) . '/hyperlight/hyperlight.php';
86 $hl = new Hyperlight($type);
87 $hdata = $hl->render($data);
89 $hdata = htmlentities($data);
92 return "<span class='source-code wezterm'>$hdata</span>";
94 $lines = preg_split("/\r?\n/", $data);
96 <table class='codeann'>
98 <th class='line'>line</th>
99 <th class='code'>code</th>
102 $nlines = count($lines);
103 for ($i = 1; $i <= $nlines; $i++) {
104 $html .= "<tr><td class='line'><a name='l$i'></a><a href='#l$i'>$i</a></td>";
106 $html .= "<td rowspan='$nlines' width='100%' class='source-code wezterm'>$hdata</td>";
110 return $html . "</table>\n";
113 static function getSchemeSelect($selected = 'wezterm') {
115 <select class='select-hl-scheme'>
117 foreach (self::$schemes as $k => $v) {
118 $sel = $selected == $k ? " selected" : '';
119 $html .= "<option value='$k'$sel>" .
120 htmlentities($v, ENT_QUOTES, 'utf-8') .
123 return $html . "</select>";
126 static function getLangSelect($name, $value) {
127 return mtrack_select_box($name, self::$langs, $value);