|
| 1 | +// Decision: rustdoc-compatible examples use `# ` setup lines so doctests can |
| 2 | +// compile while docs hide boilerplate. Astro/Shiki renders those markers, so |
| 3 | +// normalize generated HTML before deploy. |
| 4 | +import { readdirSync, readFileSync, statSync, writeFileSync } from "node:fs"; |
| 5 | +import path from "node:path"; |
| 6 | +import { fileURLToPath } from "node:url"; |
| 7 | + |
| 8 | +const scriptDir = path.dirname(fileURLToPath(import.meta.url)); |
| 9 | +const siteRoot = path.resolve(scriptDir, ".."); |
| 10 | +const distRoot = path.join(siteRoot, "dist"); |
| 11 | +let changedFiles = 0; |
| 12 | +let hiddenLines = 0; |
| 13 | +let remainingHiddenLines = 0; |
| 14 | + |
| 15 | +for (const filePath of htmlFiles(distRoot)) { |
| 16 | + const html = readFileSync(filePath, "utf8"); |
| 17 | + const normalized = normalizeRustdocHtml(html); |
| 18 | + remainingHiddenLines += countRustdocHiddenLines(normalized); |
| 19 | + |
| 20 | + if (normalized !== html) { |
| 21 | + changedFiles += 1; |
| 22 | + writeFileSync(filePath, normalized); |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +if (remainingHiddenLines > 0) { |
| 27 | + throw new Error(`Generated HTML still contains ${remainingHiddenLines} rustdoc hidden line(s).`); |
| 28 | +} |
| 29 | + |
| 30 | +console.log( |
| 31 | + `Normalized ${hiddenLines} rustdoc hidden line(s) in ${changedFiles} generated HTML file(s).`, |
| 32 | +); |
| 33 | + |
| 34 | +function* htmlFiles(dir) { |
| 35 | + for (const name of readdirSync(dir)) { |
| 36 | + const filePath = path.join(dir, name); |
| 37 | + const stats = statSync(filePath); |
| 38 | + |
| 39 | + if (stats.isDirectory()) { |
| 40 | + yield* htmlFiles(filePath); |
| 41 | + continue; |
| 42 | + } |
| 43 | + |
| 44 | + if (name.endsWith(".html")) { |
| 45 | + yield filePath; |
| 46 | + } |
| 47 | + } |
| 48 | +} |
| 49 | + |
| 50 | +function normalizeRustdocHtml(html) { |
| 51 | + return html.replace( |
| 52 | + /(<pre\b[^>]*\bdata-language="(?:rust|rs)"[^>]*><code>)([\s\S]*?)(<\/code><\/pre>)/g, |
| 53 | + (_, open, code, close) => `${open}${normalizeRustdocCode(code)}${close}`, |
| 54 | + ); |
| 55 | +} |
| 56 | + |
| 57 | +function normalizeRustdocCode(code) { |
| 58 | + const lines = code.split(/\n(?=<span class="line">)/); |
| 59 | + const kept = []; |
| 60 | + |
| 61 | + for (const line of lines) { |
| 62 | + const text = visiblePrefix(line); |
| 63 | + const escaped = /^(\s*)##/.exec(text); |
| 64 | + if (escaped) { |
| 65 | + kept.push(removeHashAfterIndent(line, escaped[1].length)); |
| 66 | + continue; |
| 67 | + } |
| 68 | + |
| 69 | + if (/^\s*#(?:\s|$)/.test(text)) { |
| 70 | + hiddenLines += 1; |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + kept.push(line); |
| 75 | + } |
| 76 | + |
| 77 | + return kept.join("\n"); |
| 78 | +} |
| 79 | + |
| 80 | +function countRustdocHiddenLines(html) { |
| 81 | + let count = 0; |
| 82 | + html.replace( |
| 83 | + /<pre\b[^>]*\bdata-language="(?:rust|rs)"[^>]*><code>([\s\S]*?)<\/code><\/pre>/g, |
| 84 | + (_, code) => { |
| 85 | + for (const line of code.split(/\n(?=<span class="line">)/)) { |
| 86 | + const text = visiblePrefix(line); |
| 87 | + if (/^\s*#(?:\s|$)/.test(text)) { |
| 88 | + count += 1; |
| 89 | + } |
| 90 | + } |
| 91 | + return ""; |
| 92 | + }, |
| 93 | + ); |
| 94 | + return count; |
| 95 | +} |
| 96 | + |
| 97 | +function visiblePrefix(line) { |
| 98 | + let prefix = ""; |
| 99 | + for (const match of line.matchAll(/>([^<]*)/g)) { |
| 100 | + prefix += match[1]; |
| 101 | + if (/^\s*##/.test(prefix) || /^\s*#(?:\s|$)/.test(prefix)) { |
| 102 | + return prefix; |
| 103 | + } |
| 104 | + if (prefix.trimStart().length > 0) { |
| 105 | + return prefix; |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + return prefix; |
| 110 | +} |
| 111 | + |
| 112 | +function removeHashAfterIndent(html, indentLength) { |
| 113 | + let remaining = indentLength; |
| 114 | + let removed = false; |
| 115 | + |
| 116 | + return html.replace(/(>)([^<]*)/g, (match, close, text) => { |
| 117 | + if (removed) { |
| 118 | + return match; |
| 119 | + } |
| 120 | + |
| 121 | + if (remaining >= text.length) { |
| 122 | + remaining -= text.length; |
| 123 | + return match; |
| 124 | + } |
| 125 | + |
| 126 | + removed = true; |
| 127 | + return `${close}${text.slice(0, remaining)}${text.slice(remaining + 1)}`; |
| 128 | + }); |
| 129 | +} |
0 commit comments