Skip to content

Commit e80d5a0

Browse files
committed
Render 6-heading only for declarations
Avoid adding headings for bits of code like '}' or 'end'.
1 parent 812289d commit e80d5a0

File tree

4 files changed

+64
-41
lines changed

4 files changed

+64
-41
lines changed

src/markdown/generator.ml

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,16 @@ let item_heading nesting_level content =
162162
in
163163
paragraph (pre_hash ++ pre_nbsp ++ content)
164164

165+
let take_code l =
166+
let c, _, rest =
167+
Take.until l ~classify:(function
168+
| DocumentedSrc.Code c -> Accum c
169+
| _ -> Stop_and_keep)
170+
in
171+
(c, rest)
172+
165173
let rec documented_src (l : DocumentedSrc.t) args nesting_level =
166174
let noop = paragraph noop in
167-
let take_code l =
168-
let c, _, rest =
169-
Take.until l ~classify:(function
170-
| DocumentedSrc.Code c -> Accum c
171-
| _ -> Stop_and_keep)
172-
in
173-
(c, rest)
174-
in
175175
match l with
176176
| [] -> noop
177177
| line :: rest -> (
@@ -182,10 +182,8 @@ let rec documented_src (l : DocumentedSrc.t) args nesting_level =
182182
| Code s ->
183183
if source_contains_text s then
184184
let c, rest = take_code l in
185-
blocks
186-
(item_heading nesting_level (source_code c args))
187-
(continue rest)
188-
else noop
185+
paragraph (source_code c args) +++ continue rest
186+
else continue rest
189187
| Alternative (Expansion { url; expansion; _ }) ->
190188
if should_inline url then documented_src expansion args nesting_level
191189
else continue rest
@@ -251,16 +249,36 @@ and item (l : Item.t list) args nesting_level =
251249
| None -> paragraph title
252250
in
253251
blocks heading' (continue rest)
254-
| Declaration { attr = _; anchor; content; doc } ->
255-
let decl = documented_src content args nesting_level in
252+
| Declaration { attr = _; anchor; content; doc } -> (
253+
(*
254+
A declaration render like this:
255+
256+
{v
257+
<a id="<id>"></a>
258+
###### <nesting_level> <code from content>
259+
260+
<rest of content, possibly big>
261+
262+
<doc>
263+
v}
264+
*)
256265
let doc =
257266
match doc with [] -> noop | doc -> paragraph (text (acc_text doc))
267+
and anchor =
268+
if args.generate_links then
269+
let anchor =
270+
match anchor with Some x -> x.anchor | None -> ""
271+
in
272+
paragraph (anchor' anchor)
273+
else noop
258274
in
259-
let item' = blocks decl doc in
260-
if args.generate_links then
261-
let anchor = match anchor with Some x -> x.anchor | None -> "" in
262-
blocks (blocks (paragraph (anchor' anchor)) item') (continue rest)
263-
else blocks item' (continue rest)
275+
match take_code content with
276+
| [], _ -> assert false (* Content doesn't begin with code ? *)
277+
| begin_code, tl ->
278+
anchor
279+
+++ item_heading nesting_level (source_code begin_code args)
280+
+++ documented_src tl args nesting_level
281+
+++ doc +++ continue rest)
264282
| Include { content = { summary; status; content }; _ } ->
265283
let inline_subpage = function
266284
| `Inline | `Open | `Default -> true

src/markdown/markup.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ let join left right = Join (left, right)
3333

3434
let blocks above below = ConcatB (above, below)
3535

36+
let ( +++ ) = blocks
37+
3638
let block_separator = Block_separator
3739

3840
let text s = String s

src/markdown/markup.mli

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ val ordered_list : blocks list -> blocks
1818

1919
val unordered_list : blocks list -> blocks
2020

21+
val ( +++ ) : blocks -> blocks -> blocks
22+
(** Alias for {!blocks} *)
23+
2124
val blocks : blocks -> blocks -> blocks
2225
(** Combine blocks. *)
2326

test/integration/markdown.t/run.t

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -29,33 +29,33 @@
2929

3030
###### module type Foo'
3131

32-
###### = sig
32+
= sig
3333

3434
######    type foo
3535

36-
###### end
36+
end
3737

3838
###### module Bar
3939

4040
###### module type Bar'
4141

42-
###### = sig
42+
= sig
4343

4444
######    type bar'
4545

46-
###### end
46+
end
4747

4848
###### module type Foo
4949

50-
###### = sig
50+
= sig
5151

5252
######    type foo
5353

5454
######    type bar'
5555

5656
######    module type Foo'
5757

58-
######    = sig
58+
= sig
5959

6060
######      type foo'
6161

@@ -71,19 +71,19 @@
7171

7272
Docs for `` `One``
7373

74-
######      ]
74+
]
7575

76-
######    end
76+
end
7777

78-
###### end
78+
end
7979

8080
###### type other_names = {
8181

8282
######    given : string ;
8383

8484
######    nickname : string ;
8585

86-
###### }
86+
}
8787

8888
###### type name = {
8989

@@ -95,7 +95,7 @@
9595

9696
######    others : other_names ;
9797

98-
###### }
98+
}
9999

100100
## Label
101101

@@ -211,13 +211,13 @@
211211

212212
###### module type Foo'
213213

214-
###### = sig
214+
= sig
215215

216216
<a id="type-foo"></a>
217217

218218
######    type foo
219219

220-
###### end
220+
end
221221

222222
<a id="module-Bar"></a>
223223

@@ -227,19 +227,19 @@
227227

228228
###### module type Bar'
229229

230-
###### = sig
230+
= sig
231231

232232
<a id="type-bar'"></a>
233233

234234
######    type bar'
235235

236-
###### end
236+
end
237237

238238
<a id="module-type-Foo"></a>
239239

240240
###### module type Foo
241241

242-
###### = sig
242+
= sig
243243

244244
<a id="type-foo"></a>
245245

@@ -253,7 +253,7 @@
253253

254254
######    module type Foo'
255255

256-
######    = sig
256+
= sig
257257

258258
<a id="type-foo'"></a>
259259

@@ -279,11 +279,11 @@
279279

280280
Docs for `` `One``
281281

282-
######      ]
282+
]
283283

284-
######    end
284+
end
285285

286-
###### end
286+
end
287287

288288
<a id="type-other_names"></a>
289289

@@ -297,7 +297,7 @@
297297

298298
######    nickname : string ;
299299

300-
###### }
300+
}
301301

302302
<a id="type-name"></a>
303303

@@ -317,7 +317,7 @@
317317

318318
######    others : [other_names](#type-other_names) ;
319319

320-
###### }
320+
}
321321

322322
## Label
323323

0 commit comments

Comments
 (0)