Skip to content

Commit d7d4631

Browse files
committed
Make output channel handling more readable
1 parent 34df814 commit d7d4631

File tree

7 files changed

+22
-25
lines changed

7 files changed

+22
-25
lines changed

src/odoc/html_fragment.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
open Odoc_utils
12
open Or_error
23

34
let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input =
@@ -47,11 +48,8 @@ let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input =
4748
Odoc_html.Generator.items ~config ~resolve:(Base xref_base_uri)
4849
(page.Odoc_document.Types.Page.preamble @ page.items)
4950
in
50-
let oc = open_out (Fs.File.to_string output) in
51-
let fmt = Format.formatter_of_out_channel oc in
52-
51+
Io_utils.with_formatter_out (Fs.File.to_string output) @@ fun fmt ->
5352
Format.fprintf fmt "%a@." (Format.pp_print_list (Tyxml.Html.pp_elt ())) html;
54-
close_out oc;
5553
Ok ()
5654
in
5755
match Fs.File.read input with

src/odoc/indexing.ml

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,8 @@ let parse_input_files input =
2424
>>= fun files -> Ok (List.concat files)
2525

2626
let compile_to_json ~output ~occurrences ~wrap ~simplified hierarchies =
27-
let output_channel =
28-
Fs.Directory.mkdir_p (Fs.File.dirname output);
29-
open_out_bin (Fs.File.to_string output)
30-
in
31-
let output = Format.formatter_of_out_channel output_channel in
27+
Fs.Directory.mkdir_p (Fs.File.dirname output);
28+
Io_utils.with_formatter_out (Fs.File.to_string output) @@ fun output ->
3229
if wrap then Format.fprintf output "let documents = ";
3330
let all =
3431
List.fold_left

src/odoc/odoc_file.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ let magic = "odoc-%%VERSION%%"
3434
(** Exceptions while saving are allowed to leak. *)
3535
let save_ file f =
3636
Fs.Directory.mkdir_p (Fs.File.dirname file);
37-
let oc = open_out_bin (Fs.File.to_string file) in
38-
output_string oc magic;
39-
Fun.protect ~finally:(fun () -> close_out oc) (fun () -> f oc)
37+
Io_utils.with_open_out_bin (Fs.File.to_string file) (fun oc ->
38+
output_string oc magic;
39+
f oc)
4040

4141
let save_unit file (root : Root.t) (t : t) =
4242
save_ file (fun oc ->

src/odoc/rendering.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
open Odoc_utils
12
open Odoc_document
23
open Or_error
34
open Odoc_model
@@ -53,10 +54,8 @@ let render_document renderer ~sidebar ~output:root_dir ~extra_suffix ~extra doc
5354
let pages = renderer.Renderer.render extra sidebar doc in
5455
Renderer.traverse pages ~f:(fun filename content ->
5556
let filename = prepare ~extra_suffix ~output_dir:root_dir filename in
56-
let oc = open_out (Fs.File.to_string filename) in
57-
let fmt = Format.formatter_of_out_channel oc in
58-
Format.fprintf fmt "%t@?" content;
59-
close_out oc)
57+
Io_utils.with_formatter_out (Fs.File.to_string filename) @@ fun fmt ->
58+
Format.fprintf fmt "%t@?" content)
6059

6160
let render_odoc ~resolver ~warnings_options ~syntax ~renderer ~output extra file
6261
=

src/odoc/sidebar.ml

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,9 @@ open Odoc_utils
44
let compile_to_json ~output sidebar =
55
let json = Odoc_html.Sidebar.to_json sidebar in
66
let text = Json.to_string json in
7-
let output_channel =
8-
Fs.Directory.mkdir_p (Fs.File.dirname output);
9-
open_out_bin (Fs.File.to_string output)
10-
in
11-
Fun.protect ~finally:(fun () -> close_out output_channel) @@ fun () ->
12-
Printf.fprintf output_channel "%s" text
7+
Fs.Directory.mkdir_p (Fs.File.dirname output);
8+
Io_utils.with_open_out_bin (Fs.File.to_string output) @@ fun oc ->
9+
Printf.fprintf oc "%s" text
1310

1411
let generate ~marshall ~output ~warnings_options:_ ~index =
1512
Odoc_file.load_index index >>= fun index ->

src/odoc/support_files.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
open Odoc_utils
2+
13
let should_include ~without_theme file =
24
if without_theme then
35
match file with
@@ -25,9 +27,7 @@ let write =
2527
let dir = Fs.File.dirname name in
2628
Fs.Directory.mkdir_p dir;
2729
let name = Fs.File.to_string name in
28-
let channel = open_out name in
29-
output_string channel content;
30-
close_out channel)
30+
Io_utils.with_open_out name (fun oc -> output_string oc content))
3131

3232
let print_filenames =
3333
iter_files (fun name _content -> print_endline (Fs.File.to_string name))

src/utils/odoc_utils.ml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,15 @@ module Io_utils = struct
101101
let read_lines fname =
102102
List.rev (fold_lines fname (fun line acc -> line :: acc) [])
103103

104+
let with_open_out fname f =
105+
_with_resource (open_out fname) ~close:close_out_noerr f
106+
104107
let with_open_out_bin fname f =
105108
_with_resource (open_out_bin fname) ~close:close_out_noerr f
106109

110+
let with_formatter_out fname f =
111+
with_open_out fname (fun oc -> f (Format.formatter_of_out_channel oc))
112+
107113
let marshal fname v =
108114
_with_resource (open_out_bin fname) ~close:close_out_noerr (fun oc ->
109115
Marshal.to_channel oc v [])

0 commit comments

Comments
 (0)