Skip to content

Commit 56e2c14

Browse files
committed
latex backend: add flag to shorten submodules documentation
1 parent 6f1d80d commit 56e2c14

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+363
-410
lines changed

src/latex/generator.ml

Lines changed: 113 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@ open Types
33
module Doctree = Odoc_document.Doctree
44
module Url = Odoc_document.Url
55

6+
type config = {
7+
with_children : bool;
8+
shorten_beyond_depth : int option;
9+
remove_functor_arg_link : bool;
10+
}
11+
612
module Link = struct
713
let rec flatten_path ppf (x : Odoc_document.Url.Path.t) =
814
let pp_parent ppf = function
@@ -19,19 +25,15 @@ module Link = struct
1925
let label (x : Odoc_document.Url.t) =
2026
match x.anchor with "" -> page x.page | a -> anchor x.page a
2127

22-
let rec is_class_or_module_path (url : Odoc_document.Url.Path.t) =
23-
match url.kind with
24-
| `Module | `LeafPage | `Class | `Page -> (
25-
match url.parent with
26-
| None -> true
27-
| Some url -> is_class_or_module_path url)
28-
| _ -> false
28+
let rec is_inside_param (x : Odoc_document.Url.Path.t) =
29+
match (x.kind, x.parent) with
30+
| `Parameter _, _ -> true
31+
| _, None -> false
32+
| _, Some p -> is_inside_param p
2933

30-
let should_inline status url =
31-
match status with
32-
| `Inline | `Open -> true
33-
| `Closed -> false
34-
| `Default -> not @@ is_class_or_module_path url
34+
let ref config (x : Odoc_document.Url.t) =
35+
if config.remove_functor_arg_link && is_inside_param x.page then ""
36+
else label x
3537

3638
let get_dir_and_file url =
3739
let open Odoc_document in
@@ -48,6 +50,30 @@ module Link = struct
4850
if add_ext then Fpath.add_ext "tex" file else file
4951
end
5052

53+
module Expansion = struct
54+
let is_class_or_module (url : Odoc_document.Url.Path.t) =
55+
match url.kind with
56+
| `Module | `LeafPage | `Class | `Page -> true
57+
| _ -> false
58+
59+
let shortened config status url =
60+
let depth x = List.length Odoc_document.Url.(Path.to_list x) in
61+
match (config.shorten_beyond_depth, status) with
62+
| None, _ | _, (`Inline | `Open | `Closed) -> false
63+
| Some d, `Default -> depth url >= d
64+
65+
let should_inline status url =
66+
match status with
67+
| `Inline | `Open -> true
68+
| `Closed -> false
69+
| `Default ->
70+
(* we don't inline contents that should appear in their own page.*)
71+
not (is_class_or_module url)
72+
73+
let remove_subpage config status url =
74+
shortened config status url || should_inline status url
75+
end
76+
5177
let style = function
5278
| `Emphasis | `Italic -> Raw.emph
5379
| `Bold -> Raw.bold
@@ -67,10 +93,7 @@ let gen_hyperref pp r ppf =
6793
in
6894
Raw.hyperref s pp ppf content
6995

70-
let label = function
71-
| None -> []
72-
| Some x (* {Odoc_document.Url.Anchor.anchor ; page; _ }*) ->
73-
[ Label (Link.label x) ]
96+
let label = function None -> [] | Some x -> [ Label (Link.label x) ]
7497

7598
let level_macro = function
7699
| 0 -> Raw.section
@@ -220,29 +243,34 @@ let source k (t : Source.t) =
220243
and tokens t = Odoc_utils.List.concat_map token t in
221244
tokens t
222245

223-
let rec internalref ~verbatim ~in_source (t : Target.internal) (c : Inline.t) =
246+
let rec internalref ~config ~verbatim ~in_source (t : Target.internal)
247+
(c : Inline.t) =
224248
let target =
225249
match t with
226-
| Target.Resolved uri -> Link.label uri
250+
| Target.Resolved uri -> Link.ref config uri
227251
| Unresolved -> "xref-unresolved"
228252
in
229-
let text = Some (inline ~verbatim ~in_source c) in
253+
let text = inline ~config ~verbatim ~in_source c in
230254
let short = in_source in
231-
Internal_ref { short; target; text }
255+
Internal_ref { short; target; text = Some text }
232256

