Codebase list erlang-asciideck / 9224a49
Fix table cells containing blocks Loïc Hoguin 5 years ago
3 changed file(s) with 46 addition(s) and 38 deletion(s). Raw diff Collapse all Expand all
3939 table_test() ->
4040 {table, _, [
4141 {row, _, [
42 {cell, _, <<"1">>, _},
43 {cell, _, <<"2">>, _},
44 {cell, _, <<"A">>, _}
42 {cell, _, [{paragraph, _, <<"1">>, _}], _},
43 {cell, _, [{paragraph, _, <<"2">>, _}], _},
44 {cell, _, [{paragraph, _, <<"A">>, _}], _}
4545 ], _},
4646 {row, _, [
47 {cell, _, <<"3">>, _},
48 {cell, _, <<"4">>, _},
49 {cell, _, <<"B">>, _}
47 {cell, _, [{paragraph, _, <<"3">>, _}], _},
48 {cell, _, [{paragraph, _, <<"4">>, _}], _},
49 {cell, _, [{paragraph, _, <<"B">>, _}], _}
5050 ], _},
5151 {row, _, [
52 {cell, _, <<"5">>, _},
53 {cell, _, <<"6">>, _},
54 {cell, _, <<"C">>, _}
52 {cell, _, [{paragraph, _, <<"5">>, _}], _},
53 {cell, _, [{paragraph, _, <<"6">>, _}], _},
54 {cell, _, [{paragraph, _, <<"C">>, _}], _}
5555 ], _}
5656 ], _} = table({table, #{}, <<
5757 "|1 |2 |A\n"
111111 lists:reverse([{cell, #{specifiers => <<>>}, Contents, #{}}|Acc]);
112112 %% Last cell. There are no further cell specifiers.
113113 do_parse_cells([Specs, Contents0], Acc) ->
114 Contents = asciideck_block_parser:trim(Contents0, both),
114 Contents = asciideck_block_parser:parse(Contents0),
115115 %% @todo Annotations.
116116 Cell = {cell, #{specifiers => Specs}, Contents, #{}},
117117 lists:reverse([Cell|Acc]);
122122 NextSpecs = <<>>, %% @todo find_r(Contents0, <<>>),
123123 Len = byte_size(Contents0) - byte_size(NextSpecs),
124124 <<Contents1:Len/binary, _/bits>> = Contents0,
125 Contents = asciideck_block_parser:trim(Contents1, both),
125 Contents = asciideck_block_parser:parse(Contents1),
126126 %% @todo Annotations.
127127 Cell = {cell, #{specifiers => Specs}, Contents, #{}},
128128 do_parse_cells([NextSpecs|Tail], [Cell|Acc]).
140140 -ifdef(TEST).
141141 parse_table_test() ->
142142 {[
143 {cell, _, <<"1">>, _},
144 {cell, _, <<"2">>, _},
145 {cell, _, <<"A">>, _},
146 {cell, _, <<"3">>, _},
147 {cell, _, <<"4">>, _},
148 {cell, _, <<"B">>, _},
149 {cell, _, <<"5">>, _},
150 {cell, _, <<"6">>, _},
151 {cell, _, <<"C">>, _}
143 {cell, _, [{paragraph, _, <<"1">>, _}], _},
144 {cell, _, [{paragraph, _, <<"2">>, _}], _},
145 {cell, _, [{paragraph, _, <<"A">>, _}], _},
146 {cell, _, [{paragraph, _, <<"3">>, _}], _},
147 {cell, _, [{paragraph, _, <<"4">>, _}], _},
148 {cell, _, [{paragraph, _, <<"B">>, _}], _},
149 {cell, _, [{paragraph, _, <<"5">>, _}], _},
150 {cell, _, [{paragraph, _, <<"6">>, _}], _},
151 {cell, _, [{paragraph, _, <<"C">>, _}], _}
152152 ], 3} = parse_table(<<
153153 "|1 |2 |A\n"
154154 "|3 |4 |B\n"
157157
158158 parse_table_escape_pipe_test() ->
159159 {[
160 {cell, _, <<"1">>, _},
161 {cell, _, <<"2">>, _},
162 {cell, _, <<"3 |4">>, _},
163 {cell, _, <<"5">>, _}
160 {cell, _, [{paragraph, _, <<"1">>, _}], _},
161 {cell, _, [{paragraph, _, <<"2">>, _}], _},
162 {cell, _, [{paragraph, _, <<"3 |4">>, _}], _},
163 {cell, _, [{paragraph, _, <<"5">>, _}], _}
164164 ], 2} = parse_table(<<
165165 "|1 |2\n"
166166 "|3 \\|4 |5">>, #{}),
168168 ].
169169
170170 table_head(Cells) ->
171 [["<th>", inline(Text), "</th>\n"]
172 || {cell, _, Text, _} <- Cells].
171 [["<th>", table_cell(AST), "</th>\n"]
172 || {cell, _, AST, _} <- Cells].
173173
174174 table_body(Rows) ->
175175 [["<tr>", table_body_cells(Cells), "</tr>\n"]
176176 || {row, _, Cells, _} <- Rows].
177177
178178 table_body_cells(Cells) ->
179 [["<td>", inline(Text), "</td>\n"]
180 || {cell, _, Text, _} <- Cells].
179 [["<td>", table_cell(AST), "</td>\n"]
180 || {cell, _, AST, _} <- Cells].
181
182 table_cell(AST0) ->
183 AST = [Node || Node={Type, _, _, _} <- AST0, Type =/= comment_line],
184 case AST of
185 [{paragraph, _, Text, _}] ->
186 inline(Text);
187 _ ->
188 ast(AST)
189 end.
181190
182191 %% Block macros.
183192
403403 %% Tables. (23)
404404
405405 table(_) ->
406 %% @todo I think I read somewhere that paragraphs are not allowed in cells... Double check.
407406 [{table, _, [
408407 {row, _, [
409 {cell, _, <<"1">>, _},
410 {cell, _, <<"2">>, _},
411 {cell, _, <<"A">>, _}
408 {cell, _, [{paragraph, _, <<"1">>, _}], _},
409 {cell, _, [{paragraph, _, <<"2">>, _}], _},
410 {cell, _, [{paragraph, _, <<"A">>, _}], _}
412411 ], _},
413412 {row, _, [
414 {cell, _, <<"3">>, _},
415 {cell, _, <<"4">>, _},
416 {cell, _, <<"B">>, _}
413 {cell, _, [{paragraph, _, <<"3">>, _}], _},
414 {cell, _, [{paragraph, _, <<"4">>, _}], _},
415 {cell, _, [{paragraph, _, <<"B">>, _}], _}
417416 ], _},
418417 {row, _, [
419 {cell, _, <<"5">>, _},
420 {cell, _, <<"6">>, _},
421 {cell, _, <<"C">>, _}
418 {cell, _, [{paragraph, _, <<"5">>, _}], _},
419 {cell, _, [{paragraph, _, <<"6">>, _}], _},
420 {cell, _, [{paragraph, _, <<"C">>, _}], _}
422421 ], _}
423422 ], _}]= parse(
424423 "|=======\n"