]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Add support for tables in LaTeX
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Tue, 14 Feb 2017 17:57:11 +0000 (12:57 -0500)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Tue, 14 Feb 2017 17:57:11 +0000 (12:57 -0500)
src/latex.c
tests/MMD6Tests/Tables.html
tests/MMD6Tests/Tables.htmlc
tests/MMD6Tests/Tables.tex [new file with mode: 0644]
tests/MMD6Tests/Tables.text

index 1371edb42dbacd1256e6556e14b75656e60939c6..d6457e233916a78e5041a0d7b2357415a4e2c926 100644 (file)
@@ -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');
index 15d4fd8b4d87a34a6ae897e812dbb6748f8abe94..c984f1f5f36d04c5d4641e2f31cd97a73908e51f 100644 (file)
 </table>
 
 <table>
-<caption id="bar"><em>foo</em></caption>
+<caption id="bar"><em>caption</em></caption>
 <colgroup>
 <col />
 <col />
 </table>
 
 <table>
-<caption id="foo"><em>foo</em></caption>
+<caption id="caption"><em>caption</em></caption>
 <colgroup>
 <col />
 <col />
index 80ae6aed9fc96fa84b11ea6069ed77735ad073fe..6f6ac070a7aede2a8fa50dd15be762b5f81218b0 100644 (file)
 <p>| foo | bar |
 | --- | --- |
 | foo | bar |
-[<em>foo</em>][bar]</p>
+[<em>caption</em>][bar]</p>
 
 <p>| foo | bar |
 | --- | --- |
 | foo | bar |
-[<em>foo</em>]</p>
+[<em>caption</em>]</p>
 
 <p>[<em>foo</em>][bar]
 | foo | bar |
diff --git a/tests/MMD6Tests/Tables.tex b/tests/MMD6Tests/Tables.tex
new file mode 100644 (file)
index 0000000..6c5c4e7
--- /dev/null
@@ -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{}
index 4b5dcf2a393b739df15e464931288c591e31967f..5e5c50a3e6f1043585fe8ddec0c6f593ec34e4e1 100644 (file)
 | foo | bar |
 | --- | --- |
 | foo | bar |
-[*foo*][bar]
+[*caption*][bar]
 
 | foo | bar |
 | --- | --- |
 | foo | bar |
-[*foo*]
+[*caption*]
 
 [*foo*][bar]
 | foo | bar |