Skip to content

Commit c52ebb3

Browse files
committed
Pull out all HTML configuration into a value
1 parent 593f076 commit c52ebb3

22 files changed

+192
-204
lines changed

src/document/renderer.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,7 @@ let traverse ~f t =
1717
in
1818
List.iter aux t
1919

20-
type 'a t = {
21-
name : string;
22-
render : 'a -> Types.Page.t -> page list;
23-
files_of_url : Url.Path.t -> Fpath.t list;
24-
}
20+
type 'a t = { name : string; render : 'a -> Types.Page.t -> page list }
2521

2622
let document_of_page ~syntax v =
2723
match syntax with Reason -> Reason.page v | OCaml -> ML.page v

src/html/config.ml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
(* HTML output configuration *)
2+
3+
type t = {
4+
theme_uri : Types.uri option;
5+
support_uri : Types.uri option;
6+
semantic_uris : bool;
7+
indent : bool;
8+
flat : bool;
9+
open_details : bool;
10+
}
11+
12+
let v ?theme_uri ?support_uri ~semantic_uris ~indent ~flat ~open_details () =
13+
{ theme_uri; support_uri; semantic_uris; indent; flat; open_details }
14+
15+
let theme_uri config =
16+
match config.theme_uri with None -> Types.Relative None | Some uri -> uri
17+
18+
let support_uri config =
19+
match config.support_uri with None -> Types.Relative None | Some uri -> uri
20+
21+
let semantic_uris config = config.semantic_uris
22+
23+
let indent config = config.indent
24+
25+
let flat config = config.flat
26+
27+
let open_details config = config.open_details

src/html/config.mli

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
(* HTML renderer configuration *)
2+
3+
type t
4+
5+
val v :
6+
?theme_uri:Types.uri ->
7+
?support_uri:Types.uri ->
8+
semantic_uris:bool ->
9+
indent:bool ->
10+
flat:bool ->
11+
open_details:bool ->
12+
unit ->
13+
t
14+
15+
val theme_uri : t -> Types.uri
16+
17+
val support_uri : t -> Types.uri
18+
19+
val semantic_uris : t -> bool
20+
21+
val indent : t -> bool
22+
23+
val flat : t -> bool
24+
25+
val open_details : t -> bool

src/html/generator.ml

Lines changed: 52 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,11 @@ and styled style ~emph_level =
7474
| `Superscript -> (emph_level, Html.sup ~a:[])
7575
| `Subscript -> (emph_level, Html.sub ~a:[])
7676

77-
let rec internallink ~emph_level ~resolve ?(a = []) (t : InternalLink.t) =
77+
let rec internallink ~config ~emph_level ~resolve ?(a = []) (t : InternalLink.t)
78+
=
7879
match t with
7980
| Resolved (uri, content) ->
80-
let href = Link.href ~resolve uri in
81+
let href = Link.href ~config ~resolve uri in
8182
let a = (a :> Html_types.a_attrib Html.attrib list) in
8283
let elt =
8384
Html.a ~a:(Html.a_href href :: a) (inline_nolink ~emph_level content)
@@ -90,7 +91,7 @@ let rec internallink ~emph_level ~resolve ?(a = []) (t : InternalLink.t) =
9091
* (ref_to_string ref)
9192
* in *)
9293
let a = Html.a_class [ "xref-unresolved" ] :: a in
93-
let elt = Html.span ~a (inline ~emph_level ~resolve content) in
94+
let elt = Html.span ~a (inline ~config ~emph_level ~resolve content) in
9495
let elt = (elt :> phrasing Html.elt) in
9596
[ elt ]
9697

@@ -100,7 +101,8 @@ and internallink_nolink ~emph_level
100101
| Resolved (_, content) | Unresolved content ->
101102
[ Html.span ~a (inline_nolink ~emph_level content) ]
102103

