-
Notifications
You must be signed in to change notification settings - Fork 54
/
index.js
223 lines (189 loc) · 5.07 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
var hljs = require('highlight.js');
var entities = require('html-entities');
var colorize;
/***
* hicat() : hicat(str, options)
* Highlights a given `str` string.
*
* var hicat = require('hicat');
* hicat("echo 'hi'", { filename: 'script.sh' })
* => "echo \033[32m'hi'\033[0m"
*
* Options available:
*
* ~ filename (String): filename
* ~ lang (String): language to use
* ~ debug (Boolean): shows extra info if `true`
* ~ numbers (Boolean): shows line numbers if `true`
*/
function hicat (str, options) {
if (!options) options = {};
var lang = options.lang || (options.filename && extname(options.filename));
var out;
if (lang) {
if (lang === 'json')
str = formatJson(str);
try {
out = hljs.highlight(lang, str);
} catch (e) {
out = hljs.highlightAuto(str);
}
} else {
out = hljs.highlightAuto(str);
}
if (!out || !out.value) throw new Error("failed to highlight");
// convert <span> tags to ANSI colors
out.ansi = html2ansi(out.value, {
lang: out.language,
debug: options.debug
});
// Add line numbers
if (options.numbers) {
var i = 0;
var lines = out.value.split("\n").length;
var digits = (""+lines).length;
if (digits < 3) digits = 3;
out.ansi = out.ansi.replace(/^/gm, function (s) {
i++;
if (i >= lines) return s;
var pad = digits - (""+i).length;
var prefix = Array(pad+1).join(" ") + i + ' ';
prefix = colorize(prefix, color('line_number'));
return prefix + s;
});
}
// Add language comment
if (options.debug) {
var info = "/* hicat language: " + out.language + " */";
info = colorize(info, color('tag', 'debug'));
out.ansi += "\n\n" + info + "\n";
}
return {
ansi: entities.decode(out.ansi, {level: 'html5', scope: 'strict'}),
language: out.language,
html: out.value,
raw: str
};
}
/**
* colors : hicat.colors
* The color scheme. This is a key-value object that `hicat.color()` refers to.
*/
hicat.colors = require('./lib/colors');
/**
* listLanguages() : hicat.listLanguages()
* Returns a list of supported languages.
*
* listLanguages()
* => ['javascript', 'python', 'c', ...]
*/
hicat.listLanguages = hljs.listLanguages;
/**
* extname() : extname(filename)
* (private) Extracts the extension from a given `filename`.
*
* extname('hi.json')
* => 'json'
*/
function extname (fname) {
var m = fname.match(/\.([^\.]+)$/);
if (m) return m[1];
}
/**
* color() : hicat.color(token)
* Returns the color for a given token.
*
* color('string')
* => '32'
* color('attribute', 'json')
* => '32'
*/
var color = hicat.color = function (token, lang) {
if (lang)
return getColor(lang + ':' + token) || getColor(token);
else
return getColor(token);
function getColor (token) {
var code = token, newcode;
while (true) {
newcode = hicat.colors[code];
if (newcode) code = newcode;
else if (token !== code) return code;
else return;
}
}
};
/**
* html2ansi() : html2ansi(str, options)
* (private) Converts hljs-style spans from a given HTML `str` into ANSI
* color codes. Available options:
*
* ~ lang (String): language
* ~ debug (Boolean): true or false
*
* html2ansi('<span class="hljs-string">"hi"</span>")
* => "\033[31m"hi"\033[0m"
*/
function html2ansi (str, options) {
// do multiple passes as spans can be multiple
while (~str.indexOf('<span class="hljs-')) {
str = replaceSpan(str, options);
}
return str
.replace(/<span class="([^"]*)">/g, '')
.replace(/<\/span>/g, '');
}
/**
* replaceSpan() : replaceSpan(html, options)
* (private) Replaces span tags with ANSI codes in the given `html` string.
* A delegate of `html2ansi`.
*
* html = '<span class="hljs-tag">hi</span>'
* replaceSpan(html, 'java')
* => "\033[0;32mhi\033[0m"
*/
function replaceSpan (str, options) {
return str
.replace(/<span class="hljs-([^"]*)">([^<]*)<\/span>/g, function (_, token, s) {
var code = color(token, options && options.lang);
if (options.debug) {
return "" +
colorize("[" + token + "]", color('tag', 'debug')) +
colorize(s, code) +
colorize("[/" + token + "]", color('tag', 'debug'));
}
return colorize(s, code);
});
}
/**
* formatJson() : formatJson(str)
* Prettifies a JSON string
*/
function formatJson (str) {
try {
// return require('util').inspect(JSON.parse(str));
return JSON.stringify(JSON.parse(str), null, 2) + "\n";
} catch (e) {
return str;
}
}
/**
* colorize() : hicat.colorize(str, color)
* Applies the color `color` to the string `str`.
*
* colorize("hello", 32)
* => "\033[32mhello\033[0m"
*/
colorize = hicat.colorize = function (s, color) {
if (!color) return s;
var reset = "\033[0m",
code = "\033[" + color + "m";
// improved support for less -R, since it automatically resets the colors per line
if (~s.indexOf("\n")) s = s.replace(/\n/g, "\n"+code);
// nesting
if (~s.indexOf(reset)) {
s = s.replace(/\033\[0m/g, code);
}
return code + s + reset;
};
module.exports = hicat;