233-
and inline ~in_source ~verbatim (l : Inline.t) =
257+
and inline ~config ~in_source ~verbatim (l : Inline.t) =
234258
let one (t : Inline.one) =
235259
match t.desc with
236260
| Text _s -> assert false
237261
| Linebreak -> [ Break Line ]
238-
| Styled (style, c) -> [ Style (style, inline ~verbatim ~in_source c) ]
262+
| Styled (style, c) ->
263+
[ Style (style, inline ~config ~verbatim ~in_source c) ]
239264
| Link { target = External ext; content = c; _ } ->
240-
let content = inline ~verbatim:false ~in_source:false c in
265+
let content = inline ~config ~verbatim:false ~in_source:false c in
241266
[ External_ref (ext, Some content) ]
242267
| Link { target = Internal ref_; content = c; _ } ->
243-
[ internalref ~in_source ~verbatim ref_ c ]
268+
[ internalref ~config ~in_source ~verbatim ref_ c ]
244269
| Source c ->
245-
[ Inlined_code (source (inline ~verbatim:false ~in_source:true) c) ]
270+
[
271+
Inlined_code
272+
(source (inline ~config ~verbatim:false ~in_source:true) c);
273+
]
246274
| Math s -> [ Raw (Format.asprintf "%a" Raw.math s) ]
247275
| Raw_markup r -> raw_markup r
248276
| Entity s -> [ entity ~in_source ~verbatim s ]
@@ -264,22 +292,22 @@ and inline ~in_source ~verbatim (l : Inline.t) =
264292
in
265293
prettify l
266294

267-
let heading p (h : Heading.t) =
268-
let content = inline ~in_source:false ~verbatim:false h.title in
295+
let heading ~config p (h : Heading.t) =
296+
let content = inline ~config ~in_source:false ~verbatim:false h.title in
269297
[
270298
Section
271299
{ label = Option.map (Link.anchor p) h.label; level = h.level; content };
272300
Break Aesthetic;
273301
]
274302

275-
let non_empty_block_code c =
276-
let s = source (inline ~verbatim:true ~in_source:true) c in
303+
let non_empty_block_code ~config c =
304+
let s = source (inline ~config ~verbatim:true ~in_source:true) c in
277305
match s with
278306
| [] -> []
279307
| _ :: _ as l -> [ Break Separation; Code_block l; Break Separation ]
280308

281-
let non_empty_code_fragment c =
282-
let s = source (inline ~verbatim:false ~in_source:true) c in
309+
let non_empty_code_fragment ~config c =
310+
let s = source (inline ~config ~verbatim:false ~in_source:true) c in
283311
match s with [] -> [] | _ :: _ as l -> [ Code_fragment l ]
284312

285313
let alt_text ~in_source (target : Target.t) alt =
@@ -299,30 +327,30 @@ let image ~in_source (internal_url : Url.t) alt =
299327
[ Image fpath ]
300328
| _ -> alt_text ~in_source (Internal (Resolved internal_url)) alt
301329

