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