Skip to content

Commit f9eb88d

Browse files
panglesdjonludlam
authored andcommitted
Fix default alignment
This commit fixes several alignment issues. 1. No alignment in a cell was encoded as an alignment to `Center. "Centering" was certainly a typo, but there is also the issue that "no specified alignment" should really be different than "specified to ..." For instance (assuming `{R ...}` exists): ``` {R {t |aaa|bbb| |---|---| |xxx|yyy|}} ``` would not be right-aligned inside the table, with the previous behaviour. This commit allows for "unspecified alignment" using an option. 2. No alignment in a table was encoded as an empty list of alignment. This was wrong, since at some point we might want to raise warnings in case the number of alignment and the number of columns differ. Signed-off-by: Paul-Elliot <[email protected]>
1 parent 5ae0826 commit f9eb88d

File tree

4 files changed

+78
-56
lines changed

4 files changed

+78
-56
lines changed

src/ast.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ type inline_element =
3333
type 'a cell = 'a with_location list * [ `Header | `Data ]
3434
type 'a row = 'a cell list
3535
type 'a grid = 'a row list
36-
type 'a abstract_table = 'a grid * alignment list
36+
type 'a abstract_table = 'a grid * alignment option list option
3737

3838
type nestable_block_element =
3939
[ `Paragraph of inline_element with_location list

src/syntax.ml

Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -40,29 +40,35 @@ let peek input =
4040

