-
Notifications
You must be signed in to change notification settings - Fork 86
/
main.ml
512 lines (453 loc) · 17 KB
/
main.ml
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
(* CR-someday trefis: the "deps" and "targets" subcommands currently output
their result on stdout.
It would make the interaction with jenga nicer if we could specify a file to
output the result to. *)
open Odoc_odoc
open Cmdliner
let convert_syntax : Odoc_document.Renderer.syntax Arg.converter =
let syntax_parser str =
match str with
| "ml" | "ocaml" -> `Ok Odoc_document.Renderer.OCaml
| "re" | "reason" -> `Ok Odoc_document.Renderer.Reason
| s -> `Error (Printf.sprintf "Unknown syntax '%s'" s)
in
let syntax_printer fmt syntax =
Format.pp_print_string fmt (Odoc_document.Renderer.string_of_syntax syntax)
in
(syntax_parser, syntax_printer)
let convert_directory ?(create=false) () : Fs.Directory.t Arg.converter =
let (dir_parser, dir_printer) = Arg.string in
let odoc_dir_parser str =
let () = if create then Fs.Directory.(mkdir_p (of_string str)) in
match dir_parser str with
| `Ok res -> `Ok (Fs.Directory.of_string res)
| `Error e -> `Error e
in
let odoc_dir_printer fmt dir = dir_printer fmt (Fs.Directory.to_string dir) in
(odoc_dir_parser, odoc_dir_printer)
(* Very basic validation and normalization for URI paths. *)
let convert_uri : Odoc_html.Tree.uri Arg.converter =
let parser str =
if String.length str = 0 then
`Error "invalid URI"
else
(* The URI is absolute if it starts with a scheme or with '/'. *)
let is_absolute =
List.exists ["http"; "https"; "file"; "data"; "ftp"]
~f:(fun scheme -> Astring.String.is_prefix ~affix:(scheme ^ ":") str)
|| String.get str 0 = '/'
in
let last_char = String.get str (String.length str - 1) in
let str = if last_char <> '/' then str ^ "/" else str in
`Ok Odoc_html.Tree.(if is_absolute then Absolute str else Relative str)
in
let printer ppf = function
| Odoc_html.Tree.Absolute uri
| Odoc_html.Tree.Relative uri -> Format.pp_print_string ppf uri
in
(parser, printer)
let handle_error = function
| Result.Ok () -> ()
| Error (`Cli_error msg) ->
Printf.eprintf "%s\n%!" msg;
exit 2
| Error (`Msg msg) ->
Printf.eprintf "ERROR: %s\n%!" msg;
exit 1
let docs = "ARGUMENTS"
let odoc_file_directories =
let doc =
"Where to look for required .odoc files. \
(Can be present several times)."
in
Arg.(value & opt_all (convert_directory ()) [] &
info ~docs ~docv:"DIR" ~doc ["I"])
let hidden =
let doc =
"Mark the unit as hidden. \
(Useful for files included in module packs)."
in
Arg.(value & flag & info ~docs ~doc ["hidden"])
let warn_error =
let doc = "Turn warnings into errors." in
let env = Arg.env_var "ODOC_WARN_ERROR" ~doc:(doc ^ " See option $(opt).") in
Arg.(value & flag & info ~docs ~doc ~env ["warn-error"])
let dst ?create () =
let doc = "Output directory where the HTML tree is expected to be saved." in
Arg.(required & opt (some (convert_directory ?create ())) None &
info ~docs ~docv:"DIR" ~doc ["o"; "output-dir"])
module Compile : sig
val output_file : dst:string option -> input:Fs.file -> Fs.file
val input : string Term.t
val dst : string option Term.t
val cmd : unit Term.t
val info: Term.info
end = struct
let has_page_prefix file =
file
|> Fs.File.basename
|> Fs.File.to_string
|> Astring.String.is_prefix ~affix:"page-"
let output_file ~dst ~input = match dst with
| Some file ->
let output = Fs.File.of_string file in
if Fs.File.has_ext ".mld" input && not (has_page_prefix output)
then (
Printf.eprintf "ERROR: the name of the .odoc file produced from a \
.mld must start with 'page-'\n%!";
exit 1
);
output
| None ->
let output =
if Fs.File.has_ext ".mld" input && not (has_page_prefix input)
then
let directory = Fs.File.dirname input in
let name = Fs.File.basename input in
let name = "page-" ^ Fs.File.to_string name in
Fs.File.create ~directory ~name
else input
in
Fs.File.(set_ext ".odoc" output)
let compile hidden directories resolve_fwd_refs dst package_name input
warn_error =
let env =
Env.create ~important_digests:(not resolve_fwd_refs) ~directories
in
let input = Fs.File.of_string input in
let output = output_file ~dst ~input in
Fs.Directory.mkdir_p (Fs.File.dirname output);
if Fs.File.has_ext ".cmti" input then
Compile.cmti ~env ~package:package_name ~hidden ~output ~warn_error input
else if Fs.File.has_ext ".cmt" input then
Compile.cmt ~env ~package:package_name ~hidden ~output ~warn_error input
else if Fs.File.has_ext ".cmi" input then
Compile.cmi ~env ~package:package_name ~hidden ~output ~warn_error input
else if Fs.File.has_ext ".mld" input then
Compile.mld ~env ~package:package_name ~output ~warn_error input
else
Error (`Cli_error "Unknown extension, expected one of: cmti, cmt, cmi or mld.\n%!")
let input =
let doc = "Input cmti, cmt, cmi or mld file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"FILE" [])
let dst =
let doc = "Output file path. Non-existing intermediate directories are
created. If absent outputs a $(i,BASE).odoc file in the same
directory as the input file where $(i,BASE) is the basename
of the input file (for mld files the \"page-\" prefix will be
added if not already present in the input basename)."
in
Arg.(value & opt (some string) None & info ~docs ~docv:"PATH" ~doc ["o"])
let cmd =
let pkg =
let doc = "Package the input is part of" in
Arg.(required & opt (some string) None &
info ~docs ~docv:"PKG" ~doc ["package"; "pkg"])
in
let resolve_fwd_refs =
let doc = "Try resolving forward references" in
Arg.(value & flag & info ~doc ["r";"resolve-fwd-refs"])
in
Term.(const handle_error $ (const compile $ hidden $ odoc_file_directories $
resolve_fwd_refs $ dst $ pkg $ input $ warn_error))
let info =
Term.info "compile"
~doc:"Compile a cmti, cmt, cmi or mld file to an odoc file."
end
module Support_files_command = struct
let support_files without_theme output_dir =
Support_files.write ~without_theme output_dir
let without_theme =
let doc = "Don't copy the default theme to output directory." in
Arg.(value & flag & info ~doc ["without-theme"])
let cmd =
Term.(const support_files $ without_theme $ dst ~create:true ())
let info =
let doc =
"Copy the support files (e.g. default theme, JavaScript files) to the \
output directory."
in
Term.info ~doc "support-files"
end
module Css = struct
let cmd = Support_files_command.cmd
let info =
let doc =
"DEPRECATED: Use `odoc support-files' to copy the CSS file for the \
default theme."
in
Term.info ~doc "css"
end
module Odoc_html : sig
val cmd : unit Term.t
val info: Term.info
end = struct
let html semantic_uris closed_details _hidden directories output_dir index_for
syntax theme_uri input_file warn_error =
Odoc_html.Link.semantic_uris := semantic_uris;
Odoc_html.Tree.open_details := not closed_details;
let env = Env.create ~important_digests:false ~directories in
let file = Fs.File.of_string input_file in
match index_for with
| None ->
Html_page.from_odoc ~env ~syntax ~theme_uri ~output:output_dir file
| Some pkg_name ->
Html_page.from_mld ~env ~syntax ~output:output_dir ~package:pkg_name ~warn_error file
let cmd =
let input =
let doc = "Input file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"file.odoc" [])
in
let semantic_uris =
let doc = "Generate pretty (semantic) links" in
Arg.(value & flag (info ~doc ["semantic-uris";"pretty-uris"]))
in
let closed_details =
let doc = "If this flag is passed <details> tags (used for includes) will \
be closed by default."
in
Arg.(value & flag (info ~doc ["closed-details"]))
in
let index_for =
let doc = "DEPRECATED: you should use 'odoc compile' to process .mld \
files. When this argument is given, then the input file is \
expected to be a .mld file. The output will be a \
\"index.html\" file in the output directory. \
PKG is using to correctly resolve and link references inside \
the input file"
in
Arg.(value & opt (some string) None & info ~docv:"PKG" ~doc ["index-for"])
in
let theme_uri =
let doc = "Where to look for theme files (e.g. `URI/odoc.css'). \
Relative URIs are resolved using `--output-dir' as a target." in
let default = Odoc_html.Tree.Relative "./" in
Arg.(value & opt convert_uri default & info ~docv:"URI" ~doc ["theme-uri"])
in
let syntax =
let doc = "Available options: ml | re" in
let env = Arg.env_var "ODOC_SYNTAX"
in
Arg.(value & opt (pconv convert_syntax) (Odoc_document.Renderer.OCaml) @@ info ~docv:"SYNTAX" ~doc ~env ["syntax"])
in
Term.(const handle_error $ (const html $ semantic_uris $ closed_details $ hidden $
odoc_file_directories $ dst ~create:true () $ index_for $ syntax $
theme_uri $ input $ warn_error))
let info =
Term.info ~doc:"Generates an html file from an odoc one" "html"
end
module Html_fragment : sig
val cmd : unit Term.t
val info: Term.info
end = struct
let html_fragment directories xref_base_uri output_file input_file warn_error =
let env = Env.create ~important_digests:false ~directories in
let input_file = Fs.File.of_string input_file in
let output_file = Fs.File.of_string output_file in
let xref_base_uri =
if xref_base_uri = "" then xref_base_uri
else
let last_char = String.get xref_base_uri (String.length xref_base_uri - 1) in
if last_char <> '/' then xref_base_uri ^ "/" else xref_base_uri
in
Html_fragment.from_mld ~env ~xref_base_uri ~output:output_file ~warn_error input_file
let cmd =
let output =
let doc = "Output HTML fragment file" in
Arg.(value & opt string "/dev/stdout" &
info ~docs ~docv:"file.html" ~doc ["o"; "output-file"])
in
let input =
let doc = "Input documentation page file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"file.mld" [])
in
let xref_base_uri =
let doc = "Base URI used to resolve cross-references. Set this to the \
root of the global docset during local development. By default \
`.' is used." in
Arg.(value & opt string "" & info ~docv:"URI" ~doc ["xref-base-uri"])
in
Term.(const handle_error $ (const html_fragment $ odoc_file_directories $
xref_base_uri $ output $ input $ warn_error))
let info =
Term.info ~doc:"Generates an html fragment file from an mld one" "html-fragment"
end
module Odoc_manpage : sig
val cmd : unit Term.t
val info: Term.info
end = struct
let manpage directories output_dir syntax input_file =
let env = Env.create ~important_digests:false ~directories in
let file = Fs.File.of_string input_file in
Man_page.from_odoc ~env ~syntax ~output:output_dir file
let cmd =
let input =
let doc = "Input file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"file.odoc" [])
in
let syntax =
let doc = "Available options: ml | re" in
let env = Arg.env_var "ODOC_SYNTAX"
in
Arg.(value & opt (pconv convert_syntax) (Odoc_document.Renderer.OCaml) @@ info ~docv:"SYNTAX" ~doc ~env ["syntax"])
in
Term.(const handle_error $ (const manpage $
odoc_file_directories $ dst ~create:true () $
syntax $
input))
let info =
Term.info ~doc:"Generates a man page file from an odoc one" "man"
end
module Odoc_latex : sig
val cmd : unit Term.t
val info: Term.info
end = struct
let latex directories output_dir syntax with_children input_file =
let env = Env.create ~important_digests:false ~directories in
let file = Fs.File.of_string input_file in
Latex.from_odoc ~env ~syntax ~output:output_dir ~with_children file
let cmd =
let input =
let doc = "Input file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"file.odoc" [])
in
let syntax =
let doc = "Available options: ml | re" in
let env = Arg.env_var "ODOC_SYNTAX"
in
Arg.(value & opt (pconv convert_syntax) (Odoc_document.Renderer.OCaml) @@ info ~docv:"SYNTAX" ~doc ~env ["syntax"])
in
let with_children =
let doc = "Include children at the end of the page" in
Arg.(value & opt bool true & info ~docv:"BOOL" ~doc ["with-children"])
in
Term.(const handle_error $ (const latex $
odoc_file_directories $ dst ~create:true () $
syntax $
with_children $
input))
let info =
Term.info ~doc:"Generates a latex file from an odoc one" "latex"
end
module Depends = struct
module Compile = struct
let list_dependencies input_file =
let deps = Depends.for_compile_step (Fs.File.of_string input_file) in
List.iter ~f:(fun t ->
Printf.printf "%s %s\n"
(Depends.Compile.name t)
(Digest.to_hex @@ Depends.Compile.digest t)
) deps;
flush stdout
let cmd =
let input =
let doc = "Input file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"file.cm{i,t,ti}" [])
in
Term.(const list_dependencies $ input)
let info =
Term.info "compile-deps"
~doc:"List units (with their digest) which needs to be compiled in order \
to compile this one. The unit itself and its digest is also \
reported in the output."
end
module Odoc_html = struct
let list_dependencies input_file =
let open Or_error in
Depends.for_html_step (Fs.Directory.of_string input_file) >>= fun depends ->
List.iter depends
~f:(fun (root : Odoc_model.Root.t) ->
Printf.printf "%s %s %s\n"
root.package
(Odoc_model.Root.Odoc_file.name root.file)
(Digest.to_hex root.digest)
);
Ok ()
let cmd =
let input =
let doc = "Input directory" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"PKG_DIR" [])
in
Term.(const handle_error $ (const list_dependencies $ input))
let info =
Term.info "html-deps"
~doc:"lists the packages which need to be in odoc's load path to process \
html from the .odoc files in the given directory"
end
end
module Targets = struct
module Compile = struct
let list_targets dst input =
let input = Fs.File.of_string input in
let output = Compile.output_file ~dst ~input in
Printf.printf "%s\n" (Fs.File.to_string output);
flush stdout
let cmd =
Term.(const list_targets $ Compile.dst $ Compile.input)
let info =
Term.info "compile-targets" ~doc:"TODO: Fill in."
end
module Odoc_html = struct
let list_targets directories output_dir odoc_file =
let open Or_error in
let env = Env.create ~important_digests:false ~directories in
let odoc_file = Fs.File.of_string odoc_file in
Targets.of_odoc_file ~env ~output:output_dir odoc_file >>= fun targets ->
let targets = List.map ~f:Fs.File.to_string targets in
Printf.printf "%s\n%!" (String.concat ~sep:"\n" targets);
Ok ()
let cmd =
let input =
let doc = "Input file" in
Arg.(required & pos 0 (some file) None & info ~doc ~docv:"file.odoc" [])
in
Term.(const handle_error $ (const list_targets $ odoc_file_directories $
dst () $ input))
let info =
Term.info "html-targets" ~doc:"TODO: Fill in."
end
module Support_files =
struct
let list_targets without_theme output_directory =
Support_files.print_filenames ~without_theme output_directory
let cmd =
Term.(const list_targets $ Support_files_command.without_theme $ dst ())
let info =
Term.info "support-files-targets"
~doc:"Lists the names of the files that 'odoc support-files' outputs."
end
end
let () =
Printexc.record_backtrace true;
let subcommands =
[ Compile.(cmd, info)
; Odoc_html.(cmd, info)
; Odoc_manpage.(cmd, info)
; Odoc_latex.(cmd,info)
; Html_fragment.(cmd, info)
; Support_files_command.(cmd, info)
; Css.(cmd, info)
; Depends.Compile.(cmd, info)
; Depends.Odoc_html.(cmd, info)
; Targets.Compile.(cmd, info)
; Targets.Odoc_html.(cmd, info)
; Targets.Support_files.(cmd, info)
]
in
let default =
let print_default () =
let available_subcommands =
List.map subcommands ~f:(fun (_, info) -> Term.name info)
in
Printf.printf "Available subcommands: %s\n\
See --help for more information.\n%!"
(String.concat ~sep:", " available_subcommands)
in
Term.(const print_default $ const ()),
Term.info ~version:"%%VERSION%%" "odoc"
in
match Term.eval_choice ~err:Format.err_formatter default subcommands with
| `Error _ ->
Format.pp_print_flush Format.err_formatter ();
exit 2
| _ -> ()