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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
open Or_error
open Odoc_model.Names
type parent_spec =
| Explicit of
Odoc_model.Paths.Identifier.ContainerPage.t
* Odoc_model.Paths.Reference.t list
| Package of Odoc_model.Paths.Identifier.ContainerPage.t
| Noparent
type parent_cli_spec =
| CliParent of string
| CliPackage of string
| CliNoparent
(** Parse parent and child references. May print warnings. *)
let parse_reference f =
let open Odoc_model in
let warnings_options = { Error.warn_error = true; print_warnings = true } in
Semantics.parse_reference f
|> Error.handle_errors_and_warnings ~warnings_options
let parent resolver parent_cli_spec =
let find_parent :
Odoc_model.Paths.Reference.t ->
(Odoc_model.Lang.Page.t, [> `Msg of string ]) Result.result =
fun r ->
match r with
| `Root (p, `TPage) | `Root (p, `TUnknown) -> (
match Resolver.lookup_page resolver p with
| Some r -> Ok r
| None -> Error (`Msg "Couldn't find specified parent page"))
| _ -> Error (`Msg "Expecting page as parent")
in
let = function
| `Page _ as container -> Ok container
| _ -> Error (`Msg "Specified parent is not a parent of this file")
in
match parent_cli_spec with
| CliParent f ->
parse_reference f >>= fun r ->
find_parent r >>= fun page ->
extract_parent page.name >>= fun parent ->
Ok (Explicit (parent, page.children))
| CliPackage package -> Ok (Package (`Page (None, PageName.make_std package)))
| CliNoparent -> Ok Noparent
let resolve_imports resolver imports =
let open Odoc_model in
List.map
(function
| Lang.Compilation_unit.Import.Resolved _ as resolved -> resolved
| Unresolved (name, _) as unresolved -> (
match Resolver.resolve_import resolver name with
| Some root -> Resolved (root, Names.ModuleName.make_std name)
| None -> unresolved))
imports
(** Raises warnings and errors. *)
let resolve_and_substitute ~resolver
(parent : Odoc_model.Paths.Identifier.ContainerPage.t option) input_file
read_file =
let filename = Fs.File.to_string input_file in
let unit =
read_file ~parent ~filename |> Odoc_model.Error.raise_errors_and_warnings
in
if not unit.Odoc_model.Lang.Compilation_unit.interface then
Printf.eprintf "WARNING: not processing the \"interface\" file.%s\n%!"
(if not (Filename.check_suffix filename "cmt") then ""
else
Printf.sprintf " Using %S while you should use the .cmti file" filename);
let unit = { unit with imports = resolve_imports resolver unit.imports } in
let env = Resolver.build_env_for_unit resolver ~linking:false unit in
let compiled =
Odoc_xref2.Compile.compile ~filename env unit
|> Odoc_model.Error.raise_warnings
in
compiled
let root_of_compilation_unit ~parent_spec ~hidden ~output ~module_name ~digest =
let open Odoc_model.Root in
let filename =
Filename.chop_extension Fs.File.(to_string @@ basename output)
in
let result parent =
let file = Odoc_file.create_unit ~force_hidden:hidden module_name in
Ok { id = `Root (parent, ModuleName.make_std module_name); file; digest }
in
let check_child : Odoc_model.Paths.Reference.t -> bool =
fun c ->
match c with
| `Root (n, `TUnknown) | `Root (n, `TModule) ->
Astring.String.Ascii.(uncapitalize n = uncapitalize filename)
| _ -> false
in
match parent_spec with
| Noparent -> result None
| Explicit (parent, children) ->
if List.exists check_child children then result (Some parent)
else Error (`Msg "Specified parent is not a parent of this file")
| Package parent -> result (Some parent)
let mld ~parent_spec ~output ~children ~warnings_options input =
List.fold_left
(fun acc child_str ->
match (acc, parse_reference child_str) with
| Ok acc, Ok r -> Ok (r :: acc)
| Error m, _ -> Error m
| _, Error (`Msg m) ->
Error (`Msg ("Failed to parse child reference: " ^ m))
| _, Error _ -> Error (`Msg "Unknown failure parsing child reference"))
(Ok []) children
>>= fun children ->
let root_name =
let page_dash_root =
Filename.chop_extension Fs.File.(to_string @@ basename output)
in
String.sub page_dash_root (String.length "page-")
(String.length page_dash_root - String.length "page-")
in
let input_s = Fs.File.to_string input in
let digest = Digest.file input_s in
let page_name = PageName.make_std root_name in
let check_child : Odoc_model.Paths.Reference.t -> bool =
fun c ->
match c with
| `Root (n, `TUnknown) | `Root (n, `TPage) -> root_name = n
| _ -> false
in
let _ =
match (parent_spec, root_name) with
| Explicit _, "index" ->
Format.eprintf
"Warning: Potential name clash - child page named 'index'\n%!"
| _ -> ()
in
let name =
let check parents_children v =
if List.exists check_child parents_children then Ok v
else Error (`Msg "Specified parent is not a parent of this file")
in
match (parent_spec, children) with
| Explicit (p, cs), [] -> check cs @@ `LeafPage (Some p, page_name)
| Explicit (p, cs), _ -> check cs @@ `Page (Some p, page_name)
| Package parent, [] -> Ok (`LeafPage (Some parent, page_name))
| Package parent, _ ->
Ok (`Page (Some parent, page_name))
| Noparent, [] -> Ok (`LeafPage (None, page_name))
| Noparent, _ -> Ok (`Page (None, page_name))
in
name >>= fun name ->
let root =
let file = Odoc_model.Root.Odoc_file.create_page root_name in
{
Odoc_model.Root.id = (name :> Odoc_model.Paths.Identifier.OdocId.t);
file;
digest;
}
in
let resolve content =
let page =
Odoc_model.Lang.Page.
{ name; root; children; content; digest; linked = false }
in
Odoc_file.save_page output ~warnings:[] page;
Ok ()
in
Fs.File.read input >>= fun str ->
Odoc_loader.read_string
(name :> Odoc_model.Paths.Identifier.LabelParent.t)
input_s str
|> Odoc_model.Error.handle_errors_and_warnings ~warnings_options
>>= function
| `Stop -> resolve []
| `Docs content -> resolve content
let compile ~resolver ~parent_cli_spec ~hidden ~children ~output
~warnings_options input =
parent resolver parent_cli_spec >>= fun parent_spec ->
let ext = Fs.File.get_ext input in
if ext = ".mld" then
mld ~parent_spec ~output ~warnings_options ~children input
else
(match ext with
| ".cmti" -> Ok Odoc_loader.read_cmti
| ".cmt" -> Ok Odoc_loader.read_cmt
| ".cmi" -> Ok Odoc_loader.read_cmi
| _ ->
Error
(`Msg "Unknown extension, expected one of: cmti, cmt, cmi or mld."))
>>= fun loader ->
let parent =
match parent_spec with
| Noparent -> Ok None
| Explicit (parent, _) -> Ok (Some parent)
| Package parent -> Ok (Some parent)
in
parent >>= fun parent ->
let make_root = root_of_compilation_unit ~parent_spec ~hidden ~output in
let result =
Odoc_model.Error.catch_errors_and_warnings (fun () ->
resolve_and_substitute ~resolver parent input (loader ~make_root))
in
let _, warnings = Odoc_model.Error.unpack_warnings result in
Odoc_model.Error.handle_errors_and_warnings ~warnings_options result
>>= fun unit ->
Odoc_file.save_unit output ~warnings unit;
Ok ()