(* parseff-shell: Starlight-inspired documentation shell for odoc. Registers as "parseff-shell", usable with --shell parseff-shell. *) open Odoc_utils module Html = Tyxml.Html module Url = Odoc_document.Url (* Register CSS and JS as support files *) let () = Odoc_extension_registry.register_support_file ~prefix:"parseff-shell" { filename = "extensions/parseff-shell.css"; content = Inline Parseff_shell_css.css; }; Odoc_extension_registry.register_support_file ~prefix:"parseff-shell" { filename = "extensions/parseff-shell.js"; content = Inline Parseff_shell_js.js; } (* --- Helpers --- *) let file_uri ~config ~url (base : Odoc_html.Types.uri) file = match base with | Odoc_html.Types.Absolute uri -> uri ^ "/" ^ file | Relative uri -> let page = Url.Path.{ kind = `File; parent = uri; name = file } in Odoc_html.Link.href ~config ~resolve:(Current url) (Url.from_path page) (* --- Prev/Next navigation from sidebar tree --- *) (** Flatten a sidebar tree into an ordered list of (url, title_html) pairs, keeping only entries with valid links. *) let flatten_sidebar (sidebar_data : Odoc_document.Sidebar.t option) = match sidebar_data with | None -> [] | Some trees -> List.concat_map (fun tree -> Odoc_utils.Tree.fold_left ~f:(fun acc (entry : Odoc_document.Sidebar.entry) -> if entry.valid_link then (entry.url, entry.content) :: acc else acc) [] tree) trees |> List.rev (** Find the previous and next entries relative to the current page. *) let find_prev_next (current_url : Url.Path.t) (entries : (Url.Anchor.t * Odoc_document.Types.Inline.t) list) = let rec aux prev = function | [] -> (prev, None) | (url, _title) :: rest when url.Url.Anchor.page = current_url && Astring.String.equal url.Url.Anchor.anchor "" -> let next = match rest with x :: _ -> Some x | [] -> None in (prev, next) | entry :: rest -> aux (Some entry) rest in aux None entries (* --- TyXML helpers --- *) let html_of_toc toc = let open Odoc_html.Types in let rec section (s : toc) = let link = Html.a ~a:[ Html.a_href s.href ] s.title in match s.children with [] -> [ link ] | cs -> [ link; sections cs ] and sections the_sections = the_sections |> List.map (fun s -> Html.li (section s)) |> Html.ul in match toc with [] -> [] | _ -> [ sections toc ] let html_of_breadcrumbs (breadcrumbs : Odoc_html.Types.breadcrumbs) = let space = Html.txt " " in let sep = [ space; Html.entity "#x00BB"; space ] in let html = List.concat_map_sep ~sep ~f:(fun (breadcrumb : Odoc_html.Types.breadcrumb) -> match breadcrumb.href with | Some href -> [ [ Html.a ~a:[ Html.a_href href ] (breadcrumb.name :> Html_types.flow5_without_interactive Html.elt list); ]; ] | None -> [ (breadcrumb.name :> Html_types.nav_content_fun Html.elt list) ]) breadcrumbs.parents |> List.flatten in let current_name :> Html_types.nav_content_fun Html.elt list = breadcrumbs.current.name in let rest = if List.is_empty breadcrumbs.parents then current_name else html @ sep @ current_name in (rest :> [< Html_types.nav_content_fun > `A `PCDATA `Wbr ] Html.elt list) (* --- Sidebar rendering --- *) (** Render the sidebar tree as nested