-
Notifications
You must be signed in to change notification settings - Fork 42
/
Main.lean
139 lines (115 loc) · 4.49 KB
/
Main.lean
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import DocGen4
import Lean
import Cli
open DocGen4 Lean Cli
def getTopLevelModules (p : Parsed) : IO (List String) := do
let topLevelModules := p.variableArgsAs! String |>.toList
if topLevelModules.length == 0 then
throw <| IO.userError "No topLevelModules provided."
return topLevelModules
def runHeaderDataCmd (p : Parsed) : IO UInt32 := do
let buildDir := match p.flag? "build" with
| some dir => dir.as! String
| none => ".lake/build"
headerDataOutput buildDir
return 0
def runSingleCmd (p : Parsed) : IO UInt32 := do
let buildDir := match p.flag? "build" with
| some dir => dir.as! String
| none => ".lake/build"
let relevantModules := #[p.positionalArg! "module" |>.as! String |> String.toName]
let sourceUri := p.positionalArg! "sourceUri" |>.as! String
let (doc, hierarchy) ← load <| .analyzeConcreteModules relevantModules
let baseConfig ← getSimpleBaseContext buildDir hierarchy
htmlOutputResults baseConfig doc (some sourceUri)
return 0
def runIndexCmd (p : Parsed) : IO UInt32 := do
let buildDir := match p.flag? "build" with
| some dir => dir.as! String
| none => ".lake/build"
let hierarchy ← Hierarchy.fromDirectory (Output.basePath buildDir)
let baseConfig ← getSimpleBaseContext buildDir hierarchy
htmlOutputIndex baseConfig
return 0
def runGenCoreCmd (p : Parsed) : IO UInt32 := do
let buildDir := match p.flag? "build" with
| some dir => dir.as! String
| none => ".lake/build"
let module := p.positionalArg! "module" |>.as! String |> String.toName
let (doc, hierarchy) ← load <| .analyzePrefixModules module
let baseConfig ← getSimpleBaseContext buildDir hierarchy
htmlOutputResults baseConfig doc none
return 0
def runDocGenCmd (_p : Parsed) : IO UInt32 := do
IO.println "You most likely want to use me via Lake now, check my README on Github on how to:"
IO.println "https://github.com/leanprover/doc-gen4"
return 0
def runBibPrepassCmd (p : Parsed) : IO UInt32 := do
let buildDir := match p.flag? "build" with
| some dir => dir.as! String
| none => ".lake/build"
if p.hasFlag "none" then
IO.println "INFO: reference page disabled"
disableBibFile buildDir
else
match p.variableArgsAs! String with
| #[source] =>
let contents ← IO.FS.readFile source
if p.hasFlag "json" then
IO.println "INFO: 'references.json' will be copied to the output path; there will be no 'references.bib'"
preprocessBibJson buildDir contents
else
preprocessBibFile buildDir contents Bibtex.process
| _ => throw <| IO.userError "there should be exactly one source file"
return 0
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:
b, build : String; "Build directory."
ARGS:
module : String; "The module to generate the HTML for. Does not have to be part of topLevelModules."
sourceUri : String; "The sourceUri as computed by the Lake facet"
]
def indexCmd := `[Cli|
index VIA runIndexCmd;
"Index the documentation that has been generated by single."
FLAGS:
b, build : String; "Build directory."
]
def genCoreCmd := `[Cli|
genCore VIA runGenCoreCmd;
"Generate documentation for the specified Lean core module as they are not lake projects."
FLAGS:
b, build : String; "Build directory."
ARGS:
module : String; "The module to generate the HTML for."
]
def bibPrepassCmd := `[Cli|
bibPrepass VIA runBibPrepassCmd;
"Run the bibliography prepass: copy the bibliography file to output directory. By default it assumes the input is '.bib'."
FLAGS:
n, none; "Disable bibliography in this project."
j, json; "The input file is '.json' which contains an array of objects with 4 fields: 'citekey', 'tag', 'html' and 'plaintext'."
b, build : String; "Build directory."
ARGS:
...source : String; "The bibliography file. We only support one file for input. Should be '.bib' or '.json' according to flags."
]
def headerDataCmd := `[Cli|
headerData VIA runHeaderDataCmd;
"Produce `header-data.bmp`, this allows embedding of doc-gen declarations into other pages and more."
FLAGS:
b, build : String; "Build directory."
]
def docGenCmd : Cmd := `[Cli|
"doc-gen4" VIA runDocGenCmd; ["0.1.0"]
"A documentation generator for Lean 4."
SUBCOMMANDS:
singleCmd;
indexCmd;
genCoreCmd;
bibPrepassCmd;
headerDataCmd
]
def main (args : List String) : IO UInt32 :=
docGenCmd.validate args