···7 | `Module | `Page | `LeafPage | `Class -> name
8 | _ -> Format.asprintf "%a-%s" Odoc_document.Url.Path.pp_kind kind name
910-let as_filename (url : Url.Path.t) =
11 let components = Url.Path.to_list url in
12 let dir, path =
13 Url.Path.split
···17 let dir = List.map segment_to_string dir in
18 let path = String.concat "." (List.map segment_to_string path) in
19 let str_path = String.concat Fpath.dir_sep (dir @ [ path ]) in
20- Fpath.(v str_path + ".3o")
2122let rec is_class_or_module_path (url : Url.Path.t) =
23 match url.kind with
···7 | `Module | `Page | `LeafPage | `Class -> name
8 | _ -> Format.asprintf "%a-%s" Odoc_document.Url.Path.pp_kind kind name
910+let as_filename ?(add_ext = true) (url : Url.Path.t) =
11 let components = Url.Path.to_list url in
12 let dir, path =
13 Url.Path.split
···17 let dir = List.map segment_to_string dir in
18 let path = String.concat "." (List.map segment_to_string path) in
19 let str_path = String.concat Fpath.dir_sep (dir @ [ path ]) in
20+ if add_ext then Fpath.(v str_path + ".3o") else Fpath.v str_path
2122let rec is_class_or_module_path (url : Url.Path.t) =
23 match url.kind with
+34-11
src/odoc/fs.ml
···2122type file = Fpath.t
2300000000000024module File = struct
25 type t = file
26···92 Result.Error (`Msg err)
93 with Sys_error e -> Result.Error (`Msg e)
9400000000000000000000095 let exists file = Sys.file_exists (Fpath.to_string file)
9697 let rec of_segs_tl acc = function
···140141 let contains ~parentdir f = Fpath.is_rooted ~root:parentdir f
142143- let mkdir_p dir =
144- let mkdir d =
145- try Unix.mkdir (Fpath.to_string d) 0o755 with
146- | Unix.Unix_error (Unix.EEXIST, _, _) -> ()
147- | exn -> raise exn
148- in
149- let rec dirs_to_create p acc =
150- if Sys.file_exists (Fpath.to_string p) then acc
151- else dirs_to_create (Fpath.parent p) (p :: acc)
152- in
153- List.iter (dirs_to_create dir []) ~f:mkdir
154155 let to_string = Fpath.to_string
156
···2122type file = Fpath.t
2324+let mkdir_p dir =
25+ let mkdir d =
26+ try Unix.mkdir (Fpath.to_string d) 0o755 with
27+ | Unix.Unix_error (Unix.EEXIST, _, _) -> ()
28+ | exn -> raise exn
29+ in
30+ let rec dirs_to_create p acc =
31+ if Sys.file_exists (Fpath.to_string p) then acc
32+ else dirs_to_create (Fpath.parent p) (p :: acc)
33+ in
34+ List.iter (dirs_to_create dir []) ~f:mkdir
35+36module File = struct
37 type t = file
38···104 Result.Error (`Msg err)
105 with Sys_error e -> Result.Error (`Msg e)
106107+ let copy ~src ~dst =
108+ let with_ open_ close filename f =
109+ let c = open_ (Fpath.to_string filename) in
110+ Odoc_utils.Fun.protect ~finally:(fun () -> close c) (fun () -> f c)
111+ in
112+ let with_ic = with_ open_in_bin close_in_noerr in
113+ let with_oc = with_ open_out_bin close_out_noerr in
114+ try
115+ with_ic src (fun ic ->
116+ mkdir_p (dirname dst);
117+ with_oc dst (fun oc ->
118+ let len = 1024 in
119+ let buf = Bytes.create len in
120+ let rec loop () =
121+ let read = input ic buf 0 len in
122+ output oc buf 0 read;
123+ if read = len then loop ()
124+ in
125+ Ok (loop ())))
126+ with Sys_error e -> Result.Error (`Msg e)
127+128 let exists file = Sys.file_exists (Fpath.to_string file)
129130 let rec of_segs_tl acc = function
···173174 let contains ~parentdir f = Fpath.is_rooted ~root:parentdir f
175176+ let mkdir_p dir = mkdir_p dir
0000000000177178 let to_string = Fpath.to_string
179
+2
src/odoc/fs.mli
···9192 val read : t -> (string, [> msg ]) result
930094 val exists : t -> bool
9596 val of_segs : string list -> t
···9192 val read : t -> (string, [> msg ]) result
9394+ val copy : src:t -> dst:t -> (unit, [> msg ]) result
95+96 val exists : t -> bool
9798 val of_segs : string list -> t
···42 match doc with
43 | Odoc_document.Types.Document.Page { url; _ } -> url
44 | Source_page { url; _ } -> url
45- | Asset { url; _ } -> url
46 in
47 let sidebar =
48 Odoc_utils.Option.map
···120 Odoc_file.load file >>= fun unit ->
121 match unit.content with
122 | Odoc_file.Asset_content unit ->
123- let doc = Renderer.document_of_asset asset_file unit in
124- render_document renderer ~output ~sidebar:None ~extra_suffix ~extra doc;
125- Ok ()
0000000126 | Page_content _ | Unit_content _ | Impl_content _ ->
127 Error (`Msg "Expected an asset unit")
128
···42 match doc with
43 | Odoc_document.Types.Document.Page { url; _ } -> url
44 | Source_page { url; _ } -> url
045 in
46 let sidebar =
47 Odoc_utils.Option.map
···119 Odoc_file.load file >>= fun unit ->
120 match unit.content with
121 | Odoc_file.Asset_content unit ->
122+ let url = Odoc_document.Url.Path.from_identifier unit.name in
123+ 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
131+ Fs.File.copy ~src:asset_file ~dst
132 | Page_content _ | Unit_content _ | Impl_content _ ->
133 Error (`Msg "Expected an asset unit")
134
+19
src/utils/odoc_utils.ml
···80module Option = struct
81 let map f = function None -> None | Some x -> Some (f x)
82end
0000000000000000000
···80module Option = struct
81 let map f = function None -> None | Some x -> Some (f x)
82end
83+84+module Fun = struct
85+ include Fun
86+ let protect ~(finally : unit -> unit) work =
87+ let finally_no_exn () =
88+ try finally ()
89+ with e ->
90+ let bt = Printexc.get_raw_backtrace () in
91+ Printexc.raise_with_backtrace (Finally_raised e) bt
92+ in
93+ match work () with
94+ | result ->
95+ finally_no_exn ();
96+ result
97+ | exception work_exn ->
98+ let work_bt = Printexc.get_raw_backtrace () in
99+ finally_no_exn ();
100+ Printexc.raise_with_backtrace work_exn work_bt
101+end