1 <?php # vim:ts=2:sw=2:et:
2 /* For licensing and copyright terms, see the file named LICENSE */
4 class MTrack_SyntaxHighlight {
5 static $schemes = array(
6 '' => 'No syntax highlighting',
8 'wezterm' => "Wez's Terminal",
9 'zenburn' => "Zenburn",
10 'vibrant-ink' => "Vibrant Ink",
13 static $lang_by_ext = array(
33 'json' => 'javascript',
44 static $langs = array(
45 '' => 'No particular file type',
47 'css' => 'CSS (Cascading Style Sheet)',
50 'shell' => 'Shell script',
51 'javascript' => 'Javascript',
52 'vb' => 'Visual Basic',
53 'xml' => 'HTML, XML, XSL',
54 'wiki' => 'Wiki Markup',
55 'diff' => 'Diff/Patch',
58 static function inferFileTypeFromContents($data) {
59 if (preg_match("/vim:.*ft=(\S+)/", $data, $M)) {
62 if (preg_match("/^#!.*env\s+(\S+)/", $data, $M)) {
65 if (preg_match("/^#!\s*(\S+)/", $data, $M)) {
66 return basename($M[1]);
71 static function highlightSource($data, $type = null, $filename = null, $line_numbers = false) {
73 $type = self::inferFileTypeFromContents($data);
74 if ($type === null && $filename !== null) {
75 if (preg_match("/\.([^.]+)$/", $filename, $M)) {
76 $ext = strtolower($M[1]);
77 if (isset(self::$lang_by_ext[$ext])) {
78 $type = self::$lang_by_ext[$ext];
83 if ($type == 'diff') {
84 return mtrack_diff($data);
86 if (strlen($type) && isset(self::$langs[$type])) {
87 require_once dirname(__FILE__) . '/hyperlight/hyperlight.php';
88 $hl = new Hyperlight($type);
89 $hdata = $hl->render($data);
91 $hdata = htmlentities($data);
94 return "<span class='source-code plain'>$hdata</span>";
96 $lines = preg_split("/\r?\n/", $data);
98 <table class='codeann'>
100 <th class='line'>line</th>
101 <th class='code'>code</th>
104 $nlines = count($lines);
105 for ($i = 1; $i <= $nlines; $i++) {
106 $html .= "<tr><td class='line'><a name='l$i'></a><a href='#l$i'>$i</a></td>";
108 $html .= "<td rowspan='$nlines' width='100%' class='source-code plain'>$hdata</td>";
112 return $html . "</table>\n";
115 static function getSchemeSelect($selected = 'plain') {
117 <select class='select-hl-scheme'>
119 foreach (self::$schemes as $k => $v) {
120 $sel = $selected == $k ? " selected" : '';
121 $html .= "<option value='$k'$sel>" .
122 htmlentities($v, ENT_QUOTES, 'utf-8') .
125 return $html . "</select>";
128 static function getLangSelect($name, $value) {
129 return mtrack_select_box($name, self::$langs, $value);