Skip to content

Commit c478a1d

Browse files
committed
Improve the logic in html/link.ml
This should additionally fix the issue mentioned in PR #528 Signed-off-by: Jon Ludlam <jon@recoil.org>
1 parent ebd01c3 commit c478a1d

File tree

22 files changed

+249
-228
lines changed

22 files changed

+249
-228
lines changed

src/html/link.ml

Lines changed: 52 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,28 @@ module Path = struct
1414
let for_printing url = List.map snd @@ to_list url
1515

1616
let segment_to_string (kind, name) =
17-
if kind = "module" || kind = "cpage" || kind = "page"
18-
then name
19-
else Printf.sprintf "%s-%s" kind name
20-
let for_linking url = List.map segment_to_string @@ to_list url
17+
match kind with
18+
| "module" | "cpage" -> name
19+
| _ -> Printf.sprintf "%s-%s" kind name
2120

2221
let is_leaf_page url = (url.Url.Path.kind = "page")
2322

2423
let rec get_dir {Url.Path. parent ; name ; kind} =
25-
let s = segment_to_string (kind, name) in
26-
match parent with
27-
| None -> Fpath.v s
28-
| Some p -> Fpath.(get_dir p / s)
24+
let ppath = match parent with | Some p -> get_dir p | None -> [] in
25+
match kind with
26+
| "page" -> ppath
27+
| _ -> ppath @ [segment_to_string (kind, name)]
28+
29+
let get_file : Url.Path.t -> string = fun t ->
30+
match t.kind with
31+
| "page" -> t.name ^ ".html"
32+
| _ -> "index.html"
33+
34+
let for_linking : Url.Path.t -> string list = fun url ->
35+
get_dir url @ [get_file url]
2936

3037
let as_filename (url : Url.Path.t) =
31-
if is_leaf_page url then
32-
Fpath.(get_dir url + ".html")
33-
else
34-
Fpath.(get_dir url / "index.html")
38+
Fpath.(v @@ String.concat Fpath.dir_sep @@ for_linking url)
3539
end
3640

3741
let semantic_uris = ref false
@@ -46,34 +50,51 @@ let rec drop_shared_prefix l1 l2 =
4650
drop_shared_prefix l1s l2s
4751
| _, _ -> l1, l2
4852

