Skip to content

Commit

Permalink
Show packages and libraries in the breadcrumbs
Browse files Browse the repository at this point in the history
This makes the breadcrumbs more readable by removing unteresting
components and make the beginning of the module path more recognizable.
  • Loading branch information
Julow committed Aug 26, 2024
1 parent 17fa6ab commit ad35d56
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 28 deletions.
35 changes: 20 additions & 15 deletions src/html/generator.ml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*)

open Odoc_utils
open Odoc_document.Types
module Html = Tyxml.Html
module Doctree = Odoc_document.Doctree
Expand Down Expand Up @@ -455,23 +456,27 @@ module Breadcrumbs = struct
open Types

let gen_breadcrumbs ~config ~url =
let rec get_parent_paths x =
match x with
| [] -> []
| x :: xs -> (
match Odoc_document.Url.Path.of_list (List.rev (x :: xs)) with
| Some x -> x :: get_parent_paths xs
| None -> get_parent_paths xs)
let resolve = Link.Current url in
let breadcrumb ?(prefix = "") url =
let href = Link.href ~config ~resolve (Url.from_path url) in
{ href; name = prefix ^ url.name; kind = url.kind }
in
let to_breadcrumb path =
let href =
Link.href ~config ~resolve:(Current url)
(Odoc_document.Url.from_path path)
in
{ href; name = path.name; kind = path.kind }
let rec package url =
match url.Url.Path.parent with
| None -> breadcrumb ~prefix:"Package " url
| Some url -> package url
in
let rec rhs (url : Url.Path.t) =
match url with
| { kind = `Library; parent; _ } ->
let package = Option.map package parent in
(* Don't list components that separates the package and library names. *)
breadcrumb ~prefix:"Library " url :: Option.to_list package
| { parent = None; kind = `Page; _ } -> [ package url ]
| { parent = None; _ } -> [ breadcrumb url ]
| { parent = Some parent; _ } -> breadcrumb url :: rhs parent
in
get_parent_paths (List.rev (Odoc_document.Url.Path.to_list url))
|> List.rev |> List.map to_breadcrumb
List.rev (rhs url)
end

module Page = struct
Expand Down
2 changes: 1 addition & 1 deletion test/integration/html_opts.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ Generate --as-json embeddable HTML fragment output:

$ odoc html-generate test.odocl -o html --as-json --indent
$ cat html/test/Test/index.html.json
{"type":"documentation","uses_katex":false,"breadcrumbs":[{"name":"test","href":"../index.html","kind":"library"},{"name":"Test","href":"#","kind":"module"}],"toc":[{"title":"Section 1","href":"#section-1","children":[]},{"title":"Section 2","href":"#section-2","children":[]}],"global_toc":null,"source_anchor":null,"preamble":"<p>Test</p>","content":"<h2 id=\"section-1\"><a href=\"#section-1\" class=\"anchor\"></a>Section 1</h2><div class=\"odoc-spec\">\u000A <div class=\"spec type anchored\" id=\"type-t\">\u000A <a href=\"#type-t\" class=\"anchor\"></a>\u000A <code><span><span class=\"keyword\">type</span> t</span></code>\u000A </div>\u000A</div><h2 id=\"section-2\"><a href=\"#section-2\" class=\"anchor\"></a>Section 2</h2><div class=\"odoc-spec\">\u000A <div class=\"spec type anchored\" id=\"type-u\">\u000A <a href=\"#type-u\" class=\"anchor\"></a>\u000A <code><span><span class=\"keyword\">type</span> u</span></code>\u000A </div>\u000A</div>"}
{"type":"documentation","uses_katex":false,"breadcrumbs":[{"name":"Library test","href":"../index.html","kind":"library"},{"name":"Test","href":"#","kind":"module"}],"toc":[{"title":"Section 1","href":"#section-1","children":[]},{"title":"Section 2","href":"#section-2","children":[]}],"global_toc":null,"source_anchor":null,"preamble":"<p>Test</p>","content":"<h2 id=\"section-1\"><a href=\"#section-1\" class=\"anchor\"></a>Section 1</h2><div class=\"odoc-spec\">\u000A <div class=\"spec type anchored\" id=\"type-t\">\u000A <a href=\"#type-t\" class=\"anchor\"></a>\u000A <code><span><span class=\"keyword\">type</span> t</span></code>\u000A </div>\u000A</div><h2 id=\"section-2\"><a href=\"#section-2\" class=\"anchor\"></a>Section 2</h2><div class=\"odoc-spec\">\u000A <div class=\"spec type anchored\" id=\"type-u\">\u000A <a href=\"#type-u\" class=\"anchor\"></a>\u000A <code><span><span class=\"keyword\">type</span> u</span></code>\u000A </div>\u000A</div>"}

$ odoc html-targets test.odocl -o html --as-json --indent
html/test/Test/index.html.json
Expand Down
2 changes: 1 addition & 1 deletion test/integration/json_expansion_with_sources.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,4 @@ Test the JSON output in the presence of expanded modules.
{"type":"documentation","uses_katex":false,"breadcrumbs":[{"name":"Main","href":"../../index.html","kind":"module"},{"name":"A","href":"../index.html","kind":"module"},{"name":"B","href":"#","kind":"module"}],"toc":[],"global_toc":null,"source_anchor":"../../../src/a.ml.html#module-B","preamble":"","content":""}

