From: Fletcher T. Penney Date: Sun, 5 Mar 2017 19:51:45 +0000 (-0500) Subject: CHANGED: Finish refactoring note-related code X-Git-Tag: 0.4.2-b^2~2 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=35d6ccf1219d89af8336091fc3d4e395ac347983;p=multimarkdown CHANGED: Finish refactoring note-related code --- diff --git a/Sources/libMultiMarkdown/html.c b/Sources/libMultiMarkdown/html.c index e50d7f0..c4fa0dd 100644 --- a/Sources/libMultiMarkdown/html.c +++ b/Sources/libMultiMarkdown/html.c @@ -1138,62 +1138,95 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc break; case PAIR_BRACKET_CITATION: parse_citation: - temp_bool = true; + temp_bool = true; // Track whether this is regular vs 'not cited' + temp_token = t; // Remember whether we need to skip ahead - if (t->type == PAIR_BRACKET) { - // This is a locator for subsequent citation - temp_char = text_inside_pair(source, t); - temp_char2 = label_from_string(temp_char); + if (scratch->extensions & EXT_NOTES) { + // Note-based syntax enabled + + if (t->type == PAIR_BRACKET) { + // This is a locator for a subsequent citation (e.g. `[foo][#bar]`) + temp_char = text_inside_pair(source, t); + temp_char2 = label_from_string(temp_char); + + if (strcmp(temp_char2, "notcited") == 0) { + free(temp_char); + temp_char = strdup(""); + temp_bool = false; + } - if (strcmp(temp_char2, "notcited") == 0) { free(temp_char2); - free(temp_char); + + // Move ahead to actual citation + t = t->next; + } else { + // This is the actual citation (e.g. `[#foo]`) + // No locator temp_char = strdup(""); - temp_bool = false; } - if (temp_char[0] == '\0') - temp_char2 = strdup(""); - else - temp_char2 = strdup(", "); - + // Classify this use + temp_short2 = scratch->used_citations->size; + temp_short3 = scratch->inline_citations_to_free->size; + citation_from_bracket(source, scratch, t, &temp_short); - // Process the actual citation - t = t->next; - } else { - temp_char = strdup(""); - temp_char2 = strdup(""); - } + if (temp_short == -1) { + // This instance is not properly formed + print_const("[#"); + mmd_export_token_tree_html(out, source, t->child->next, scratch); + print_const("]"); - if (scratch->extensions & EXT_NOTES) { - temp_short2 = scratch->used_citations->size; + free(temp_char); + break; + } - citation_from_bracket(source, scratch, t, &temp_short); + // Get instance of the note used + temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); if (temp_bool) { - if (temp_short2 == scratch->used_citations->size) { - // Repeat of earlier citation - printf("[%s%s%d]", - temp_short, LC("see citation"), temp_char, temp_char2, temp_short); + // This is a regular citation + + if (temp_char[0] == '\0') { + // No locator + + if (temp_short2 == scratch->used_citations->size) { + // This is a re-use of a previously used note + printf("[%d]", + temp_short, LC("see citation"), temp_short); + } else { + // This is the first time this note was used + printf("[%d]", + temp_short, temp_short, LC("see citation"), temp_short); + } } else { - // New citation - printf("[%s%s%d]", - temp_short, temp_short, LC("see citation"), temp_char, temp_char2, temp_short); + // Locator present + + if (temp_short2 == scratch->used_citations->size) { + // This is a re-use of a previously used note + printf("[%s, %d]", + temp_short, LC("see citation"), temp_char, temp_short); + } else { + // This is the first time this note was used + printf("[%s, %d]", + temp_short, temp_short, LC("see citation"), temp_char, temp_short); + } } + } else { + // This is a "nocite" } - if (t->prev && (t->prev->type == PAIR_BRACKET)) { + if (temp_token != t) { // Skip citation on next pass scratch->skip_token = 1; } + + free(temp_char); } else { - // Footnotes disabled + // Note-based syntax disabled mmd_export_token_tree_html(out, source, t->child, scratch); } - - free(temp_char); - free(temp_char2); break; + case PAIR_BRACKET_FOOTNOTE: if (scratch->extensions & EXT_NOTES) { // Note-based syntax enabled diff --git a/Sources/libMultiMarkdown/latex.c b/Sources/libMultiMarkdown/latex.c index 9742ca8..a6f5119 100644 --- a/Sources/libMultiMarkdown/latex.c +++ b/Sources/libMultiMarkdown/latex.c @@ -1053,12 +1053,14 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat break; case PAIR_BRACKET_CITATION: parse_citation: - temp_bool = true; // Track whether this is a 'not cited' - temp_token = t; // Remember whether we need to skip ahead - + temp_bool = true; // Track whether this is regular vs 'not cited' + temp_token = t; // Remember whether we need to skip ahead + if (scratch->extensions & EXT_NOTES) { + // Note-based syntax enabled + if (t->type == PAIR_BRACKET) { - // This is a locator for subsequent citation (e.g. `[foo][#bar]` + // This is a locator for a subsequent citation (e.g. `[foo][#bar]`) temp_char = text_inside_pair(source, t); temp_char2 = label_from_string(temp_char); @@ -1070,25 +1072,46 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat free(temp_char2); - // Process the actual citation + // Move ahead to actual citation t = t->next; } else { - // This is just a citation (e.g. `[#foo]`) + // This is the actual citation (e.g. `[#foo]`) + // No locator temp_char = strdup(""); } - // See if we're a citep or cite - temp_char2 = clean_inside_pair(source, t, false); - + // Classify this use + temp_short2 = scratch->used_citations->size; + temp_short3 = scratch->inline_citations_to_free->size; citation_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->next, scratch); + print_const("]"); + + free(temp_char); + break; + } + + // Get instance of the note used temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); if (temp_bool) { - // This is not a "not cited" + // This is a regular citation + + // Are we citep vs citet? + temp_char2 = clean_inside_pair(source, t, false); + if (temp_char2[strlen(temp_char2) - 1] == ';') { + temp_bool = true; // citet + } else { + temp_bool = false; // citep + } + if (temp_char[0] == '\0') { // No locator - if (temp_char2[strlen(temp_char2) - 1] == ';') { + if (temp_bool) { print_const("\\citet"); } else { print_const("~\\citep"); @@ -1096,7 +1119,7 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat } else { // Locator present - // Does the locator contain two options? + // Are there two arguments in the locator? // e.g. `[foo\]\[bar]` temp_char3 = strstr(temp_char, "\\]\\["); if (temp_char3) { @@ -1105,7 +1128,7 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat memmove(temp_char3 + 1, temp_char3 + 3, strlen(temp_char3 - 3)); } - if (temp_char2[strlen(temp_char2) - 1] == ';') { + if (temp_bool) { printf("\\citet[%s]", temp_char); } else { printf("~\\citep[%s]", temp_char); @@ -1113,22 +1136,22 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat } printf("{%s}", temp_note->label_text); + free(temp_char2); } else { // This is a "nocite" - printf("~\\nocite{%s}",temp_note->label_text); + printf("~\\nocite{%s}", temp_note->label_text); } if (temp_token != t) { // Skip citation on next pass scratch->skip_token = 1; } + + free(temp_char); } else { - // Footnotes disabled + // Note-based syntax disabled mmd_export_token_tree_latex(out, source, t->child, scratch); } - - free(temp_char); - free(temp_char2); break; case PAIR_BRACKET_FOOTNOTE: if (scratch->extensions & EXT_NOTES) { diff --git a/Sources/libMultiMarkdown/odf.c b/Sources/libMultiMarkdown/odf.c index 8712479..cb6d69f 100644 --- a/Sources/libMultiMarkdown/odf.c +++ b/Sources/libMultiMarkdown/odf.c @@ -983,12 +983,14 @@ void mmd_export_token_odf(DString * out, const char * source, token * t, scratch break; case PAIR_BRACKET_CITATION: parse_citation: - temp_bool = true; // Track whether this is a 'not cited' - temp_token = t; // Remember whether we need to skip ahead - + temp_bool = true; // Track whether this is regular vs 'not cited' + temp_token = t; // Remember whether we need to skip ahead + if (scratch->extensions & EXT_NOTES) { + // Note-based syntax enabled + if (t->type == PAIR_BRACKET) { - // This is a locator for subsequent citation (e.g. `[foo][#bar]` + // This is a locator for a subsequent citation (e.g. `[foo][#bar]`) temp_char = text_inside_pair(source, t); temp_char2 = label_from_string(temp_char); @@ -1000,45 +1002,95 @@ void mmd_export_token_odf(DString * out, const char * source, token * t, scratch free(temp_char2); - // Process the actual citation + // Move ahead to actual citation t = t->next; } else { - // This is just a citation (e.g. `[#foo]`) + // This is the actual citation (e.g. `[#foo]`) + // No locator temp_char = strdup(""); } - temp_short3 = scratch->used_citations->size; - + // Classify this use + temp_short2 = scratch->used_citations->size; + temp_short3 = scratch->inline_citations_to_free->size; citation_from_bracket(source, scratch, t, &temp_short); - temp_short2 = scratch->odf_para_type; - scratch->odf_para_type = PAIR_BRACKET_CITATION; - - if (temp_short3 == scratch->used_citations->size) { - // Re-using previous citation - print_const("%d", temp_short, temp_short); - } else { - // New citation - printf("", temp_short); - temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); + if (temp_short == -1) { + // This instance is not properly formed + print_const("[#"); + mmd_export_token_tree_odf(out, source, t->child->next, scratch); + print_const("]"); - mmd_export_token_tree_odf(out, source, temp_note->content, scratch); - print_const(""); + free(temp_char); + break; } - scratch->odf_para_type = temp_short2; + // Get instance of the note used + temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); + + temp_short3 = scratch->odf_para_type; + scratch->odf_para_type = PAIR_BRACKET_FOOTNOTE; + + if (temp_bool) { + // This is a regular citation + + if (temp_char[0] == '\0') { + // No locator + + if (temp_short2 == scratch->used_citations->size) { + // This is a re-use of a previously used note + print_const("%d", temp_short, temp_short); + } else { + // This is the first time this note was used + printf("", temp_short); + temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); + + mmd_export_token_tree_odf(out, source, temp_note->content, scratch); + print_const(""); + } + } else { + // Locator present + + if (temp_short2 == scratch->used_citations->size) { + // This is a re-use of a previously used note + print_const("%d", temp_short, temp_short); + } else { + // This is the first time this note was used + printf("", temp_short); + temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); + + mmd_export_token_tree_odf(out, source, temp_note->content, scratch); + print_const(""); + } + } + } else { + if (temp_short2 == scratch->used_citations->size) { + // This is a re-use of a previously used note + } else { + // This is the first time this note was used + // TODO: Not sure how to add an endnote without inserting a marker in the text + printf("", temp_short); + temp_note = stack_peek_index(scratch->used_citations, temp_short - 1); + + mmd_export_token_tree_odf(out, source, temp_note->content, scratch); + print_const(""); + } + } if (temp_token != t) { // Skip citation on next pass scratch->skip_token = 1; } + + scratch->odf_para_type = temp_short3; + + free(temp_char); } else { - // Footnotes disabled + // Note-based syntax disabled mmd_export_token_tree_odf(out, source, t->child, scratch); } - - free(temp_char); break; case PAIR_BRACKET_FOOTNOTE: if (scratch->extensions & EXT_NOTES) { diff --git a/tests/MMD6Tests/Citations.fodt b/tests/MMD6Tests/Citations.fodt index ede03d9..53fefd6 100644 --- a/tests/MMD6Tests/Citations.fodt +++ b/tests/MMD6Tests/Citations.fodt @@ -271,7 +271,7 @@ citep -John Doe. A Totally Fake Book. Vanity Press, 2006. +John Doe. A Totally Fake Book 1. Vanity Press, 2006. 1[] @@ -282,7 +282,7 @@ 1 -1John Doe. A Totally Fake Book. Vanity Press, 2006. +1John Doe. A Totally Fake Book 2. Vanity Press, 2006. citet @@ -299,7 +299,7 @@ 12 -2 +John Doe. A Totally Fake Book 3. Vanity Press, 2006. diff --git a/tests/MMD6Tests/Citations.html b/tests/MMD6Tests/Citations.html index 3dab242..506eff5 100644 --- a/tests/MMD6Tests/Citations.html +++ b/tests/MMD6Tests/Citations.html @@ -41,15 +41,19 @@
  1. -

    John Doe. A Totally Fake Book. Vanity Press, 2006. +

    John Doe. A Totally Fake Book 1. Vanity Press, 2006.  ↩

  2. -

    John Doe. A Totally Fake Book. Vanity Press, 2006. +

    John Doe. A Totally Fake Book 2. Vanity Press, 2006.  ↩

  3. +
  4. +

    John Doe. A Totally Fake Book 3. Vanity Press, 2006.  ↩

    +
  5. +