4141
module Table = struct
4242
module Light_syntax = struct
43-
let default_align = `Center
44-
4543
let valid_align = function
4644
| [ { Loc.value = `Word w; _ } ] -> (
4745
match String.length w with
48-
| 0 -> Some default_align
46+
| 0 -> `Valid None
4947
| 1 -> (
5048
match w with
51-
| "-" -> Some default_align
52-
| ":" -> Some `Center
53-
| _ -> None)
49+
| "-" -> `Valid None
50+
| ":" -> `Valid (Some `Center)
51+
| _ -> `Invalid)
5452
| len ->
5553
if String.for_all (Char.equal '-') (String.sub w 1 (len - 2)) then
5654
match (String.get w 0, String.get w (len - 1)) with
57-
| ':', ':' -> Some `Center
58-
| ':', '-' -> Some `Left
59-
| '-', ':' -> Some `Right
60-
| '-', '-' -> Some default_align
61-
| _ -> None
62-
else None)
63-
| _ -> None
64-
65-
let valid_align_row lx = List.map valid_align lx |> Option.join_list
55+
| ':', ':' -> `Valid (Some `Center)
56+
| ':', '-' -> `Valid (Some `Left)
57+
| '-', ':' -> `Valid (Some `Right)
58+
| '-', '-' -> `Valid None
59+
| _ -> `Invalid
60+
else `Invalid)
61+
| _ -> `Invalid
62+
63+
let valid_align_row lx =
64+
let rec loop acc = function
65+
| [] -> Some (List.rev acc)
66+
| x :: q -> (
67+
match valid_align x with
68+
| `Invalid -> None
69+
| `Valid alignment -> loop (alignment :: acc) q)
70+
in
71+
loop [] lx
6672

6773
let create ~grid ~align : Ast.table =
6874
let to_block x = Loc.at x.Loc.location (`Paragraph [ x ]) in
@@ -76,33 +82,33 @@ module Table = struct
7682

7783
let from_raw_data grid : Ast.table =
7884
match grid with
79-
| [] -> create ~grid:[] ~align:[]
85+
| [] -> create ~grid:[] ~align:None
8086
| row1 :: rows2_N -> (
8187
match valid_align_row row1 with
8288
(* If the first line is the align row, everything else is data. *)
83-
| Some align ->
89+
| Some _ as align ->
8490
create ~grid:(List.map (with_kind `Data) rows2_N) ~align
8591
| None -> (
8692
match rows2_N with
8793
(* Only 1 line, if this is not the align row this is data. *)
88-
| [] -> create ~grid:[ with_kind `Data row1 ] ~align:[]
94+
| [] -> create ~grid:[ with_kind `Data row1 ] ~align:None
8995
| row2 :: rows3_N -> (
9096
match valid_align_row row2 with
9197
(* If the second line is the align row, the first one is the
9298
header and the rest is data. *)
93-
| Some align ->
99+
| Some _ as align ->
94100
let header = with_kind `Header row1 in
95101
let data = List.map (with_kind `Data) rows3_N in
96102
create ~grid:(header :: data) ~align
97103
(* No align row in the first 2 lines, everything is considered
98104
data. *)
99105
| None ->
100-
create ~grid:(List.map (with_kind `Data) grid) ~align:[]))
101-
)
106+
create ~grid:(List.map (with_kind `Data) grid) ~align:None
107+
)))
102108
end
103109

104110
module Heavy_syntax = struct
105-
let create ~grid : Ast.table = ((grid, []), `Heavy)
111+
let create ~grid : Ast.table = ((grid, None), `Heavy)
106112
let from_grid grid : Ast.table = create ~grid
107113
end
108114
end

test/test.ml

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,11 @@ module Ast_to_sexp = struct
2929
| `Superscript -> Atom "superscript"
3030
| `Subscript -> Atom "subscript"
3131

32-
let alignment : Ast.alignment -> sexp = function
33-
| `Left -> Atom "left"
34-
| `Center -> Atom "center"
35-
| `Right -> Atom "right"
32+
let alignment : Ast.alignment option -> sexp = function
33+
| Some `Left -> Atom "left"
34+
| Some `Center -> Atom "center"
35+
| Some `Right -> Atom "right"
36+
| None -> Atom "default"
3637

3738
let reference_kind : Ast.reference_kind -> sexp = function
3839
| `Simple -> Atom "simple"
@@ -89,14 +90,19 @@ module Ast_to_sexp = struct
8990
let syntax = function `Light -> "light" | `Heavy -> "heavy" in
9091
let kind = function `Header -> "header" | `Data -> "data" in
9192
let map name x f = List [ Atom name; List (List.map f x) ] in
93+
let alignment =
94+
match align with
95+
| None -> List [ Atom "align"; Atom "no alignment" ]
96+
| Some align -> map "align" align @@ alignment
97+
in
9298
List
9399
[
94100
Atom "table";
95101
List [ Atom "syntax"; Atom (syntax s) ];
96102
( map "data" data @@ fun row ->
97103
map "row" row @@ fun (cell, k) ->
98104
map (kind k) cell @@ at.at (nestable_block_element at) );
99-
map "align" align @@ alignment;
105+
alignment;
100106
]
101107

102108
let tag at : Ast.tag -> sexp = function

test/test_tables.ml

Lines changed: 38 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@ let%expect_test _ =
88
test "{table }";
99
[%expect
1010
{|
11-
((output (((f.ml (1 0) (1 8)) (table (syntax heavy) (data ()) (align ())))))
11+
((output
12+
(((f.ml (1 0) (1 8))
13+
(table (syntax heavy) (data ()) (align "no alignment")))))
1214
(warnings ())) |}]
1315

1416
let empty_row =
1517
test "{table {tr } }";
1618
[%expect
1719
{|
1820
((output
19-
(((f.ml (1 0) (1 14)) (table (syntax heavy) (data ((row ()))) (align ())))))
21+
(((f.ml (1 0) (1 14))
22+
(table (syntax heavy) (data ((row ()))) (align "no alignment")))))
2023
(warnings ()))|}]
2124

2225
let no_header =
@@ -25,7 +28,7 @@ let%expect_test _ =
2528
{|
2629
((output
2730
(((f.ml (1 0) (1 17))
28-
(table (syntax heavy) (data ((row ((data ()))))) (align ())))))
31+
(table (syntax heavy) (data ((row ((data ()))))) (align "no alignment")))))
2932
(warnings ())) |}]
3033

3134
let no_data =
@@ -34,14 +37,17 @@ let%expect_test _ =
3437
{|
3538
((output
3639
(((f.ml (1 0) (1 17))
37-
(table (syntax heavy) (data ((row ((header ()))))) (align ())))))
40+
(table (syntax heavy) (data ((row ((header ())))))
41+
(align "no alignment")))))
3842
(warnings ())) |}]
3943