$ cat html/src/a.ml.html.json
{"type":"source","breadcrumbs":[{"name":"src","href":"index.html","kind":"page"},{"name":"a.ml","href":"#","kind":"source"}],"content":"<pre class=\"source_container\"><code class=\"source_line_column\"><a id=\"L1\" class=\"source_line\" href=\"#L1\">1</a>\u000A</code><code class=\"source_code\"><span><span id=\"module-B\"><span class=\"MODULE\">module</span> <span class=\"UIDENT\">B</span> <span class=\"EQUAL\">=</span> <span class=\"STRUCT\">struct</span> <span class=\"END\">end</span></span><span class=\"EOL\">\u000A</span></span></code></pre>"}
{"type":"source","breadcrumbs":[{"name":"Package src","href":"index.html","kind":"page"},{"name":"a.ml","href":"#","kind":"source"}],"content":"<pre class=\"source_container\"><code class=\"source_line_column\"><a id=\"L1\" class=\"source_line\" href=\"#L1\">1</a>\u000A</code><code class=\"source_code\"><span><span id=\"module-B\"><span class=\"MODULE\">module</span> <span class=\"UIDENT\">B</span> <span class=\"EQUAL\">=</span> <span class=\"STRUCT\">struct</span> <span class=\"END\">end</span></span><span class=\"EOL\">\u000A</span></span></code></pre>"}
2 changes: 1 addition & 1 deletion test/model/index_page_name.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ The breadcrumbs shouldn't show the name of the page, "index".
Expected to fail:

$ grep odoc-nav test/index.html
<nav class="odoc-nav"><a href="../index.html">Up</a> – test</nav>
<nav class="odoc-nav"><a href="../index.html">Up</a> – Package test</nav>
8 changes: 4 additions & 4 deletions test/xref2/include_module_type_of_preamble.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ Foo contains "Preamble for O" once.
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../index.html">test</a> &#x00BB; Foo
<a href="../index.html">Library test</a> &#x00BB; Foo
</nav>
<header class="odoc-preamble"><h1>Module <code><span>Foo</span></code></h1>
<p>Preamble for Foo.</p>
Expand Down Expand Up @@ -59,7 +59,7 @@ Bar doesn't contain "Preamble for Foo" on purpose.
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../index.html">test</a> &#x00BB; Bar
<a href="../index.html">Library test</a> &#x00BB; Bar
</nav>
<header class="odoc-preamble"><h1>Module <code><span>Bar</span></code></h1>
</header>
Expand Down Expand Up @@ -134,7 +134,7 @@ Check the preambles:
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../../index.html">test</a> &#x00BB;
<a href="../../index.html">Library test</a> &#x00BB;
<a href="../index.html">Bar</a> &#x00BB; Q
</nav>
<header class="odoc-preamble">
Expand All @@ -155,7 +155,7 @@ Check the preambles:
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../../index.html">test</a> &#x00BB;
<a href="../../index.html">Library test</a> &#x00BB;
<a href="../index.html">Bar</a> &#x00BB; P
</nav>
<header class="odoc-preamble">
Expand Down
4 changes: 2 additions & 2 deletions test/xref2/labels/labels.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ There are two references in N, one should point to a local label and the other t
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../../index.html">test</a> &#x00BB;
<a href="../../index.html">Library test</a> &#x00BB;
<a href="../index.html">Test</a> &#x00BB; N
</nav>
<header class="odoc-preamble">
Expand Down Expand Up @@ -87,7 +87,7 @@ The second occurence of 'B' in the main page should be disambiguated
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../index.html">test</a> &#x00BB; Test
<a href="../index.html">Library test</a> &#x00BB; Test
</nav>
<header class="odoc-preamble">
<h1>Module <code><span>Test</span></code></h1>
Expand Down
8 changes: 4 additions & 4 deletions test/xref2/module_preamble.t/run.t
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ and that "hidden" modules (eg. `A__b`, rendered to `html/A__b`) are not rendered
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../index.html">test</a> &#x00BB; A
<a href="../index.html">Library test</a> &#x00BB; A
</nav>
<header class="odoc-preamble"><h1>Module <code><span>A</span></code></h1>
<p>Module A.</p>
Expand Down Expand Up @@ -78,8 +78,8 @@ and that "hidden" modules (eg. `A__b`, rendered to `html/A__b`) are not rendered
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../../index.html">test</a> &#x00BB; <a href="../index.html">A</a>
&#x00BB; B
<a href="../../index.html">Library test</a> &#x00BB;
<a href="../index.html">A</a> &#x00BB; B
</nav>
<header class="odoc-preamble"><h1>Module <code><span>A.B</span></code></h1>
<p>Module B. This paragraph is the synopsis.</p>
Expand Down Expand Up @@ -120,7 +120,7 @@ and that "hidden" modules (eg. `A__b`, rendered to `html/A__b`) are not rendered
</head>
<body class="odoc">
<nav class="odoc-nav"><a href="../index.html">Up</a> –
<a href="../index.html">test</a> &#x00BB; A__b
<a href="../index.html">Library test</a> &#x00BB; A__b
</nav>
<header class="odoc-preamble">
<h1>Module <code><span>A__b</span></code></h1>
Expand Down

0 comments on commit ad35d56

Please sign in to comment.