forked from davidmogar/gitbook-plugin-terminal
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
148 lines (124 loc) · 4.16 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
var XRegExp = require('xregexp');
var path = require('path');
var hljs = require('highlight.js');
var MAP = {
'py': 'python',
'js': 'javascript',
'json': 'javascript',
'rb': 'ruby',
'csharp': 'cs',
};
function normalize(lang) {
if(!lang) { return null; }
var lower = lang.toLowerCase();
return MAP[lower] || lower;
}
function highlight(lang, code) {
if(!lang) return {
body: code,
html: false
};
// Normalize lang
lang = normalize(lang);
try {
return hljs.highlight(lang, code).value;
} catch(e) { }
return {
body: code,
html: false
};
}
function processLine(line, config) {
var str = "";
var commands = "";
re = XRegExp(config.prompt);
if(re.test(line)) {
var parts = XRegExp.exec(line, re);
str += `<span class="t-line t-prompt-line">`
for(var i=0; i<re.xregexp.captureNames.length; i++) {
var name = re.xregexp.captureNames[i];
if(parts[name] != "") {
str += `<span class="t-${name}">${parts[name]}</span>`
}
}
str += '\n';
if(config.copyButtons) {
if(!parts.hasOwnProperty('command')) {
throw Error("Cannot use copyButtons if command does not exist as a named group in the prompt regexp!");
}
commands += parts.command.replace(/"/g,'"') + '\n';
}
} else {
if(config.lineStyles && line.startsWith("[warning]")) {
str += '<span class="t-line t-warning">';
str += line.replace("[warning]","") + '\n';
} else if (config.lineStyles && line.startsWith("[error]")) {
str += '<span class="t-line t-error">';
str += line.replace("[error]","") + '\n';
} else {
str += '<span class="t-line">';
str += line + '\n';
}
}
str += '</span>';
return {"line":str, "command":commands};
}
function process(thebody, config, isweb) {
var str = `<pre class="term t-${config.style}${config.fade ? ' t-fade' : ''}">`;
var body = thebody.trim().split('\n');
var lines = "";
var commands = ""
for(i=0; i<body.length; i++) {
var tmp = processLine(body[i], config);
lines += tmp.line;
commands += tmp.command;
}
/* JSON backend (README) should not have buttons (it does not have js or styles) */
if(isweb && config.copyButtons && commands != "") {
str += `<button class="btn t-copy" data-clipboard-text="${commands.trim()}"><svg class="octicon octicon-clippy" viewBox="0 0 14 16" version="1.1" width="14" height="14" aria-hidden="true"><path fill-rule="evenodd" d="M2 13h4v1H2v-1zm5-6H2v1h5V7zm2 3V8l-3 3 3 3v-2h5v-2H9zM4.5 9H2v1h2.5V9zM2 12h2.5v-1H2v1zm9 1h1v2c-.02.28-.11.52-.3.7-.19.18-.42.28-.7.3H1c-.55 0-1-.45-1-1V4c0-.55.45-1 1-1h3c0-1.11.89-2 2-2 1.11 0 2 .89 2 2h3c.55 0 1 .45 1 1v5h-1V6H1v9h10v-2zM2 5h8c0-.55-.45-1-1-1H8c-.55 0-1-.45-1-1s-.45-1-1-1-1 .45-1 1-.45 1-1 1H3c-.55 0-1 .45-1 1z"></path></svg></button>`
}
str += lines + '</pre>';
return {body:str, html:true};
}
function processBlock(block) {
var book = this;
/* Make a copy */
var orig_config = Object.assign({}, book.config.get('pluginsConfig.term', {}));
/* Merge dictionaries */
var config = Object.assign(orig_config, block.kwargs);
var isweb = book.output.name == 'website';
return process(block.body, config, isweb).body;
}
module.exports = {
website: {
assets: './assets',
css: [
'plugin.css'
],
js: [
'clipboard.min.js',
'initclip.js',
'plugin.js'
]
},
ebook: {
assets: './assets',
css: [
'plugin.css'
]
},
blocks: {
term: {
process: processBlock
},
code: function(block) {
var language = normalize(block.kwargs.language);
if(language && language == "term") {
var opts = this.config.get('pluginsConfig.term');
var isweb = this.output.name == 'website';
return process(block.body, opts, isweb).body;
} else
return highlight(language, block.body);
}
}
};