Skip to content

Commit 1e9cf37

Browse files
committed
Separate html-generate and html-generate-asset
1 parent e395d1a commit 1e9cf37

File tree

5 files changed

+88
-61
lines changed

5 files changed

+88
-61
lines changed

src/odoc/bin/main.ml

Lines changed: 42 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -784,6 +784,8 @@ module Make_renderer (R : S) : sig
784784
val generate : docs:string -> unit Term.t * Term.info
785785

786786
val generate_source : docs:string -> unit Term.t * Term.info
787+
788+
val generate_asset : docs:string -> unit Term.t * Term.info
787789
end = struct
788790
let input_odoc =
789791
let doc = "Input file." in
@@ -832,10 +834,10 @@ end = struct
832834

833835
module Generate = struct
834836
let generate extra _hidden output_dir syntax extra_suffix input_file
835-
warnings_options sidebar asset_path =
837+
warnings_options sidebar =
836838
let file = Fs.File.of_string input_file in
837839
Rendering.generate_odoc ~renderer:R.renderer ~warnings_options ~syntax
838-
~output:output_dir ~extra_suffix ~sidebar ~asset_path extra file
840+
~output:output_dir ~extra_suffix ~sidebar extra file
839841

840842
let sidebar =
841843
let doc = "A .odoc-index file, used eg to generate the sidebar." in
@@ -844,13 +846,6 @@ end = struct
844846
& opt (some convert_fpath) None
845847
& info [ "index" ] ~doc ~docv:"FILE.odoc-index")
846848

847-
let asset_path =
848-
let doc = "The path to the asset file, when generating an asset unit." in
849-
Arg.(
850-
value
851-
& opt (some convert_fpath) None
852-
& info [ "asset-path" ] ~doc ~docv:"path/to/asset.ext")
853-
854849
let cmd =
855850
let syntax =
856851
let doc = "Available options: ml | re" in
@@ -863,8 +858,7 @@ end = struct
863858
Term.(
864859
const handle_error
865860
$ (const generate $ R.extra_args $ hidden $ dst ~create:true () $ syntax
866-
$ extra_suffix $ input_odocl $ warnings_options $ sidebar $ asset_path
867-
))
861+
$ extra_suffix $ input_odocl $ warnings_options $ sidebar))
868862

869863
let info ~docs =
870864
let doc =
@@ -919,6 +913,42 @@ end = struct
919913

920914
let generate_source ~docs = Generate_source.(cmd, info ~docs)
921915

916+
module Generate_asset = struct
917+
let generate extra output_dir extra_suffix input_file warnings_options
918+
asset_file =
919+
Rendering.generate_asset_odoc ~renderer:R.renderer ~warnings_options
920+
~output:output_dir ~extra_suffix ~asset_file extra input_file
921+
922+
let input_odocl =
923+
let doc = "Odoc asset unit." in
924+
Arg.(
925+
required
926+
& opt (some convert_fpath) None
927+
& info [ "asset-unit" ] ~doc ~docv:"asset-file.odocl")
928+
929+
let asset_file =
930+
let doc = "The asset file" in
931+
Arg.(
932+
required
933+
& pos 0 (some convert_fpath) None
934+
& info ~doc ~docv:"FILE.ext" [])
935+
936+
let cmd =
937+
Term.(
938+
const handle_error
939+
$ (const generate $ R.extra_args $ dst ~create:true () $ extra_suffix
940+
$ input_odocl $ warnings_options $ asset_file))
941+
942+
let info ~docs =
943+
let doc =
944+
Format.sprintf "Generate %s files from a $(i,impl-*.odocl)."
945+
R.renderer.name
946+
in
947+
Term.info ~docs ~doc (R.renderer.name ^ "-generate-asset")
948+
end
949+
950+
let generate_asset ~docs = Generate_asset.(cmd, info ~docs)
951+
922952
module Targets = struct
923953
let list_targets output_dir directories extra odoc_file =
924954
let odoc_file = Fs.File.of_string odoc_file in
@@ -1548,6 +1578,7 @@ let () =
15481578
Odoc_link.(cmd, info ~docs:section_pipeline);
15491579
Odoc_html.generate ~docs:section_pipeline;
15501580
Odoc_html.generate_source ~docs:section_pipeline;
1581+
Odoc_html.generate_asset ~docs:section_pipeline;
15511582
Support_files_command.(cmd, info ~docs:section_pipeline);
15521583
Compile_impl.(cmd, info ~docs:section_pipeline);
15531584
Indexing.(cmd, info ~docs:section_pipeline);

src/odoc/rendering.ml

Lines changed: 34 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2,67 +2,53 @@ open Odoc_document
22
open Or_error
33
open Odoc_model
44

