Skip to content

Commit fc52c23

Browse files
committed
Add ability to call generate commands on asset units
1 parent 468ab02 commit fc52c23

File tree

5 files changed

+66
-18
lines changed

5 files changed

+66
-18
lines changed

src/document/renderer.ml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,7 @@ let document_of_compilation_unit ~syntax v =
4242
match syntax with
4343
| Reason -> Reason.compilation_unit v
4444
| OCaml -> ML.compilation_unit v
45+
46+
let document_of_asset path (v : Odoc_model.Lang.Asset.t) =
47+
let url = Url.Path.from_identifier v.name in
48+
Types.Document.Asset { url; src = path }

src/odoc/bin/main.ml

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,11 +935,11 @@ end = struct
935935
exit 1
936936

937937
let generate extra _hidden output_dir syntax extra_suffix input_file
938-
warnings_options source_file source_root sidebar =
938+
warnings_options source_file source_root sidebar asset_path =
939939
let source = source_of_args source_root source_file in
940940
let file = Fs.File.of_string input_file in
941941
Rendering.generate_odoc ~renderer:R.renderer ~warnings_options ~syntax
942-
~output:output_dir ~extra_suffix ~source ~sidebar extra file
942+
~output:output_dir ~extra_suffix ~source ~sidebar ~asset_path extra file
943943

944944
let source_file =
945945
let doc =
@@ -969,6 +969,13 @@ end = struct
969969
& opt (some convert_fpath) None
970970
& info [ "index" ] ~doc ~docv:"FILE.odoc-index")
971971

972+
let asset_path =
973+
let doc = "The path to the asset file, when generating an asset unit." in
974+
Arg.(
975+
value
976+
& opt (some convert_fpath) None
977+
& info [ "asset-path" ] ~doc ~docv:"path/to/asset.ext")
978+
972979
let cmd =
973980
let syntax =
974981
let doc = "Available options: ml | re" in
@@ -982,7 +989,7 @@ end = struct
982989
const handle_error
983990
$ (const generate $ R.extra_args $ hidden $ dst ~create:true () $ syntax
984991
$ extra_suffix $ input_odocl $ warnings_options $ source_file
985-
$ source_root $ sidebar))
992+
$ source_root $ sidebar $ asset_path))
986993

987994
let info ~docs =
988995
let doc =

src/odoc/rendering.ml

Lines changed: 41 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,24 +22,44 @@ let check_empty_source_arg source filename =
2222
an implementation"
2323
filename
2424

25+
let check_empty_asset_path asset_path filename =
26+
if asset_path <> None then
27+
Error.raise_warning
28+
@@ Error.filename_only
29+
"--asset-path only has an effect when generating from an asset"
30+
filename
31+
2532
let documents_of_unit ~warnings_options ~syntax ~source ~renderer ~extra
26-
~filename unit =
33+
~asset_path ~filename unit =
2734
Error.catch_warnings (fun () ->
2835
check_empty_source_arg source filename;
36+
check_empty_asset_path asset_path filename;
2937
renderer.Renderer.extra_documents extra (CU unit))
3038
|> Error.handle_warnings ~warnings_options
3139
>>= fun extra_docs ->
3240
Ok (Renderer.document_of_compilation_unit ~syntax unit :: extra_docs)
3341

42+
let documents_of_asset ~warnings_options ~source ~filename ~asset_path unit =
43+
Error.catch_warnings (fun () ->
44+
check_empty_source_arg source filename;
45+
match asset_path with None -> failwith "TODO" | Some a -> a)
46+
|> Error.handle_warnings ~warnings_options
47+
>>= fun asset_path -> Ok [ Renderer.document_of_asset asset_path unit ]
48+
3449
let documents_of_page ~warnings_options ~syntax ~source ~renderer ~extra
35-
~filename page =
50+
~asset_path ~filename page =
3651
Error.catch_warnings (fun () ->
3752
check_empty_source_arg source filename;
53+
check_empty_asset_path asset_path filename;
3854
renderer.Renderer.extra_documents extra (Page page))
3955
|> Error.handle_warnings ~warnings_options
4056
>>= fun extra_docs -> Ok (Renderer.document_of_page ~syntax page :: extra_docs)
4157

