-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathmake-doc.js
More file actions
executable file
·208 lines (176 loc) · 5.11 KB
/
Copy pathmake-doc.js
File metadata and controls
executable file
·208 lines (176 loc) · 5.11 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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
#!/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.24` - to make docs for version 0.24
* `./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 versionFolder = hasVersion ? ("release_" + arguments[3].replaceAll(".", "")) : "pre-release"
const PRE_START = "<pre>";
const PRE_END = "</pre>";
const PRE_CURLY_START = "<pre>{`"
const PRE_CURLY_END = "`}</pre>"
var body = []
var toc = []
function replaceCodeSnippetWord(str) {
return str.replaceAll("\\n", "\\\\n")
.replaceAll("\\0", "\\\\0")
.replaceAll(">", ">")
.replaceAll("<", "<");
}
async function processAdoc() {
const destinationPath = path.join(
__dirname,
'/src/pages/docs/',
versionFolder + '/cli.js');
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=\"table-of-contents table-of-contents__left-border\">");
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=\"")
.replaceAll("https://github.com/bpftrace/bpftrace/blob/master/docs/language.md", "language")
.replaceAll("https://github.com/bpftrace/bpftrace/blob/master/docs/stdlib.md", "stdlib")
);
}
}
}
}
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>The Command Line Tool (" + 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);
});
});
}
async function processMarkdownDoc(filename) {
const filePath = path.join(
__dirname,
'/bpftrace/docs/',
filename);
const destinationPath = path.join(
__dirname,
'/src/pages/docs/',
versionFolder,
'/',
filename)
const fileStream = createReadStream(filePath);
const rl = createInterface({
input: fileStream,
crlfDelay: Infinity,
});
const newLines = [];
let firstLine = true;
for await (var line of rl) {
if (firstLine) {
newLines.push(line + ' (' + versionArg + ')');
firstLine = false;
continue;
}
newLines.push(
line
.replaceAll("../man/adoc/bpftrace.adoc", "cli")
.replaceAll("stdlib.md", "stdlib")
.replaceAll("language.md", "language")
);
}
fs.writeFile(destinationPath, newLines.join('\n'), err => {
if (err) {
console.error("Error writing to path: ", destinationPath);
console.error(err);
return;
}
console.log("Success.");
console.log("Wrote: ", destinationPath);
});
}
const folderPath = path.join(__dirname, '/src/pages/docs/', versionFolder);
if (!fs.existsSync(folderPath)) {
fs.mkdirSync(folderPath, { recursive: true });
}
processAdoc();
processMarkdownDoc("language.md");
processMarkdownDoc("stdlib.md");