4044
let bad_data =
4145
test "{table absurd content}";
4246
[%expect
4347
{|
44-
((output (((f.ml (1 0) (1 22)) (table (syntax heavy) (data ()) (align ())))))
48+
((output
49+
(((f.ml (1 0) (1 22))
50+
(table (syntax heavy) (data ()) (align "no alignment")))))
4551
(warnings
4652
( "File \"f.ml\", line 1, characters 7-13:\
4753
\n'absurd' is not allowed in '{table ...}' (table).\
@@ -55,7 +61,8 @@ let%expect_test _ =
5561
[%expect
5662
{|
5763
((output
58-
(((f.ml (1 0) (1 27)) (table (syntax heavy) (data ((row ()))) (align ())))))
64+
(((f.ml (1 0) (1 27))
65+
(table (syntax heavy) (data ((row ()))) (align "no alignment")))))
5966
(warnings
6067
( "File \"f.ml\", line 1, characters 11-17:\
6168
\n'absurd' is not allowed in '{tr ...}' (table row).\
@@ -72,7 +79,7 @@ let%expect_test _ =
7279
(((f.ml (1 0) (1 37))
7380
(table (syntax heavy)
7481
(data ((row ((header ()))) (row ((header ()))) (row ((data ())))))
75-
(align ())))))
82+
(align "no alignment")))))
7683
(warnings ())) |}]
7784

7885
let complex_table =
@@ -130,7 +137,8 @@ let%expect_test _ =
130137
(italic (((f.ml (8 32) (8 35)) (word ddd))))))))))
131138
(data
132139
(((f.ml (11 15) (11 32))
133-
(table (syntax heavy) (data ((row ((data ()))))) (align ())))))))
140+
(table (syntax heavy) (data ((row ((data ())))))
141+
(align "no alignment")))))))
134142
(row
135143
((data
136144
(((f.ml (16 15) (18 20))
@@ -165,8 +173,8 @@ let%expect_test _ =
165173
(data
166174
(((f.ml (24 25) (24 26))
167175
(paragraph (((f.ml (24 25) (24 26)) (word 3)))))))))))
168-
(align (center center center))))))))))
169-
(align ())))))
176+
(align (default default default))))))))))
177+
(align "no alignment")))))
170178
(warnings ())) |}]
171179
end in
172180
()
@@ -177,7 +185,9 @@ let%expect_test _ =
177185
test "{t }";
178186
[%expect
179187
{|
180-
((output (((f.ml (1 0) (1 4)) (table (syntax light) (data ()) (align ())))))
188+
((output
189+
(((f.ml (1 0) (1 4))
190+
(table (syntax light) (data ()) (align "no alignment")))))
181191
(warnings ())) |}]
182192

183193
let simple =
@@ -196,7 +206,7 @@ let%expect_test _ =
196206
((data
197207
(((f.ml (3 12) (3 13))
198208
(paragraph (((f.ml (3 12) (3 13)) (word a)))))))))))
199-
(align ())))))
209+
(align "no alignment")))))
200210
(warnings ())) |}]
201211

202212
let stars =
@@ -227,7 +237,7 @@ let%expect_test _ =
227237
(data
228238
(((f.ml (4 15) (4 17))
229239
(paragraph (((f.ml (4 15) (4 17)) (word d*)))))))))))
230-
(align ())))))
240+
(align "no alignment")))))
231241
(warnings ())) |}]
232242

