Skip to content

Commit 8399da5

Browse files
committed
address review comments
Signed-off-by: lubegasimon <[email protected]>
1 parent b0cc543 commit 8399da5

File tree

3 files changed

+93
-61
lines changed

3 files changed

+93
-61
lines changed

src/markdown/generator.ml

Lines changed: 89 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -8,23 +8,26 @@ module Markup = struct
88
| Block of t list
99
| Concat of t list
1010
| Break
11+
| Space
1112
| Anchor of string
1213
| String of string
13-
| Env of indent * string * t * string
14-
| Command of string * t
14+
| Backticks
15+
| Nbsp
1516
| OpenSqBracket
1617
| CloseSqBracket
1718
| OpenParenthesis
1819
| CloseParenthesis
1920

20-
and indent = Any
21-
22-
let env indent s s' t = Env (indent, s, t, s')
23-
2421
let noop = Concat []
2522

2623
let break = Break
2724

25+
let nbsp = Nbsp
26+
27+
let space = Space
28+
29+
let backticks = Backticks
30+
2831
let open_sq_bracket, close_sq_bracket = (OpenSqBracket, CloseSqBracket)
2932

3033
let open_parenthesis, close_parenthesis = (OpenParenthesis, CloseParenthesis)
@@ -38,7 +41,7 @@ module Markup = struct
3841

3942
let ( ++ ) = append
4043

41-
let concat = List.fold_left ( ++ ) (Concat [])
44+
let concat ts = Concat ts
4245

4346
let rec intersperse ~sep = function
4447
| [] -> []
@@ -47,11 +50,13 @@ module Markup = struct
4750

4851
let list ?(sep = Concat []) l = concat @@ intersperse ~sep l
4952

50-
let str fmt = Format.ksprintf (fun s -> String s) fmt
53+
let anchor' s = Anchor s
54+
55+
let string s = String s
5156

52-
let escaped fmt = Format.ksprintf (fun s -> String s) fmt
57+
let str fmt = Format.ksprintf (fun s -> string s) fmt
5358

54-
let command s t = Command (s, t)
59+
let escaped fmt = Format.ksprintf (fun s -> string s) fmt
5560

