forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblockfilter
More file actions
executable file
·88 lines (75 loc) · 2.35 KB
/
blockfilter
File metadata and controls
executable file
·88 lines (75 loc) · 2.35 KB
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
#!/usr/bin/env node
var fs = require("fs");
var text = "";
var addBookmark = process.argv.indexOf("--bookmark") > -1;
process.stdin.resume();
process.stdin.on("data", function(data) {
text += data.toString("utf8");
});
process.stdin.on("end", function() {
var pre = "";
if (addBookmark) {
var mark = hash(toPlainText(text));
pre = "<a class=c_ident id=\"c_" + mark + "\" href=\"#c_" + mark + "\"></a>";
}
var out = highlight(text, process.argv[process.argv.indexOf("-s") + 1].toLowerCase())
process.stdout.write(pre + out, "utf8");
});
function hash(string) {
var sum = require("crypto").createHash("sha1");
sum.update(string);
return sum.digest("base64").slice(0, 10);
}
function toPlainText(text) {
return text.replace(/<[^>]+>|&[^;]+;/g, "");
}
function startAndEnd(text) {
var words = text.split(/\W+/);
if (!words[0]) words.shift();
if (!words[words.length - 1]) words.pop();
if (words.length <= 6) return words.join(" ");
return words.slice(0, 3).join(" ") + " " + words.slice(words.length - 3).join(" ");
}
CodeMirror = require("../node_modules/codemirror/addon/runmode/runmode.node.js");
require("../node_modules/codemirror/mode/meta.js");
function ensureMode(name) {
if (CodeMirror.modes[name] || name == "null") return;
try {
require("../node_modules/codemirror/mode/" + name + "/" + name + ".js");
} catch(e) {
console.error("Could not load mode " + name + ".");
process.exit(1);
}
var obj = CodeMirror.modes[name];
if (obj.dependencies) obj.dependencies.forEach(ensureMode);
}
function esc(str) {
return str.replace(/[<&]/g, function(ch) { return ch == "&" ? "&" : "<"; });
}
function highlight(code, lang) {
var modeName = lang;
CodeMirror.modeInfo.forEach(function(info) {
if (info.mime == lang) {
modeName = info.mode;
} else if (info.name.toLowerCase() == lang) {
modeName = info.mode;
lang = info.mime;
}
});
ensureMode(modeName);
var curStyle = null, accum = "", output = "";
function flush() {
if (curStyle) output += "<span class=\"" + curStyle.replace(/(^|\s+)/g, "$1cm-") + "\">" + esc(accum) + "</span>";
else output += esc(accum);
}
CodeMirror.runMode(code, lang, function(text, style) {
if (style != curStyle) {
flush();
curStyle = style; accum = text;
} else {
accum += text;
}
});
flush();
return output;
}