Skip to content

Commit

Permalink
Tables
Browse files Browse the repository at this point in the history
  • Loading branch information
aarroyoc committed Jan 21, 2023
1 parent 8443df3 commit 63db777
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
124 changes: 124 additions & 0 deletions djota.pl
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,11 @@
{ phrase(((colons(N), " ", seq(ClassName)) | colons(N), ... ), Line), N >= 3 },
djot_div_ast_(Lines, N, "", ClassName).

% Pipe table
djot_ast_([Line|Lines]) -->
{ phrase(pipe_table(Row), Line) },
djot_table_ast_(Lines, [row(Row)]).

% Paragraph
djot_ast_([Line|Lines]) -->
{ Line \= "" },
Expand Down Expand Up @@ -211,6 +216,74 @@
{ djot_ast(Block, InsideAst) },
[div_block(ClassName, InsideAst)].

pipe_table(Row) -->
"|",
table_row(Row),
"|",
whites(_).

table_row([Ast|Xs]) -->
seq(X), { length(X, N), N > 0, inline_text_ast(X, Ast) },
"|",
table_row(Xs).
table_row([Ast]) -->
seq(X), { length(X, N), N > 0, inline_text_ast(X, Ast) }.

separator_table(Style) -->
"|",
table_style(Style),
"|",
whites(_).
table_style([none|Xs]) -->
dashes,
"|",
table_style(Xs).
table_style([none]) -->
dashes.
table_style([left|Xs]) -->
":", dashes,
"|",
table_style(Xs).
table_style([left]) -->
":", dashes.
table_style([right|Xs]) -->
dashes, ":",
"|",
table_style(Xs).
table_style([right]) -->
dashes, ":".
table_style([center|Xs]) -->
":", dashes, ":",
"|",
table_style(Xs).
table_style([center]) -->
":", dashes, ":".

dashes --> "-" | "-", dashes.


djot_table_ast_([Line|Lines], Rows) -->
{
phrase(separator_table(Style), Line),
append(RestRows, [row(Row)], Rows),
append(RestRows, [header(Row), set_style(Style)], Rows1)
},
djot_table_ast_(Lines, Rows1).

djot_table_ast_([Line|Lines], Rows) -->
{
phrase(pipe_table(Row), Line),
append(Rows, [row(Row)], Rows1)
},
djot_table_ast_(Lines, Rows1).

djot_table_ast_([Line|Lines], Rows) -->
{ \+ phrase(pipe_table(_), Line) },
[table(Rows)],
djot_ast_(Lines).
djot_table_ast_([], Rows) -->
djot_table_ast_([""], Rows).

djot_paragraph_ast_([Line|Lines], Paragraph0) -->
{
Line \= "",
Expand Down Expand Up @@ -276,6 +349,10 @@
ast_html_node_(div_block(ClassName, Block)) -->
{ nonvar(ClassName), phrase(ast_html_(Block), Html) },
"<div class=\"", ClassName, "\">", Html, "</div>".
ast_html_node_(table(Rows)) -->
"<table>",
ast_html_rows_(Rows, []),
"</table>".
ast_html_node_(link(TextAst, Url, Attrs)) -->
{ phrase(ast_html_(TextAst), TextHtml) },
{ attrs_html(Attrs, AttrsHtml) },
Expand Down Expand Up @@ -326,6 +403,53 @@
"</li>",
ast_html_node_items_(Items, tight).

ast_html_rows_([], _) --> "".
ast_html_rows_([row(Row)|Rows], Style) -->
"<tr>",
ast_html_cell_("td", Row, Style),
"</tr>",
ast_html_rows_(Rows, Style).
ast_html_rows_([header(Row)|Rows], Style) -->
"<tr>",
ast_html_cell_("th", Row, Style),
"</tr>",
ast_html_rows_(Rows, Style).

ast_html_rows_([set_style(Style)|Rows], _) -->
ast_html_rows_(Rows, Style).

ast_html_cell_(_, [], _) --> "".
ast_html_cell_(Type, [X|Xs], [none|Style]) -->
{ phrase(ast_html_(X), Html) },
"<", Type, ">",
Html,
"</", Type, ">",
ast_html_cell_(Type, Xs, Style).
ast_html_cell_(Type, [X|Xs], [left|Style]) -->
{ phrase(ast_html_(X), Html) },
"<", Type, " style=\"text-align:left;\">",
Html,
"</", Type, ">",
ast_html_cell_(Type, Xs, Style).
ast_html_cell_(Type, [X|Xs], [right|Style]) -->
{ phrase(ast_html_(X), Html) },
"<", Type, " style=\"text-align:right;\">",
Html,
"</", Type, ">",
ast_html_cell_(Type, Xs, Style).
ast_html_cell_(Type, [X|Xs], [center|Style]) -->
{ phrase(ast_html_(X), Html) },
"<", Type, " style=\"text-align:center;\">",
Html,
"</", Type, ">",
ast_html_cell_(Type, Xs, Style).
ast_html_cell_(Type, [X|Xs], []) -->
{ phrase(ast_html_(X), Html) },
"<", Type, ">",
Html,
"</", Type, ">",
ast_html_cell_(Type, Xs, Style).

escape_html_([]) --> [].
escape_html_(Html) -->
[Char],
Expand Down
7 changes: 7 additions & 0 deletions test.lgt
Original file line number Diff line number Diff line change
Expand Up @@ -105,4 +105,11 @@ test(attr_pairs) :-
test(link_emphasis) :-
djota:djot("CLP(B) is an instance of the general [CLP(_X_) scheme](#clp)", "<p>CLP(B) is an instance of the general <a href=\"#clp\">CLP(<em>X</em>) scheme</a></p>").

test(table_ast) :-
djota:djot_ast("| 1 | 2 |\n| 3 | 4 | 5 |", [table([row([[str(" 1 ")],[str(" 2 ")]]),row([[str(" 3 ")],[str(" 4 ")],[str(" 5 ")]])])]).

test(table) :-
djota:djot("| 1 | 2 |\n| 3 | 4 | 5 |", "<table><tr><td> 1 </td><td> 2 </td></tr><tr><td> 3 </td><td> 4 </td><td> 5 </td></tr></table>"),
djota:djot("|fruit|price|\n|---|---:|\n|apple|4|\n|banana|10|", "<table><tr><th>fruit</th><th>price</th></tr><tr><td>apple</td><td style=\"text-align:right;\">4</td></tr><tr><td>banana</td><td style=\"text-align:right;\">10</td></tr></table>").

:- end_object.

0 comments on commit 63db777

Please sign in to comment.