]> granicus.if.org Git - multimarkdown/commitdiff
CHANGED: Finish refactoring note-related code
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Sun, 5 Mar 2017 19:51:45 +0000 (14:51 -0500)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Sun, 5 Mar 2017 19:51:45 +0000 (14:51 -0500)
Sources/libMultiMarkdown/html.c
Sources/libMultiMarkdown/latex.c
Sources/libMultiMarkdown/odf.c
tests/MMD6Tests/Citations.fodt
tests/MMD6Tests/Citations.html
tests/MMD6Tests/Citations.htmlc
tests/MMD6Tests/Citations.tex
tests/MMD6Tests/Citations.text

index e50d7f0052f10f5038b9a6312c180445b5c689cd..c4fa0ddcde58f2cf37f2e1f5fec848ecde2482df 100644 (file)
@@ -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("<a href=\"#cn:%d\" title=\"%s\" class=\"citation\">[%s%s%d]</a>",
-                                                               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("<a href=\"#cn:%d\" title=\"%s\" class=\"citation\">[%d]</a>",
+                                                                       temp_short, LC("see citation"), temp_short);
+                                               } else {
+                                                       // This is the first time this note was used
+                                                       printf("<a href=\"#cn:%d\" id=\"cnref:%d\" title=\"%s\" class=\"citation\">[%d]</a>",
+                                                                       temp_short, temp_short, LC("see citation"), temp_short);
+                                               }
                                        } else {
-                                               // New citation
-                                               printf("<a href=\"#cn:%d\" id=\"cnref:%d\" title=\"%s\" class=\"citation\">[%s%s%d]</a>",
-                                                               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("<a href=\"#cn:%d\" title=\"%s\" class=\"citation\">[%s, %d]</a>",
+                                                                       temp_short, LC("see citation"), temp_char, temp_short);
+                                               } else {
+                                                       // This is the first time this note was used
+                                                       printf("<a href=\"#cn:%d\" id=\"cnref:%d\" title=\"%s\" class=\"citation\">[%s, %d]</a>",
+                                                                       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
index 9742ca8678085f060347bdd3b8547aea2e467514..a6f51194c74b848b0b1f8fcfd95f3b545c40b34e 100644 (file)
@@ -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) {
index 87124793370949b70a48995d53d6e82206d2a571..cb6d69f77b57cd3d01dc806932550b5527421bab 100644 (file)
@@ -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("<text:span text:style-name=\"Footnote_20_anchor\"><text:note-ref text:note-class=\"endnote\" text:reference-format=\"text\" ");
-                                       printf("text:ref-name=\"cite%d\">%d</text:note-ref></text:span>", temp_short, temp_short);
-                               } else {
-                                       // New citation
-                                       printf("<text:note text:id=\"cite%d\" text:note-class=\"endnote\"><text:note-body>", 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("</text:note-body></text:note>");
+                                       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("<text:span text:style-name=\"Footnote_20_anchor\"><text:note-ref text:note-class=\"endnote\" text:reference-format=\"text\" ");
+                                                       printf("text:ref-name=\"cite%d\">%d</text:note-ref></text:span>", temp_short, temp_short);
+                                               } else {
+                                                       // This is the first time this note was used
+                                                       printf("<text:note text:id=\"cite%d\" text:note-class=\"endnote\"><text:note-body>", 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("</text:note-body></text:note>");
+                                               }
+                                       } else {
+                                               // Locator present
+
+                                               if (temp_short2 == scratch->used_citations->size) {
+                                                       // This is a re-use of a previously used note
+                                                       print_const("<text:span text:style-name=\"Footnote_20_anchor\"><text:note-ref text:note-class=\"endnote\" text:reference-format=\"text\" ");
+                                                       printf("text:ref-name=\"cite%d\">%d</text:note-ref></text:span>", temp_short, temp_short);
+                                               } else {
+                                                       // This is the first time this note was used
+                                                       printf("<text:note text:id=\"cite%d\" text:note-class=\"endnote\"><text:note-body>", 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("</text:note-body></text:note>");
+                                               }
+                                       }
+                               } 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("<text:note text:id=\"cite%d\" text:note-class=\"endnote\"><text:note-body>", 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("</text:note-body></text:note>");
+                                               }
+                               }
 
                                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) {
index ede03d9f7a39e36bbeb21a7f64c65ac971845b00..53fefd6252ff0992ea76bb254e37ef6881d89e07 100644 (file)
 <office:text>
 <text:h text:outline-level="1"><text:bookmark text:name="citep"/>citep <text:bookmark-end text:name="citep"/></text:h>
 
-<text:p text:style-name="Standard"><text:note text:id="cite1" text:note-class="endnote"><text:note-body><text:p text:style-name="Footnote">John Doe. <text:span text:style-name="MMD-Italic">A Totally Fake Book</text:span>. Vanity Press, 2006.
+<text:p text:style-name="Standard"><text:note text:id="cite1" text:note-class="endnote"><text:note-body><text:p text:style-name="Footnote">John Doe. <text:span text:style-name="MMD-Italic">A Totally Fake Book 1</text:span>. Vanity Press, 2006.
 </text:p></text:note-body></text:note></text:p>
 
 <text:p text:style-name="Standard"><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite1">1</text:note-ref></text:span>[]</text:p>
 
 <text:p text:style-name="Standard"><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite1">1</text:note-ref></text:span></text:p>
 
-<text:p text:style-name="Standard"><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite1">1</text:note-ref></text:span><text:note text:id="cite2" text:note-class="endnote"><text:note-body><text:p text:style-name="Footnote">John Doe. <text:span text:style-name="MMD-Italic">A Totally Fake Book</text:span>. Vanity Press, 2006.
+<text:p text:style-name="Standard"><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite1">1</text:note-ref></text:span><text:note text:id="cite2" text:note-class="endnote"><text:note-body><text:p text:style-name="Footnote">John Doe. <text:span text:style-name="MMD-Italic">A Totally Fake Book 2</text:span>. Vanity Press, 2006.
 </text:p></text:note-body></text:note></text:p>
 
 <text:h text:outline-level="1"><text:bookmark text:name="citet"/>citet <text:bookmark-end text:name="citet"/></text:h>
 
 <text:p text:style-name="Standard"><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite1">1</text:note-ref></text:span><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite2">2</text:note-ref></text:span></text:p>
 
-<text:p text:style-name="Standard"><text:span text:style-name="Footnote_20_anchor"><text:note-ref text:note-class="endnote" text:reference-format="text" text:ref-name="cite2">2</text:note-ref></text:span></text:p>
+<text:p text:style-name="Standard"><text:note text:id="cite3" text:note-class="endnote"><text:note-body><text:p text:style-name="Footnote">John Doe. <text:span text:style-name="MMD-Italic">A Totally Fake Book 3</text:span>. Vanity Press, 2006.</text:p></text:note-body></text:note></text:p>
 </office:text>
 </office:body>
 </office:document>
index 3dab2422ee4842828e3d81f64b33cdb3bfd1dc98..506eff5e038c76749a2ac0441c7d1374635badc6 100644 (file)
 <ol>
 
 <li id="cn:1">
-<p>John Doe. <em>A Totally Fake Book</em>. Vanity Press, 2006.
+<p>John Doe. <em>A Totally Fake Book 1</em>. Vanity Press, 2006.
  <a href="#cnref:1" title="return to body" class="reversecitation">&#160;&#8617;</a></p>
 </li>
 
 <li id="cn:2">
-<p>John Doe. <em>A Totally Fake Book</em>. Vanity Press, 2006.
+<p>John Doe. <em>A Totally Fake Book 2</em>. Vanity Press, 2006.
  <a href="#cnref:2" title="return to body" class="reversecitation">&#160;&#8617;</a></p>
 </li>
 
+<li id="cn:3">
+<p>John Doe. <em>A Totally Fake Book 3</em>. Vanity Press, 2006. <a href="#cnref:3" title="return to body" class="reversecitation">&#160;&#8617;</a></p>
+</li>
+
 </ol>
 </div>
 
index bf32072b5882a31324562c7c4347ed28d3147f68..c7ad0c72a7564781bbb87c28fc03bf7fdf49f11e 100644 (file)
@@ -29,10 +29,10 @@ latex config:       article</p>
 
 <p>[#foo1;][#foo2;]</p>
 
-<p>[Not Cited][#foo2]</p>
+<p>[Not Cited][#foo3]</p>
 
-<p>[#foo1]: John Doe. <em>A Totally Fake Book</em>. Vanity Press, 2006.</p>
+<p>[#foo1]: John Doe. <em>A Totally Fake Book 1</em>. Vanity Press, 2006.</p>
 
-<p>[#foo2]: John Doe. <em>A Totally Fake Book</em>. Vanity Press, 2006.</p>
+<p>[#foo2]: John Doe. <em>A Totally Fake Book 2</em>. Vanity Press, 2006.</p>
 
-<p>[#foo3]: John Doe. <em>A Totally Fake Book</em>. Vanity Press, 2006.</p>
+<p>[#foo3]: John Doe. <em>A Totally Fake Book 3</em>. Vanity Press, 2006.</p>
index 380a5cecbc8b37605bd6d72a25baae328f021da7..ed445e8dff06a06c0a62dc32eb22dfffd67ca653 100644 (file)
 
 \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}
index 5ea3504c3406c58deb764a0859f070ac01bc516a..96090f51bacbf4e2d94dc6f15499b9b09dbe9386 100644 (file)
@@ -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.