5-
let check_empty_asset_path asset_path filename =
6-
if asset_path <> None then
7-
Error.raise_warning
8-
@@ Error.filename_only
9-
"--asset-path only has an effect when generating from an asset"
10-
filename
11-
12-
let documents_of_unit ~warnings_options ~syntax ~renderer ~extra ~asset_path
13-
~filename unit =
5+
let documents_of_unit ~warnings_options ~syntax ~renderer ~extra unit =
146
Error.catch_warnings (fun () ->
15-
check_empty_asset_path asset_path filename;
167
renderer.Renderer.extra_documents extra (CU unit))
178
|> Error.handle_warnings ~warnings_options
189
>>= fun extra_docs ->
1910
Ok (Renderer.document_of_compilation_unit ~syntax unit :: extra_docs)
2011

21-
let documents_of_asset ~warnings_options ~asset_path unit =
22-
Error.catch_warnings (fun () ->
23-
match asset_path with None -> failwith "TODO" | Some a -> a)
24-
|> Error.handle_warnings ~warnings_options
25-
>>= fun asset_path -> Ok [ Renderer.document_of_asset asset_path unit ]
26-
27-
let documents_of_page ~warnings_options ~syntax ~renderer ~extra ~asset_path
28-
~filename page =
12+
let documents_of_page ~warnings_options ~syntax ~renderer ~extra page =
2913
Error.catch_warnings (fun () ->
30-
check_empty_asset_path asset_path filename;
3114
renderer.Renderer.extra_documents extra (Page page))
3215
|> Error.handle_warnings ~warnings_options
3316
>>= fun extra_docs -> Ok (Renderer.document_of_page ~syntax page :: extra_docs)
3417

