-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
66 lines (55 loc) · 2.06 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
// Generated by CoffeeScript 1.4.0
/*
vimify - vim2html
Bringing vim syntax highlighting to node!
@author: Johann Philipp Strathausen <[email protected]>
*/
var Tempfile, async, exec, fs, parseHtml, text, vim2html;
exec = require('child_process').exec;
async = require('async');
Tempfile = require('temporary/lib/file');
fs = require('fs');
module.exports = vim2html = function(text, type, cb) {
var codeFile, htmlFilePath, opts;
if ((type.search(/^[a-z-_]+$/i)) === -1) {
return cb(new Error('illegal characters in vimify file type'));
}
codeFile = new Tempfile;
htmlFilePath = codeFile.path + '.html';
opts = ['-n', '-f', '+"set columns=79"', '+"syn on"', '+"let html_use_css=1"', '+"let use_xhtml=1"', '+"set filetype=' + type + '"', '+"run! syntax/2html.vim"', '+"w ' + htmlFilePath + '"', '+"qa!"', codeFile.path];
return async.series({
codeFile: function(cb) {
return codeFile.writeFile(text, 'utf8', cb);
},
vim: async.apply(exec, 'vim ' + (opts.join(' '))),
htmlFile: async.apply(fs.readFile, htmlFilePath, 'utf8'),
delCode: function(cb) {
return codeFile.unlink(cb);
},
delHtml: async.apply(fs.unlink, htmlFilePath)
}, function(err, _arg) {
var html, htmlFile, style, _ref;
htmlFile = _arg.htmlFile;
if (err) {
return cb(err);
}
_ref = parseHtml(htmlFile), style = _ref.style, html = _ref.html;
return cb(null, style, html);
});
};
module.exports.parseHtml = parseHtml = function(rawHtml) {
var a, b, html, parts, style;
parts = rawHtml.split(/<style\stype="text\/css">\n|<\/style>|<pre>\n|<\/pre>\n<\/body>\n<\/html>\n/);
a = parts[0], style = parts[1], b = parts[2], html = parts[3];
style = style.replace(/\n(body|pre)[^\n]*/g, '');
return {
style: style,
html: html
};
};
if (!module.parent) {
text = "vimify = require 'vimify'\nvimify 'x = (y) -> y', 'coffee', (err, style, html) ->\n console.log 'the css', style\n console.log 'the markup', html";
vim2html(text, 'coffee', function(err, style, html) {
return console.log('finished', style, html);
});
}