42-
let documents_of_implementation ~warnings_options:_ ~syntax impl source =
58+
let documents_of_implementation ~warnings_options ~syntax ~filename ~asset_path
59+
impl source =
60+
Error.catch_warnings (fun () -> check_empty_asset_path asset_path filename)
61+
|> Error.handle_warnings ~warnings_options
62+
>>= fun () ->
4363
match (source, impl.Lang.Implementation.id) with
4464
| Some source, Some source_id -> (
4565
let source_file =
@@ -82,35 +102,39 @@ let documents_of_source_tree ~warnings_options ~syntax ~source ~filename srctree
82102
|> Error.handle_warnings ~warnings_options
83103
>>= fun () -> Ok (Renderer.documents_of_source_tree ~syntax srctree)
84104

85-
let documents_of_odocl ~warnings_options ~renderer ~extra ~source ~syntax input
86-
=
105+
let documents_of_odocl ~warnings_options ~renderer ~extra ~source ~syntax
106+
~asset_path input =
87107
Odoc_file.load input >>= fun unit ->
88108
let filename = Fpath.to_string input in
89109
match unit.content with
90110
| Odoc_file.Page_content odoctree ->
91111
documents_of_page ~warnings_options ~syntax ~source ~renderer ~extra
92-
~filename odoctree
112+
~asset_path ~filename odoctree
93113
| Source_tree_content srctree ->
94114
documents_of_source_tree ~warnings_options ~syntax ~source ~filename
95115
srctree
96116
| Impl_content impl ->
97117
documents_of_implementation ~warnings_options ~syntax impl source
118+
~asset_path ~filename
98119
| Unit_content odoctree ->
99120
documents_of_unit ~warnings_options ~source ~syntax ~renderer ~extra
100-
~filename odoctree
101-
| Asset_content _ -> Ok [] (* TODO *)
121+
~asset_path ~filename odoctree
122+
| Asset_content a ->
123+
documents_of_asset ~warnings_options ~source ~filename ~asset_path a
102124

103125
let documents_of_input ~renderer ~extra ~resolver ~warnings_options ~syntax
104-
input =
126+
~asset_path input =
105127
let output = Fs.File.(set_ext ".odocl" input) in
106128
Odoc_link.from_odoc ~resolver ~warnings_options input output >>= function
107129
| `Source_tree st -> Ok (Renderer.documents_of_source_tree ~syntax st)
108130
| `Page page -> Ok [ Renderer.document_of_page ~syntax page ]
109131
| `Impl impl -> Ok (Renderer.documents_of_implementation ~syntax impl [] "")
110132
| `Module m ->
111133
documents_of_unit ~warnings_options ~source:None ~filename:"" ~syntax
112-
~renderer ~extra m
113-
| `Asset _ -> Ok [] (* TODO *)
134+
~asset_path ~renderer ~extra m
135+
| `Asset a ->
136+
documents_of_asset ~warnings_options ~source:None ~filename:"" ~asset_path
137+
a
114138

115139
let render_document renderer ~sidebar ~output:root_dir ~extra_suffix ~extra doc
116140
=
@@ -144,21 +168,23 @@ let render_odoc ~resolver ~warnings_options ~syntax ~renderer ~output extra file
144168
=
145169
let extra_suffix = None in
146170
documents_of_input ~renderer ~extra ~resolver ~warnings_options ~syntax file
171+
~asset_path:None
147172
>>= fun docs ->
148173
List.iter
149174
(render_document renderer ~sidebar:None ~output ~extra_suffix ~extra)
150175
docs;
151176
Ok ()
152177

153178
let generate_odoc ~syntax ~warnings_options ~renderer ~output ~extra_suffix
154-
~source ~sidebar extra file =
179+
~source ~sidebar ~asset_path extra file =
155180
(match sidebar with
156181
| None -> Ok None
157182
| Some x ->
158183
Odoc_file.load_index x >>= fun (sidebar, _) ->
159184
Ok (Some (Odoc_document.Sidebar.of_lang sidebar)))
160185
>>= fun sidebar ->
161-
documents_of_odocl ~warnings_options ~renderer ~source ~extra ~syntax file
186+
documents_of_odocl ~warnings_options ~renderer ~source ~extra ~syntax
187+
~asset_path file
162188
>>= fun docs ->
163189
List.iter
164190
(render_document renderer ~output ~sidebar ~extra_suffix ~extra)
@@ -170,10 +196,10 @@ let targets_odoc ~resolver ~warnings_options ~syntax ~renderer ~output:root_dir
170196
let docs =
171197
if Fpath.get_ext odoctree = ".odoc" then
172198
documents_of_input ~renderer ~extra ~resolver ~warnings_options ~syntax
173-
odoctree
199+
~asset_path:None odoctree
174200
else
175201
documents_of_odocl ~warnings_options ~renderer ~extra ~syntax ~source
176-
odoctree
202+
~asset_path:None odoctree
177203
in
178204
docs >>= fun docs ->
179205
List.iter

src/odoc/rendering.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ val generate_odoc :
2929
extra_suffix:string option ->
3030
source:source option ->
3131
sidebar:Fpath.t option ->
32+
asset_path:Fpath.t option ->
3233
'a ->
3334
Fpath.t ->
3435
(unit, [> msg ]) result

test/pages/new_assets.t/run.t

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,13 @@
1010
},
1111
"root": "<root>"
1212
}
13+
14+
$ echo "Hello!" > img.png
15+
16+
$ odoc html-generate --output-dir _html --asset-path img.png odoc/root/test/asset-img.png.odoc
17+
18+
$ find _html -name img.png
19+
_html/root/test/img.png
20+
21+
$ cat $(find _html -name img.png)
22+
Hello!

0 commit comments

Comments
 (0)