Skip to content

Commit 6403717

Browse files
gpetiotjonludlam
authored andcommitted
Handle '+' to separate columns in 'align' row
1 parent f9eb88d commit 6403717

File tree

3 files changed

+137
-22
lines changed

3 files changed

+137
-22
lines changed

src/dune

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
(public_name odoc-parser)
66
(instrumentation
77
(backend bisect_ppx))
8+
(inline_tests)
9+
(preprocess
10+
(pps ppx_expect))
811
(flags
912
(:standard -w -50))
1013
(libraries astring result camlp-streams))

src/syntax.ml

Lines changed: 69 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,33 +40,80 @@ let peek input =
4040

4141
module Table = struct
4242
module Light_syntax = struct
43+
let split_on_plus w =
44+
let len = String.length w in
45+
match w with
46+
| "+" -> [ "" ]
47+
| _ when len > 1 ->
48+
let plus = function '+' -> true | _ -> false in
49+
let w =
50+
if plus (String.get w 0) then String.sub w 1 (len - 1) else w
51+
in
52+
let len = String.length w in
53+
let w =
54+
if plus (String.get w (len - 1)) then String.sub w 0 (len - 1)
55+
else w
56+
in
57+
String.split_on_char '+' w
58+
| _ -> [ w ]
59+
60+
let%expect_test _ =
61+
let f x =
62+
let pp x = Printf.printf "%S " x in
63+
List.iter pp (split_on_plus x)
64+
in
65+
f "";
66+
[%expect {| "" |}];
67+
f "+";
68+
[%expect {| "" |}];
69+
f "++";
70+
[%expect {| "" |}];
71+
f "+--+";
72+
[%expect {| "--" |}];
73+
f "--";
74+
[%expect {| "--" |}];
75+
f "--+--+--";
76+
[%expect {| "--" "--" "--" |}];
77+
f "+----+----+----+";
78+
[%expect {| "----" "----" "----" |}]
79+
4380
let valid_align = function
44-
| [ { Loc.value = `Word w; _ } ] -> (
45-
match String.length w with
46-
| 0 -> `Valid None
47-
| 1 -> (
48-
match w with
49-
| "-" -> `Valid None
50-
| ":" -> `Valid (Some `Center)
51-
| _ -> `Invalid)
52-
| len ->
53-
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2)) then
54-
match (String.get w 0, String.get w (len - 1)) with
55-
| ':', ':' -> `Valid (Some `Center)
56-
| ':', '-' -> `Valid (Some `Left)
57-
| '-', ':' -> `Valid (Some `Right)
58-
| '-', '-' -> `Valid None
59-
| _ -> `Invalid
60-
else `Invalid)
61-
| _ -> `Invalid
81+
| [ { Loc.value = `Word w; _ } ] ->
82+
(* We consider [+----+----+----+] a valid row, as it is a common format. *)
83+
let valid_word w =
84+
match String.length w with
85+
| 0 -> `Valid None
86+
| 1 -> (
87+
match w with
88+
| "-" -> `Valid None
89+
| ":" -> `Valid (Some `Center)
90+
| _ -> `Invalid)
91+
| len ->
92+
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2))
93+
then
94+
match (String.get w 0, String.get w (len - 1)) with
95+
| ':', ':' -> `Valid (Some `Center)
96+
| ':', '-' -> `Valid (Some `Left)
97+
| '-', ':' -> `Valid (Some `Right)
98+
| '-', '-' -> `Valid None
99+
| _ -> `Invalid
100+
else `Invalid
101+
in
102+
List.map valid_word (split_on_plus w)
103+
| _ -> [ `Invalid ]
62104

63105
let valid_align_row lx =
64106
let rec loop acc = function
65107
| [] -> Some (List.rev acc)
66-
| x :: q -> (
67-
match valid_align x with
68-
| `Invalid -> None
69-
| `Valid alignment -> loop (alignment :: acc) q)
108+
| x :: q ->
109+
let all_aligns = valid_align x in
110+
let valid_aligns =
111+
List.filter_map
112+
(function `Valid a -> Some a | `Invalid -> None)
113+
all_aligns
114+
in
115+
if List.(length valid_aligns < length all_aligns) then None
116+
else loop (List.rev_append valid_aligns acc) q
70117
in
71118
loop [] lx
72119

test/test_tables.ml

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -751,5 +751,70 @@ let%expect_test _ =
751751
(((f.ml (5 7) (5 8)) (paragraph (((f.ml (5 7) (5 8)) (word x)))))))))))
752752
(align (default default))))))
753753
(warnings ())) |}]
754+
755+
let with_pluses =
756+
test
757+
{|
758+
{t
759+
xx | yy | zz
760+
---------+-----------+--------------
761+
}
762+
763+
{t
764+
xx | yy | zz
765+
+---------+-----------+-------------+
766+
}
767+
768+
{t
769+
xx | yy | zz
770+
+---------|-----------+-------------|
771+
}
772+
|};
773+
[%expect
774+
{|
775+
((output
776+
(((f.ml (2 8) (5 9))
777+
(table (syntax light)
778+
(data
779+
((row
780+
((header
781+
(((f.ml (3 8) (3 10))
782+
(paragraph (((f.ml (3 8) (3 10)) (word xx)))))))
783+
(header
784+
(((f.ml (3 19) (3 21))
785+
(paragraph (((f.ml (3 19) (3 21)) (word yy)))))))
786+
(header
787+
(((f.ml (3 31) (3 33))
788+
(paragraph (((f.ml (3 31) (3 33)) (word zz)))))))))))
789+
(align (default default default))))
790+
((f.ml (7 8) (10 9))
791+
(table (syntax light)
792+
(data
793+
((row
794+
((header
795+
(((f.ml (8 9) (8 11))
796+
(paragraph (((f.ml (8 9) (8 11)) (word xx)))))))
797+
(header
798+
(((f.ml (8 20) (8 22))
799+
(paragraph (((f.ml (8 20) (8 22)) (word yy)))))))
800+
(header
801+
(((f.ml (8 32) (8 34))
802+
(paragraph (((f.ml (8 32) (8 34)) (word zz)))))))))))
803+
(align (default default default))))
804+
((f.ml (12 8) (15 9))
805+
(table (syntax light)
806+
(data
807+
((row
808+
((header
809+
(((f.ml (13 9) (13 11))
810+
(paragraph (((f.ml (13 9) (13 11)) (word xx)))))))
811+
(header
812+
(((f.ml (13 20) (13 22))
813+
(paragraph (((f.ml (13 20) (13 22)) (word yy)))))))
814+
(header
815+
(((f.ml (13 32) (13 34))
816+
(paragraph (((f.ml (13 32) (13 34)) (word zz)))))))))))
817+
(align (default default default))))))
818+
(warnings ())) |}]
754819
end in
755820
()

0 commit comments

Comments
 (0)