Skip to content

Commit

Permalink
Process documentation with Handlebars
Browse files Browse the repository at this point in the history
  • Loading branch information
chances committed Feb 18, 2024
1 parent 783e578 commit 2230d33
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 6 deletions.
5 changes: 4 additions & 1 deletion dub.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
],
"buildOptions": ["syntaxOnly"],
"dflags": ["-Dddocs"],
"postBuildCommands-posix": ["cp views/index.html docs"]
"postBuildCommands-posix": [
"cp views/index.html docs",
"node scripts/docs.js"
]
}
},
"configurations": [
Expand Down
39 changes: 39 additions & 0 deletions scripts/docs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import Handlebars from "handlebars"
import { JSDOM } from "jsdom"
import { readFileSync, writeFileSync } from "node:fs"
import * as path from "node:path"
import * as util from "node:util"

const shell = {
exec: await (async () => util.promisify((await import('node:child_process')).exec))()
}

const controller = new AbortController();

async function sh(cmdAndArgs) {
if (cmdAndArgs.length === 0 || cmdAndArgs[0].length === 0) throw new Error("Expected a command.")

const { stdout, stderr } = await shell.exec(cmdAndArgs.join("").split(" ").filter(x => x.length).join(" "), { signal: controller.signal });
if (stderr && stderr.length) console.error(stderr);
return stdout?.trim();
}

const files = (await sh`find docs -name '*.html'`).split("\n").map(doc => doc.trim())

const DUB_VERSION = await sh`git describe --tags --abbrev=0`
// TODO: Calculate tree of symbols
const SYMBOLS = []
const MODULES = files.filter(file => file.includes("docs/index.html") === false).map(file => ({
name: path.basename(file, ".html"),
file: path.basename(file)
}))
const constants = {
DUB_VERSION,
SYMBOLS,
MODULES
}

files.forEach(file => {
const template = Handlebars.compile(readFileSync(file).toString(), { strict: true })
writeFileSync(file, template(constants))
})
6 changes: 4 additions & 2 deletions views/docs.ddoc
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ DDOC=<!DOCTYPE html>
<main>
<a id="downloadButton" href="https://code.dlang.org/packages/playdate-d">
<div class="bs-hbtn right black">
<div class="content">get <strong>playdate-d</strong><br><large>{{ DUB_VERSION }}</large></div>
<div class="content">get <strong>playdate</strong><br><large>{{ DUB_VERSION }}</large></div>
</div><div class="bs-hbtn left red">
<div class="dub-logo"></div>
</div>
Expand All @@ -42,7 +42,9 @@ DDOC=<!DOCTYPE html>
</li>
</ul>
<h2>API Reference</h2>
<ul>{{ MODULE_TREE }}</ul>
<ul id="symbols">{{#each array}}
<li>{{ this }}</li>
{{/each}}</ul>
</nav>
</aside>
<article class="module">
Expand Down
13 changes: 10 additions & 3 deletions views/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<main>
<a id="downloadButton" href="https://code.dlang.org/packages/playdate-d">
<div class="bs-hbtn right black">
<div class="content">get <strong>playdate-d</strong><br><large>{{ DUB_VERSION }}</large></div>
<div class="content">get <strong>playdate</strong><br><large>{{ DUB_VERSION }}</large></div>
</div><div class="bs-hbtn left red">
<div class="dub-logo"></div>
</div>
Expand All @@ -36,7 +36,12 @@ <h1>
</li>
</ul>
<h2>API Reference</h2>
<ul>{{ MODULE_TREE }}</ul>
<form id="symbolSearchForm" action="#" method="GET">
<input id="symbolSearch" type="text" name="q" placeholder="Search for symbols" autocomplete="off" onchange="performSymbolSearch(40);" onkeypress="this.onchange();" onpaste="this.onchange();" oninput="this.onchange();" tabindex="1000">
</form>
<ul id="symbols">{{#each SYMBOLS}}
<li>{{ this }}</li>
{{/each}}</ul>
</nav>
</aside>
<article>
Expand Down Expand Up @@ -71,11 +76,13 @@ <h2>Examples</h2>
<h3>Shapes</h3>
<pre>dub run playdate:shapes</pre>
<h2>Modules</h2>
{{ MODULE_LIST }}
{{#each MODULES}}<li><a href="{{ this.file }}">{{ this.name }}</a></li>{{/each}}
</article>
</main>
<footer>
<a href="https://github.com/chances/playdate-d#readme">GitHub</a>
<span class="faint" style="float: right;">Generated using <a href="https://dlang.org/spec/ddoc.html">Ddoc</a></span>
</footer>
<script type="application/javascript">const symbols = [{{#each SYMBOLS}}{{ this }},{{/each}}]</script>
<script type="application/javascript" src="scripts/symbols.js"></script>
</body>

0 comments on commit 2230d33

Please sign in to comment.