From 21c7b9647f13b9a1a69058396a4c91a95993d166 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Tue, 14 Feb 2017 12:57:11 -0500 Subject: [PATCH] ADDED: Add support for tables in LaTeX --- src/latex.c | 139 ++++++++++++++++++++++++++++++++++- tests/MMD6Tests/Tables.html | 4 +- tests/MMD6Tests/Tables.htmlc | 4 +- tests/MMD6Tests/Tables.tex | 125 +++++++++++++++++++++++++++++++ tests/MMD6Tests/Tables.text | 4 +- 5 files changed, 269 insertions(+), 7 deletions(-) create mode 100644 tests/MMD6Tests/Tables.tex diff --git a/src/latex.c b/src/latex.c index 1371edb..d6457e2 100644 --- a/src/latex.c +++ b/src/latex.c @@ -620,6 +620,89 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat mmd_export_token_tree_latex(out, source, t->child, scratch); scratch->padded = 0; break; + case BLOCK_TABLE: + pad(out, 2, scratch); + + print("\\begin{table}[htbp]\n"); + + print("\\begin{minipage}{\\linewidth}\n\\setlength{\\tymax}{0.5\\linewidth}\n\\centering\n\\small\n"); + + // Are we followed by a caption? + if (table_has_caption(t)) { + temp_token = t->next->child; + + if (temp_token->next && + temp_token->next->type == PAIR_BRACKET) { + temp_token = temp_token->next; + } + + temp_char = label_from_token(source, temp_token); + + t->next->child->child->type = TEXT_EMPTY; + t->next->child->child->mate->type = TEXT_EMPTY; + + print("\\caption{"); + mmd_export_token_tree_latex(out, source, t->next->child->child, scratch); + print("}\n"); + + printf("\\label{%s}\n", temp_char); + free(temp_char); + + temp_short = 1; + } else { + temp_short = 0; + } + + read_table_column_alignments(source, t, scratch); + + mmd_export_token_tree_latex(out, source, t->child, scratch); + pad(out, 1, scratch); + print("\n\\end{tabulary}\n\\end{minipage}"); + + print("\n\\end{table}"); + + scratch->skip_token = temp_short; + scratch->padded = 0; + break; + case BLOCK_TABLE_HEADER: + pad(out, 2, scratch); + + print("\\begin{tabulary}{\\textwidth}{@{}"); + + for (int i = 0; i < scratch->table_column_count; ++i) + { + switch (scratch->table_alignment[i]) { + case 'l': + case 'L': + case 'r': + case 'R': + case 'c': + case 'C': + print_char(scratch->table_alignment[i]); + break; + default: + print_char('l'); + break; + } + } + + print("@{}} \\toprule\n"); + + scratch->in_table_header = 1; + mmd_export_token_tree_latex(out, source, t->child, scratch); + scratch->in_table_header = 0; + + print("\\midrule\n"); + + scratch->padded = 1; + break; + case BLOCK_TABLE_SECTION: + pad(out, 2, scratch); + scratch->padded = 2; + mmd_export_token_tree_latex(out, source, t->child, scratch); + print("\\bottomrule"); + scratch->padded = 0; + break; case BLOCK_TERM: pad(out, 2, scratch); print("\\item["); @@ -1196,7 +1279,10 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat print(")"); break; case PIPE: - print("\\textbar{}"); + for (int i = 0; i < t->len; ++i) + { + print("\\textbar{}"); + } break; case PLUS: print_token(t); @@ -1253,6 +1339,51 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat print("\\^{}"); } break; + case TABLE_CELL: + if (t->next && t->next->type == TABLE_DIVIDER) { + if (t->next->len > 1) { + printf("\\multicolumn{%lu}{",t->next->len); + switch (scratch->table_alignment[scratch->table_cell_count]) { + case 'l': + case 'L': + print("l}{"); + break; + case 'r': + case 'R': + print("r}{"); + break; + default: + print("c}{"); + break; + } + } + } + + mmd_export_token_tree_latex(out, source, t->child, scratch); + + if (t->next && t->next->type == TABLE_DIVIDER) { + if (t->next->len > 1) + print("}"); + } + + if (t->next && t->next->type == TABLE_DIVIDER) { + t = t->next; + + if (t->next && t->next->type == TABLE_CELL) + print("&"); + + scratch->table_cell_count += t->next->len; + } else + scratch->table_cell_count++; + + break; + case TABLE_DIVIDER: + break; + case TABLE_ROW: + scratch->table_cell_count = 0; + mmd_export_token_tree_latex(out, source, t->child, scratch); + print("\\\\\n"); + break; case TEXT_BACKSLASH: print("\\textbackslash{}"); break; @@ -1261,6 +1392,12 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat case TEXT_HASH: print("\\#"); break; + case TEXT_LINEBREAK: + if (t->next) { + print("\n"); + scratch->padded = 1; + } + break; case TEXT_NL: if (t->next) print_char('\n'); diff --git a/tests/MMD6Tests/Tables.html b/tests/MMD6Tests/Tables.html index 15d4fd8..c984f1f 100644 --- a/tests/MMD6Tests/Tables.html +++ b/tests/MMD6Tests/Tables.html @@ -133,7 +133,7 @@ - +@@ -155,7 +155,7 @@
foocaption
- +diff --git a/tests/MMD6Tests/Tables.htmlc b/tests/MMD6Tests/Tables.htmlc index 80ae6ae..6f6ac07 100644 --- a/tests/MMD6Tests/Tables.htmlc +++ b/tests/MMD6Tests/Tables.htmlc @@ -36,12 +36,12 @@

