Skip to content

Commit 2d23b42

Browse files
committed
docs: start refgen v2
1 parent 1ae54ba commit 2d23b42

24 files changed

Lines changed: 402 additions & 165 deletions

.lute/refgen.luau

Lines changed: 0 additions & 118 deletions
This file was deleted.

.lute/refgen/filepaths.luau

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
local docs = require("@config/docs")
2+
local path = require("@std/path")
3+
4+
return {
5+
libraries = path.join(docs.directory, docs.documentationPaths.libraries),
6+
mechanics = path.join(docs.directory, docs.documentationPaths.mechanics),
7+
}

.lute/refgen/init.luau

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
local pp = require("@batteries/pp")
2+
local docs = require("@config/docs")
3+
local project = require("@config/project")
4+
local clearDirectory = require("@scripts/lib/clearDirectory")
5+
local text = require("@scripts/lib/text")
6+
local filepaths = require("@self/filepaths")
7+
local moonwaveModel = require("@self/moonwaveModel")
8+
local process = require("@std/process")
9+
local json = require("@std/json")
10+
local moonwave = require("@self/moonwave")
11+
local templates = require("@self/templates")
12+
local fs = require("@std/fs")
13+
local path = require("@std/path")
14+
15+
local raw = process.run({
16+
"moonwave-extractor",
17+
"extract",
18+
project.source,
19+
}).stdout
20+
21+
local model = moonwaveModel(json.deserialize(raw) :: moonwave.Extracted)
22+
23+
clearDirectory(filepaths.libraries)
24+
25+
local libraryNav: { { [string]: { string } } } = {}
26+
27+
for _, library in model.libraries do
28+
local libraryContents = templates.Library:gsub("{(.-)}", library :: { [string]: string })
29+
30+
local hyperlink = model.nameToHyperlinks[library.name]
31+
model.nameToHyperlinks[library.name] = nil
32+
libraryContents = libraryContents:gsub("%w*", model.nameToHyperlinks)
33+
model.nameToHyperlinks[library.name] = hyperlink
34+
35+
local index = path.join(library.outputDirectory, "index.md")
36+
print("Writing library", library.name)
37+
fs.createdirectory(library.outputDirectory, { makeparents = true })
38+
fs.writestringtofile(index, libraryContents)
39+
40+
table.insert(libraryNav, { [library.name] = { path.format(index) } })
41+
end
42+
43+
local zensical = fs.readfiletostring(docs.zensical)
44+
zensical = text.replaceSectionedBlock(
45+
zensical,
46+
-- json is close enough lmao
47+
-- + toml breaks anyway
48+
json.serialize(libraryNav :: any):gsub(":", "="),
49+
"# refgen: libraries start",
50+
"# refgen: libraries end"
51+
)
52+
fs.writestringtofile(docs.zensical, zensical)

