Skip to content

Commit 91e7cb0

Browse files
committed
Render formatted code out of headings
1 parent cba655d commit 91e7cb0

File tree

2 files changed

+65
-20
lines changed

2 files changed

+65
-20
lines changed

src/markdown/generator.ml

+53-16
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,27 @@ let rec source_contains_text (s : Source.t) =
3636
in
3737
List.exists check_source s
3838

39+
(** Split source code at the first [:] or [=]. *)
40+
let source_take_until_punctuation code =
41+
let rec is_punctuation s i =
42+
if i >= String.length s then false
43+
else
44+
match s.[i] with
45+
| ' ' -> is_punctuation s (i + 1)
46+
| ':' | '=' -> true
47+
| _ -> false
48+
in
49+
let is_punctuation i =
50+
List.exists
51+
(function
52+
| { Inline.desc = Text s; _ } -> is_punctuation s 0 | _ -> false)
53+
i
54+
in
55+
Take.until code ~classify:(function
56+
| Source.Elt i as t ->
57+
if is_punctuation i then Stop_and_accum ([ t ], None) else Accum [ t ]
58+
| Tag (_, c) -> Rec c)
59+
3960
let rec source_code (s : Source.t) args =
4061
match s with
4162
| [] -> noop
@@ -249,7 +270,7 @@ and item (l : Item.t list) args nesting_level =
249270
| None -> paragraph title
250271
in
251272
blocks heading' (continue rest)
252-
| Declaration { attr = _; anchor; content; doc } ->
273+
| Declaration { attr = _; anchor; content; doc } -> (
253274
(*
254275
Declarations render like this:
255276
@@ -262,18 +283,8 @@ and item (l : Item.t list) args nesting_level =
262283
<doc>
263284
v}
264285
*)
265-
let doc =
266-
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
274-
and begin_code, content =
286+
let take_code_from_declaration content =
275287
match take_code content with
276-
| [], _ -> assert false (* Content doesn't begin with code ? *)
277288
| begin_code, Alternative (Expansion e) :: tl
278289
when should_inline e.url ->
279290
(* Take the code from inlined expansion. For example, to catch
@@ -282,10 +293,36 @@ and item (l : Item.t list) args nesting_level =
282293
(begin_code @ e_code, e_tl @ tl)
283294
| begin_code, content -> (begin_code, content)
284295
in
285-
anchor
286-
+++ item_heading nesting_level (source_code begin_code args)
287-
+++ documented_src content args nesting_level
288-
+++ doc +++ continue rest
296+
let render_declaration ~anchor ~doc heading content =
297+
let doc =
298+
match doc with
299+
| [] -> noop
300+
| doc -> paragraph (text (acc_text doc))
301+
and anchor =
302+
if args.generate_links then
303+
let anchor =
304+
match anchor with Some x -> x.Url.Anchor.anchor | None -> ""
305+
in
306+
paragraph (anchor' anchor)
307+
else noop
308+
in
309+
anchor
310+
+++ item_heading nesting_level (source_code heading args)
311+
+++ content +++ doc +++ continue rest
312+
in
313+
match take_code_from_declaration content with
314+
| code, [] ->
315+
(* Declaration is only code, render formatted code. *)
316+
let code, _, content = source_take_until_punctuation code in
317+
let content =
318+
if source_contains_text content then
319+
paragraph (source_code content args)
320+
else noop
321+
in
322+
render_declaration ~anchor ~doc code content
323+
| code, content ->
324+
render_declaration ~anchor ~doc code
325+
(documented_src content args nesting_level))
289326
| Include { content = { summary; status; content }; _ } ->
290327
let inline_subpage = function
291328
| `Inline | `Open | `Default -> true

test/integration/markdown.t/run.t

+12-4
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,17 @@
3232

3333
<a id="val-x"></a>
3434

35-
###### val x : [t](#type-t)
35+
###### val x :
36+
37+
[t](#type-t)
3638

3739
Doc for `val x`
3840

3941
<a id="type-a"></a>
4042

41-
###### type a = [t](#type-t)
43+
###### type a =
44+
45+
[t](#type-t)
4246

4347
Type alias
4448

@@ -82,15 +86,19 @@
8286

8387
<a id="val-y"></a>
8488

85-
###### val y : [ `One | `Two ]
89+
###### val y :
90+
91+
[ `One | `Two ]
8692

8793
Polymorphic variant.
8894

8995
Floating comment.
9096

9197
<a id="val-z"></a>
9298

93-
###### val z : [t](#type-t) -> ( [t](#type-t) -> [t](#type-t) ) -> foo : [t](#type-t) -> ? bar : [t](#type-t) -> [ `One of [t](#type-t) ] -> [t](#type-t) * [t](#type-t)
99+
###### val z :
100+
101+
[t](#type-t) -> ( [t](#type-t) -> [t](#type-t) ) -> foo : [t](#type-t) -> ? bar : [t](#type-t) -> [ `One of [t](#type-t) ] -> [t](#type-t) * [t](#type-t)
94102

95103
Type complicated enough to be rendered differently.
96104

0 commit comments

Comments
 (0)