Skip to content

Commit 1759051

Browse files
committed
Preliminary work to allow table-of-contents to be output as json
1 parent 3e4c67a commit 1759051

File tree

3 files changed

+50
-21
lines changed

3 files changed

+50
-21
lines changed

src/html/generator.ml

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -338,32 +338,34 @@ and items ~resolve l : item Html.elt list =
338338
module Toc = struct
339339
open Odoc_document.Doctree
340340

341-
let render_toc ~resolve (toc : Toc.t) =
341+
type t = Tree.toc = {
342+
title : Html_types.flow5_without_interactive Html.elt list;
343+
title_str : string;
344+
href : string;
345+
children : t list;
346+
}
347+
348+
let on_sub : Subpage.status -> bool = function
349+
| `Closed | `Open | `Default -> false
350+
| `Inline -> true
351+
352+
let gen_toc ~resolve ~path i =
353+
let toc = Toc.compute path ~on_sub i in
342354
let rec section { Toc.url; text; children } =
343355
let text = inline_nolink text in
344-
let text =
356+
let title =
345357
(text
346358
: non_link_phrasing Html.elt list
347359
:> Html_types.flow5_without_interactive Html.elt list)
348360
in
361+
let title_str =
362+
List.map (Format.asprintf "%a" (Tyxml.Html.pp_elt ())) text
363+
|> String.concat " "
364+
in
349365
let href = Link.href ~resolve url in
350-
let link = Html.a ~a:[ Html.a_href href ] text in
351-
match children with [] -> [ link ] | _ -> [ link; sections children ]
352-
and sections the_sections =
353-
the_sections
354-
|> List.map (fun the_section -> Html.li (section the_section))
355-
|> Html.ul
366+
{ title; title_str; href; children = List.map section children }
356367
in
357-
match toc with
358-
| [] -> []
359-
| _ -> [ Html.nav ~a:[ Html.a_class [ "odoc-toc" ] ] [ sections toc ] ]
360-
361-
let on_sub : Subpage.status -> bool = function
362-
| `Closed | `Open | `Default -> false
363-
| `Inline -> true
364-
365-
let from_items ~resolve ~path i =
366-
render_toc ~resolve @@ Toc.compute path ~on_sub i
368+
List.map section toc
367369
end
368370

369371
module Page = struct
@@ -390,7 +392,7 @@ module Page = struct
390392
in
391393
let resolve = Link.Current url in
392394
let i = Doctree.Shift.compute ~on_sub i in
393-
let toc = Toc.from_items ~resolve ~path:url i in
395+
let toc = Toc.gen_toc ~resolve ~path:url i in
394396
let header = items ~resolve header in
395397
let content = (items ~resolve i :> any Html.elt list) in
396398
Tree.make ?theme_uri ?support_uri ~indent ~header ~toc ~url title content

src/html/tree.ml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,26 @@ module Html = Tyxml.Html
1818

1919
type uri = Absolute of string | Relative of Odoc_document.Url.Path.t option
2020

21+
type toc = {
22+
title : Html_types.flow5_without_interactive Html.elt list;
23+
title_str : string;
24+
href : string;
25+
children : toc list;
26+
}
27+
28+
let html_of_toc toc =
29+
let rec section section =
30+
let link = Html.a ~a:[ Html.a_href section.href ] section.title in
31+
match section.children with [] -> [ link ] | cs -> [ link; sections cs ]
32+
and sections the_sections =
33+
the_sections
34+
|> List.map (fun the_section -> Html.li (section the_section))
35+
|> Html.ul
36+
in
37+
match toc with
38+
| [] -> []
39+
| _ -> [ Html.nav ~a:[ Html.a_class [ "odoc-toc" ] ] [ sections toc ] ]
40+
2141
let page_creator ?(theme_uri = Relative None) ?(support_uri = Relative None)
2242
~url name header toc content =
2343
let path = Link.Path.for_printing url in
@@ -119,7 +139,7 @@ let page_creator ?(theme_uri = Relative None) ?(support_uri = Relative None)
119139
let body =
120140
breadcrumbs
121141
@ [ Html.header ~a:[ Html.a_class [ "odoc-preamble" ] ] header ]
122-
@ toc
142+
@ html_of_toc toc
123143
@ [ Html.div ~a:[ Html.a_class [ "odoc-content" ] ] content ]
124144
in
125145
Html.html head (Html.body ~a:[ Html.a_class [ "odoc" ] ] body)

src/html/tree.mli

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,13 @@ module Html = Tyxml.Html
1919

2020
(** Supported languages for printing code parts. *)
2121

22+
type toc = {
23+
title : Html_types.flow5_without_interactive Html.elt list;
24+
title_str : string;
25+
href : string;
26+
children : toc list;
27+
}
28+
2229
type uri =
2330
| Absolute of string
2431
| Relative of Odoc_document.Url.Path.t option
@@ -33,7 +40,7 @@ val make :
3340
indent:bool ->
3441
url:Url.Path.t ->
3542
header:Html_types.flow5_without_header_footer Html.elt list ->
36-
toc:Html_types.flow5 Html.elt list ->
43+
toc:toc list ->
3744
string ->
3845
Html_types.div_content Html.elt list ->
3946
Renderer.page list ->

0 commit comments

Comments
 (0)