Skip to content

Commit

Permalink
Make output channel handling more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
Julow committed Feb 25, 2025
1 parent 7f5b24f commit 0cf38ff
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 25 deletions.
6 changes: 2 additions & 4 deletions src/odoc/html_fragment.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Odoc_utils
open Or_error

let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input =
Expand Down Expand Up @@ -40,11 +41,8 @@ let from_mld ~xref_base_uri ~resolver ~output ~warnings_options input =
~open_details:false ~as_json:false ~remap:[] ()
in
let html = Odoc_html.Generator.doc ~config ~xref_base_uri page in
let oc = open_out (Fs.File.to_string output) in
let fmt = Format.formatter_of_out_channel oc in

Io_utils.with_formatter_out (Fs.File.to_string output) @@ fun fmt ->
Format.fprintf fmt "%a@." (Format.pp_print_list (Tyxml.Html.pp_elt ())) html;
close_out oc;
Ok ()
in
match Fs.File.read input with
Expand Down
7 changes: 2 additions & 5 deletions src/odoc/indexing.ml
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,8 @@ let parse_input_files input =
>>= fun files -> Ok (List.concat files)

let compile_to_json ~output ~occurrences ~wrap ~simplified hierarchies =
let output_channel =
Fs.Directory.mkdir_p (Fs.File.dirname output);
open_out_bin (Fs.File.to_string output)
in
let output = Format.formatter_of_out_channel output_channel in
Fs.Directory.mkdir_p (Fs.File.dirname output);
Io_utils.with_formatter_out (Fs.File.to_string output) @@ fun output ->
if wrap then Format.fprintf output "let documents = ";
let all =
List.fold_left
Expand Down
6 changes: 3 additions & 3 deletions src/odoc/odoc_file.ml
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ let magic = "odoc-%%VERSION%%"
(** Exceptions while saving are allowed to leak. *)
let save_ file f =
Fs.Directory.mkdir_p (Fs.File.dirname file);
let oc = open_out_bin (Fs.File.to_string file) in
output_string oc magic;
Fun.protect ~finally:(fun () -> close_out oc) (fun () -> f oc)
Io_utils.with_open_out_bin (Fs.File.to_string file) (fun oc ->
output_string oc magic;
f oc)

let save_unit file (root : Root.t) (t : t) =
save_ file (fun oc ->
Expand Down
7 changes: 3 additions & 4 deletions src/odoc/rendering.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
open Odoc_utils
open Odoc_document
open Or_error
open Odoc_model
Expand Down Expand Up @@ -52,10 +53,8 @@ let render_document renderer ~sidebar ~output:root_dir ~extra_suffix ~extra doc
let pages = renderer.Renderer.render extra sidebar doc in
Renderer.traverse pages ~f:(fun filename content ->
let filename = prepare ~extra_suffix ~output_dir:root_dir filename in
let oc = open_out (Fs.File.to_string filename) in
let fmt = Format.formatter_of_out_channel oc in
Format.fprintf fmt "%t@?" content;
close_out oc)
Io_utils.with_formatter_out (Fs.File.to_string filename) @@ fun fmt ->
Format.fprintf fmt "%t@?" content)

let render_odoc ~resolver ~warnings_options ~syntax ~renderer ~output extra file
=
Expand Down
9 changes: 3 additions & 6 deletions src/odoc/sidebar.ml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,9 @@ open Odoc_utils
let compile_to_json ~output sidebar =
let json = Odoc_html.Sidebar.to_json sidebar in
let text = Json.to_string json in
let output_channel =
Fs.Directory.mkdir_p (Fs.File.dirname output);
open_out_bin (Fs.File.to_string output)
in
Fun.protect ~finally:(fun () -> close_out output_channel) @@ fun () ->
Printf.fprintf output_channel "%s" text
Fs.Directory.mkdir_p (Fs.File.dirname output);
Io_utils.with_open_out_bin (Fs.File.to_string output) @@ fun oc ->
Printf.fprintf oc "%s" text

let generate ~marshall ~output ~warnings_options:_ ~index =
Odoc_file.load_index index >>= fun index ->
Expand Down
6 changes: 3 additions & 3 deletions src/odoc/support_files.ml
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
open Odoc_utils

let should_include ~without_theme file =
if without_theme then
match file with
Expand Down Expand Up @@ -25,9 +27,7 @@ let write =
let dir = Fs.File.dirname name in
Fs.Directory.mkdir_p dir;
let name = Fs.File.to_string name in
let channel = open_out name in
output_string channel content;
close_out channel)
Io_utils.with_open_out name (fun oc -> output_string oc content))

let print_filenames =
iter_files (fun name _content -> print_endline (Fs.File.to_string name))
6 changes: 6 additions & 0 deletions src/utils/odoc_utils.ml
Original file line number Diff line number Diff line change
Expand Up @@ -101,9 +101,15 @@ module Io_utils = struct
let read_lines fname =
List.rev (fold_lines fname (fun line acc -> line :: acc) [])

let with_open_out fname f =
_with_resource (open_out fname) ~close:close_out_noerr f

let with_open_out_bin fname f =
_with_resource (open_out_bin fname) ~close:close_out_noerr f

let with_formatter_out fname f =
with_open_out fname (fun oc -> f (Format.formatter_of_out_channel oc))

let marshal fname v =
_with_resource (open_out_bin fname) ~close:close_out_noerr (fun oc ->
Marshal.to_channel oc v [])
Expand Down

0 comments on commit 0cf38ff

Please sign in to comment.