forked from marijnh/Eloquent-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparafilter
More file actions
executable file
·32 lines (26 loc) · 869 Bytes
/
parafilter
File metadata and controls
executable file
·32 lines (26 loc) · 869 Bytes
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
#!/usr/bin/env node
var fs = require("fs");
var text = "";
process.stdin.resume();
process.stdin.on("data", function(data) {
text += data.toString("utf8");
});
process.stdin.on("end", function() {
var mark = hash(startAndEnd(toPlainText(text)));
process.stdout.write("<a class=p_ident id=\"p_" + mark + "\" href=\"#p_" + mark + "\"></a>" + text, "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(" ");
}