103-
and inline ?(emph_level = 0) ~resolve (l : Inline.t) : phrasing Html.elt list =
104+
and inline ~config ?(emph_level = 0) ~resolve (l : Inline.t) :
105+
phrasing Html.elt list =
104106
let one (t : Inline.one) =
105107
let a = class_ t.attr in
106108
match t.desc with
@@ -112,13 +114,13 @@ and inline ?(emph_level = 0) ~resolve (l : Inline.t) : phrasing Html.elt list =
112114
| Linebreak -> [ Html.br ~a () ]
113115
| Styled (style, c) ->
114116
let emph_level, app_style = styled style ~emph_level in
115-
[ app_style @@ inline ~emph_level ~resolve c ]
117+
[ app_style @@ inline ~config ~emph_level ~resolve c ]
116118
| Link (href, c) ->
117119
let a = (a :> Html_types.a_attrib Html.attrib list) in
118120
let content = inline_nolink ~emph_level c in
119121
[ Html.a ~a:(Html.a_href href :: a) content ]
120-
| InternalLink c -> internallink ~emph_level ~resolve ~a c
121-
| Source c -> source (inline ~emph_level ~resolve) ~a c
122+
| InternalLink c -> internallink ~config ~emph_level ~resolve ~a c
123+
| Source c -> source (inline ~config ~emph_level ~resolve) ~a c
122124
| Raw_markup r -> raw_markup r
123125
in
124126
Utils.list_concat_map ~f:one l
@@ -144,13 +146,13 @@ and inline_nolink ?(emph_level = 0) (l : Inline.t) :
144146
in
145147
Utils.list_concat_map ~f:one l
146148

147-
let heading ~resolve (h : Heading.t) =
149+
let heading ~config ~resolve (h : Heading.t) =
148150
let a, anchor =
149151
match h.label with
150152
| Some id -> ([ Html.a_id id ], mk_anchor_link id)
151153
| None -> ([], [])
152154
in
153-
let content = inline ~resolve h.title in
155+
let content = inline ~config ~resolve h.title in
154156
let mk =
155157
match h.level with
156158
| 0 -> Html.h1
@@ -162,7 +164,7 @@ let heading ~resolve (h : Heading.t) =
162164
in
163165
mk ~a (anchor @ content)
164166