5661
let rec pp fmt t =
5762
match t with
@@ -65,13 +70,17 @@ module Markup = struct
6570
inner b
6671
| Concat l -> pp_many fmt l
6772
| Break -> Format.fprintf fmt "@\n"
73+
| Space -> Format.fprintf fmt " "
6874
| Anchor s -> Format.fprintf fmt "<a id=\"%s\"></a>" s
6975
| String s -> Format.fprintf fmt "%s" s
70-
| Env (indent, s, t, s') ->
71-
Format.fprintf fmt "@[%s%s%a@]@,%s@]"
72-
(match indent with Any -> "")
73-
s pp t s'
74-
| Command (s, t) -> Format.fprintf fmt "@[%s%a@]" s pp t
76+
(* We use double backticks to take care of polymorphic variants or content
77+
within backtick, and the spaces before and after the backticks for
78+
clarity on what should be enclosed in backticks. For example,
79+
"type nums = [ | `One | `Two ]" would be rendered as "``|`````Monday`` "
80+
if the spaces were not there.
81+
*)
82+
| Backticks -> Format.fprintf fmt " `` "
83+
| Nbsp -> Format.fprintf fmt "&nbsp; "
7584
| OpenSqBracket -> Format.fprintf fmt "["
7685
| CloseSqBracket -> Format.fprintf fmt "]"
7786
| OpenParenthesis -> Format.fprintf fmt "("
@@ -89,12 +98,17 @@ let raw_markup (_ : Raw_markup.t) = noop
8998

9099
let style (style : style) content =
91100
match style with
92-
| `Bold -> command "**" (content ++ str "**")
93-
| `Italic | `Emphasis -> command "_" (content ++ str "_")
94-
| `Superscript -> command "<sup>" content
95-
| `Subscript -> command "<sub>" content
101+
| `Bold -> string "**" ++ (content ++ str "**")
102+
| `Italic | `Emphasis -> string "_" ++ (content ++ str "_")
103+
| `Superscript -> string "<sup>" ++ content
104+
| `Subscript -> string "<sub>" ++ content
105+
106+
(*I'm not sure if `make_hashes` is the best name to use! *)
107+
let make_hashes n = String.make n '#'
108+
109+
type args = { generate_links : bool ref }
96110

97-
let generate_links = ref false
111+
let args = { generate_links = ref true }
98112

99113
let rec source_code (s : Source.t) =
100114
match s with
@@ -114,42 +128,50 @@ and inline (l : Inline.t) =
114128
| Text _ ->
115129
let l, _, rest =
116130
Doctree.Take.until l ~classify:(function
117-
| { Inline.desc = Text s; _ } -> Accum [ s ]
131+
| { Inline.desc = Text s; _ } -> (
132+
match s with
133+
| "end" ->
134+
Accum
135+
[
136+
break ++ space
137+
++ string (make_hashes 6)
138+
++ space ++ nbsp ++ string s;
139+
]
140+
| _ -> Accum [ str "%s" s ])
118141
| _ -> Stop_and_keep)
119142
in
120-
str {|%s|} (String.concat "" l) ++ inline rest
143+
concat l ++ inline rest
121144
| Entity e ->
122145
let x = entity e in
123146
x ++ inline rest
124147
| Styled (sty, content) -> style sty (inline content) ++ inline rest
125148
| Linebreak -> break ++ inline rest
126149
| Link (href, content) ->
127-
if !generate_links then
128-
(let rec f (content : Inline.t) =
129-
match content with
130-
| [] -> noop
131-
| i :: rest -> (
132-
match i.desc with
133-
| Text s ->
134-
command "" (str "[%s]" s ++ str "(%s)" href) ++ f rest
135-
| _ -> noop ++ f rest)
136-
in
137-
f content)
138-
++ inline rest
150+
if !(args.generate_links) then
151+
match content with
152+
| [] -> noop
153+
| i :: rest ->
154+
(match i.desc with
155+
| Text _ ->
156+
open_sq_bracket ++ inline content ++ close_sq_bracket
157+
++ open_parenthesis ++ string href ++ close_parenthesis
158+
++ inline rest
159+
| _ -> inline content ++ inline rest)
160+
++ inline rest
139161
else inline content ++ inline rest
140162
| InternalLink (Resolved (link, content)) ->
141-
if !generate_links then
163+
if !(args.generate_links) then
142164
match link.page.parent with
143165
| Some _ -> inline content ++ inline rest
144166
| None ->
145167
open_sq_bracket ++ inline content ++ close_sq_bracket
146168
++ open_parenthesis
147-
++ String ("#" ^ link.anchor)
169+
++ string (make_hashes 1 ^ link.anchor)
148170
++ close_parenthesis ++ inline rest
149171
else inline content ++ inline rest
150172
| InternalLink (Unresolved content) -> inline content ++ inline rest
151173
| Source content ->
152-
env Any "`` " "`` " (source_code content) ++ inline rest
174+
backticks ++ source_code content ++ backticks ++ inline rest
153175
| Raw_markup t -> raw_markup t ++ inline rest)
154176

155177
let rec block (l : Block.t) =
@@ -165,7 +187,7 @@ let rec block (l : Block.t) =
165187
let bullet =
166188
match list_typ with
167189
| Unordered -> escaped "- "
168-
| Ordered -> str "%d) " (n + 1)
190+
| Ordered -> str "%d. " (n + 1)
169191
in
170192
bullet ++ block b ++ break
171193
in
@@ -183,7 +205,10 @@ let rec block (l : Block.t) =
183205
in
184206
list ~sep:break (List.map f descrs) ++ continue rest
185207
| Source content -> source_code content ++ continue rest
186-
| Verbatim content -> str "%s" content ++ continue rest
208+
(*TODO: I'm not sure if indenting using spaces is the better way, or
209+
creating an indent constructor*)
210+
| Verbatim content ->
211+
space ++ space ++ space ++ str "%s" content ++ continue rest
187212
| Raw_markup t -> raw_markup t ++ continue rest)
188213

189214
let expansion_not_inlined url = not (Link.should_inline url)
@@ -202,24 +227,28 @@ let take_code l =
202227
let heading { Heading.label; level; title } =
203228
let level =
204229
match level with
205-
| 1 -> "#"
206-
| 2 -> "##"
207-
| 3 -> "###"
208-
| 4 -> "####"
209-
| 5 -> "#####"
210-
| 6 -> "######"
230+
(*TODO: We may want to create markup type for these! *)
231+
| 1 -> make_hashes 1
232+
| 2 -> make_hashes 2
233+
| 3 -> make_hashes 3
234+
| 4 -> make_hashes 4
235+
| 5 -> make_hashes 5
236+
| 6 -> make_hashes 6
211237
| _ -> ""
212238
(* We can be sure that h6 will never be exceded! *)
213239
in
214240
match label with
215-
| Some _ -> command level (str " " ++ inline title)
216-
| None -> command (level ^ " ") (inline title)
241+
| Some _ -> (
242+
match level with
243+
| "#" -> string level ++ (space ++ inline title)
244+
| _ -> string level ++ (space ++ inline title ++ break ++ str "---"))
245+
| None -> string level ++ space ++ inline title
217246

