1 | var hljs = require('highlight.js');
|
2 | var colorize;
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | function hicat (str, options) {
|
21 | if (!options) options = {};
|
22 |
|
23 | var lang = options.lang || (options.filename && extname(options.filename));
|
24 | var out;
|
25 |
|
26 | if (lang) {
|
27 | try {
|
28 | out = hljs.highlight(lang, str);
|
29 | } catch (e) {
|
30 | out = hljs.highlightAuto(str);
|
31 | }
|
32 | } else {
|
33 | out = hljs.highlightAuto(str);
|
34 | }
|
35 |
|
36 | if (!out || !out.value) throw new Error("failed to highlight");
|
37 |
|
38 |
|
39 | out.ansi = html2ansi(out.value, {
|
40 | lang: out.language,
|
41 | debug: options.debug
|
42 | });
|
43 |
|
44 |
|
45 | if (options.numbers) {
|
46 | var i = 0;
|
47 | var lines = out.value.split("\n").length;
|
48 | var digits = (""+lines).length;
|
49 | if (digits < 3) digits = 3;
|
50 | out.ansi = out.ansi.replace(/^/gm, function (s) {
|
51 | i++;
|
52 | if (i >= lines) return s;
|
53 |
|
54 | var pad = digits - (""+i).length;
|
55 | var prefix = Array(pad+1).join(" ") + i + ' ';
|
56 | prefix = colorize(prefix, color('line_number'));
|
57 | return prefix + s;
|
58 | });
|
59 | }
|
60 |
|
61 |
|
62 | if (options.debug) {
|
63 | var info = "/* hicat language: " + out.language + " */";
|
64 | info = colorize(info, color('tag', 'debug'));
|
65 | out.ansi += "\n\n" + info + "\n";
|
66 | }
|
67 |
|
68 | return {
|
69 | ansi: out.ansi,
|
70 | language: out.language,
|
71 | html: out.value,
|
72 | raw: str
|
73 | };
|
74 | }
|
75 |
|
76 |
|
77 |
|
78 |
|
79 |
|
80 |
|
81 | hicat.colors = require('./lib/colors');
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | hicat.listLanguages = hljs.listLanguages;
|
92 |
|
93 |
|
94 |
|
95 |
|
96 |
|
97 |
|
98 |
|
99 |
|
100 |
|
101 | function extname (fname) {
|
102 | var m = fname.match(/\.([^\.]+)$/);
|
103 | if (m) return m[1];
|
104 | }
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 |
|
114 |
|
115 |
|
116 | var color = hicat.color = function (token, lang) {
|
117 | if (lang)
|
118 | return getColor(lang + ':' + token) || getColor(token);
|
119 | else
|
120 | return getColor(token);
|
121 |
|
122 | function getColor (token) {
|
123 | var code = token, newcode;
|
124 | while (true) {
|
125 | newcode = hicat.colors[code];
|
126 | if (newcode) code = newcode;
|
127 | else if (token !== code) return code;
|
128 | else return;
|
129 | }
|
130 | }
|
131 | };
|
132 |
|
133 |
|
134 |
|
135 |
|
136 |
|
137 |
|
138 |
|
139 |
|
140 |
|
141 |
|
142 |
|
143 |
|
144 |
|
145 | function html2ansi (str, options) {
|
146 |
|
147 | while (~str.indexOf('<span class="hljs-')) {
|
148 | str = replaceSpan(str, options);
|
149 | }
|
150 |
|
151 | return str
|
152 | .replace(/<span class="([^"]*)">/g, '')
|
153 | .replace(/<\/span>/g, '')
|
154 | .replace(/</g, '<')
|
155 | .replace(/>/g, '>')
|
156 | .replace(/&/g, '&');
|
157 | }
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 |
|
166 |
|
167 |
|
168 |
|
169 | function replaceSpan (str, options) {
|
170 | return str
|
171 | .replace(/<span class="hljs-([^"]*)">([^<]*)<\/span>/g, function (_, token, s) {
|
172 | var code = color(token, options && options.lang);
|
173 | if (options.debug) {
|
174 | return "" +
|
175 | colorize("[" + token + "]", color('tag', 'debug')) +
|
176 | colorize(s, code) +
|
177 | colorize("[/" + token + "]", color('tag', 'debug'));
|
178 | }
|
179 | return colorize(s, code);
|
180 | });
|
181 | }
|
182 |
|
183 |
|
184 |
|
185 |
|
186 |
|
187 |
|
188 |
|
189 |
|
190 |
|
191 | colorize = hicat.colorize = function (s, color) {
|
192 | if (!color) return s;
|
193 |
|
194 | var reset = "\033[0m",
|
195 | code = "\033[" + color + "m";
|
196 |
|
197 |
|
198 | if (~s.indexOf("\n")) s = s.replace(/\n/g, "\n"+code);
|
199 |
|
200 |
|
201 | if (~s.indexOf(reset)) {
|
202 | s = s.replace(/\033\[0m/g, code);
|
203 | }
|
204 |
|
205 | return code + s + reset;
|
206 | };
|
207 |
|
208 | module.exports = hicat;
|