From c36fad013cdf33d6dd6d25a745ddab35ffe6ac8f Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Sun, 5 Mar 2017 10:54:05 -0500 Subject: [PATCH] ADDED: Refactor abbreviation code; Add inline abbreviations; Fix abbreviations in ODF --- Sources/libMultiMarkdown/html.c | 39 +++- Sources/libMultiMarkdown/latex.c | 34 +++- Sources/libMultiMarkdown/odf.c | 58 ++++-- Sources/libMultiMarkdown/writer.c | 13 +- Sources/libMultiMarkdown/writer.h | 1 + tests/MMD6Tests/Abbreviations.fodt | 287 ++++++++++++++++++++++++++++ tests/MMD6Tests/Abbreviations.html | 2 + tests/MMD6Tests/Abbreviations.htmlc | 2 + tests/MMD6Tests/Abbreviations.tex | 2 + tests/MMD6Tests/Abbreviations.text | 3 + 10 files changed, 406 insertions(+), 35 deletions(-) create mode 100644 tests/MMD6Tests/Abbreviations.fodt diff --git a/Sources/libMultiMarkdown/html.c b/Sources/libMultiMarkdown/html.c index 2726c1e..a826b4d 100644 --- a/Sources/libMultiMarkdown/html.c +++ b/Sources/libMultiMarkdown/html.c @@ -354,6 +354,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc short temp_short; short temp_short2; + short temp_short3; link * temp_link = NULL; char * temp_char = NULL; char * temp_char2 = NULL; @@ -1093,28 +1094,46 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc mmd_export_token_tree_html(out, source, t->child, scratch); break; case PAIR_BRACKET_ABBREVIATION: - if (scratch->extensions & EXT_COMPATIBILITY) { - mmd_export_token_tree_html(out, source, t->child, scratch); - } else { + if (scratch->extensions & EXT_NOTES) { + // Note-based syntax enabled + + // Classify this use + temp_short2 = scratch->used_abbreviations->size; + temp_short3 = scratch->inline_abbreviations_to_free->size; abbreviation_from_bracket(source, scratch, t, &temp_short); if (temp_short == -1) { + // This instance is not properly formed print_const("[>"); - mmd_export_token_tree_html(out, source, t->child, scratch); - print_char(']'); + mmd_export_token_tree_html(out, source, t->child->next, scratch); + print_const("]"); break; } + // Get instance of the note used temp_note = stack_peek_index(scratch->used_abbreviations, temp_short - 1); t->child->type = TEXT_EMPTY; t->child->mate->type = TEXT_EMPTY; - - print_const("clean_text, false); - print_const("\">"); + + if (temp_short3 == scratch->inline_abbreviations_to_free->size) { + // This is a reference definition + print_const("clean_text, false); + print_const("\">"); + mmd_export_token_tree_html(out, source, t->child, scratch); + print_const(""); + } else { + // This is an inline definition + print_const("clean_text, false); + print_const("\">"); + mmd_print_string_html(out, temp_note->label_text, false); + print_const(""); + } + } else { + // Note-based syntax disabled mmd_export_token_tree_html(out, source, t->child, scratch); - print_const(""); } break; case PAIR_BRACKET_CITATION: diff --git a/Sources/libMultiMarkdown/latex.c b/Sources/libMultiMarkdown/latex.c index 01b9c66..d6667f4 100644 --- a/Sources/libMultiMarkdown/latex.c +++ b/Sources/libMultiMarkdown/latex.c @@ -1012,21 +1012,43 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat mmd_export_token_tree_latex(out, source, t->child, scratch); break; case PAIR_BRACKET_ABBREVIATION: - if (scratch->extensions & EXT_COMPATIBILITY) { - mmd_export_token_tree_latex(out, source, t->child, scratch); - } else { + if (scratch->extensions & EXT_NOTES) { + // Note-based syntax enabled + + // Classify this use + temp_short2 = scratch->used_abbreviations->size; + temp_short3 = scratch->inline_abbreviations_to_free->size; abbreviation_from_bracket(source, scratch, t, &temp_short); if (temp_short == -1) { + // This instance is not properly formed print_const("[>"); - mmd_export_token_tree_latex(out, source, t->child, scratch); - print_char(']'); + mmd_export_token_tree_latex(out, source, t->child->next, scratch); + print_const("]"); break; } + // Get instance of the note used temp_note = stack_peek_index(scratch->used_abbreviations, temp_short - 1); - printf("\\gls{%s}", temp_note->label_text); + if (temp_short3 == scratch->inline_abbreviations_to_free->size) { + // This is a reference definition + printf("\\gls{%s}", temp_note->label_text); + } else { + // This is an inline definition + print_const("\\newacronym{"); + print(temp_note->label_text); + print_const("}{"); + print(temp_note->label_text); + print_const("}{"); + print(temp_note->clean_text); + print_const("}"); + + printf("\\gls{%s}", temp_note->label_text); + } + } else { + // Note-based syntax disabled + mmd_export_token_tree_latex(out, source, t->child, scratch); } break; case PAIR_BRACKET_CITATION: diff --git a/Sources/libMultiMarkdown/odf.c b/Sources/libMultiMarkdown/odf.c index 7d170bc..b45ee83 100644 --- a/Sources/libMultiMarkdown/odf.c +++ b/Sources/libMultiMarkdown/odf.c @@ -1069,38 +1069,60 @@ void mmd_export_token_odf(DString * out, const char * source, token * t, scratch break; case PAIR_BRACKET_ABBREVIATION: if (scratch->extensions & EXT_NOTES) { + // Note-based syntax enabled + + // Classify this use + temp_short2 = scratch->used_abbreviations->size; + temp_short3 = scratch->inline_abbreviations_to_free->size; abbreviation_from_bracket(source, scratch, t, &temp_short); if (temp_short == -1) { + // This instance is not properly formed print_const("[>"); - mmd_export_token_tree_odf(out, source, t->child, scratch); + mmd_export_token_tree_odf(out, source, t->child->next, scratch); print_const("]"); break; } - temp_short2 = scratch->odf_para_type; - scratch->odf_para_type = PAIR_BRACKET_ABBREVIATION; - - if (temp_short < scratch->used_abbreviations->size) { - // Re-using previous footnote - print("\\footnote{reuse"); + // Get instance of the note used + temp_note = stack_peek_index(scratch->used_abbreviations, temp_short - 1); - print("}"); - } else { - // This is a new abbreviation item - temp_note = stack_peek_index(scratch->used_abbreviations, temp_short - 1); + t->child->type = TEXT_EMPTY; + t->child->mate->type = TEXT_EMPTY; - mmd_print_string_odf(out, temp_note->label_text); + if (temp_short2 == scratch->used_abbreviations->size) { + // This is a re-use of a previously used note - printf("", temp_short); + if (temp_short3 == scratch->inline_abbreviations_to_free->size) { + // This is a reference definition + mmd_export_token_tree_odf(out, source, t->child, scratch); + } else { + // This is an inline definition + mmd_export_token_tree_odf(out, source, t->child, scratch); + } + } else { + // This is the first time this note was used + temp_short2 = scratch->odf_para_type; + scratch->odf_para_type = PAIR_BRACKET_ABBREVIATION; + + if (temp_short3 == scratch->inline_abbreviations_to_free->size) { + // This is a reference definition + mmd_print_string_odf(out, temp_note->label_text); + printf("", temp_short); + mmd_export_token_tree_odf(out, source, temp_note->content, scratch); + print_const(""); + } else { + // This is an inline definition + mmd_print_string_odf(out, temp_note->label_text); + printf("", temp_short); + mmd_export_token_tree_odf(out, source, temp_note->content, scratch); + print_const(""); + } - mmd_export_token_tree_odf(out, source, temp_note->content, scratch); - print_const(""); + scratch->odf_para_type = temp_short2; } - - scratch->odf_para_type = temp_short2; } else { - // Footnotes disabled + // Note-based syntax disabled mmd_export_token_tree_odf(out, source, t->child, scratch); } break; diff --git a/Sources/libMultiMarkdown/writer.c b/Sources/libMultiMarkdown/writer.c index e7ce8fc..d658e20 100644 --- a/Sources/libMultiMarkdown/writer.c +++ b/Sources/libMultiMarkdown/writer.c @@ -194,6 +194,7 @@ scratch_pad * scratch_pad_new(mmd_engine * e, short format) { // Store abbreviations in a hash for rapid retrieval when exporting p->used_abbreviations = stack_new(0); + p->inline_abbreviations_to_free = stack_new(0); p->abbreviation_hash = NULL; @@ -287,6 +288,11 @@ void scratch_pad_free(scratch_pad * scratch) { stack_free(scratch->used_abbreviations); + while (scratch->inline_abbreviations_to_free->size) { + footnote_free(stack_pop(scratch->inline_abbreviations_to_free)); + } + stack_free(scratch->inline_abbreviations_to_free); + // Free metadata hash meta * m, * m_tmp; @@ -2070,6 +2076,11 @@ void abbreviation_from_bracket(const char * source, scratch_pad * scratch, token if (label) { footnote * temp = footnote_new(source, label, label->next, false); + // Adjust the properties + free(temp->label_text); + temp->label_text = temp->clean_text; + temp->clean_text = clean_string_from_range(source, temp->content->child->start, t->start + t->len - t->child->mate->len - temp->content->child->start, false); + // Store as used stack_push(scratch->used_abbreviations, temp); *num = scratch->used_abbreviations->size; @@ -2077,7 +2088,7 @@ void abbreviation_from_bracket(const char * source, scratch_pad * scratch, token // We need to free this one later since it doesn't exist // in the engine's stack, on the scratch_pad stack - //stack_push(scratch->inline_glossaries_to_free, temp); + stack_push(scratch->inline_abbreviations_to_free, temp); } else { // Improperly formatted glossary *num = -1; diff --git a/Sources/libMultiMarkdown/writer.h b/Sources/libMultiMarkdown/writer.h index 3ae4e4f..3ff9441 100644 --- a/Sources/libMultiMarkdown/writer.h +++ b/Sources/libMultiMarkdown/writer.h @@ -99,6 +99,7 @@ typedef struct { short glossary_being_printed; stack * used_abbreviations; + stack * inline_abbreviations_to_free; struct fn_holder * abbreviation_hash; short language; diff --git a/tests/MMD6Tests/Abbreviations.fodt b/tests/MMD6Tests/Abbreviations.fodt new file mode 100644 index 0000000..cb83998 --- /dev/null +++ b/tests/MMD6Tests/Abbreviations.fodt @@ -0,0 +1,287 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Bibliography + + + + Abbreviations + + + +fooFOO + +barBAR + +foo bar + +foobarFOOBAR + +foo barFOO BAR + +5 + +bazBAZ (BAT) + + + diff --git a/tests/MMD6Tests/Abbreviations.html b/tests/MMD6Tests/Abbreviations.html index 0ac6fc7..099d516 100644 --- a/tests/MMD6Tests/Abbreviations.html +++ b/tests/MMD6Tests/Abbreviations.html @@ -18,6 +18,8 @@

5

+

baz

+ diff --git a/tests/MMD6Tests/Abbreviations.htmlc b/tests/MMD6Tests/Abbreviations.htmlc index d830efb..ff3286b 100644 --- a/tests/MMD6Tests/Abbreviations.htmlc +++ b/tests/MMD6Tests/Abbreviations.htmlc @@ -12,3 +12,5 @@ latex config: article

>foo bar

5

+ +

[>(baz) BAZ (BAT)]

diff --git a/tests/MMD6Tests/Abbreviations.tex b/tests/MMD6Tests/Abbreviations.tex index f63ffbc..3e1c4b4 100644 --- a/tests/MMD6Tests/Abbreviations.tex +++ b/tests/MMD6Tests/Abbreviations.tex @@ -22,5 +22,7 @@ 5 +\newacronym{baz}{baz}{BAZ (BAT)}\gls{baz} + \input{mmd6-article-footer} \end{document} diff --git a/tests/MMD6Tests/Abbreviations.text b/tests/MMD6Tests/Abbreviations.text index 802b45c..9bd19fa 100644 --- a/tests/MMD6Tests/Abbreviations.text +++ b/tests/MMD6Tests/Abbreviations.text @@ -13,6 +13,9 @@ latex config: article 5 +[>(baz) BAZ (BAT)] + + [>foo]: FOO [>bar]: BAR [>foobar]: FOOBAR -- 2.40.0