From 9224a497d79acbb8ce5690bf8dd9e19855e50586 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lo=C3=AFc=20Hoguin?= Date: Tue, 12 Jun 2018 07:45:29 +0200 Subject: Fix table cells containing blocks --- src/asciideck_tables_pass.erl | 48 +++++++++++++++++++++---------------------- src/asciideck_to_html.erl | 17 +++++++++++---- test/parser_SUITE.erl | 19 ++++++++--------- 3 files changed, 46 insertions(+), 38 deletions(-) diff --git a/src/asciideck_tables_pass.erl b/src/asciideck_tables_pass.erl index fdda6ef..c0740cc 100644 --- a/src/asciideck_tables_pass.erl +++ b/src/asciideck_tables_pass.erl @@ -40,19 +40,19 @@ table({table, Attrs, Contents, Ann}) -> table_test() -> {table, _, [ {row, _, [ - {cell, _, <<"1">>, _}, - {cell, _, <<"2">>, _}, - {cell, _, <<"A">>, _} + {cell, _, [{paragraph, _, <<"1">>, _}], _}, + {cell, _, [{paragraph, _, <<"2">>, _}], _}, + {cell, _, [{paragraph, _, <<"A">>, _}], _} ], _}, {row, _, [ - {cell, _, <<"3">>, _}, - {cell, _, <<"4">>, _}, - {cell, _, <<"B">>, _} + {cell, _, [{paragraph, _, <<"3">>, _}], _}, + {cell, _, [{paragraph, _, <<"4">>, _}], _}, + {cell, _, [{paragraph, _, <<"B">>, _}], _} ], _}, {row, _, [ - {cell, _, <<"5">>, _}, - {cell, _, <<"6">>, _}, - {cell, _, <<"C">>, _} + {cell, _, [{paragraph, _, <<"5">>, _}], _}, + {cell, _, [{paragraph, _, <<"6">>, _}], _}, + {cell, _, [{paragraph, _, <<"C">>, _}], _} ], _} ], _} = table({table, #{}, << "|1 |2 |A\n" @@ -112,7 +112,7 @@ do_parse_cells([Contents], Acc) -> lists:reverse([{cell, #{specifiers => <<>>}, Contents, #{}}|Acc]); %% Last cell. There are no further cell specifiers. do_parse_cells([Specs, Contents0], Acc) -> - Contents = asciideck_block_parser:trim(Contents0, both), + Contents = asciideck_block_parser:parse(Contents0), %% @todo Annotations. Cell = {cell, #{specifiers => Specs}, Contents, #{}}, lists:reverse([Cell|Acc]); @@ -123,7 +123,7 @@ do_parse_cells([Specs, Contents0|Tail], Acc) -> NextSpecs = <<>>, %% @todo find_r(Contents0, <<>>), Len = byte_size(Contents0) - byte_size(NextSpecs), <> = Contents0, - Contents = asciideck_block_parser:trim(Contents1, both), + Contents = asciideck_block_parser:parse(Contents1), %% @todo Annotations. Cell = {cell, #{specifiers => Specs}, Contents, #{}}, do_parse_cells([NextSpecs|Tail], [Cell|Acc]). @@ -141,15 +141,15 @@ do_parse_cells([Specs, Contents0|Tail], Acc) -> -ifdef(TEST). parse_table_test() -> {[ - {cell, _, <<"1">>, _}, - {cell, _, <<"2">>, _}, - {cell, _, <<"A">>, _}, - {cell, _, <<"3">>, _}, - {cell, _, <<"4">>, _}, - {cell, _, <<"B">>, _}, - {cell, _, <<"5">>, _}, - {cell, _, <<"6">>, _}, - {cell, _, <<"C">>, _} + {cell, _, [{paragraph, _, <<"1">>, _}], _}, + {cell, _, [{paragraph, _, <<"2">>, _}], _}, + {cell, _, [{paragraph, _, <<"A">>, _}], _}, + {cell, _, [{paragraph, _, <<"3">>, _}], _}, + {cell, _, [{paragraph, _, <<"4">>, _}], _}, + {cell, _, [{paragraph, _, <<"B">>, _}], _}, + {cell, _, [{paragraph, _, <<"5">>, _}], _}, + {cell, _, [{paragraph, _, <<"6">>, _}], _}, + {cell, _, [{paragraph, _, <<"C">>, _}], _} ], 3} = parse_table(<< "|1 |2 |A\n" "|3 |4 |B\n" @@ -158,10 +158,10 @@ parse_table_test() -> parse_table_escape_pipe_test() -> {[ - {cell, _, <<"1">>, _}, - {cell, _, <<"2">>, _}, - {cell, _, <<"3 |4">>, _}, - {cell, _, <<"5">>, _} + {cell, _, [{paragraph, _, <<"1">>, _}], _}, + {cell, _, [{paragraph, _, <<"2">>, _}], _}, + {cell, _, [{paragraph, _, <<"3 |4">>, _}], _}, + {cell, _, [{paragraph, _, <<"5">>, _}], _} ], 2} = parse_table(<< "|1 |2\n" "|3 \\|4 |5">>, #{}), diff --git a/src/asciideck_to_html.erl b/src/asciideck_to_html.erl index 6f8a393..ab0aa98 100644 --- a/src/asciideck_to_html.erl +++ b/src/asciideck_to_html.erl @@ -169,16 +169,25 @@ table({table, _, [{row, _, Head, _}|Rows], _}) -> ]. table_head(Cells) -> - [["", inline(Text), "\n"] - || {cell, _, Text, _} <- Cells]. + [["", table_cell(AST), "\n"] + || {cell, _, AST, _} <- Cells]. table_body(Rows) -> [["", table_body_cells(Cells), "\n"] || {row, _, Cells, _} <- Rows]. table_body_cells(Cells) -> - [["", inline(Text), "\n"] - || {cell, _, Text, _} <- Cells]. + [["", table_cell(AST), "\n"] + || {cell, _, AST, _} <- Cells]. + +table_cell(AST0) -> + AST = [Node || Node={Type, _, _, _} <- AST0, Type =/= comment_line], + case AST of + [{paragraph, _, Text, _}] -> + inline(Text); + _ -> + ast(AST) + end. %% Block macros. diff --git a/test/parser_SUITE.erl b/test/parser_SUITE.erl index e5eda88..f3b4b27 100644 --- a/test/parser_SUITE.erl +++ b/test/parser_SUITE.erl @@ -404,22 +404,21 @@ comment_line(_) -> %% Tables. (23) table(_) -> - %% @todo I think I read somewhere that paragraphs are not allowed in cells... Double check. [{table, _, [ {row, _, [ - {cell, _, <<"1">>, _}, - {cell, _, <<"2">>, _}, - {cell, _, <<"A">>, _} + {cell, _, [{paragraph, _, <<"1">>, _}], _}, + {cell, _, [{paragraph, _, <<"2">>, _}], _}, + {cell, _, [{paragraph, _, <<"A">>, _}], _} ], _}, {row, _, [ - {cell, _, <<"3">>, _}, - {cell, _, <<"4">>, _}, - {cell, _, <<"B">>, _} + {cell, _, [{paragraph, _, <<"3">>, _}], _}, + {cell, _, [{paragraph, _, <<"4">>, _}], _}, + {cell, _, [{paragraph, _, <<"B">>, _}], _} ], _}, {row, _, [ - {cell, _, <<"5">>, _}, - {cell, _, <<"6">>, _}, - {cell, _, <<"C">>, _} + {cell, _, [{paragraph, _, <<"5">>, _}], _}, + {cell, _, [{paragraph, _, <<"6">>, _}], _}, + {cell, _, [{paragraph, _, <<"C">>, _}], _} ], _} ], _}]= parse( "|=======\n" -- cgit v1.2.3