-
-
Notifications
You must be signed in to change notification settings - Fork 570
/
prism.js
75 lines (68 loc) · 2.52 KB
/
prism.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
/**@license
* __ _____ ________ __
* / // _ /__ __ _____ ___ __ _/__ ___/__ ___ ______ __ __ __ ___ / /
* __ / // // // // // _ // _// // / / // _ // _// // // \/ // _ \/ /
* / / // // // // // ___// / / // / / // ___// / / / / // // /\ // // / /__
* \___//____ \\___//____//_/ _\_ / /_//____//_/ /_/ /_//_//_/ /_/ \__\_\___/
* \/ /____/
* http://terminal.jcubic.pl
*
* this is utility that monkey patch Prism functions to output
* terminal formatting (it was first created here https://codepen.io/jcubic/pen/zEyxjJ)
*
* usage:
*
* you need to include both css and js (it need to be before this file)
*
* js code:
*
* var grammar = Prism.languages[language];
* var tokens = Prism.tokenize(file, grammar);
* var code = Prism.Token.stringify(tokens, language);
*
* term.echo(code); // or term.less(code) if you include less.js
*
* Copyright (c) 2018 Jakub Jankiewicz <http://jcubic.pl/me>
* Released under the MIT license
*
*/
/* global jQuery, Prism */
(function(Token, $) {
var _ = Prism;
_.Token = function(type, content, alias, matchedStr, greedy) {
Token.apply(this, [].slice.call(arguments));
};
_.Token.stringify = function(o, language, parent) {
if (typeof o == 'string') {
return o;
}
if (_.util.type(o) === 'Array') {
return o.map(function(element) {
return _.Token.stringify(element, language, o);
}).join('');
}
var env = {
type: o.type,
content: Token.stringify(o.content, language, parent),
tag: 'span',
classes: ['token', o.type],
attributes: {},
language: language,
parent: parent
};
if (env.type == 'comment') {
env.attributes['spellcheck'] = 'true';
}
if (o.alias) {
var aliases = _.util.type(o.alias) === 'Array' ? o.alias : [o.alias];
Array.prototype.push.apply(env.classes, aliases);
}
_.hooks.run('wrap', env);
var attributes = Object.keys(env.attributes).map(function(name) {
return name + '="' + (env.attributes[name] || '').replace(/"/g, '"') + '"';
}).join(' ');
return env.content.split(/\n/).map(function(content) {
return '[[b;;;' + env.classes.join(' ') + ']' + $.terminal.escape_brackets(content) + ']';
}).join('\n');
};
})(Prism.Token, jQuery);