| foo | bar | | --- | --- | | foo | bar | -[foo][bar]

+[caption][bar]

| foo | bar | | --- | --- | | foo | bar | -[foo]

+[caption]

[foo][bar] | foo | bar | diff --git a/tests/MMD6Tests/Tables.tex b/tests/MMD6Tests/Tables.tex new file mode 100644 index 0000000..6c5c4e7 --- /dev/null +++ b/tests/MMD6Tests/Tables.tex @@ -0,0 +1,125 @@ +\begin{table}[htbp] +\begin{minipage}{\linewidth} +\setlength{\tymax}{0.5\linewidth} +\centering +\small +\begin{tabulary}{\textwidth}{@{}lc@{}} \toprule + foo & bar \\ +\midrule + + \emph{foo} & \emph{bar} \\ +\multicolumn{2}{c}{ \textbf{foo bar} }\\ +\bottomrule + +\end{tabulary} +\end{minipage} +\end{table} + +\begin{table}[htbp] +\begin{minipage}{\linewidth} +\setlength{\tymax}{0.5\linewidth} +\centering +\small +\begin{tabulary}{\textwidth}{@{}Lr@{}} \toprule + foo1 & bar1 \\ +\midrule + +\emph{foo} & \emph{bar} \\ +\multicolumn{2}{l}{ \textbf{foo bar} }\\ +\bottomrule + +\end{tabulary} +\end{minipage} +\end{table} + +\begin{table}[htbp] +\begin{minipage}{\linewidth} +\setlength{\tymax}{0.5\linewidth} +\centering +\small +\begin{tabulary}{\textwidth}{@{}lr@{}} \toprule + foo2 & bar2 \\ +\midrule + +\emph{foo} & \emph{bar} \\ +\multicolumn{2}{l}{ \textbf{foo bar} }\\ +\bottomrule + + foo3 & bar3 \\ +\emph{foo} & \emph{bar} \\ +\multicolumn{2}{l}{ \textbf{foo bar} }\\ +\bottomrule + +\end{tabulary} +\end{minipage} +\end{table} + +\textbar{} foo4 \textbar{} bar4 \textbar{} +\emph{foo} \textbar{} \emph{bar} \textbar{} +\textbar{} \textbf{foo bar} \textbar{}\textbar{} + +5 + +\begin{table}[htbp] +\begin{minipage}{\linewidth} +\setlength{\tymax}{0.5\linewidth} +\centering +\small +\begin{tabulary}{\textwidth}{@{}lr@{}} \toprule + foo5 & bar5 \\ +\midrule + +\emph{foo} & \emph{bar} \\ +\multicolumn{2}{l}{ \textbf{foo bar} \textbar{} }\\ +\bottomrule + + foo6 & bar6 \\ + :----- & -----: \\ +\emph{foo} & \emph{bar} \\ +\multicolumn{2}{l}{ \textbf{foo bar} }\\ +\bottomrule + +\end{tabulary} +\end{minipage} +\end{table} + +\begin{table}[htbp] +\begin{minipage}{\linewidth} +\setlength{\tymax}{0.5\linewidth} +\centering +\small +\caption{\emph{caption}} +\label{bar} +\begin{tabulary}{\textwidth}{@{}ll@{}} \toprule + foo & bar \\ +\midrule + + foo & bar \\ +\bottomrule + +\end{tabulary} +\end{minipage} +\end{table} + +\begin{table}[htbp] +\begin{minipage}{\linewidth} +\setlength{\tymax}{0.5\linewidth} +\centering +\small +\caption{\emph{caption}} +\label{caption} +\begin{tabulary}{\textwidth}{@{}ll@{}} \toprule + foo & bar \\ +\midrule + + foo & bar \\ +\bottomrule + +\end{tabulary} +\end{minipage} +\end{table} + +[\emph{foo}][bar] +\textbar{} foo \textbar{} bar \textbar{} +\textbar{} --- \textbar{} --- \textbar{} +\textbar{} foo \textbar{} bar \textbar{} diff --git a/tests/MMD6Tests/Tables.text b/tests/MMD6Tests/Tables.text index 4b5dcf2..5e5c50a 100644 --- a/tests/MMD6Tests/Tables.text +++ b/tests/MMD6Tests/Tables.text @@ -40,12 +40,12 @@ | foo | bar | | --- | --- | | foo | bar | -[*foo*][bar] +[*caption*][bar] | foo | bar | | --- | --- | | foo | bar | -[*foo*] +[*caption*] [*foo*][bar] | foo | bar | -- 2.40.0

foocaption