bookshelf-doc/Main.lean

93 lines
2.8 KiB
Plaintext
Raw Normal View History

2021-11-27 15:19:56 +00:00
import DocGen4
import Lean
2022-02-23 21:54:10 +00:00
import Cli
2021-11-27 15:19:56 +00:00
2022-02-23 21:54:10 +00:00
open DocGen4 Lean Cli
2021-11-27 15:19:56 +00:00
def findLeanInk? (p : Parsed) : IO (Option System.FilePath) := do
match p.flag? "ink" with
| some ink =>
let inkPath := System.FilePath.mk ink.value
if ←inkPath.pathExists then
2022-07-23 11:01:25 +00:00
pure <| some inkPath
else
2022-07-23 11:01:25 +00:00
throw <| IO.userError "Invalid path to LeanInk binary provided"
| none => pure none
def getTopLevelModules (p : Parsed) : IO (List String) := do
let topLevelModules := p.variableArgsAs! String |>.toList
if topLevelModules.length == 0 then
2022-07-23 11:01:25 +00:00
throw <| IO.userError "No topLevelModules provided."
pure topLevelModules
def runSingleCmd (p : Parsed) : IO UInt32 := do
let relevantModules := [p.positionalArg! "module" |>.as! String]
2022-07-21 20:43:33 +00:00
let res ← lakeSetup (relevantModules)
match res with
2022-07-21 00:25:26 +00:00
| Except.ok ws =>
2022-07-21 20:43:33 +00:00
let relevantModules := relevantModules.map String.toName
let (doc, hierarchy) ← load (.loadAllLimitAnalysis relevantModules)
IO.println "Outputting HTML"
let baseConfig := getSimpleBaseContext hierarchy
htmlOutputResults baseConfig doc ws (←findLeanInk? p)
pure 0
| Except.error rc => pure rc
2022-02-23 21:54:10 +00:00
2022-07-21 19:12:15 +00:00
def runIndexCmd (p : Parsed) : IO UInt32 := do
let hierarchy ← Hierarchy.fromDirectory basePath
let baseConfig := getSimpleBaseContext hierarchy
htmlOutputIndex baseConfig
pure 0
def runDocGenCmd (p : Parsed) : IO UInt32 := do
let modules : List String := p.variableArgsAs! String |>.toList
if modules.length == 0 then
2022-07-23 11:01:25 +00:00
throw <| IO.userError "No modules provided."
let res ← lakeSetup modules
match res with
| Except.ok ws =>
IO.println s!"Loading modules from: {←searchPathRef.get}"
2022-07-21 20:43:33 +00:00
let modules := modules.map String.toName
let (doc, hierarchy) ← load (.loadAll modules)
IO.println "Outputting HTML"
htmlOutput doc hierarchy ws (←findLeanInk? p)
pure 0
| Except.error rc => pure rc
def singleCmd := `[Cli|
single VIA runSingleCmd;
"Only generate the documentation for the module it was given, might contain broken links unless all documentation is generated."
FLAGS:
ink : String; "Path to a LeanInk binary to use for rendering the Lean sources."
ARGS:
module : String; "The module to generate the HTML for. Does not have to be part of topLevelModules."
]
2022-07-21 19:12:15 +00:00
def indexCmd := `[Cli|
index VIA runIndexCmd;
"Index the documentation that has been generated by single."
2022-07-21 17:07:35 +00:00
ARGS:
...topLevelModule : String; "The top level modules this documentation will be for."
]
2022-02-23 21:54:10 +00:00
def docGenCmd : Cmd := `[Cli|
"doc-gen4" VIA runDocGenCmd; ["0.0.1"]
"A documentation generator for Lean 4."
FLAGS:
ink : String; "Path to a LeanInk binary to use for rendering the Lean sources."
2022-02-23 21:54:10 +00:00
ARGS:
...modules : String; "The modules to generate the HTML for."
SUBCOMMANDS:
singleCmd;
2022-07-21 19:12:15 +00:00
indexCmd
2022-02-23 21:54:10 +00:00
]
def main (args : List String) : IO UInt32 :=
docGenCmd.validate args