forked from bpftrace/website
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmake-doc.js
executable file
·108 lines (92 loc) · 2.64 KB
/
make-doc.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
#!/usr/bin/env node
/*
* This script takes an html file, specifically generated by asciidoctor
* via this command:
* `asciidoctor man/adoc/bpftrace.adoc -b html5 -o adoc.html`
* and transforms it into a js file for the bpftrace website's docs section.
*
* Usage:
* `./make-doc.js adoc.html 0.22` - to make docs from version 0.22
* `./make-doc.js adoc.html` - to make the unreleased/master docs
*/
var fs = require('fs')
var path = require('path')
const { createReadStream } = require('node:fs')
const { createInterface } = require('node:readline')
const templatePath = path.join(__dirname, '/src/pages/docs/__template.js')
const arguments = process.argv
if (arguments.length < 3) {
console.error("Need a adoc html file path e.g. adoc.html");
process.exit(1);
}
const filePath = arguments[2]
const hasVersion = arguments.length == 4
const versionArg = hasVersion ? arguments[3] : "pre-release"
var body = []
var toc = []
const destinationPath = path.join(
__dirname,
'/src/pages/docs/',
versionArg + '.js')
async function processAdoc() {
const fileStream = createReadStream(filePath);
const rl = createInterface({
input: fileStream,
crlfDelay: Infinity,
});
var startToc = false;
var startBody = false;
for await (const line of rl) {
if (line.includes("<ul class=\"sectlevel1\">")) {
startToc = true;
toc.push("<ul className=\"sectlevel1\">");
continue;
}
if (line.includes("<div id=\"content\">")) {
startBody = true;
}
if (startToc) {
toc.push(line);
if (line.includes("</ul>")) {
startToc = false;
}
continue;
}
if (startBody) {
if (line.includes("<div id=\"footer\">")) {
break;
}
if (line.startsWith("<col ") || line === "<col>") {
body.push("<col />")
} else {
body.push(
line.replace(/<br>/ig, "<br />")
.replace(/{/ig, "{")
.replace(/}/ig, "}")
.replace(/class="/ig, "className=\"")
);
}
}
}
fs.readFile(templatePath, {encoding: 'utf-8'}, function(err, data){
if (err) {
console.error("Error reading js template at path: ", templatePath);
console.error(err);
return;
}
const versionHeader = "<h1>Version: " + versionArg + "</h1>"
var versionPage = data.replace("<div id=\"version-content\" />", versionHeader)
.replace("<div id=\"body-content\" />", body.join("\n"))
.replace("<div id=\"toc-content\" />", toc.join("\n"))
fs.writeFile(destinationPath, versionPage, err => {
if (err) {
console.error("Error writing new version doc to path: ", destinationPath);
console.error(err);
return;
}
console.log("Success.");
console.log("Wrote: ", destinationPath);
});
});
}
processAdoc();