From: Fletcher T. Penney Date: Sun, 19 Mar 2017 14:14:25 +0000 (-0400) Subject: ADDED: Include tables as targets for cross-reference links X-Git-Tag: 6.0.0-rc2^2~7 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=947b30cde61c6afe74fe9c7fdcb4ef1b42f7d322;p=multimarkdown ADDED: Include tables as targets for cross-reference links --- diff --git a/Sources/libMultiMarkdown/mmd.c b/Sources/libMultiMarkdown/mmd.c index 5b06583..8b38659 100644 --- a/Sources/libMultiMarkdown/mmd.c +++ b/Sources/libMultiMarkdown/mmd.c @@ -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 diff --git a/Sources/libMultiMarkdown/mmd.h b/Sources/libMultiMarkdown/mmd.h index 3e545aa..03ac7f7 100644 --- a/Sources/libMultiMarkdown/mmd.h +++ b/Sources/libMultiMarkdown/mmd.h @@ -87,6 +87,7 @@ struct mmd_engine { stack * header_stack; stack * link_stack; stack * metadata_stack; + stack * table_stack; short language; short quotes_lang; diff --git a/Sources/libMultiMarkdown/parser.c b/Sources/libMultiMarkdown/parser.c index 7926ab1..ec10510 100644 --- a/Sources/libMultiMarkdown/parser.c +++ b/Sources/libMultiMarkdown/parser.c @@ -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 */ diff --git a/Sources/libMultiMarkdown/parser.y b/Sources/libMultiMarkdown/parser.y index 62e0e58..ec0c52e 100644 --- a/Sources/libMultiMarkdown/parser.y +++ b/Sources/libMultiMarkdown/parser.y @@ -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 diff --git a/Sources/libMultiMarkdown/writer.c b/Sources/libMultiMarkdown/writer.c index 45c03f4..a3b3351 100644 --- a/Sources/libMultiMarkdown/writer.c +++ b/Sources/libMultiMarkdown/writer.c @@ -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); diff --git a/tests/MMD6Tests/Tables.fodt b/tests/MMD6Tests/Tables.fodt index b6da10c..87d87a4 100644 --- a/tests/MMD6Tests/Tables.fodt +++ b/tests/MMD6Tests/Tables.fodt @@ -508,7 +508,7 @@ Table Update Fields to calculate numbers:caption -[foo][bar] +foo | foo | bar | | — | — | | foo | bar | diff --git a/tests/MMD6Tests/Tables.html b/tests/MMD6Tests/Tables.html index d001693..04a73bf 100644 --- a/tests/MMD6Tests/Tables.html +++ b/tests/MMD6Tests/Tables.html @@ -184,7 +184,7 @@ -

[foo][bar] +

foo | foo | bar | | — | — | | foo | bar |

diff --git a/tests/MMD6Tests/Tables.tex b/tests/MMD6Tests/Tables.tex index 67cc2af..4af980e 100644 --- a/tests/MMD6Tests/Tables.tex +++ b/tests/MMD6Tests/Tables.tex @@ -123,7 +123,7 @@ \end{minipage} \end{table} -[\emph{foo}][bar] +\emph{foo} (\autoref{bar}) \textbar{} foo \textbar{} bar \textbar{} \textbar{} --- \textbar{} --- \textbar{} \textbar{} foo \textbar{} bar \textbar{}