|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +/* |
| 4 | + * This script takes an html file, specifically generated by asciidoctor |
| 5 | + * via this command: |
| 6 | + * `asciidoctor man/adoc/bpftrace.adoc -b html5 -o adoc.html` |
| 7 | + * and transforms it into a js file for the bpftrace website's docs section. |
| 8 | + * |
| 9 | + * Usage: |
| 10 | + * `./make-doc.js adoc.html 0.22` - to make docs from version 0.22 |
| 11 | + * `./make-doc.js adoc.html` - to make the unreleased/master docs |
| 12 | + */ |
| 13 | + |
| 14 | +var fs = require('fs') |
| 15 | +var path = require('path') |
| 16 | +const { createReadStream } = require('node:fs') |
| 17 | +const { createInterface } = require('node:readline') |
| 18 | +const templatePath = path.join(__dirname, '/src/pages/docs/__template.js') |
| 19 | + |
| 20 | +const arguments = process.argv |
| 21 | + |
| 22 | +if (arguments.length < 3) { |
| 23 | + console.error("Need a adoc html file path e.g. adoc.html"); |
| 24 | + process.exit(1); |
| 25 | +} |
| 26 | + |
| 27 | +const filePath = arguments[2] |
| 28 | +const hasVersion = arguments.length == 4 |
| 29 | +const versionArg = hasVersion ? arguments[3] : "pre-release" |
| 30 | + |
| 31 | +var body = [] |
| 32 | +var toc = [] |
| 33 | +const destinationPath = path.join( |
| 34 | + __dirname, |
| 35 | + '/src/pages/docs/', |
| 36 | + versionArg + '.js') |
| 37 | + |
| 38 | +async function processAdoc() { |
| 39 | + const fileStream = createReadStream(filePath); |
| 40 | + |
| 41 | + const rl = createInterface({ |
| 42 | + input: fileStream, |
| 43 | + crlfDelay: Infinity, |
| 44 | + }); |
| 45 | + |
| 46 | + var startToc = false; |
| 47 | + var startBody = false; |
| 48 | + |
| 49 | + for await (const line of rl) { |
| 50 | + if (line.includes("<ul class=\"sectlevel1\">")) { |
| 51 | + startToc = true; |
| 52 | + toc.push("<ul className=\"sectlevel1\">"); |
| 53 | + continue; |
| 54 | + } |
| 55 | + if (line.includes("<div id=\"content\">")) { |
| 56 | + startBody = true; |
| 57 | + } |
| 58 | + if (startToc) { |
| 59 | + toc.push(line); |
| 60 | + if (line.includes("</ul>")) { |
| 61 | + startToc = false; |
| 62 | + } |
| 63 | + continue; |
| 64 | + } |
| 65 | + if (startBody) { |
| 66 | + if (line.includes("<div id=\"footer\">")) { |
| 67 | + break; |
| 68 | + } |
| 69 | + if (line.startsWith("<col ") || line === "<col>") { |
| 70 | + body.push("<col />") |
| 71 | + } else { |
| 72 | + body.push( |
| 73 | + line.replace(/<br>/ig, "<br />") |
| 74 | + .replace(/{/ig, "{") |
| 75 | + .replace(/}/ig, "}") |
| 76 | + .replace(/class="/ig, "className=\"") |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + fs.readFile(templatePath, {encoding: 'utf-8'}, function(err, data){ |
| 84 | + if (err) { |
| 85 | + console.error("Error reading js template at path: ", templatePath); |
| 86 | + console.error(err); |
| 87 | + return; |
| 88 | + } |
| 89 | + |
| 90 | + const versionHeader = "<h1> Version: " + versionArg + "</h1>" |
| 91 | + var versionPage = data.replace("<div id=\"version-content\" />", versionHeader) |
| 92 | + .replace("<div id=\"body-content\" />", body.join("\n")) |
| 93 | + .replace("<div id=\"toc-content\" />", toc.join("\n")) |
| 94 | + |
| 95 | + fs.writeFile(destinationPath, versionPage, err => { |
| 96 | + if (err) { |
| 97 | + console.error("Error writing new version doc to path: ", destinationPath); |
| 98 | + console.error(err); |
| 99 | + return; |
| 100 | + } |
| 101 | + |
| 102 | + console.log("Success."); |
| 103 | + console.log("Wrote: ", destinationPath); |
| 104 | + }); |
| 105 | + }); |
| 106 | +} |
| 107 | + |
| 108 | +processAdoc(); |
0 commit comments