]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Include tables as targets for cross-reference links
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Sun, 19 Mar 2017 14:14:25 +0000 (10:14 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Sun, 19 Mar 2017 14:14:25 +0000 (10:14 -0400)
Sources/libMultiMarkdown/mmd.c
Sources/libMultiMarkdown/mmd.h
Sources/libMultiMarkdown/parser.c
Sources/libMultiMarkdown/parser.y
Sources/libMultiMarkdown/writer.c
tests/MMD6Tests/Tables.fodt
tests/MMD6Tests/Tables.html
tests/MMD6Tests/Tables.tex

index 5b06583b1c3c0974ae50812e722deb4379564461..8b3865930340547f30d79202bb114cfea28b2890 100644 (file)
@@ -108,6 +108,7 @@ mmd_engine * mmd_engine_create(DString * d, unsigned long extensions) {
                e->header_stack = stack_new(0);
                e->link_stack = stack_new(0);
                e->metadata_stack = stack_new(0);
+               e->table_stack = stack_new(0);
                e->asset_hash = NULL;
 
                e->pairings1 = token_pair_engine_new();
@@ -232,6 +233,7 @@ void mmd_engine_free(mmd_engine * e, bool freeDString) {
        // Pointers to blocks that are freed elsewhere
        stack_free(e->definition_stack);
        stack_free(e->header_stack);
+       stack_free(e->table_stack);
 
 
        // Abbreviations need to be freed
index 3e545aaee6c1dcaa4da8c8b0c542811d12d7e2a2..03ac7f7a9c575e49b7ce6000822579075a14fa6c 100644 (file)
@@ -87,6 +87,7 @@ struct mmd_engine {
        stack *                                 header_stack;
        stack *                                 link_stack;
        stack *                                 metadata_stack;
+       stack *                                 table_stack;
 
        short                                   language;
        short                                   quotes_lang;
index 7926ab198b28becce3801ed059332c1b9deec823..ec10510a413e9acf541703a9701db51d08696a63 100644 (file)
@@ -1244,7 +1244,7 @@ static void yy_reduce(
   yymsp[0].minor.yy0 = yylhsminor.yy0;
         break;
       case 28: /* block ::= table */
-{ yylhsminor.yy0 = token_new_parent(yymsp[0].minor.yy0, BLOCK_TABLE); }
+{ yylhsminor.yy0 = token_new_parent(yymsp[0].minor.yy0, BLOCK_TABLE); stack_push(engine->table_stack, yylhsminor.yy0); }
   yymsp[0].minor.yy0 = yylhsminor.yy0;
         break;
       case 29: /* chunk ::= chunk chunk_line */
index 62e0e588fb2b23d9412eda68ce520b2517933bdb..ec0c52e251fcc4ad475b9d43847a381ed28dbb55 100644 (file)
@@ -130,7 +130,7 @@ block(A)                    ::= meta_block(B).                      { A = token_new_parent(B, BLOCK_META); }
 block(A)                       ::= para(B).                            { A = token_new_parent(B, BLOCK_PARA); is_para_html(engine, A); }
 block(A)                       ::= setext_1(B).                        { A = token_new_parent(B, BLOCK_SETEXT_1); if (!(engine->extensions & EXT_NO_LABELS)) stack_push(engine->header_stack, A); }
 block(A)                       ::= setext_2(B).                        { A = token_new_parent(B, BLOCK_SETEXT_2); if (!(engine->extensions & EXT_NO_LABELS)) stack_push(engine->header_stack, A); }
-block(A)                       ::= table(B).                           { A = token_new_parent(B, BLOCK_TABLE); }
+block(A)                       ::= table(B).                           { A = token_new_parent(B, BLOCK_TABLE); stack_push(engine->table_stack, A); }
 
 
 // Reusable components
index 45c03f47b1c0b0c04411356813da98f808b81900..a3b33511d17aa8e530690c7226a8b349d19da227 100644 (file)
@@ -1430,6 +1430,39 @@ void process_header_stack(mmd_engine * e) {
 }
 
 
+void process_table_to_link(mmd_engine * e, token * t) {
+       // Is there a caption
+       if (table_has_caption(t)) {
+               token * temp_token = t->next->child;
+
+               if (temp_token->next &&
+                       temp_token->next->type == PAIR_BRACKET) {
+                       temp_token = temp_token->next;
+               }
+
+               char * label = label_from_token(e->dstr->str, temp_token);
+
+               DString * url = d_string_new("#");
+               d_string_append(url, label);
+
+               link * l = link_new(e->dstr->str, temp_token, url->str, NULL, NULL);
+
+               stack_push(e->link_stack, l);
+
+               d_string_free(url, true);
+               free(label);
+       }
+}
+
+
+void process_table_stack(mmd_engine * e) {
+       for (int i = 0; i < e->table_stack->size; ++i)
+       {
+               process_table_to_link(e, stack_peek_index(e->table_stack, i));  
+       }
+}
+
+
 /// Parse metadata
 void process_metadata_stack(mmd_engine * e, scratch_pad * scratch) {
        if ((scratch->extensions & EXT_NO_METADATA) ||
@@ -1640,6 +1673,9 @@ void mmd_export_token_tree(DString * out, mmd_engine * e, short format) {
        // Process headers for potential cross-reference targets
        process_header_stack(e);
 
+       // Process tables for potential cross-reference targets
+       process_table_stack(e);
+
        // Create scratch pad
        scratch_pad * scratch = scratch_pad_new(e, format);
 
index b6da10c8e36cea1ac60c3019552fe717181ebd3a..87d87a44197bc14c4aa767e44c5c03218c6cdaab 100644 (file)
 <text:p><text:bookmark text:name="caption"/>Table <text:sequence text:name="Table" text:formula="ooow:Table+1" style:num-format="1"> Update Fields to calculate numbers</text:sequence>:<text:span text:style-name="MMD-Italic">caption</text:span><text:bookmark-end text:name="caption"/></text:p>
 
 
-<text:p text:style-name="Standard">[<text:span text:style-name="MMD-Italic">foo</text:span>][bar]
+<text:p text:style-name="Standard"><text:a xlink:type="simple" xlink:href="#bar"><text:span text:style-name="MMD-Italic">foo</text:span></text:a>
 | foo | bar |
 | &#8212; | &#8212; |
 | foo | bar |</text:p>
index d001693b7862136ea8514fab8c2bbc4de86d2c68..04a73bf7cae9272056238b9ebc37f73f6c2f35c2 100644 (file)
 </tbody>
 </table>
 
-<p>[<em>foo</em>][bar]
+<p><a href="#bar"><em>foo</em></a>
 | foo | bar |
 | &#8212; | &#8212; |
 | foo | bar |</p>
index 67cc2af2e38e42c51e22e5b6cc3bdaeb946722b4..4af980ebc3d22198a667374210df1fde6d297179 100644 (file)
 \end{minipage}
 \end{table}
 
-[\emph{foo}][bar]
+\emph{foo} (\autoref{bar})
 \textbar{} foo \textbar{} bar \textbar{}
 \textbar{} --- \textbar{} --- \textbar{}
 \textbar{} foo \textbar{} bar \textbar{}