Skip to content

Commit 18cb404

Browse files
committed
Fully remove Assets from the document abstraction
1 parent 51cd330 commit 18cb404

File tree

16 files changed

+96
-55
lines changed

16 files changed

+96
-55
lines changed

src/document/renderer.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type input =
2424
type 'a t = {
2525
name : string;
2626
render : 'a -> Types.Block.t option -> Types.Document.t -> page list;
27+
filepath : 'a -> Url.Path.t -> Fpath.t;
2728
}
2829

2930
let document_of_page ~syntax v =
@@ -38,7 +39,3 @@ let document_of_compilation_unit ~syntax v =
3839
match syntax with
3940
| Reason -> Reason.compilation_unit v
4041
| OCaml -> ML.compilation_unit v
41-
42-
let document_of_asset path (v : Odoc_model.Lang.Asset.t) =
43-
let url = Url.Path.from_identifier v.name in
44-
Types.Document.Asset { url; src = path }

src/document/types.ml

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -196,13 +196,8 @@ and Source_page : sig
196196
end =
197197
Source_page
198198

199-
and Asset : sig
200-
type t = { url : Url.Path.t; src : Fpath.t }
201-
end =
202-
Asset
203-
204199
module Document = struct
205-
type t = Page of Page.t | Source_page of Source_page.t | Asset of Asset.t
200+
type t = Page of Page.t | Source_page of Source_page.t
206201
end
207202

208203
let inline ?(attr = []) desc = Inline.{ attr; desc }

src/html/generator.ml

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -535,32 +535,14 @@ module Page = struct
535535
if Config.as_json config then
536536
Html_fragment_json.make_src ~config ~url ~breadcrumbs [ doc ]
537537
else Html_page.make_src ~breadcrumbs ~header ~config ~url title [ doc ]
538-
539-
let asset ~config { Asset.url; src } =
540-
let filename = Link.Path.as_filename ~is_flat:(Config.flat config) url in
541-
let content ppf =
542-
let ic = open_in_bin (Fpath.to_string src) in
543-
let len = 1024 in
544-
let buf = Bytes.create len in
545-
let rec loop () =
546-
let read = input ic buf 0 len in
547-
if read = len then (
548-
Format.fprintf ppf "%s" (Bytes.to_string buf);
549-
loop ())
550-
else if len > 0 then
551-
let buf = Bytes.sub buf 0 read in
552-
Format.fprintf ppf "%s" (Bytes.to_string buf)
553-
in
554-
loop ();
555-
close_in ic
556-
in
557-
{ Odoc_document.Renderer.filename; content; children = [] }
558538
end
559539

560540
let render ~config ~sidebar = function
561541
| Document.Page page -> [ Page.page ~config ~sidebar page ]
562542
| Source_page src -> [ Page.source_page ~config src ]
563-
| Asset asset -> [ Page.asset ~config asset ]
543+
544+
let filepath ~config url =
545+
Link.Path.as_filename ~is_flat:(Config.flat config) url
564546

565547
let doc ~config ~xref_base_uri b =
566548
let resolve = Link.Base xref_base_uri in

src/html/generator.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ val render :
44
Odoc_document.Types.Document.t ->
55
Odoc_document.Renderer.page list
66

7+
val filepath : config:Config.t -> Odoc_document.Url.Path.t -> Fpath.t
8+
79
val doc :
810
config:Config.t ->
911
xref_base_uri:string ->

src/latex/generator.ml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,10 @@ module Link = struct
4040
( List.map segment_to_string dir,
4141
String.concat "." (List.map segment_to_string file) )
4242

43-
let filename url =
43+
let filename ?(add_ext = true) url =
4444
let dir, file = get_dir_and_file url in
4545
let file = Fpath.(v (String.concat dir_sep (dir @ [ file ]))) in
46-
Fpath.(add_ext "tex" file)
46+
if add_ext then Fpath.add_ext "tex" file else file
4747
end
4848

4949
let style = function
@@ -477,4 +477,6 @@ end
477477

478478
let render ~with_children = function
479479
| Document.Page page -> [ Page.page ~with_children page ]
480-
| Source_page _ | Asset _ -> []
480+
| Source_page _ -> []
481+
482+
let filepath url = Link.filename ~add_ext:false url

src/latex/generator.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,5 @@ val render :
66
with_children:bool ->
77
Odoc_document.Types.Document.t ->
88
Odoc_document.Renderer.page list
9+
10+
val filepath : Odoc_document.Url.Path.t -> Fpath.t

src/manpage/generator.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,4 +562,6 @@ and render_page (p : Page.t) =
562562

563563
let render = function
564564
| Document.Page page -> [ render_page page ]
565-
| Source_page _ | Asset _ -> []
565+
| Source_page _ -> []
566+
567+
let filepath url = Link.as_filename ~add_ext:false url

src/manpage/generator.mli

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
val render : Odoc_document.Types.Document.t -> Odoc_document.Renderer.page list
2+
3+
val filepath : Odoc_document.Url.Path.t -> Fpath.t

src/manpage/link.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ let segment_to_string (kind, name) =
77
| `Module | `Page | `LeafPage | `Class -> name
88
| _ -> Format.asprintf "%a-%s" Odoc_document.Url.Path.pp_kind kind name
99

10-
let as_filename (url : Url.Path.t) =
10+
let as_filename ?(add_ext = true) (url : Url.Path.t) =
1111
let components = Url.Path.to_list url in
1212
let dir, path =
1313
Url.Path.split
@@ -17,7 +17,7 @@ let as_filename (url : Url.Path.t) =
1717
let dir = List.map segment_to_string dir in
1818
let path = String.concat "." (List.map segment_to_string path) in
1919
let str_path = String.concat Fpath.dir_sep (dir @ [ path ]) in
20-
Fpath.(v str_path + ".3o")
20+
if add_ext then Fpath.(v str_path + ".3o") else Fpath.v str_path
2121

2222
let rec is_class_or_module_path (url : Url.Path.t) =
2323
match url.kind with

src/odoc/fs.ml

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ type directory = Fpath.t
2121

2222
type file = Fpath.t
2323

24+
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+
2436
module File = struct
2537
type t = file
2638

@@ -92,6 +104,27 @@ module File = struct
92104
Result.Error (`Msg err)
93105
with Sys_error e -> Result.Error (`Msg e)
94106

107+
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+
95128
let exists file = Sys.file_exists (Fpath.to_string file)
96129

97130
let rec of_segs_tl acc = function
@@ -140,17 +173,7 @@ module Directory = struct
140173

141174
let contains ~parentdir f = Fpath.is_rooted ~root:parentdir f
142175

143-
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
176+
let mkdir_p dir = mkdir_p dir
154177

155178
let to_string = Fpath.to_string
156179

0 commit comments

Comments
 (0)