Skip to content

Commit 3f9277d

Browse files
committed
Asset generation: factor out code
1 parent 18cb404 commit 3f9277d

File tree

1 file changed

+13
-15
lines changed

1 file changed

+13
-15
lines changed

src/odoc/rendering.ml

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,17 @@ open Odoc_document
22
open Or_error
33
open Odoc_model
44

5+
let prepare ~extra_suffix ~output_dir filename =
6+
let filename =
7+
match extra_suffix with
8+
| Some s -> Fpath.add_ext s filename
9+
| None -> filename
10+
in
11+
let filename = Fpath.normalize @@ Fs.File.append output_dir filename in
12+
let directory = Fs.File.dirname filename in
13+
Fs.Directory.mkdir_p directory;
14+
filename
15+
516
let document_of_odocl ~syntax input =
617
Odoc_file.load input >>= fun unit ->
718
match unit.content with
@@ -50,14 +61,7 @@ let render_document renderer ~sidebar ~output:root_dir ~extra_suffix ~extra doc
5061
in
5162
let pages = renderer.Renderer.render extra sidebar doc in
5263
Renderer.traverse pages ~f:(fun filename content ->
53-
let filename =
54-
match extra_suffix with
55-
| Some s -> Fpath.add_ext s filename
56-
| None -> filename
57-
in
58-
let filename = Fpath.normalize @@ Fs.File.append root_dir filename in
59-
let directory = Fs.File.dirname filename in
60-
Fs.Directory.mkdir_p directory;
64+
let filename = prepare ~extra_suffix ~output_dir:root_dir filename in
6165
let oc = open_out (Fs.File.to_string filename) in
6266
let fmt = Format.formatter_of_out_channel oc in
6367
Format.fprintf fmt "%t@?" content;
@@ -121,13 +125,7 @@ let generate_asset_odoc ~warnings_options:_ ~renderer ~output ~asset_file
121125
| Odoc_file.Asset_content unit ->
122126
let url = Odoc_document.Url.Path.from_identifier unit.name in
123127
let filename = renderer.Renderer.filepath extra url in
124-
let filename =
125-
match extra_suffix with
126-
| Some s -> Fpath.add_ext s filename
127-
| None -> filename
128-
in
129-
130-
let dst = Fs.File.append output filename in
128+
let dst = prepare ~extra_suffix ~output_dir:output filename in
131129
Fs.File.copy ~src:asset_file ~dst
132130
| Page_content _ | Unit_content _ | Impl_content _ ->
133131
Error (`Msg "Expected an asset unit")

0 commit comments

Comments
 (0)