302-
let rec block ~in_source (l : Block.t) =
330+
let rec block ~config ~in_source (l : Block.t) =
303331
let one (t : Block.one) =
304332
match t.desc with
305-
| Inline i -> inline ~verbatim:false ~in_source:false i
333+
| Inline i -> inline ~config ~verbatim:false ~in_source:false i
306334
| Image (Internal (Resolved x), alt) -> image ~in_source x alt
307335
| Image (t, alt) | Audio (t, alt) | Video (t, alt) ->
308336
alt_text ~in_source t alt
309337
| Paragraph i ->
310-
inline ~in_source:false ~verbatim:false i
338+
inline ~config ~in_source:false ~verbatim:false i
311339
@ if in_source then [] else [ Break Paragraph ]
312340
| List (typ, l) ->
313-
[ List { typ; items = List.map (block ~in_source:false) l } ]
314-
| Table t -> table_block t
341+
[ List { typ; items = List.map (block ~config ~in_source:false) l } ]
342+
| Table t -> table_block ~config t
315343
| Description l ->
316344
[
317345
(let item i =
318-
( inline ~in_source ~verbatim:false i.Description.key,
319-
block ~in_source i.Description.definition )
346+
( inline ~config ~in_source ~verbatim:false i.Description.key,
347+
block ~config ~in_source i.Description.definition )
320348
in
321349
Description (List.map item l));
322350
]
323351
| Raw_markup r -> raw_markup r
324352
| Verbatim s -> [ Verbatim s ]
325-
| Source (_, c) -> non_empty_block_code c
353+
| Source (_, c) -> non_empty_block_code ~config c
326354
| Math s ->
327355
[
328356
Break Paragraph;
@@ -332,11 +360,11 @@ let rec block ~in_source (l : Block.t) =
332360
in
333361
Odoc_utils.List.concat_map one l
334362

335-
and table_block { Table.data; align } =
363+
and table_block ~config { Table.data; align } =
336364
let data =
337365
List.map
338366
(List.map (fun (cell, cell_type) ->
339-
let content = block ~in_source:false cell in
367+
let content = block ~config ~in_source:false cell in
340368
match cell_type with
341369
| `Header -> [ Style (`Bold, content) ]
342370
| `Data -> content))
@@ -352,7 +380,7 @@ let rec is_only_text l =
352380
in
353381
List.for_all is_text l
354382

355-
let rec documentedSrc (t : DocumentedSrc.t) =
383+
let rec documentedSrc ~config (t : DocumentedSrc.t) =
356384
let open DocumentedSrc in
357385
let rec to_latex t =
358386
match t with
@@ -364,13 +392,23 @@ let rec documentedSrc (t : DocumentedSrc.t) =
364392
| _ -> Stop_and_keep)
365393
in
366394
let code, _, rest = take_code t in
367-
non_empty_code_fragment code @ to_latex rest
395+
non_empty_code_fragment ~config code @ to_latex rest
368396
| Alternative (Expansion e) :: rest ->
369-
(if Link.should_inline e.status e.url then to_latex e.expansion
370-
else non_empty_code_fragment e.summary)
371-
@ to_latex rest
397+
let elt =
398+
(* In the [should_inline] or [shortened], we are replacing the
399+
independent page by the inlined contents, thus we need to redirect
400+
the links to the missing page to the inlined contents.
401+
redirect the *)
402+
if Expansion.should_inline e.status e.url then
403+
Label (Link.page e.url) :: to_latex e.expansion
404+
else if Expansion.shortened config e.status e.url then
405+
Label (Link.page e.url) :: non_empty_code_fragment ~config e.summary
406+
else non_empty_code_fragment ~config e.summary
407+
in
408+
elt @ to_latex rest
372409
| Subpage subp :: rest ->
373-
Indented (items subp.content.url subp.content.items) :: to_latex rest
410+
Indented (items ~config subp.content.url subp.content.items)
411+
:: to_latex rest
374412
| (Documented _ | Nested _) :: _ ->
375413
let take_descr l =
376414
Doctree.Take.until l ~classify:(function
@@ -402,17 +440,17 @@ let rec documentedSrc (t : DocumentedSrc.t) =
402440
let one dsrc =
403441
let content =
404442
match dsrc.code with
405-
| `D code -> inline ~verbatim:false ~in_source:true code
443+
| `D code -> inline ~config ~verbatim:false ~in_source:true code
406444
| `N n -> to_latex n
407445
in
408-
let doc = [ block ~in_source:true dsrc.doc ] in
446+
let doc = [ block ~config ~in_source:true dsrc.doc ] in
409447
(content @ label dsrc.anchor) :: doc
410448
in
411449
layout_table (List.map one l) @ to_latex rest
412450
in
413451
to_latex t
414452

415-
and items page_url l =
453+
and items ~config page_url l =
416454
let rec walk_items ~page_url ~only_text acc (t : Item.t list) =
417455
let continue_with rest elts =
418456
walk_items ~page_url ~only_text (List.rev_append elts acc) rest
@@ -425,10 +463,10 @@ and items page_url l =
425463
| Item.Text text -> Accum text
426464
| _ -> Stop_and_keep)
427465
in
428-
let content = block ~in_source:false text in
466+
let content = block ~config ~in_source:false text in
429467
let elts = content in
430468
elts |> continue_with rest
431-
| Heading h :: rest -> heading page_url h |> continue_with rest
469+
| Heading h :: rest -> heading ~config page_url h |> continue_with rest
432470
| Include
433471
{
434472
attr = _;
@@ -439,19 +477,24 @@ and items page_url l =
439477
}
440478
:: rest ->
441479
let included = items page_url content in
442-
let docs = block ~in_source:true doc in
443-
let summary = source (inline ~verbatim:false ~in_source:true) summary in
480+
let docs = block ~config ~in_source:true doc in
481+
let summary =
482+
source (inline ~config ~verbatim:false ~in_source:true) summary
483+
in
444484
let content = included in
445485
label anchor @ docs @ summary @ content |> continue_with rest
446486
| Declaration { Item.attr = _; source_anchor = _; anchor; content; doc }
447487
:: rest ->
448-
let content = label anchor @ documentedSrc content in
488+
let content = label anchor @ documentedSrc ~config content in
449489
let elts =
450490
match doc with
451491
| [] -> content @ [ Break Line ]
452492
| docs ->
453493
content
454-
@ [ Indented (block ~in_source:true docs); Break Separation ]
494+
@ [
495+
Indented (block ~config ~in_source:true docs);
496+
Break Separation;
497+
]
455498
in
456499
continue_with rest elts
457500
and items page_url l =
@@ -466,7 +509,7 @@ module Doc = struct
466509
in
467510
Fmt.list input_child ppf children
468511

