Skip to content

Commit bd60049

Browse files
gpetiotjonludlam
authored andcommitted
Do not allow alignment in heavy tables
1 parent 4003ba4 commit bd60049

File tree

2 files changed

+25
-81
lines changed

2 files changed

+25
-81
lines changed

src/syntax.ml

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -93,31 +93,25 @@ module Table = struct
9393
end
9494

9595
module Heavy_syntax = struct
96-
let create ~header ~data ~align : Ast.table = ((header, data, align), `Heavy)
96+
let create ~header ~data : Ast.table = ((header, data, []), `Heavy)
9797

9898
let valid_header_row row =
99-
List.map
100-
(function
101-
| `Header align, x -> Some (Option.value align ~default:`Center, x)
102-
| `Data, _ -> None)
103-
row
99+
List.map (function `Header, x -> Some x | `Data, _ -> None) row
104100
|> Option.join_list
105101

106102
let from_grid grid : Ast.table =
107103
match grid with
108-
| [] -> create ~header:[] ~data:[] ~align:[]
104+
| [] -> create ~header:[] ~data:[]
109105
| row1 :: rows2_N ->
110-
let header, data, align =
106+
let header, data =
111107
(* If the first line is the header row, everything else is data. *)
112108
match valid_header_row row1 with
113-
| Some header ->
114-
let align, header = List.split header in
115-
(header, rows2_N, align)
109+
| Some header -> (header, rows2_N)
116110
(* Otherwise everything is considered data. *)
117-
| None -> ([], grid, [])
111+
| None -> ([], grid)
118112
in
119113
let data = List.map (List.map snd) data in
120-
create ~header ~data ~align
114+
create ~header ~data
121115
end
122116
end
123117

@@ -1349,14 +1343,12 @@ and heavy_table_row ~parent_markup input =
13491343
let rec consume_cell_items acc =
13501344
Reader.until_rbrace input acc >>> fun next_token ->
13511345
match next_token.Loc.value with
1352-
| `Begin_table_header as token -> (
1346+
| `Begin_table_header as token ->
13531347
junk input;
13541348
let content, _brace_location =
13551349
heavy_table_header input ~parent_markup:token
13561350
in
1357-
match content with
1358-
| None -> consume_cell_items ((`Header None, []) :: acc)
1359-
| Some (x, b) -> consume_cell_items ((`Header x, b) :: acc))
1351+
consume_cell_items ((`Header, content) :: acc)
13601352
| `Begin_table_data as token ->
13611353
junk input;
13621354
let content, token_after_list_item, _where_in_line =
@@ -1378,49 +1370,27 @@ and heavy_table_row ~parent_markup input =
13781370
in
13791371
consume_cell_items []
13801372

1381-
(* Consumes a table header (that might start with '{L ...}', '{C ...}' or '{R ... }',
1382-
which are represented by [`Begin_paragraph_style _] tokens).
1373+
(* Consumes a table header.
13831374
13841375
This function is called immediately after '{th' ([`Begin_table_header]) is
13851376
read. The only "valid" way to exit is by reading a [`Right_brace] token,
13861377
which is consumed. *)
13871378
and heavy_table_header ~parent_markup input =
13881379
let rec consume_items acc =
13891380
Reader.until_rbrace input acc >>> fun next_token ->
1390-
match next_token.Loc.value with
1391-
| `Begin_paragraph_style style as token ->
1392-
junk input;
1393-
(match acc with
1394-
| Some _ ->
1395-
Parse_error.not_allowed next_token.location
1396-
~what:(Token.describe token)
1397-
~in_what:(Token.describe parent_markup)
1398-
|> add_warning input
1399-
| None -> ());
1400-
let content, token_after_list_item, _where_in_line =
1401-
block_element_list In_table_header ~parent_markup input
1402-
in
1403-
(match token_after_list_item.value with
1404-
| `Right_brace -> junk input
1405-
| `End ->
1406-
Parse_error.not_allowed token_after_list_item.location
1407-
~what:(Token.describe `End) ~in_what:(Token.describe token)
1408-
|> add_warning input);
1409-
consume_items (Some (Some style, content))
1410-
| token ->
1411-
(match acc with
1412-
| Some _ ->
1413-
Parse_error.not_allowed next_token.location
1414-
~what:(Token.describe token)
1415-
~in_what:(Token.describe parent_markup)
1416-
|> add_warning input
1417-
| None -> ());
1418-
let content, _token_after_list_item, _where_in_line =
1419-
block_element_list In_table_header ~parent_markup input
1420-
in
1421-
consume_items (Some (None, content))
1381+
(match acc with
1382+
| _ :: _ ->
1383+
Parse_error.not_allowed next_token.location
1384+
~what:(Token.describe next_token.value)
1385+
~in_what:(Token.describe parent_markup)
1386+
|> add_warning input
1387+
| [] -> ());
1388+
let content, _token_after_list_item, _where_in_line =
1389+
block_element_list In_table_header ~parent_markup input
1390+
in
1391+
consume_items content
14221392
in
1423-
consume_items None
1393+
consume_items []
14241394

14251395
(* {2 Entry point} *)
14261396

test/test_tables.ml

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ let%expect_test _ =
3737
{|
3838
((output
3939
(((f.ml (1 0) (1 17))
40-
(table (syntax heavy) (header ((cell ()))) (data ()) (align (center))))))
40+
(table (syntax heavy) (header ((cell ()))) (data ()) (align ())))))
4141
(warnings ())) |}]
4242

4343
let multiple_headers =
@@ -47,7 +47,7 @@ let%expect_test _ =
4747
((output
4848
(((f.ml (1 0) (1 37))
4949
(table (syntax heavy) (header ((cell ())))
50-
(data ((row ((cell ()))) (row ((cell ()))))) (align (center))))))
50+
(data ((row ((cell ()))) (row ((cell ()))))) (align ())))))
5151
(warnings ())) |}]
5252

5353
let complex_table =
@@ -142,33 +142,7 @@ let%expect_test _ =
142142
(((f.ml (24 25) (24 26))
143143
(paragraph (((f.ml (24 25) (24 26)) (word 3)))))))))))
144144
(align (center center center))))))))))
145-
(align (center center))))))
146-
(warnings ())) |}]
147-
148-
let align =
149-
test
150-
{|
151-
{table
152-
{tr
153-
{th {L a}}
154-
{th {C b}}
155-
{th {R c}}
156-
}
157-
}
158-
|};
159-
[%expect
160-
{|
161-
((output
162-
(((f.ml (2 8) (8 9))
163-
(table (syntax heavy)
164-
(header
165-
((cell
166-
(((f.ml (4 19) (4 20)) (paragraph (((f.ml (4 19) (4 20)) (word a)))))))
167-
(cell
168-
(((f.ml (5 19) (5 20)) (paragraph (((f.ml (5 19) (5 20)) (word b)))))))
169-
(cell
170-
(((f.ml (6 19) (6 20)) (paragraph (((f.ml (6 19) (6 20)) (word c)))))))))
171-
(data ()) (align (left center right))))))
145+
(align ())))))
172146
(warnings ())) |}]
173147
end in
174148
()

0 commit comments

Comments
 (0)