49-
let href ~resolve { Url.Anchor. page; anchor; kind } =
50-
let leaf = if !semantic_uris || kind = "page" then [] else ["index.html"] in
51-
let target = Path.for_linking page @ leaf in
52-
match resolve with
53+
let href ~resolve t =
54+
let { Url.Anchor. page; anchor; _ } = t in
55+
56+
let target_loc = Path.for_linking page in
57+
5358
(* If xref_base_uri is defined, do not perform relative URI resolution. *)
59+
match resolve with
5460
| Base xref_base_uri ->
55-
let page = xref_base_uri ^ String.concat "/" target in
61+
let page = xref_base_uri ^ (String.concat "/" target_loc) in
5662
begin match anchor with
5763
| "" -> page
5864
| anchor -> page ^ "#" ^ anchor
5965
end
6066
| Current path ->
61-
let current_loc =
62-
let l = Path.for_linking path in
63-
if Path.is_leaf_page path then
64-
(* Sadness. *)
65-
List.tl l
66-
else l
67-
in
67+
let current_loc = Path.for_linking path in
68+
6869
let current_from_common_ancestor, target_from_common_ancestor =
69-
drop_shared_prefix current_loc target
70+
drop_shared_prefix current_loc target_loc
7071
in
72+
7173
let relative_target =
72-
List.map (fun _ -> "..") current_from_common_ancestor
73-
@ target_from_common_ancestor
74+
match current_from_common_ancestor with
75+
| [] -> (* We're already on the right page *)
76+
(* If we're already on the right page, the target from our common
77+
ancestor can't be anything other than the empty list *)
78+
assert(target_from_common_ancestor = []);
79+
[]
80+
| [_] -> (* We're already in the right dir *)
81+
target_from_common_ancestor
82+
| l -> (* We need to go up some dirs *)
83+
List.map (fun _ -> "..") (List.tl l)
84+
@ target_from_common_ancestor
7485
in
75-
let page = String.concat "/" relative_target in
76-
begin match anchor with
77-
| "" -> page
78-
| anchor -> page ^ "#" ^ anchor
86+
let remove_index_html l =
87+
match List.rev l with
88+
| "index.html" :: rest -> List.rev ("" :: rest)
89+
| _ -> l
90+
in
91+
let relative_target =
92+
if !semantic_uris
93+
then remove_index_html relative_target
94+
else relative_target
95+
in
96+
begin match relative_target, anchor with
97+
| [], "" -> "#"
98+
| page, "" -> String.concat "/" page
99+
| page, anchor -> String.concat "/" page ^ "#" ^ anchor
79100
end

test/html/expect/test_package+custom_theme,ml/Module/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h1>
5858
<a href="#type-result" class="anchor"></a><code><span class="keyword">type</span> <span>('a, 'b) result</span></code>
5959
</div>
6060
<div class="spec module-type" id="module-type-S6">
61-
<a href="#module-type-S6" class="anchor"></a><code><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-S6/index.html">S6</a> = <a href="module-type-S/index.html">S</a> <span class="keyword">with</span> <span class="keyword">type</span> <span>('a, 'b) <a href="module-type-S/index.html#type-w">w</a></span> := <span><span>(<span class="type-var">'a</span>,&nbsp;<span class="type-var">'b</span>)</span> <a href="index.html#type-result">result</a></span></code>
61+
<a href="#module-type-S6" class="anchor"></a><code><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-S6/index.html">S6</a> = <a href="module-type-S/index.html">S</a> <span class="keyword">with</span> <span class="keyword">type</span> <span>('a, 'b) <a href="module-type-S/index.html#type-w">w</a></span> := <span><span>(<span class="type-var">'a</span>,&nbsp;<span class="type-var">'b</span>)</span> <a href="#type-result">result</a></span></code>
6262
</div>
6363
<div class="spec module" id="module-M'">
6464
<a href="#module-M'" class="anchor"></a><code><span class="keyword">module</span> <a href="M'/index.html">M'</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code>

test/html/expect/test_package+ml/Labels/index.html

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h2 id="L2">
5151
</div>
5252
<div>
5353
<div class="spec value" id="val-f">
54-
<a href="#val-f" class="anchor"></a><code><span class="keyword">val</span> f : <a href="index.html#type-t">t</a></code>
54+
<a href="#val-f" class="anchor"></a><code><span class="keyword">val</span> f : <a href="#type-t">t</a></code>
5555
</div>
5656
<div>
5757
<p>
@@ -61,7 +61,7 @@ <h2 id="L2">
6161
</div>
6262
<div>
6363
<div class="spec external" id="val-e">
64-
<a href="#val-e" class="anchor"></a><code><span class="keyword">val</span> e : unit <span>-&gt;</span> <a href="index.html#type-t">t</a></code>
64+
<a href="#val-e" class="anchor"></a><code><span class="keyword">val</span> e : unit <span>-&gt;</span> <a href="#type-t">t</a></code>
6565
</div>
6666
<div>
6767
<p>
@@ -93,7 +93,7 @@ <h2 id="L2">
9393
</div>
9494
<div>
9595
<div class="spec extension">
96-
<code><span class="keyword">type</span> <a href="index.html#type-x">x</a> += </code>
96+
<code><span class="keyword">type</span> <a href="#type-x">x</a> += </code>
9797
<table>
9898
<tbody>
9999
<tr id="extension-X" class="anchored">
@@ -122,7 +122,7 @@ <h2 id="L2">
122122
</div>
123123
<div>
124124
<div class="spec type-subst" id="type-s">
125-
<a href="#type-s" class="anchor"></a><code><span class="keyword">type</span> s := <a href="index.html#type-t">t</a></code>
125+
<a href="#type-s" class="anchor"></a><code><span class="keyword">type</span> s := <a href="#type-t">t</a></code>
126126
</div>
127127
<div>
128128
<p>
@@ -153,7 +153,7 @@ <h2 id="L2">
153153
<tbody>
154154
<tr id="type-v.f" class="anchored">
155155
<td class="def record field">
156-
<a href="#type-v.f" class="anchor"></a><code>f : <a href="index.html#type-t">t</a>;</code>
156+
<a href="#type-v.f" class="anchor"></a><code>f : <a href="#type-t">t</a>;</code>
157157
</td>
158158
<td class="doc">
159159
<p>
@@ -171,43 +171,43 @@ <h2 id="L2">
171171
</p>
172172
<ul>
173173
<li>
174-
<a href="index.html#L1">Attached to unit</a>
174+
<a href="#L1">Attached to unit</a>
175175
</li>
176176
<li>
177-
<a href="index.html#L2">Attached to nothing</a>
177+
<a href="#L2">Attached to nothing</a>
178178
</li>
179179
<li>
180-
<a href="index.html#L3">Attached to module</a>
180+
<a href="#L3">Attached to module</a>
181181
</li>
182182
<li>
183-
<a href="index.html#L4">Attached to type</a>
183+
<a href="#L4">Attached to type</a>
184184
</li>
185185
<li>
186-
<a href="index.html#L5">Attached to value</a>
186+
<a href="#L5">Attached to value</a>
187187
</li>
188188
<li>
189-
<a href="index.html#L6">Attached to class</a>
189+
<a href="#L6">Attached to class</a>
190190
</li>
191191
<li>
192-
<a href="index.html#L7">Attached to class type</a>
192+
<a href="#L7">Attached to class type</a>
193193
</li>
194194
<li>
195-
<a href="index.html#L8">Attached to exception</a>
195+
<a href="#L8">Attached to exception</a>
196196
</li>
197197
<li>
198-
<a href="index.html#L9">Attached to extension</a>
198+
<a href="#L9">Attached to extension</a>
199199
</li>
200200
<li>
201-
<a href="index.html#L10">Attached to module subst</a>
201+
<a href="#L10">Attached to module subst</a>
202202
</li>
203203
<li>
204-
<a href="index.html#L11">Attached to type subst</a>
204+
<a href="#L11">Attached to type subst</a>
205205
</li>
206206
<li>
207-
<a href="index.html#L12">Attached to constructor</a>
207+
<a href="#L12">Attached to constructor</a>
208208
</li>
209209
<li>
210-
<a href="index.html#L13">Attached to field</a>
210+
<a href="#L13">Attached to field</a>
211211
</li>
212212
</ul>
213213
</aside>

test/html/expect/test_package+ml/Markup/index.html

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ <h4 id="anchors">
112112
</h4>
113113
<aside>
114114
<p>
115-
Sections can have attached <a href="index.html#anchors">Anchors</a>, and it is possible to <a href="index.html#anchors">link</a> to them. Links to section headers should not be set in source code style.
115+
Sections can have attached <a href="#anchors">Anchors</a>, and it is possible to <a href="#anchors">link</a> to them. Links to section headers should not be set in source code style.
116116
</p>
117117
</aside>
118118
<h5 id="paragraph">
@@ -162,7 +162,7 @@ <h2 id="links-and-references">
162162
This is a <a href="#">link</a>. It sends you to the top of this page. Links can have markup inside them: <a href="#"><b>bold</b></a>, <a href="#"><i>italics</i></a>, <a href="#"><em>emphasis</em></a>, <a href="#">super<sup>script</sup></a>, <a href="#">sub<sub>script</sub></a>, and <a href="#"><code>code</code></a>. Links can also be nested <em><a href="#">inside</a></em> markup. Links cannot be nested inside each other. This link has no replacement text: <a href="#">#</a>. The text is filled in by odoc. This is a shorthand link: <a href="#">#</a>. The text is also filled in by odoc in this case.
163163
</p>
164164
<p>
165-
This is a reference to <a href="index.html#val-foo"><code>foo</code></a>. References can have replacement text: <a href="index.html#val-foo">the value foo</a>. Except for the special lookup support, references are pretty much just like links. The replacement text can have nested styles: <a href="index.html#val-foo"><b>bold</b></a>, <a href="index.html#val-foo"><i>italic</i></a>, <a href="index.html#val-foo"><em>emphasis</em></a>, <a href="index.html#val-foo">super<sup>script</sup></a>, <a href="index.html#val-foo">sub<sub>script</sub></a>, and <a href="index.html#val-foo"><code>code</code></a>. It's also possible to surround a reference in a style: <b><a href="index.html#val-foo"><code>foo</code></a></b>. References can't be nested inside references, and links and references can't be nested inside each other.
165+
This is a reference to <a href="#val-foo"><code>foo</code></a>. References can have replacement text: <a href="#val-foo">the value foo</a>. Except for the special lookup support, references are pretty much just like links. The replacement text can have nested styles: <a href="#val-foo"><b>bold</b></a>, <a href="#val-foo"><i>italic</i></a>, <a href="#val-foo"><em>emphasis</em></a>, <a href="#val-foo">super<sup>script</sup></a>, <a href="#val-foo">sub<sub>script</sub></a>, and <a href="#val-foo"><code>code</code></a>. It's also possible to surround a reference in a style: <b><a href="#val-foo"><code>foo</code></a></b>. References can't be nested inside references, and links and references can't be nested inside each other.
166166
</p>
167167
</aside>
168168
<h2 id="preformatted-text">
@@ -256,7 +256,7 @@ <h2 id="lists">
256256
and can include references
257257
</li>
258258
<li>
259-
<a href="index.html#val-foo"><code>foo</code></a>
259+
<a href="#val-foo"><code>foo</code></a>
260260
</li>
261261
</ul>
262262
</li>

test/html/expect/test_package+ml/Module/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ <h1>
5858
<a href="#type-result" class="anchor"></a><code><span class="keyword">type</span> <span>('a, 'b) result</span></code>
5959
</div>
6060
<div class="spec module-type" id="module-type-S6">
61-
<a href="#module-type-S6" class="anchor"></a><code><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-S6/index.html">S6</a> = <a href="module-type-S/index.html">S</a> <span class="keyword">with</span> <span class="keyword">type</span> <span>('a, 'b) <a href="module-type-S/index.html#type-w">w</a></span> := <span><span>(<span class="type-var">'a</span>,&nbsp;<span class="type-var">'b</span>)</span> <a href="index.html#type-result">result</a></span></code>
61+
<a href="#module-type-S6" class="anchor"></a><code><span class="keyword">module</span> <span class="keyword">type</span> <a href="module-type-S6/index.html">S6</a> = <a href="module-type-S/index.html">S</a> <span class="keyword">with</span> <span class="keyword">type</span> <span>('a, 'b) <a href="module-type-S/index.html#type-w">w</a></span> := <span><span>(<span class="type-var">'a</span>,&nbsp;<span class="type-var">'b</span>)</span> <a href="#type-result">result</a></span></code>
6262
</div>
6363
<div class="spec module" id="module-M'">
6464
<a href="#module-M'" class="anchor"></a><code><span class="keyword">module</span> <a href="M'/index.html">M'</a> : <span class="keyword">sig</span> ... <span class="keyword">end</span></code>

test/html/expect/test_package+ml/Nested/F/argument-1-Arg1/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ <h2 id="values">
5151
</h2>
5252
<div>
5353
<div class="spec value" id="val-y">
54-
<a href="#val-y" class="anchor"></a><code><span class="keyword">val</span> y : <a href="index.html#type-t">t</a></code>
54+
<a href="#val-y" class="anchor"></a><code><span class="keyword">val</span> y : <a href="#type-t">t</a></code>
5555
</div>
5656
<div>
5757
<p>

test/html/expect/test_package+ml/Nested/X/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ <h2 id="values">
5757
</h2>
5858
<div>
5959
<div class="spec value" id="val-x">
60-
<a href="#val-x" class="anchor"></a><code><span class="keyword">val</span> x : <a href="index.html#type-t">t</a></code>
60+
<a href="#val-x" class="anchor"></a><code><span class="keyword">val</span> x : <a href="#type-t">t</a></code>
6161
</div>
6262
<div>
6363
<p>

test/html/expect/test_package+ml/Nested/module-type-Y/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ <h2 id="values">
5757
</h2>
5858
<div>
5959
<div class="spec value" id="val-y">
60-
<a href="#val-y" class="anchor"></a><code><span class="keyword">val</span> y : <a href="index.html#type-t">t</a></code>
60+
<a href="#val-y" class="anchor"></a><code><span class="keyword">val</span> y : <a href="#type-t">t</a></code>
6161
</div>
6262
<div>
6363
<p>

0 commit comments

Comments
 (0)