|
| 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 |
0 commit comments