-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmake-doc.js
executable file
·146 lines (126 loc) · 3.62 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
#!/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"
const PRE_START = "<pre>";
const PRE_END = "</pre>";
const PRE_CURLY_START = "<pre>{`"
const PRE_CURLY_END = "`}</pre>"
var body = []
var toc = []
const destinationPath = path.join(
__dirname,
'/src/pages/docs/',
versionArg + '.js')
function replaceCodeSnippetWord(str) {
return str.replaceAll("\\n", "\\\\n")
.replaceAll("\\0", "\\\\0")
.replaceAll(">", ">")
.replaceAll("<", "<");
}
async function processAdoc() {
const fileStream = createReadStream(filePath);
const rl = createInterface({
input: fileStream,
crlfDelay: Infinity,
});
var insideToc = false;
var insideBody = false;
var insidePre = false;
for await (var line of rl) {
if (line.includes("<ul class=\"sectlevel1\">")) {
insideToc = true;
toc.push("<ul className=\"sectlevel1\">");
continue;
}
if (line.includes("<div id=\"content\">")) {
insideBody = true;
}
if (insideToc) {
toc.push(line);
if (line.includes("</ul>")) {
insideToc = false;
}
continue;
}
if (insideBody) {
if (line.includes("<div id=\"footer\">")) {
break;
}
if (line.includes("<pre")) {
if (line.includes(PRE_END)) {
body.push(
replaceCodeSnippetWord(line)
.replace(PRE_START, PRE_CURLY_START)
.replace(PRE_END, PRE_CURLY_END)
);
continue;
}
insidePre = true;
// For cases like <pre className="highlight"><code language="c++">
let idx = line.lastIndexOf('>');
line = PRE_CURLY_START + line.substring(idx + 1);
}
if (line.startsWith("<col ") || line === "<col>") {
body.push("<col />")
} else {
if (insidePre) {
if (line.includes(PRE_END)) {
insidePre = false;
line = line.replace(PRE_END, PRE_CURLY_END)
.replace("</code>", "");
}
body.push(replaceCodeSnippetWord(line));
} 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();