Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
420 changes: 420 additions & 0 deletions src/voodoo-gen/generator.ml

Large diffs are not rendered by default.

9 changes: 9 additions & 0 deletions src/voodoo-gen/generator.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
open Odoc_document

val render_content : indent:bool -> Types.Page.t -> Renderer.page
val render_toc : indent:bool -> Types.Page.t -> Renderer.page

val doc :
xref_base_uri:string ->
Types.Block.t ->
Html_types.flow5_without_sectioning_heading_header_footer Tyxml.Html.elt list
96 changes: 96 additions & 0 deletions src/voodoo-gen/link.ml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
module Url = Odoc_document.Url

let flat = ref false

(* Translation from Url.Path *)
module Path = struct
let for_printing url = List.map snd @@ Url.Path.to_list url

let segment_to_string (kind, name) =
match kind with
| `Module | `Page -> name
| _ -> Format.asprintf "%a-%s" Url.Path.pp_kind kind name

let is_leaf_page url = url.Url.Path.kind = `LeafPage

let get_dir_and_file url =
let l = Url.Path.to_list url in
let is_dir =
if !flat then function `Page -> `Always | _ -> `Never
else function `LeafPage -> `Never | `File -> `Never | _ -> `Always
in
let dir, file = Url.Path.split ~is_dir l in
let dir = List.map segment_to_string dir in
let file =
match file with
| [] -> "index.html"
| [ (`LeafPage, name) ] -> name ^ ".html"
| [ (`File, name) ] -> name
| xs ->
assert !flat;
String.concat "-" (List.map segment_to_string xs) ^ ".html"
in
(dir, file)

let for_linking url =
let dir, file = get_dir_and_file url in
dir @ [ file ]

let as_filename (url : Url.Path.t) =
Fpath.(v @@ String.concat Fpath.dir_sep @@ for_linking url)
end

let semantic_uris = ref false

type resolve = Current of Url.Path.t | Base of string

let rec drop_shared_prefix l1 l2 =
match (l1, l2) with
| l1 :: l1s, l2 :: l2s when l1 = l2 -> drop_shared_prefix l1s l2s
| _, _ -> (l1, l2)

let href ~resolve t =
let { Url.Anchor.page; anchor; _ } = t in

let target_loc = Path.for_linking page in

(* If xref_base_uri is defined, do not perform relative URI resolution. *)
match resolve with
| Base xref_base_uri -> (
let page = xref_base_uri ^ String.concat "/" target_loc in
match anchor with "" -> page | anchor -> page ^ "#" ^ anchor)
| Current path -> (
let current_loc = Path.for_linking path in

let current_from_common_ancestor, target_from_common_ancestor =
drop_shared_prefix current_loc target_loc
in

let relative_target =
match current_from_common_ancestor with
| [] ->
(* We're already on the right page *)
(* If we're already on the right page, the target from our common
ancestor can't be anything other than the empty list *)
assert (target_from_common_ancestor = []);
[]
| [ _ ] ->
(* We're already in the right dir *)
target_from_common_ancestor
| l ->
(* We need to go up some dirs *)
List.map (fun _ -> "..") (List.tl l) @ target_from_common_ancestor
in
let remove_index_html l =
match List.rev l with
| "index.html" :: rest -> List.rev ("" :: rest)
| _ -> l
in
let relative_target =
if !semantic_uris then remove_index_html relative_target
else relative_target
in
match (relative_target, anchor) with
| [], "" -> "#"
| page, "" -> String.concat "/" page
| page, anchor -> String.concat "/" page ^ "#" ^ anchor)
17 changes: 17 additions & 0 deletions src/voodoo-gen/link.mli
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
(** HTML-specific interpretation of {!Odoc_document.Url} *)

module Url = Odoc_document.Url

val semantic_uris : bool ref
(** Whether to generate pretty/semantics links or not. *)

type resolve = Current of Url.Path.t | Base of string

val href : resolve:resolve -> Url.t -> string

module Path : sig
val is_leaf_page : Url.Path.t -> bool
val for_printing : Url.Path.t -> string list
val for_linking : Url.Path.t -> string list
val as_filename : Url.Path.t -> Fpath.t
end
17 changes: 9 additions & 8 deletions src/voodoo-gen/markdown.ml
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,7 @@ let rec block : 'attr block -> intermediate = function
| Code_block (_, _a, b) ->
Bl
[
{
desc = Source ("markdown", [ Elt [ { desc = Text b; attr = [] } ] ]);
attr = [];
};
{ desc = Source [ Elt [ { desc = Text b; attr = [] } ] ]; attr = [] };
]
| Html_block _ -> Bl []
| Definition_list _ -> Bl []
Expand All @@ -71,13 +68,16 @@ let of_content content ~name ~url =
let items = List.map (function It x -> x | Bl x -> Text x) intermediate in
Ok
(match items with
| [] -> Odoc_document.Types.Page.{ preamble = []; items = []; url }
| [] ->
Odoc_document.Types.Page.{ title = name; header = []; items = []; url }
| (Heading _ as x) :: rest ->
Odoc_document.Types.Page.{ preamble = [ x ]; items = rest; url }
Odoc_document.Types.Page.
{ title = name; header = [ x ]; items = rest; url }
| _ ->
Odoc_document.Types.Page.
{
preamble =
title = name;
header =
[
Heading
{
Expand Down Expand Up @@ -111,9 +111,10 @@ let read_plain f url =
Ok
Odoc_document.Types.Page.
{
title = name;
url;
items = [ Text [ { desc = Verbatim content; attr = [] } ] ];
preamble =
header =
[
Heading
{
Expand Down
25 changes: 8 additions & 17 deletions src/voodoo-gen/rendering.ml
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,24 @@ let render_document ~output odoctree =
Format.fprintf fmt "%t@?" content;
close_out oc)
in
aux
@@ Odoc_html.Generator.render
~config:
(Odoc_html.Config.v ~semantic_uris:true ~indent:false ~flat:false
~open_details:true ~as_json:true ())
odoctree;
aux @@ Generator.render_content ~indent:false odoctree;
aux @@ Generator.render_toc ~indent:false odoctree;
Ok ()

let docs_ids parent docs =
Odoc_file.load parent >>= fun root ->
match root.content with
| Page_content odoctree -> (
match odoctree.Odoc_model.Lang.Page.name with
| { iv = `LeafPage _; _ } -> Error (`Msg "Parent is a leaf!")
| { iv = `Page (maybe_container_page, _); _ } as parent_id ->
| `LeafPage _ -> Error (`Msg "Parent is a leaf!")
| `Page _ as parent_id ->
let result =
List.map
(fun doc ->
let id =
let basename = Fpath.basename doc in
{
parent_id with
iv =
`LeafPage
( maybe_container_page,
Odoc_model.Names.PageName.make_std basename );
}
`LeafPage
(Some parent_id, Odoc_model.Names.PageName.make_std basename)
in
(id, doc))
docs
Expand All @@ -62,8 +53,8 @@ let otherversions parent vs =
match root.content with
| Page_content odoctree -> (
match odoctree.Odoc_model.Lang.Page.name with
| { iv = `LeafPage _; _ } -> Error (`Msg "Parent is a leaf!")
| { iv = `Page (parent_id, _); _ } ->
| `LeafPage _ -> Error (`Msg "Parent is a leaf!")
| `Page (parent_id, _) ->
let result =
List.map
(fun v -> `Page (parent_id, Odoc_model.Names.PageName.make_std v))
Expand Down
1 change: 0 additions & 1 deletion voodoo-gen.opam
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,4 @@ build: [
dev-repo: "git+https://github.com/jonludlam/voodoo.git"
pin-depends: [
["pandoc.dev" "git+https://github.com/tatchi/opam-pandoc-bin#5eeb415c7023323045371ad803f88365c7003b38"]
["odoc.dev" "git+https://github.com/ocaml/odoc#503a2e895c211f555ff1a5e0ae11f2ab1697db73"]
]
1 change: 0 additions & 1 deletion voodoo-gen.opam.template
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pin-depends: [
["pandoc.dev" "git+https://github.com/tatchi/opam-pandoc-bin#5eeb415c7023323045371ad803f88365c7003b38"]
["odoc.dev" "git+https://github.com/ocaml/odoc#503a2e895c211f555ff1a5e0ae11f2ab1697db73"]
]