469-
let make ~with_children url content children =
512+
let make ~config url content children =
470513
let filename = Link.filename url in
471514
let label = Label (Link.page url) in
472515
let content =
@@ -476,7 +519,7 @@ module Doc = struct
476519
| q -> label :: q
477520
in
478521
let children_input ppf =
479-
if with_children then link_children ppf children else ()
522+
if config.with_children then link_children ppf children else ()
480523
in
481524
let content ppf = Fmt.pf ppf "@[<v>%a@,%t@]@." pp content children_input in
482525
{ Odoc_document.Renderer.filename; content; children; path = url }
@@ -485,27 +528,27 @@ end
485528
module Page = struct
486529
let on_sub = function `Page _ -> Some 1 | `Include _ -> None
487530

488-
let rec subpage ~with_children (p : Subpage.t) =
489-
if Link.should_inline p.status p.content.url then []
490-
else [ page ~with_children p.content ]
531+
let rec subpage ~config (p : Subpage.t) =
532+
if Expansion.remove_subpage config p.status p.content.url then []
533+
else [ page ~config p.content ]
491534

492-
and subpages ~with_children subpages =
493-
List.flatten @@ List.map (subpage ~with_children) subpages
535+
and subpages ~config subpages =
536+
List.flatten @@ List.map (subpage ~config) subpages
494537

495-
and page ~with_children p =
538+
and page ~config p =
496539
let { Page.items = i; url; _ } =
497540
Doctree.Labels.disambiguate_page ~enter_subpages:true p
498-
and subpages = subpages ~with_children @@ Doctree.Subpages.compute p in
541+
and subpages = subpages ~config @@ Doctree.Subpages.compute p in
499542
let i = Doctree.Shift.compute ~on_sub i in
500543
let header, preamble = Doctree.PageTitle.render_title p in
501-
let header = items url (header @ preamble) in
502-
let content = items url i in
503-
let page = Doc.make ~with_children url (header @ content) subpages in
544+
let header = items ~config url (header @ preamble) in
545+
let content = items ~config url i in
546+
let page = Doc.make ~config url (header @ content) subpages in
504547
page
505548
end
506549

507-
let render ~with_children = function
508-
| Document.Page page -> [ Page.page ~with_children page ]
550+
let render ~config = function
551+
| Document.Page page -> [ Page.page ~config page ]
509552
| Source_page _ -> []
510553

511554
let filepath url = Link.filename ~add_ext:false url

src/latex/generator.mli

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,14 @@ module Link : sig
22
val label : Odoc_document.Url.t -> string
33
end
44

5+
type config = {
6+
with_children : bool;
7+
shorten_beyond_depth : int option;
8+
remove_functor_arg_link : bool;
9+
}
10+
511
val render :
6-
with_children:bool ->
12+
config:config ->
713
Odoc_document.Types.Document.t ->
814
Odoc_document.Renderer.page list
915

0 commit comments

Comments
 (0)