233243
let backquotes =
@@ -249,7 +259,7 @@ let%expect_test _ =
249259
(data
250260
(((f.ml (3 15) (3 16))
251261
(paragraph (((f.ml (3 15) (3 16)) (word `)))))))))))
252-
(align ())))))
262+
(align "no alignment")))))
253263
(warnings ())) |}]
254264

255265
let no_header =
@@ -271,7 +281,7 @@ let%expect_test _ =
271281
(data
272282
(((f.ml (4 13) (4 14))
273283
(paragraph (((f.ml (4 13) (4 14)) (word y)))))))))))
274-
(align (center center))))))
284+
(align (default default))))))
275285
(warnings ())) |}]
276286

277287
let no_align =
@@ -299,7 +309,7 @@ let%expect_test _ =
299309
(data
300310
(((f.ml (4 13) (4 14))
301311
(paragraph (((f.ml (4 13) (4 14)) (word y)))))))))))
302-
(align ())))))
312+
(align "no alignment")))))
303313
(warnings ())) |}]
304314

305315
let only_align =
@@ -312,7 +322,7 @@ let%expect_test _ =
312322
{|
313323
((output
314324
(((f.ml (2 6) (4 7))
315-
(table (syntax light) (data ()) (align (center center))))))
325+
(table (syntax light) (data ()) (align (default default))))))
316326
(warnings ())) |}]
317327

318328
let no_data =
@@ -334,7 +344,7 @@ let%expect_test _ =
334344
(header
335345
(((f.ml (3 13) (3 14))
336346
(paragraph (((f.ml (3 13) (3 14)) (word y)))))))))))
337-
(align (center center))))))
347+
(align (default default))))))
338348
(warnings ())) |}]
339349

340350
let alignment =
@@ -363,7 +373,7 @@ let%expect_test _ =
363373
(header
364374
(((f.ml (3 21) (3 22))
365375
(paragraph (((f.ml (3 21) (3 22)) (word d)))))))))))
366-
(align (center left right center))))))
376+
(align (default left right center))))))
367377
(warnings ())) |}]
368378

369379
let no_bars =
@@ -405,7 +415,7 @@ let%expect_test _ =
405415
(data
406416
(((f.ml (5 20) (5 21))
407417
(paragraph (((f.ml (5 20) (5 21)) (word d)))))))))))
408-
(align (center left right center))))))
418+
(align (default left right center))))))
409419
(warnings ())) |}]
410420

411421
let light_table_new_lines =
@@ -451,7 +461,7 @@ let%expect_test _ =
451461
(data
452462
(((f.ml (8 21) (8 22))
453463
(paragraph (((f.ml (8 21) (8 22)) (word d)))))))))))
454-
(align (center center center center))))))
464+
(align (default default default default))))))
455465
(warnings ())) |}]
456466

457467
let light_table_markup =
@@ -494,7 +504,7 @@ let%expect_test _ =
494504
(((f.ml (3 60) (3 65)) (bold (((f.ml (3 63) (3 64)) (word d))))))))
495505
((f.ml (3 66) (3 71))
496506
(paragraph (((f.ml (3 66) (3 71)) (code_span foo)))))))))))
497-
(align (center center center center))))))
507+
(align (default default default default))))))
498508
(warnings ())) |}]
499509

500510
let light_table_markup_with_newlines =
@@ -534,7 +544,7 @@ let%expect_test _ =
534544
(((f.ml (5 26) (5 31)) (bold (((f.ml (5 29) (5 30)) (word d))))))))
535545
((f.ml (5 32) (5 37))
536546
(paragraph (((f.ml (5 32) (5 37)) (code_span foo)))))))))))
537-
(align (center center))))))
547+
(align (default default))))))
538548
(warnings
539549
( "File \"f.ml\", line 4, character 18 to line 5, character 14:\
540550
\nLine break is not allowed in '{t ...}' (table)."))) |}]
@@ -566,7 +576,7 @@ let%expect_test _ =
566576
(header
567577
(((f.ml (3 21) (3 22))
568578
(paragraph (((f.ml (3 21) (3 22)) (word d)))))))))))
569-
(align (center right left center))))))
579+
(align (default right left center))))))
570580
(warnings ())) |}]
571581

572582
let multiple_headers =
@@ -650,7 +660,7 @@ let%expect_test _ =
650660
(header
651661
(((f.ml (3 23) (3 24))
652662
(paragraph (((f.ml (3 23) (3 24)) (word b)))))))))))
653-
(align (center center))))))
663+
(align (default default))))))
654664
(warnings
655665
( "File \"f.ml\", line 3, characters 13-20:\
656666
\n'{[...]}' (code block) is not allowed in '{t ...}' (table)."))) |}]
@@ -677,7 +687,7 @@ let%expect_test _ =
677687
(header
678688
(((f.ml (4 17) (4 18))
679689
(paragraph (((f.ml (4 17) (4 18)) (word b)))))))))))
680-
(align (center center))))))
690+
(align (default default))))))
681691
(warnings
682692
( "File \"f.ml\", line 3, characters 11-18:\
683693
\n'{[...]}' (code block) is not allowed in '{t ...}' (table)."))) |}]
@@ -712,7 +722,7 @@ let%expect_test _ =
712722
(data
713723
(((f.ml (5 17) (5 18))
714724
(paragraph (((f.ml (5 17) (5 18)) (word z)))))))))))
715-
(align (center center))))))
725+
(align (default default))))))
716726
(warnings ())) |}]
717727

718728
let less_cells_later =
@@ -739,7 +749,7 @@ let%expect_test _ =
739749
(row
740750
((data
741751
(((f.ml (5 7) (5 8)) (paragraph (((f.ml (5 7) (5 8)) (word x)))))))))))
742-
(align (center center))))))
752+
(align (default default))))))
743753
(warnings ())) |}]
744754
end in
745755
()

0 commit comments

Comments
 (0)