165-
let rec block ~resolve (l : Block.t) : flow Html.elt list =
167+
let rec block ~config ~resolve (l : Block.t) : flow Html.elt list =
166168
let as_flow x = (x : phrasing Html.elt list :> flow Html.elt list) in
167169
let one (t : Block.one) =
168170
let mk_block ?(extra_class = []) mk content =
@@ -171,29 +173,29 @@ let rec block ~resolve (l : Block.t) : flow Html.elt list =
171173
in
172174
match t.desc with
173175
| Inline i ->
174-
if t.attr = [] then as_flow @@ inline ~resolve i
175-
else mk_block Html.span (inline ~resolve i)
176-
| Paragraph i -> mk_block Html.p (inline ~resolve i)
176+
if t.attr = [] then as_flow @@ inline ~config ~resolve i
177+
else mk_block Html.span (inline ~config ~resolve i)
178+
| Paragraph i -> mk_block Html.p (inline ~config ~resolve i)
177179
| List (typ, l) ->
178180
let mk = match typ with Ordered -> Html.ol | Unordered -> Html.ul in
179-
mk_block mk (List.map (fun x -> Html.li (block ~resolve x)) l)
181+
mk_block mk (List.map (fun x -> Html.li (block ~config ~resolve x)) l)
180182
| Description l ->
181183
let item i =
182184
let a = class_ i.Description.attr in
183185
let term =
184-
(inline ~resolve i.Description.key
186+
(inline ~config ~resolve i.Description.key
185187
: phrasing Html.elt list
186188
:> flow Html.elt list)
187189
in
188-
let def = block ~resolve i.Description.definition in
190+
let def = block ~config ~resolve i.Description.definition in
189191
Html.li ~a (term @ (Html.txt " " :: def))
190192
in
191193
mk_block Html.ul (List.map item l)
192194
| Raw_markup r -> raw_markup r
193195
| Verbatim s -> mk_block Html.pre [ Html.txt s ]
194196
| Source (lang_tag, c) ->
195197
let extra_class = [ "language-" ^ lang_tag ] in
196-
mk_block ~extra_class Html.pre (source (inline ~resolve) c)
198+
mk_block ~extra_class Html.pre (source (inline ~config ~resolve) c)
197199
in
198200
Utils.list_concat_map l ~f:one
199201

@@ -210,13 +212,14 @@ let div : ([< Html_types.div_attrib ], [< item ], [> Html_types.div ]) Html.star
210212

211213
let spec_class attr = class_ ("spec" :: attr)
212214

213-
let spec_doc_div ~resolve = function
215+
let spec_doc_div ~config ~resolve = function
214216
| [] -> []
215217
| docs ->
216218
let a = [ Html.a_class [ "spec-doc" ] ] in
217-
[ div ~a (flow_to_item @@ block ~resolve docs) ]
219+
[ div ~a (flow_to_item @@ block ~config ~resolve docs) ]
218220

219-
let rec documentedSrc ~resolve (t : DocumentedSrc.t) : item Html.elt list =
221+
let rec documentedSrc ~config ~resolve (t : DocumentedSrc.t) :
222+
item Html.elt list =
220223
let open DocumentedSrc in
221224
let take_code l =
222225
Doctree.Take.until l ~classify:(function
@@ -239,14 +242,14 @@ let rec documentedSrc ~resolve (t : DocumentedSrc.t) : item Html.elt list =
239242
| [] -> []
240243
| (Code _ | Alternative _) :: _ ->
241244
let code, _, rest = take_code t in
242-
source (inline ~resolve) code @ to_html rest
243-
| Subpage subp :: _ -> subpage ~resolve subp
245+
source (inline ~config ~resolve) code @ to_html rest
246+
| Subpage subp :: _ -> subpage ~config ~resolve subp
244247
| (Documented _ | Nested _) :: _ ->
245248
let l, _, rest = take_descr t in
246249
let one { DocumentedSrc.attrs; anchor; code; doc; markers } =
247250
let content =
248251
match code with
249-
| `D code -> (inline ~resolve code :> item Html.elt list)
252+
| `D code -> (inline ~config ~resolve code :> item Html.elt list)
250253
| `N n -> to_html n
251254
in
252255
let doc =
@@ -259,7 +262,7 @@ let rec documentedSrc ~resolve (t : DocumentedSrc.t) : item Html.elt list =
259262
in
260263
[
261264
Html.div ~a:(class_ [ "def-doc" ])
262-
(delim opening @ block ~resolve doc @ delim closing);
265+
(delim opening @ block ~config ~resolve doc @ delim closing);
263266
]
264267
in
265268
let extra_attr, extra_class, link = mk_anchor anchor in
@@ -272,10 +275,10 @@ let rec documentedSrc ~resolve (t : DocumentedSrc.t) : item Html.elt list =
272275
in
273276
to_html t
274277

275-
and subpage ~resolve (subp : Subpage.t) : item Html.elt list =
276-
items ~resolve subp.content.items
278+
and subpage ~config ~resolve (subp : Subpage.t) : item Html.elt list =
279+
items ~config ~resolve subp.content.items
277280

278-
and items ~resolve l : item Html.elt list =
281+
and items ~config ~resolve l : item Html.elt list =
279282
let rec walk_items acc (t : Item.t list) : item Html.elt list =
280283
let continue_with rest elts =
281284
(walk_items [@tailcall]) (List.rev_append elts acc) rest
@@ -288,13 +291,13 @@ and items ~resolve l : item Html.elt list =
288291
| Item.Text text -> Accum text
289292
| _ -> Stop_and_keep)
290293
in
291-
let content = flow_to_item @@ block ~resolve text in
294+
let content = flow_to_item @@ block ~config ~resolve text in
292295
(continue_with [@tailcall]) rest content
293296
| Heading h :: rest ->
294-
(continue_with [@tailcall]) rest [ heading ~resolve h ]
297+
(continue_with [@tailcall]) rest [ heading ~config ~resolve h ]
295298
| Include { attr; anchor; doc; content = { summary; status; content } }
296299
:: rest ->
297-
let doc = spec_doc_div ~resolve doc in
300+
let doc = spec_doc_div ~config ~resolve doc in
298301
let included_html = (items content :> item Html.elt list) in
299302
let a_class =
300303
if List.length content = 0 then [ "odoc-include"; "shadowed-include" ]
@@ -306,7 +309,8 @@ and items ~resolve l : item Html.elt list =
306309
let summary =
307310
let extra_attr, extra_class, anchor_link = mk_anchor anchor in
308311
let a = spec_class (attr @ extra_class) @ extra_attr in
309-
Html.summary ~a @@ anchor_link @ source (inline ~resolve) summary
312+
Html.summary ~a @@ anchor_link
313+
@ source (inline ~config ~resolve) summary
310314
in
311315
let inner =
312316
[
@@ -320,15 +324,15 @@ and items ~resolve l : item Html.elt list =
320324
| `Inline -> doc @ included_html
321325
| `Closed -> details ~open':false
322326
| `Open -> details ~open':true
323-
| `Default -> details ~open':!Tree.open_details
327+
| `Default -> details ~open':(Config.open_details config)
324328
in
325329
(continue_with [@tailcall]) rest content
326330
| Declaration { Item.attr; anchor; content; doc } :: rest ->
327331
let extra_attr, extra_class, anchor_link = mk_anchor anchor in
328332
let a = spec_class (attr @ extra_class) @ extra_attr in
329-
let content = anchor_link @ documentedSrc ~resolve content in
333+
let content = anchor_link @ documentedSrc ~config ~resolve content in
330334
let spec =
331-
let doc = spec_doc_div ~resolve doc in
335+
let doc = spec_doc_div ~config ~resolve doc in
332336
[ div ~a:[ Html.a_class [ "odoc-spec" ] ] (div ~a content :: doc) ]
333337
in
334338
(continue_with [@tailcall]) rest spec
@@ -343,7 +347,7 @@ module Toc = struct
343347
| `Closed | `Open | `Default -> false
344348
| `Inline -> true
345349

346-
let gen_toc ~resolve ~path i =
350+
let gen_toc ~config ~resolve ~path i =
347351
let toc = Toc.compute path ~on_sub i in
348352
let rec section { Toc.url; text; children } =
349353
let text = inline_nolink text in
@@ -356,7 +360,7 @@ module Toc = struct
356360
List.map (Format.asprintf "%a" (Tyxml.Html.pp_elt ())) text
357361
|> String.concat " "
358362
in
359-
let href = Link.href ~resolve url in
363+
let href = Link.href ~config ~resolve url in
360364
{ title; title_str; href; children = List.map section children }
361365
in
362366
List.map section toc
@@ -370,32 +374,29 @@ module Page = struct
370374
| `Closed | `Open | `Default -> None
371375
| `Inline -> Some 0)
372376

373-
let rec include_ ?theme_uri indent { Subpage.content; _ } =
374-
page ?theme_uri indent content
377+
let rec include_ ~config { Subpage.content; _ } = page ~config content
375378

376-
and subpages ?theme_uri indent subpages =
377-
Utils.list_concat_map ~f:(include_ ?theme_uri indent) subpages
379+
and subpages ~config subpages =
380+
Utils.list_concat_map ~f:(include_ ~config) subpages
378381

379-
and page ?theme_uri ?support_uri indent p : Odoc_document.Renderer.page list =
382+
and page ~config p : Odoc_document.Renderer.page list =
380383
let { Page.title; header; items = i; url } =
381384
Doctree.Labels.disambiguate_page p
382385
and subpages =
383386
(* Don't use the output of [disambiguate_page] to avoid unecessarily
384387
mangled labels. *)
385-
subpages ?theme_uri indent @@ Doctree.Subpages.compute p
388+
subpages ~config @@ Doctree.Subpages.compute p
386389
in
387390
let resolve = Link.Current url in
388391
let i = Doctree.Shift.compute ~on_sub i in
389-
let toc = Toc.gen_toc ~resolve ~path:url i in
390-
let header = items ~resolve header in
391-
let content = (items ~resolve i :> any Html.elt list) in
392-
Tree.make ?theme_uri ?support_uri ~indent ~header ~toc ~url title content
393-
subpages
392+
let toc = Toc.gen_toc ~config ~resolve ~path:url i in
393+
let header = items ~config ~resolve header in
394+
let content = (items ~config ~resolve i :> any Html.elt list) in
395+
Tree.make ~config ~header ~toc ~url title content subpages
394396
end
395397

396-
let render ?theme_uri ?support_uri ~indent page =
397-
Page.page ?theme_uri ?support_uri indent page
398+
let render ~config page = Page.page ~config page
398399

399-
let doc ~xref_base_uri b =
400+
let doc ~config ~xref_base_uri b =
400401
let resolve = Link.Base xref_base_uri in
401-
block ~resolve b
402+
block ~config ~resolve b

src/html/generator.mli

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
1-
21
val render :
3-
?theme_uri:Types.uri ->
4-
?support_uri:Types.uri ->
5-
indent:bool ->
2+
config:Config.t ->
63
Odoc_document.Types.Page.t ->
74
Odoc_document.Renderer.page list
85

96
val doc :
7+
config:Config.t ->
108
xref_base_uri:string ->
119
Odoc_document.Types.Block.t ->
1210
Html_types.flow5_without_sectioning_heading_header_footer Tyxml.Html.elt list

0 commit comments

Comments
 (0)