1- local fs = require ("@lute/fs" )
2- local process = require ("@lute/process" )
3-
4- local function projectRelative (... )
5- local cwd = process .cwd ()
6- local pathSep = if string.find (string.lower (require ("@lute/system" ).os ), "windows" ) then "\\ " else "/"
7- return table.concat ({ cwd , ".." , ... }, pathSep )
8- end
1+ local fs = require ("@std/fs" )
2+ local process = require ("@std/process" )
3+ local path = require ("@std/path" )
94
105local function extractPropertySignature (module : string , line : string ): (string ? , string ? )
116 -- Match property declarations like: process.env = {} :: { [string]: string }
@@ -26,13 +21,18 @@ local function extractPropertySignature(module: string, line: string): (string?,
2621 return nil , nil
2722end
2823
29- local function parseDefinitionFile (module : string , filePath : string ): { { name : string , signature : string } }
24+ -- docs for definitions/*.luau --
25+
26+ local function parseDefinitionFile (
27+ module : path .pathlike ,
28+ filePath : path .pathlike
29+ ): { { name : string , signature : string } }
3030 local content = fs .readfiletostring (filePath )
3131 local lines = string.split (content , "\n " )
3232 local definitions = {}
3333
3434 for _ , line in lines do
35- local name , signature = extractPropertySignature (module , line )
35+ local name , signature = extractPropertySignature (tostring ( module ) , line )
3636
3737 if name and signature then
3838 table.insert (definitions , {
@@ -45,12 +45,109 @@ local function parseDefinitionFile(module: string, filePath: string): { { name:
4545 return definitions
4646end
4747
48- local function generateMarkdown (moduleName : string , definitions : { { name : string , signature : string } }): string
48+ local function generateDefinitionsMarkdown (
49+ moduleName : string ,
50+ definitions : { { name : string , signature : string } }
51+ ): string
52+ local lines = {
53+ `# {moduleName }` ,
54+ "" ,
55+ "```luau" ,
56+ `local {moduleName } = require("@lute/{moduleName }")` ,
57+ "```" ,
58+ "" ,
59+ }
60+
61+ table.sort (definitions , function (a , b )
62+ return a .name < b .name
63+ end )
64+
65+ for _ , def in definitions do
66+ table.insert (lines , `## {moduleName }.{def .name }` )
67+ table.insert (lines , "```luau" )
68+ table.insert (lines , def .signature )
69+ table.insert (lines , "```" )
70+ table.insert (lines , "" )
71+ end
72+
73+ return table.concat (lines , "\n " )
74+ end
75+
76+ local function processDefinitionsDir (definitionsPath : path .pathlike , referencePath : path .pathlike )
77+ fs .createdirectory (referencePath , { makeparents = true })
78+
79+ local definitionFiles = fs .listdir (definitionsPath )
80+
81+ for _ , file in definitionFiles do
82+ if file .type == "file" and string.find (file .name , "%.luau$" ) then
83+ local moduleName = string.gsub (file .name , "%.luau$" , "" )
84+ local definitionFilePath = path .join (definitionsPath , file .name )
85+ local referenceFilePath = path .join (referencePath , moduleName .. ".md" )
86+
87+ print (`Processing {moduleName }...` )
88+
89+ local definitions = parseDefinitionFile (moduleName , definitionFilePath )
90+ local markdown = generateDefinitionsMarkdown (moduleName , definitions )
91+
92+ fs .writestringtofile (referenceFilePath , markdown )
93+
94+ print (`Generated {tostring (referenceFilePath )}` )
95+ end
96+ end
97+ end
98+
99+ -- docs for lute/std/libs/*.luau --
100+
101+ local STDLIB_MODULE_ALIASES = {
102+ assert = "assertions" ,
103+ fs = "fslib" ,
104+ io = "iolib" ,
105+ process = "processlib" ,
106+ task = "tasklib" ,
107+ path = "pathlib" ,
108+ system = "systemlib" ,
109+ }
110+
111+ local function parseStdlibFile (module : path .pathlike , filePath : path .pathlike ): { { name : string , signature : string } }
112+ local content = fs .readfiletostring (filePath )
113+ local lines = string.split (content , "\n " )
114+ local stdlibDefinitions = {}
115+
116+ for _ , line in lines do
117+ local mod = STDLIB_MODULE_ALIASES [tostring (module )] or tostring (module )
118+
119+ local name , signature = extractPropertySignature (mod , line )
120+
121+ if name and signature then
122+ table.insert (stdlibDefinitions , {
123+ name = name ,
124+ signature = signature ,
125+ })
126+ end
127+ end
128+
129+ return stdlibDefinitions
130+ end
131+
132+ local function generateStdLibMarkdown (
133+ moduleName : string ,
134+ definitions : { { name : string , signature : string } },
135+ stdlibFilePath : path .pathlike
136+ ): string
137+ local referencePath = path .join (process .cwd (), ".." , "lute" , "std" , "libs" )
138+
139+ local refPathStr = tostring (referencePath )
140+ local stdlibFilePathStr = tostring (stdlibFilePath )
141+
142+ local relativePath = string.sub (stdlibFilePathStr , # refPathStr + 2 ) -- gets the path relative to std/libs for the require path
143+ relativePath = string.gsub (relativePath , "%.luau$" , "" )
144+ local requirePath = string.gsub (relativePath , "/init$" , "" )
145+
49146 local lines = {
50- "# " .. moduleName ,
147+ `# { moduleName }` ,
51148 "" ,
52149 "```luau" ,
53- " local " .. moduleName .. ' = require("@lute/' .. moduleName .. '")' ,
150+ ` local { moduleName } = require("@std/{ requirePath }")` ,
54151 "```" ,
55152 "" ,
56153 }
@@ -60,7 +157,7 @@ local function generateMarkdown(moduleName: string, definitions: { { name: strin
60157 end )
61158
62159 for _ , def in definitions do
63- table.insert (lines , " ## " .. def .name )
160+ table.insert (lines , ` ## { moduleName }.{ def .name }` )
64161 table.insert (lines , "```luau" )
65162 table.insert (lines , def .signature )
66163 table.insert (lines , "```" )
@@ -70,28 +167,44 @@ local function generateMarkdown(moduleName: string, definitions: { { name: strin
70167 return table.concat (lines , "\n " )
71168end
72169
73- local definitionsPath = projectRelative ("definitions" )
74- local referencePath = projectRelative ("docs" , "reference" )
170+ local function processStdlibDir (sourceDir : path .pathlike , referenceDir : path .pathlike )
171+ fs .createdirectory (referenceDir , { makeparents = true })
172+
173+ local entries = fs .listdir (sourceDir )
75174
76- pcall (fs .mkdir , referencePath )
175+ for _ , entry in entries do
176+ local sourcePath = path .join (sourceDir , entry .name )
177+ local referencePath = path .join (referenceDir , entry .name )
77178
78- local definitionFiles = fs .listdir (definitionsPath )
179+ if entry .type == "dir" then
180+ fs .createdirectory (referencePath , { makeparents = true })
181+ processStdlibDir (sourcePath , referencePath )
182+ elseif entry .type == "file" and string.find (entry .name , "%.luau$" ) then
183+ local moduleName = string.gsub (entry .name , "%.luau$" , "" )
184+ if moduleName == "init" then
185+ moduleName = string.match (tostring (sourceDir ), "([^/\\ ]+)$" ) or moduleName
186+ end
79187
80- for _ , file in definitionFiles do
81- if file .type == "file" and string.find (file .name , "%.luau$" ) then
82- local moduleName = string.gsub (file .name , "%.luau$" , "" )
83- local definitionFilePath = definitionsPath .. "/" .. file .name
84- local referenceFilePath = referencePath .. "/" .. moduleName .. ".md"
188+ local referenceFilePath = path .join (referenceDir , moduleName .. ".md" )
85189
86- print (" Processing " .. moduleName .. "..." )
190+ print (` Processing { moduleName }...` )
87191
88- local definitions = parseDefinitionFile (moduleName , definitionFilePath )
89- local markdown = generateMarkdown (moduleName , definitions )
192+ local stdlibDefinitions = parseStdlibFile (moduleName , sourcePath )
193+ local markdown = generateStdLibMarkdown (moduleName , stdlibDefinitions , sourcePath )
90194
91- fs .writestringtofile (referenceFilePath , markdown )
195+ fs .writestringtofile (referenceFilePath , markdown )
92196
93- print ("Generated " .. referenceFilePath )
197+ print (`Generated {tostring (referenceFilePath )}` )
198+ end
94199 end
95200end
96201
202+ local definitionsPath = path .join (process .cwd (), ".." , "definitions" )
203+ local referencePath = path .join (process .cwd (), "reference" , "definitions" )
204+ processDefinitionsDir (definitionsPath , referencePath )
205+
206+ local stdlibPath = path .join (process .cwd (), ".." , "lute" , "std" , "libs" )
207+ local stdlibReferencePath = path .join (process .cwd (), "reference" , "std" )
208+ processStdlibDir (stdlibPath , stdlibReferencePath )
209+
97210print ("Reference documentation generation complete!" )
0 commit comments