-
Notifications
You must be signed in to change notification settings - Fork 3
/
vimhl.js
84 lines (73 loc) · 2.9 KB
/
vimhl.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
// Generated by CoffeeScript 2.7.0
(function() {
// File: vimhl.coffee
// Date: Thu May 28 18:23:18 2015 +0800
var cheerio, crypto, exec, formatFilter, fs, temp, util, vimHighlight, wrench;
util = require('util');
exec = util.promisify(require('child_process').exec);
fs = require('fs');
temp = require('temp').track();
cheerio = require('cheerio');
crypto = require('crypto');
wrench = require('wrench');
formatFilter = function(data) {
// trim the extra empty line
data = data.trim();
data = data.replace('\n', '');
// delete the extra line break between code
data = data.replace(/<br>/gi, '');
// replace dark color with lighter one
data = data.replace(/#0000c0/gi, '#2277cc');
data = data.replace(/#af5f00/gi, '#d5e617');
data = data.replace(/#008080/gi, '#0edbcb');
data = data.replace(/#008000/gi, '#00c000');
data = data.replace(/#804000/gi, '#d5e617');
data = data.replace(/#c00000/gi, '#ff4040');
// https://stackoverflow.com/questions/2083754/why-shouldnt-apos-be-used-to-escape-single-quotes
data = data.replace(/'/gi, '');
return data;
};
vimHighlight = async function(data, ft, useLineN, cacheDir) {
var $, cacheFileHash, cacheFileName, htmlPath, info, lineOpt, opt, result, ret;
cacheFileHash = crypto.createHash('md5');
cacheFileHash.update(data);
cacheFileHash.update(ft);
if (useLineN) {
cacheFileHash.update("show line numbers");
} else {
cacheFileHash.update("no line numbers");
}
cacheFileName = cacheFileHash.digest('hex') + '.html';
if (!fs.existsSync(cacheDir)) {
wrench.mkdirSyncRecursive(cacheDir, '0777');
}
if (!fs.existsSync(cacheDir + '/' + cacheFileName)) {
if (useLineN) {
lineOpt = ' +"let g:html_number_lines=1" ';
} else {
lineOpt = ' +"let g:html_number_lines=0" ';
}
info = (await temp.open({
suffix: '.' + ft
}));
await fs.promises.writeFile(info.path, data);
// https://bitbucket.org/fritzophrenic/vim-tohtml/issues/25/cannot-recognize-urls-with-ip-address
opt = '+"let g:html_no_progress=1" +"let g:html_ignore_folding=1" +"let g:html_use_css=0" +"let g:html_pre_wrap=0" +"hi clear Constant"' + lineOpt;
await exec('vim -X -i NONE -f ' + opt + ' +"TOhtml" -ncwqa ' + info.path + ' > /dev/null 2>&1');
htmlPath = info.path + '.html';
result = (await fs.promises.readFile(htmlPath));
result = String(result);
fs.unlink(htmlPath, function(err) {});
fs.unlink(info.path, function(err) {});
$ = cheerio.load(result);
ret = $('body');
ret = formatFilter(ret.html());
await fs.promises.writeFile(cacheDir + '/' + cacheFileName, ret);
} else {
result = (await fs.promises.readFile(cacheDir + '/' + cacheFileName));
ret = String(result);
}
return ret;
};
exports.vimHighlight = vimHighlight;
}).call(this);