@@ -8,23 +8,26 @@ module Markup = struct
8
8
| Block of t list
9
9
| Concat of t list
10
10
| Break
11
+ | Space
11
12
| Anchor of string
12
13
| String of string
13
- | Env of indent * string * t * string
14
- | Command of string * t
14
+ | Backticks
15
+ | Nbsp
15
16
| OpenSqBracket
16
17
| CloseSqBracket
17
18
| OpenParenthesis
18
19
| CloseParenthesis
19
20
20
- and indent = Any
21
-
22
- let env indent s s' t = Env (indent, s, t, s')
23
-
24
21
let noop = Concat []
25
22
26
23
let break = Break
27
24
25
+ let nbsp = Nbsp
26
+
27
+ let space = Space
28
+
29
+ let backticks = Backticks
30
+
28
31
let open_sq_bracket, close_sq_bracket = (OpenSqBracket , CloseSqBracket )
29
32
30
33
let open_parenthesis, close_parenthesis = (OpenParenthesis , CloseParenthesis )
@@ -38,7 +41,7 @@ module Markup = struct
38
41
39
42
let ( ++ ) = append
40
43
41
- let concat = List. fold_left ( ++ ) ( Concat [] )
44
+ let concat ts = Concat ts
42
45
43
46
let rec intersperse ~sep = function
44
47
| [] -> []
@@ -47,11 +50,13 @@ module Markup = struct
47
50
48
51
let list ?(sep = Concat [] ) l = concat @@ intersperse ~sep l
49
52
50
- let str fmt = Format. ksprintf (fun s -> String s) fmt
53
+ let anchor' s = Anchor s
54
+
55
+ let string s = String s
51
56
52
- let escaped fmt = Format. ksprintf (fun s -> String s) fmt
57
+ let str fmt = Format. ksprintf (fun s -> string s) fmt
53
58
54
- let command s t = Command (s, t)
59
+ let escaped fmt = Format. ksprintf ( fun s -> string s) fmt
55
60
56
61
let rec pp fmt t =
57
62
match t with
@@ -65,13 +70,17 @@ module Markup = struct
65
70
inner b
66
71
| Concat l -> pp_many fmt l
67
72
| Break -> Format. fprintf fmt " @\n "
73
+ | Space -> Format. fprintf fmt " "
68
74
| Anchor s -> Format. fprintf fmt " <a id=\" %s\" ></a>" s
69
75
| 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 " "
75
84
| OpenSqBracket -> Format. fprintf fmt " ["
76
85
| CloseSqBracket -> Format. fprintf fmt " ]"
77
86
| OpenParenthesis -> Format. fprintf fmt " ("
@@ -89,12 +98,17 @@ let raw_markup (_ : Raw_markup.t) = noop
89
98
90
99
let style (style : style ) content =
91
100
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 }
96
110
97
- let generate_links = ref false
111
+ let args = { generate_links = ref true }
98
112
99
113
let rec source_code (s : Source.t ) =
100
114
match s with
@@ -114,42 +128,50 @@ and inline (l : Inline.t) =
114
128
| Text _ ->
115
129
let l, _, rest =
116
130
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 ])
118
141
| _ -> Stop_and_keep )
119
142
in
120
- str { |% s | } ( String. concat " " l) ++ inline rest
143
+ concat l ++ inline rest
121
144
| Entity e ->
122
145
let x = entity e in
123
146
x ++ inline rest
124
147
| Styled (sty , content ) -> style sty (inline content) ++ inline rest
125
148
| Linebreak -> break ++ inline rest
126
149
| 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
139
161
else inline content ++ inline rest
140
162
| InternalLink (Resolved (link , content )) ->
141
- if ! generate_links then
163
+ if ! (args. generate_links) then
142
164
match link.page.parent with
143
165
| Some _ -> inline content ++ inline rest
144
166
| None ->
145
167
open_sq_bracket ++ inline content ++ close_sq_bracket
146
168
++ open_parenthesis
147
- ++ String ( " # " ^ link.anchor)
169
+ ++ string (make_hashes 1 ^ link.anchor)
148
170
++ close_parenthesis ++ inline rest
149
171
else inline content ++ inline rest
150
172
| InternalLink (Unresolved content ) -> inline content ++ inline rest
151
173
| Source content ->
152
- env Any " `` " " `` " ( source_code content) ++ inline rest
174
+ backticks ++ source_code content ++ backticks ++ inline rest
153
175
| Raw_markup t -> raw_markup t ++ inline rest)
154
176
155
177
let rec block (l : Block.t ) =
@@ -165,7 +187,7 @@ let rec block (l : Block.t) =
165
187
let bullet =
166
188
match list_typ with
167
189
| Unordered -> escaped " - "
168
- | Ordered -> str " %d) " (n + 1 )
190
+ | Ordered -> str " %d. " (n + 1 )
169
191
in
170
192
bullet ++ block b ++ break
171
193
in
@@ -183,7 +205,10 @@ let rec block (l : Block.t) =
183
205
in
184
206
list ~sep: break (List. map f descrs) ++ continue rest
185
207
| 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
187
212
| Raw_markup t -> raw_markup t ++ continue rest)
188
213
189
214
let expansion_not_inlined url = not (Link. should_inline url)
@@ -202,24 +227,28 @@ let take_code l =
202
227
let heading { Heading. label; level; title } =
203
228
let level =
204
229
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
211
237
| _ -> " "
212
238
(* We can be sure that h6 will never be exceded! *)
213
239
in
214
240
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
217
246
218
247
let inline_subpage = function
219
248
| `Inline | `Open | `Default -> true
220
249
| `Closed -> false
221
250
222
- let item_prop nbsp = String ( " ###### " ^ nbsp)
251
+ let item_prop nbsp = string (make_hashes 6 ) ++ space ++ nbsp
223
252
224
253
let rec documented_src (l : DocumentedSrc.t ) nbsp =
225
254
match l with
@@ -255,8 +284,8 @@ let rec documented_src (l : DocumentedSrc.t) nbsp =
255
284
| `N l -> documented_src l nbsp
256
285
in
257
286
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
260
289
in
261
290
let l = list ~sep: noop (List. map f lines) in
262
291
l ++ continue rest)
@@ -277,11 +306,11 @@ and item nbsp (l : Item.t list) : Markup.t =
277
306
| Text b -> block b ++ continue rest
278
307
| Heading h -> break ++ heading h ++ break ++ continue rest
279
308
| 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
282
311
let doc = match doc with [] -> noop | doc -> block doc ++ break in
283
312
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
285
314
++ doc ++ continue rest
286
315
| Include
287
316
{ attr = _; anchor = _; content = { summary; status; content }; doc }
@@ -299,19 +328,20 @@ let on_sub subp =
299
328
| `Page p -> if Link. should_inline p.Subpage. content.url then Some 1 else None
300
329
| `Include incl -> if inline_subpage incl.Include. status then Some 0 else None
301
330
302
- let rec calc_subpages ( generate_links : bool ) { Subpage. content; _ } =
331
+ let rec calc_subpages { Subpage. content; _ } ( generate_links : bool ) =
303
332
[ page generate_links content ]
304
333
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
307
337
308
338
and page generate_links ({ Page. header; items; url; _ } as p ) =
309
339
let header = Shift. compute ~on_sub header in
310
340
let items = Shift. compute ~on_sub items in
311
- let subpages = subpages generate_links p in
341
+ let subpages = subpages p generate_links in
312
342
Block
313
343
([ Inline (Link. for_printing url) ]
314
- @ [ item " & nbsp; " header ++ item " & nbsp; " items ]
344
+ @ [ item nbsp header ++ item nbsp items ]
315
345
@ subpages)
316
346
317
347
let rec subpage subp =
@@ -320,7 +350,7 @@ let rec subpage subp =
320
350
321
351
and render (p : Page.t ) =
322
352
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)
324
354
in
325
355
let children = Utils. flatmap ~f: subpage @@ Subpages. compute p in
326
356
let filename = Link. as_filename p.url in
0 commit comments