218247
let inline_subpage = function
219248
| `Inline | `Open | `Default -> true
220249
| `Closed -> false
221250

222-
let item_prop nbsp = String ("###### " ^ nbsp)
251+
let item_prop nbsp = string (make_hashes 6) ++ space ++ nbsp
223252

224253
let rec documented_src (l : DocumentedSrc.t) nbsp =
225254
match l with
@@ -255,8 +284,8 @@ let rec documented_src (l : DocumentedSrc.t) nbsp =
255284
| `N l -> documented_src l nbsp
256285
in
257286
let anchor = match anchor with Some a -> a.anchor | None -> "" in
258-
break ++ break ++ Anchor anchor ++ break ++ item_prop nbsp
259-
++ content ++ break ++ break ++ str " " ++ doc ++ break ++ break
287+
break ++ break ++ anchor' anchor ++ break ++ item_prop nbsp
288+
++ content ++ break ++ break ++ space ++ doc ++ break ++ break
260289
in
261290
let l = list ~sep:noop (List.map f lines) in
262291
l ++ continue rest)
@@ -277,11 +306,11 @@ and item nbsp (l : Item.t list) : Markup.t =
277306
| Text b -> block b ++ continue rest
278307
| Heading h -> break ++ heading h ++ break ++ continue rest
279308
| Declaration { attr = _; anchor; content; doc } ->
280-
let nbsp' = "&nbsp; &nbsp; &nbsp;" in
281-
let decl = documented_src content (nbsp ^ nbsp') in
309+
let nbsp' = nbsp ++ nbsp ++ nbsp in
310+
let decl = documented_src content (nbsp ++ nbsp') in
282311
let doc = match doc with [] -> noop | doc -> block doc ++ break in
283312
let anchor = match anchor with Some x -> x.anchor | None -> "" in
284-
Anchor anchor ++ break ++ item_prop nbsp ++ decl ++ break ++ break
313+
anchor' anchor ++ break ++ item_prop nbsp ++ decl ++ break ++ break
285314
++ doc ++ continue rest
286315
| Include
287316
{ attr = _; anchor = _; content = { summary; status; content }; doc }
@@ -299,19 +328,20 @@ let on_sub subp =
299328
| `Page p -> if Link.should_inline p.Subpage.content.url then Some 1 else None
300329
| `Include incl -> if inline_subpage incl.Include.status then Some 0 else None
301330

302-
let rec calc_subpages (generate_links : bool) { Subpage.content; _ } =
331+
let rec calc_subpages { Subpage.content; _ } (generate_links : bool) =
303332
[ page generate_links content ]
304333

305-
and subpages generate_links i =
306-
Utils.flatmap ~f:(calc_subpages generate_links) @@ Doctree.Subpages.compute i
334+
and subpages p (generate_links : bool) =
335+
Utils.flatmap ~f:(fun sp -> calc_subpages sp generate_links)
336+
@@ Doctree.Subpages.compute p
307337

308338
and page generate_links ({ Page.header; items; url; _ } as p) =
309339
let header = Shift.compute ~on_sub header in
310340
let items = Shift.compute ~on_sub items in
311-
let subpages = subpages generate_links p in
341+
let subpages = subpages p generate_links in
312342
Block
313343
([ Inline (Link.for_printing url) ]
314-
@ [ item "&nbsp; " header ++ item "&nbsp; " items ]
344+
@ [ item nbsp header ++ item nbsp items ]
315345
@ subpages)
316346

317347
let rec subpage subp =
@@ -320,7 +350,7 @@ let rec subpage subp =
320350

321351
and render (p : Page.t) =
322352
let content fmt =
323-
Format.fprintf fmt "%a" Markup.pp (page !generate_links p)
353+
Format.fprintf fmt "%a" Markup.pp (page !(args.generate_links) p)
324354
in
325355
let children = Utils.flatmap ~f:subpage @@ Subpages.compute p in
326356
let filename = Link.as_filename p.url in

src/markdown/generator.mli

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1-
val generate_links : bool ref
1+
type args = { generate_links : bool ref }
2+
3+
val args : args
24

35
val render : Odoc_document.Types.Page.t -> Odoc_document.Renderer.page

src/odoc/markdown.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ type args = { generate_links : bool }
44

55
let render args (page : Odoc_document.Types.Page.t) :
66
Odoc_document.Renderer.page =
7-
Odoc_markdown.Generator.generate_links := args.generate_links;
7+
Odoc_markdown.Generator.args.generate_links := args.generate_links;
88
Odoc_markdown.Generator.render page
99

1010
let files_of_url url = Odoc_markdown.Link.files_of_url url

0 commit comments

Comments
 (0)