diff --git a/tests/MMD6Tests/Citations.htmlc b/tests/MMD6Tests/Citations.htmlc index bf32072..c7ad0c7 100644 --- a/tests/MMD6Tests/Citations.htmlc +++ b/tests/MMD6Tests/Citations.htmlc @@ -29,10 +29,10 @@ latex config: article

[#foo1;][#foo2;]

-

[Not Cited][#foo2]

+

[Not Cited][#foo3]

-

[#foo1]: John Doe. A Totally Fake Book. Vanity Press, 2006.

+

[#foo1]: John Doe. A Totally Fake Book 1. Vanity Press, 2006.

-

[#foo2]: John Doe. A Totally Fake Book. Vanity Press, 2006.

+

[#foo2]: John Doe. A Totally Fake Book 2. Vanity Press, 2006.

-

[#foo3]: John Doe. A Totally Fake Book. Vanity Press, 2006.

+

[#foo3]: John Doe. A Totally Fake Book 3. Vanity Press, 2006.

diff --git a/tests/MMD6Tests/Citations.tex b/tests/MMD6Tests/Citations.tex index 380a5ce..ed445e8 100644 --- a/tests/MMD6Tests/Citations.tex +++ b/tests/MMD6Tests/Citations.tex @@ -32,17 +32,20 @@ \citet{foo1}\citet{foo2} -~\nocite{foo2} +~\nocite{foo3} \begin{thebibliography}{0} \bibitem{foo1} -John Doe. \emph{A Totally Fake Book}. Vanity Press, 2006. +John Doe. \emph{A Totally Fake Book 1}. Vanity Press, 2006. \bibitem{foo2} -John Doe. \emph{A Totally Fake Book}. Vanity Press, 2006. +John Doe. \emph{A Totally Fake Book 2}. Vanity Press, 2006. + +\bibitem{foo3} +John Doe. \emph{A Totally Fake Book 3}. Vanity Press, 2006. \end{thebibliography} \input{mmd6-article-footer} diff --git a/tests/MMD6Tests/Citations.text b/tests/MMD6Tests/Citations.text index 5ea3504..96090f5 100644 --- a/tests/MMD6Tests/Citations.text +++ b/tests/MMD6Tests/Citations.text @@ -31,10 +31,11 @@ latex config: article [#foo1;][#foo2;] -[Not Cited][#foo2] +[Not Cited][#foo3] -[#foo1]: John Doe. *A Totally Fake Book*. Vanity Press, 2006. -[#foo2]: John Doe. *A Totally Fake Book*. Vanity Press, 2006. +[#foo1]: John Doe. *A Totally Fake Book 1*. Vanity Press, 2006. -[#foo3]: John Doe. *A Totally Fake Book*. Vanity Press, 2006. +[#foo2]: John Doe. *A Totally Fake Book 2*. Vanity Press, 2006. + +[#foo3]: John Doe. *A Totally Fake Book 3*. Vanity Press, 2006.