.lute/refgen/moonwaveModel.luau

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
local docs = require("@config/docs")
2+
local project = require("@config/project")
3+
local text = require("@scripts/lib/text")
4+
local reconstructRequirePath = require("./reconstructRequirePath")
5+
local sourcemap = require("./sourcemap")
6+
local path = require("@std/path")
7+
local filepaths = require("./filepaths")
8+
local moonwave = require("./moonwave")
9+
10+
export type Library = {
11+
name: string,
12+
slug: string,
13+
summary: string,
14+
description: string,
15+
16+
sourcePath: string,
17+
sourceLine: number,
18+
importPath: string,
19+
20+
outputDirectory: path.path,
21+
outputUrl: path.path,
22+
}
23+
24+
export type Mechanic = {}
25+
26+
export type Model = {
27+
libraries: { Library },
28+
mechanics: { Mechanic },
29+
sourcemapPaths: { [string]: { string } },
30+
nameToUrls: { [string]: string },
31+
nameToHyperlinks: { [string]: string },
32+
}
33+
34+
local function modelLibrary(model: Model, library: moonwave.Class): Library
35+
local sourcePath = project.source .. "/" .. library.source.path
36+
local slug = string.lower(library.name)
37+
local url = path.join(docs.documentationPaths.libraries, slug)
38+
model.nameToUrls[library.name] = path.format(url)
39+
model.nameToHyperlinks[library.name] = `<a href={text.quote("/" .. path.format(url))}>{library.name}</a>`
40+
41+
return {
42+
name = library.name,
43+
slug = slug,
44+
summary = string.match(library.desc, "^[^\n]+") or "",
45+
description = library.desc,
46+
47+
sourcePath = sourcePath,
48+
sourceLine = library.source.line,
49+
importPath = table.concat(
50+
reconstructRequirePath(model.sourcemapPaths, sourcePath)
51+
or error(`library {library.source.path} cannot be imported`),
52+
"/"
53+
),
54+
55+
outputDirectory = path.join(filepaths.libraries, slug),
56+
outputUrl = url,
57+
}
58+
end
59+
60+
local function moonwaveModel(classes: { moonwave.Class })
61+
local model: Model = {
62+
libraries = {},
63+
mechanics = {},
64+
sourcemapPaths = sourcemap.process(),
65+
nameToUrls = {},
66+
nameToHyperlinks = {},
67+
}
68+
69+
for _, class in ipairs(classes) do
70+
local tags: { string } = class.tags or {}
71+
72+
for _, tag in tags do
73+
if tag == "Library" then
74+
table.insert(model.libraries, modelLibrary(model, class))
75+
break
76+
end
77+
end
78+
end
79+
80+
return model
81+
end
82+
83+
return moonwaveModel
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
local path = require("@std/path")
2+
3+
local function reconstructRequirePath(sourcemapPaths: { [string]: { string } }, inputPath: string): { string }?
4+
local bestSource, bestLen = "", 0
5+
for sourcePath in sourcemapPaths do
6+
if string.find(inputPath, "^" .. sourcePath) then
7+
local len = string.len(sourcePath)
8+
if bestLen < len then
9+
bestSource, bestLen = sourcePath, len
10+
end
11+
end
12+
end
13+
14+
if bestLen == 0 then
15+
return nil
16+
end
17+
18+
local requirePath = table.clone(sourcemapPaths[bestSource])
19+
if requirePath[1] == "game" then
20+
requirePath[1] = "@game"
21+
end
22+
23+
local sourceParts = path.parse(bestSource).parts
24+
local parts = path.parse(inputPath).parts
25+
local lenParts = #parts
26+
parts[lenParts] = string.match(parts[lenParts], "(.+)%..+$")
27+
table.move(parts, #sourceParts + 1, #parts, #requirePath + 1, requirePath)
28+
return requirePath
29+
end
30+
31+
return reconstructRequirePath

.lute/refgen/sourcemap.luau

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
local project = require("@config/project")
2+
local fs = require("@std/fs")
3+
local json = require("@std/json")
4+
5+
type ProjectPath = string | { optional: string }
6+
type ProjectNode = { [string]: ProjectNode, ["$path"]: ProjectPath? }
7+
type Project = { tree: ProjectNode }
8+
9+
local BLACKLISTED_NAMES = {
10+
["$className"] = true,
11+
["$path"] = true,
12+
["$properties"] = true,
13+
}
14+
15+
local function processNode(node: ProjectNode, paths: { [string]: { string } }, currentPath: { string })
16+
local nodePath = node["$path"]
17+
if nodePath then
18+
nodePath = type(nodePath) == "string" and nodePath or nodePath.optional
19+
paths[nodePath] = table.clone(currentPath)
20+
end
21+
22+
for name, child in node do
23+
if not BLACKLISTED_NAMES[name] and type(child) == "table" then
24+
local newPath = table.clone(currentPath)
25+
table.insert(newPath, name)
26+
processNode(child, paths, newPath)
27+
end
28+
end
29+
end
30+
31+
local function process()
32+
local sourcemapProject = json.deserialize(fs.readfiletostring(project.rojoProjects.sourcemap)) :: Project
33+
local sourcemapPaths = {}
34+
processNode(sourcemapProject.tree, sourcemapPaths, { "game" })
35+
return sourcemapPaths
36+
end
37+
38+
return {
39+
processNode = processNode,
40+
process = process,
41+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
return [[<div class="wth-api-item">
2+
<span class="wth-api-symbol">{apiSymbol}</span>
3+
<a href="{url}" class="wth-api-content">
4+
<span class="wth-api-header">{name}</span>
5+
<p>{summary}</p>
6+
</a>
7+
<div>]]
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
return [[<div class="wth-api-list">
2+
{api}
3+
</div>]]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
return [[{header}
2+
3+
# <span class="wth-ref-header-function">function</span> {name}
4+
5+
```luau
6+
{signature}
7+
```
8+
9+
{desc}
10+
11+
## Parameters
12+
13+
{parameters}
14+
15+
## Returns
16+
17+
{returns}
18+
19+
]]

0 commit comments

Comments
 (0)