35-
let documents_of_odocl ~warnings_options ~filename ~renderer ~extra ~syntax
36-
~asset_path input =
18+
let documents_of_odocl ~warnings_options ~renderer ~extra ~syntax input =
3719
Odoc_file.load input >>= fun unit ->
3820
match unit.content with
3921
| Odoc_file.Page_content odoctree ->
40-
documents_of_page ~warnings_options ~syntax ~renderer ~extra ~asset_path
41-
~filename odoctree
22+
documents_of_page ~warnings_options ~syntax ~renderer ~extra odoctree
23+
| Unit_content odoctree ->
24+
documents_of_unit ~warnings_options ~syntax ~renderer ~extra odoctree
4225
| Impl_content _ ->
4326
Error
4427
(`Msg
4528
"Wrong kind of unit: Expected a page or module unit, got an \
4629
implementation. Use the dedicated command for implementation.")
47-
| Unit_content odoctree ->
48-
documents_of_unit ~warnings_options ~syntax ~renderer ~extra ~asset_path
49-
~filename odoctree
50-
| Asset_content a -> documents_of_asset ~warnings_options ~asset_path a
30+
| Asset_content _ ->
31+
Error
32+
(`Msg
33+
"Wrong kind of unit: Expected a page or module unit, got an asset \
34+
unit. Use the dedicated command for assets.")
5135

5236
let documents_of_input ~renderer ~extra ~resolver ~warnings_options ~syntax
53-
~asset_path input =
37+
input =
5438
let output = Fs.File.(set_ext ".odocl" input) in
5539
Odoc_link.from_odoc ~resolver ~warnings_options input output >>= function
5640
| `Page page -> Ok [ Renderer.document_of_page ~syntax page ]
57-
| `Impl _impl ->
41+
| `Module m -> documents_of_unit ~warnings_options ~syntax ~renderer ~extra m
42+
| `Impl _ ->
5843
Error
5944
(`Msg
6045
"Wrong kind of unit: Expected a page or module unit, got an \
6146
implementation. Use the dedicated command for implementation.")
62-
| `Module m ->
63-
documents_of_unit ~warnings_options ~filename:"" ~syntax ~asset_path
64-
~renderer ~extra m
65-
| `Asset a -> documents_of_asset ~warnings_options ~asset_path a
47+
| `Asset _ ->
48+
Error
49+
(`Msg
50+
"Wrong kind of unit: Expected a page or module unit, got an asset \
51+
unit. Use the dedicated command for assets.")
6652

6753
let render_document renderer ~sidebar ~output:root_dir ~extra_suffix ~extra doc
6854
=
@@ -96,24 +82,21 @@ let render_odoc ~resolver ~warnings_options ~syntax ~renderer ~output extra file
9682
=
9783
let extra_suffix = None in
9884
documents_of_input ~renderer ~extra ~resolver ~warnings_options ~syntax file
99-
~asset_path:None
10085
>>= fun docs ->
10186
List.iter
10287
(render_document renderer ~sidebar:None ~output ~extra_suffix ~extra)
10388
docs;
10489
Ok ()
10590

10691
let generate_odoc ~syntax ~warnings_options ~renderer ~output ~extra_suffix
107-
~sidebar ~asset_path extra file =
108-
let filename = Fpath.filename file in
92+
~sidebar extra file =
10993
(match sidebar with
11094
| None -> Ok None
11195
| Some x ->
11296
Odoc_file.load_index x >>= fun (sidebar, _) ->
11397
Ok (Some (Odoc_document.Sidebar.of_lang sidebar)))
11498
>>= fun sidebar ->
115-
documents_of_odocl ~warnings_options ~filename ~renderer ~extra ~syntax
116-
~asset_path file
99+
documents_of_odocl ~warnings_options ~renderer ~extra ~syntax file
117100
>>= fun docs ->
118101
List.iter
119102
(render_document renderer ~output ~sidebar ~extra_suffix ~extra)
@@ -152,16 +135,24 @@ let generate_source_odoc ~syntax ~warnings_options ~renderer ~output
152135
| Page_content _ | Unit_content _ | Asset_content _ ->
153136
Error (`Msg "Expected an implementation unit")
154137

138+
let generate_asset_odoc ~warnings_options:_ ~renderer ~output ~asset_file
139+
~extra_suffix extra file =
140+
Odoc_file.load file >>= fun unit ->
141+
match unit.content with
142+
| Odoc_file.Asset_content unit ->
143+
let doc = Renderer.document_of_asset asset_file unit in
144+
render_document renderer ~output ~sidebar:None ~extra_suffix ~extra doc;
145+
Ok ()
146+
| Page_content _ | Unit_content _ | Impl_content _ ->
147+
Error (`Msg "Expected an asset unit")
148+
155149
let targets_odoc ~resolver ~warnings_options ~syntax ~renderer ~output:root_dir
156150
~extra odoctree =
157-
let filename = Fpath.filename odoctree in
158151
let docs =
159152
if Fpath.get_ext odoctree = ".odoc" then
160153
documents_of_input ~renderer ~extra ~resolver ~warnings_options ~syntax
161-
~asset_path:None odoctree
162-
else
163-
documents_of_odocl ~warnings_options ~renderer ~extra ~syntax ~filename
164-
~asset_path:None odoctree
154+
odoctree
155+
else documents_of_odocl ~warnings_options ~renderer ~extra ~syntax odoctree
165156
in
166157
docs >>= fun docs ->
167158
List.iter

src/odoc/rendering.mli

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ val generate_odoc :
1818
output:Fs.directory ->
1919
extra_suffix:string option ->
2020
sidebar:Fpath.t option ->
21-
asset_path:Fpath.t option ->
2221
'a ->
2322
Fpath.t ->
2423
(unit, [> msg ]) result
@@ -34,6 +33,16 @@ val generate_source_odoc :
3433
Fpath.t ->
3534
(unit, [> msg ]) result
3635

36+
val generate_asset_odoc :
37+
warnings_options:Odoc_model.Error.warnings_options ->
38+
renderer:'a Renderer.t ->
39+
output:Fs.directory ->
40+
asset_file:Fs.file ->
41+
extra_suffix:string option ->
42+
'a ->
43+
Fs.file ->
44+
(unit, [> Or_error.msg ]) result
45+
3746
val targets_odoc :
3847
resolver:Resolver.t ->
3948
warnings_options:Odoc_model.Error.warnings_options ->

test/pages/new_assets.t/run.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
$ echo "Hello!" > img.png
1515

16-
$ odoc html-generate --output-dir _html --asset-path img.png odoc/root/test/asset-img.png.odoc
16+
$ odoc html-generate-asset --output-dir _html --asset-unit odoc/root/test/asset-img.png.odoc img.png
1717

1818
$ find _html -name img.png
1919
_html/root/test/img.png

test/sources/recursive_module.t/run.t

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,7 @@ Checking that source links exists inside recursive modules.
66
$ odoc link -I . impl-main.odoc
77
$ odoc link -I . main.odoc
88
$ odoc html-generate --indent -o html main.odocl
9-
$ odoc html-generate-impl --source main.ml --indent -o html impl-main.odocl
10-
odoc: unknown command 'html-generate-impl', must be one of 'aggregate-occurrences', 'classify', 'compile', 'compile-asset', 'compile-deps', 'compile-impl', 'compile-index', 'compile-targets', 'count-occurrences', 'css', 'errors', 'html', 'html-deps', 'html-fragment', 'html-generate', 'html-generate-source', 'html-targets', 'html-targets-source', 'html-url', 'latex', 'latex-generate', 'latex-targets', 'latex-url', 'link', 'link-deps', 'man', 'man-generate', 'man-targets', 'support-files' or 'support-files-targets'.
11-
Usage: odoc [COMMAND] …
12-
Try 'odoc --help' for more information.
13-
[2]
9+
$ odoc html-generate-source --impl impl-main.odocl --indent -o html main.ml
1410

1511
Both modules should contain source links
1612

0 commit comments

Comments
 (0)