Skip to content

Commit 6ff5381

Browse files
committed
Rewrite InternalLink.t into a record
Allows to add more fields.
1 parent 7e01c74 commit 6ff5381

File tree

8 files changed

+49
-62
lines changed

8 files changed

+49
-62
lines changed

src/document/comment.ml

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,9 @@ module Reference = struct
6969
| None ->
7070
let s = source_of_code s in
7171
[ inline @@ Inline.Source s ]
72-
| Some s ->
73-
[ inline @@ Inline.InternalLink (InternalLink.Unresolved s) ])
72+
| Some content ->
73+
let link = { InternalLink.target = Unresolved; content } in
74+
[ inline @@ Inline.InternalLink link ])
7475
| `Dot (parent, s) -> unresolved ?text (parent :> t) s
7576
| `Module (parent, s) ->
7677
unresolved ?text (parent :> t) (ModuleName.to_string s)
@@ -100,25 +101,28 @@ module Reference = struct
100101
| `Resolved r -> (
101102
(* IDENTIFIER MUST BE RENAMED TO DEFINITION. *)
102103
let id = Reference.Resolved.identifier r in
103-
let txt =
104+
let content =
104105
match text with
105106
| None ->
106107
[ inline @@ Inline.Source (source_of_code (render_resolved r)) ]
107108
| Some s -> s
108109
in
109110
match Url.from_identifier ~stop_before id with
110111
| Ok url ->
111-
[ inline @@ Inline.InternalLink (InternalLink.Resolved (url, txt)) ]
112-
| Error (Not_linkable _) -> txt
112+
let target = InternalLink.Resolved url in
113+
[ inline @@ Inline.InternalLink { InternalLink.target; content } ]
114+
| Error (Not_linkable _) -> content
113115
| Error exn ->
114116
(* FIXME: better error message *)
115117
Printf.eprintf "Id.href failed: %S\n%!" (Url.Error.to_string exn);
116-
txt)
118+
content)
117119

118120
and unresolved : ?text:Inline.t -> Reference.t -> string -> Inline.t =
119121
fun ?text parent field ->
120122
match text with
121-
| Some s -> [ inline @@ InternalLink (InternalLink.Unresolved s) ]
123+
| Some content ->
124+
let link = { InternalLink.target = Unresolved; content } in
125+
[ inline @@ InternalLink link ]
122126
| None ->
123127
let tail = [ inline @@ Text ("." ^ field) ] in
124128
let content = to_ir ~stop_before:true parent in

src/document/doctree.ml

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -377,16 +377,12 @@ end = struct
377377
match x.desc with
378378
| Styled (_, x) -> inline x
379379
| Link (_, x) -> inline x
380-
| InternalLink x -> internallink x
380+
| InternalLink x -> inline x.content
381381
| Math _ -> true
382382
| Text _ | Entity _ | Linebreak | Source _ | Raw_markup _ -> false
383383
in
384384
List.exists inline_ x
385385

386-
and internallink : InternalLink.t -> bool = function
387-
| Resolved (_, x) -> inline x
388-
| Unresolved x -> inline x
389-
390386
and description : Description.t -> bool =
391387
fun x ->
392388
let description_ : Description.one -> bool =

src/document/generator.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ let type_var tv = tag "type-var" (O.txt tv)
3232

3333
let enclose ~l ~r x = O.span (O.txt l ++ x ++ O.txt r)
3434

35-
let path p txt =
36-
O.elt
37-
[ inline @@ InternalLink (InternalLink.Resolved (Url.from_path p, txt)) ]
35+
let resolved p content =
36+
let link = { InternalLink.target = Resolved p; content } in
37+
O.elt [ inline @@ InternalLink link ]
3838

39-
let resolved p txt =
40-
O.elt [ inline @@ InternalLink (InternalLink.Resolved (p, txt)) ]
39+
let path p content = resolved (Url.from_path p) content
4140

42-
let unresolved txt =
43-
O.elt [ inline @@ InternalLink (InternalLink.Unresolved txt) ]
41+
let unresolved content =
42+
let link = { InternalLink.target = Unresolved; content } in
43+
O.elt [ inline @@ InternalLink link ]
4444

4545
let path_to_id path =
4646
match Url.Anchor.from_identifier (path :> Paths.Identifier.t) with
@@ -1814,8 +1814,8 @@ module Make (Syntax : SYNTAX) = struct
18141814
in
18151815
let li ?(attr = []) name url =
18161816
let link url desc =
1817-
Inline.InternalLink
1818-
InternalLink.(Resolved (url, [ Inline.{ attr = []; desc } ]))
1817+
let content = [ Inline.{ attr = []; desc } ] in
1818+
Inline.InternalLink { InternalLink.target = Resolved url; content }
18191819
in
18201820
[ block ~attr @@ Block.Inline (inline @@ link url (Text name)) ]
18211821
in

src/document/types.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,9 @@ end =
66
Class
77

88
and InternalLink : sig
9-
type resolved = Url.t * Inline.t
9+
type target = Resolved of Url.t | Unresolved
1010

11-
type unresolved = Inline.t
12-
13-
type t = Resolved of resolved | Unresolved of Inline.t
11+
type t = { target : target; content : Inline.t }
1412
end =
1513
InternalLink
1614

src/document/utils.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,7 @@ and compute_length_inline (t : Types.Inline.t) : int =
3333
| Text s -> acc + String.length s
3434
| Entity _e -> acc + 1
3535
| Linebreak -> 0 (* TODO *)
36-
| Styled (_, t)
37-
| Link (_, t)
38-
| InternalLink (Resolved (_, t))
39-
| InternalLink (Unresolved t) ->
36+
| Styled (_, t) | Link (_, t) | InternalLink { content = t; _ } ->
4037
acc + compute_length_inline t
4138
| Source s -> acc + compute_length_source s
4239
| Math _ -> assert false

src/html/generator.ml

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -94,24 +94,21 @@ and styled style ~emph_level =
9494

9595
let rec internallink ~config ~emph_level ~resolve ?(a = []) (t : InternalLink.t)
9696
=
97-
match t with
98-
| Resolved (uri, content) ->
99-
let href = Link.href ~config ~resolve uri in
100-
let a = (a :> Html_types.a_attrib Html.attrib list) in
101-
let elt =
102-
Html.a ~a:(Html.a_href href :: a) (inline_nolink ~emph_level content)
103-
in
104-
let elt = (elt :> phrasing Html.elt) in
105-
[ elt ]
106-
| Unresolved content ->
107-
(* let title =
108-
* Html.a_title (Printf.sprintf "unresolved reference to %S"
109-
* (ref_to_string ref)
110-
* in *)
111-
let a = Html.a_class [ "xref-unresolved" ] :: a in
112-
let elt = Html.span ~a (inline ~config ~emph_level ~resolve content) in
113-
let elt = (elt :> phrasing Html.elt) in
114-
[ elt ]
97+
let elt =
98+
match t.target with
99+
| Resolved uri ->
100+
let href = Link.href ~config ~resolve uri in
101+
let a = (a :> Html_types.a_attrib Html.attrib list) in
102+
Html.a ~a:(Html.a_href href :: a) (inline_nolink ~emph_level t.content)
103+
| Unresolved ->
104+
(* let title =
105+
* Html.a_title (Printf.sprintf "unresolved reference to %S"
106+
* (ref_to_string ref)
107+
* in *)
108+
let a = Html.a_class [ "xref-unresolved" ] :: a in
109+
Html.span ~a (inline ~config ~emph_level ~resolve t.content)
110+
in
111+
[ (elt :> phrasing Html.elt) ]
115112

116113
and inline ~config ?(emph_level = 0) ~resolve (l : Inline.t) :
117114
phrasing Html.elt list =

src/latex/generator.ml

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -231,17 +231,14 @@ let source k (t : Source.t) =
231231
tokens t
232232

233233
let rec internalref ~verbatim ~in_source (t : InternalLink.t) =
234-
match t with
235-
| Resolved (uri, content) ->
236-
let target = Link.label uri in
237-
let text = Some (inline ~verbatim ~in_source content) in
238-
let short = in_source in
239-
Internal_ref { short; text; target }
240-
| Unresolved content ->
241-
let target = "xref-unresolved" in
242-
let text = Some (inline ~verbatim ~in_source content) in
243-
let short = in_source in
244-
Internal_ref { short; target; text }
234+
let target =
235+
match t.target with
236+
| InternalLink.Resolved uri -> Link.label uri
237+
| Unresolved -> "xref-unresolved"
238+
in
239+
let text = Some (inline ~verbatim ~in_source t.content) in
240+
let short = in_source in
241+
Internal_ref { short; target; text }
245242

246243
and inline ~in_source ~verbatim (l : Inline.t) =
247244
let one (t : Inline.one) =

src/manpage/generator.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,9 +238,7 @@ let strip l =
238238
{ h with desc = Styled (sty, List.rev @@ loop [] content) }
239239
in
240240
loop (h :: acc) t
241-
| Link (_, content)
242-
| InternalLink (Resolved (_, content))
243-
| InternalLink (Unresolved content) ->
241+
| Link (_, content) | InternalLink { content; _ } ->
244242
let acc = loop acc content in
245243
loop acc t
246244
| Source code ->
@@ -298,7 +296,7 @@ and inline (l : Inline.t) =
298296
| Styled (sty, content) -> style sty (inline content) ++ inline rest
299297
| Link (href, content) ->
300298
env "UR" "UE" href (inline @@ strip content) ++ inline rest
301-
| InternalLink (Resolved (_, content) | Unresolved content) ->
299+
| InternalLink { content; _ } ->
302300
font "CI" (inline @@ strip content) ++ inline rest
303301
| Source content -> source_code content ++ inline rest
304302
| Math s -> math s ++ inline rest

0 commit comments

Comments
 (0)