]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Add support for random header labels when not manually specified (Addresses...
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Tue, 9 Oct 2018 04:44:59 +0000 (00:44 -0400)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Tue, 9 Oct 2018 04:44:59 +0000 (00:44 -0400)
Sources/libMultiMarkdown/epub.c
Sources/libMultiMarkdown/html.c
Sources/libMultiMarkdown/include/libMultiMarkdown.h
Sources/libMultiMarkdown/latex.c
Sources/libMultiMarkdown/mmd.h
Sources/libMultiMarkdown/opendocument-content.c
Sources/libMultiMarkdown/writer.c
Sources/libMultiMarkdown/writer.h
Sources/multimarkdown/main.c

index 45392202bb88fda3ee5406feb21d5733eceada13..441c8bca75a745240a4fdd7fab7651716b06a2df 100644 (file)
@@ -251,7 +251,8 @@ void epub_export_nav_entry(DString * out, const char * source, scratch_pad * scr
 
                if (entry_level >= level) {
                        // This entry is a direct descendant of the parent
-                       temp_char = label_from_header(source, entry);
+                       scratch->label_counter = *counter;
+                       temp_char = label_from_header(source, entry, scratch);
                        printf("<li><a href=\"main.xhtml#%s\">", temp_char);
                        mmd_export_token_tree_html(out, source, entry->child, scratch);
                        print_const("</a>");
@@ -287,8 +288,9 @@ void epub_export_nav_entry(DString * out, const char * source, scratch_pad * scr
 void epub_export_nav(DString * out, mmd_engine * e, scratch_pad * scratch) {
        size_t counter = 0;
 
-
        epub_export_nav_entry(out, e->dstr->str, scratch, &counter, 0);
+
+       scratch->label_counter = 0;
 }
 
 
@@ -472,6 +474,7 @@ void epub_write_wrapper(const char * filepath, DString * body, mmd_engine * e, c
 DString * epub_create(DString * body, mmd_engine * e, const char * directory) {
        DString * result = d_string_new("");
        scratch_pad * scratch = scratch_pad_new(e, FORMAT_EPUB);
+       scratch->random_seed_base_labels = e->random_seed_base_labels;
 
        mz_bool status;
        char * data;
index de33c01e708970efc0adc1b801fd00755c7bdbf9..410b928fe6d68800874ccce2532b61130be67338 100644 (file)
@@ -470,7 +470,8 @@ void mmd_export_toc_entry_html(DString * out, const char * source, scratch_pad *
 
                if (entry_level >= level) {
                        // This entry is a direct descendant of the parent
-                       temp_char = label_from_header(source, entry);
+                       scratch->label_counter = *counter;
+                       temp_char = label_from_header(source, entry, scratch);
                        printf("<li><a href=\"#%s\">", temp_char);
                        mmd_export_token_tree_html(out, source, entry->child, scratch);
                        print_const("</a>");
@@ -507,6 +508,8 @@ void mmd_export_toc_html(DString * out, const char * source, scratch_pad * scrat
        size_t counter = 0;
 
        mmd_export_toc_entry_html(out, source, scratch, &counter, 0);
+
+       scratch->label_counter = 0;
 }
 
 
@@ -685,7 +688,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc
                        if (scratch->extensions & EXT_NO_LABELS) {
                                printf("<h%1d>", temp_short + scratch->base_header_level - 1);
                        } else {
-                               temp_char = label_from_header(source, t);
+                               temp_char = label_from_header(source, t, scratch);
                                printf("<h%1d id=\"%s\">", temp_short + scratch->base_header_level - 1, temp_char);
                                free(temp_char);
                        }
@@ -853,14 +856,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc
                        if (scratch->extensions & EXT_NO_LABELS) {
                                printf("<h%1d>", temp_short + scratch->base_header_level - 1);
                        } else {
-                               temp_token = manual_label_from_header(t, source);
-
-                               if (temp_token) {
-                                       temp_char = label_from_token(source, temp_token);
-                               } else {
-                                       temp_char = label_from_token(source, t);
-                               }
-
+                               temp_char = label_from_header(source, t, scratch);
                                printf("<h%1d id=\"%s\">", temp_short + scratch->base_header_level - 1, temp_char);
                                free(temp_char);
                        }
@@ -877,14 +873,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, scratc
                        if (scratch->extensions & EXT_NO_LABELS) {
                                printf("<h%1d>", temp_short + scratch->base_header_level - 1);
                        } else {
-                               temp_token = manual_label_from_header(t, source);
-
-                               if (temp_token) {
-                                       temp_char = label_from_token(source, temp_token);
-                               } else {
-                                       temp_char = label_from_token(source, t);
-                               }
-
+                               temp_char = label_from_header(source, t, scratch);
                                printf("<h%1d id=\"%s\">", temp_short + scratch->base_header_level - 1, temp_char);
                                free(temp_char);
                        }
index 800e9d9526549e3fb24fef098df95087246e9fef..4a1cc613c74fbf0bb315af41485637af2aff2737 100644 (file)
@@ -582,6 +582,7 @@ enum parser_extensions {
        EXT_TRANSCLUDE          = 1 << 13,   //!< Perform transclusion(s)
        EXT_PARSE_OPML          = 1 << 14,   //!< Convert from OPML before processing source text
        EXT_PARSE_ITMZ                  = 1 << 15,   //!< Convert from ITMZ (iThoughts) before processing source text
+       EXT_RANDOM_LABELS               = 1 << 16,   //!< Use random numbers for header labels (unless manually defined)
        EXT_FAKE                = 1 << 31,   //!< 31 is highest number allowed
 };
 
index a3ef057988a4eb7551546dfec6f20177e6253c5b..a7b91fd1b3cd119402edc09a71af3452843a7ea3 100644 (file)
@@ -447,7 +447,8 @@ void mmd_export_toc_entry_latex(DString * out, const char * source, scratch_pad
 
                if (entry_level >= level) {
                        // This entry is a direct descendant of the parent
-                       temp_char = label_from_header(source, entry);
+                       scratch->label_counter = *counter;
+                       temp_char = label_from_header(source, entry, scratch);
                        print_const("\\item ");
                        mmd_export_token_tree_latex(out, source, entry->child, scratch);
                        printf("(\\autoref{%s})\n\n", temp_char);
@@ -483,6 +484,8 @@ void mmd_export_toc_latex(DString * out, const char * source, scratch_pad * scra
        size_t counter = 0;
 
        mmd_export_toc_entry_latex(out, source, scratch, &counter, 0);
+
+       scratch->label_counter = 0;
 }
 
 
@@ -696,14 +699,7 @@ void mmd_export_token_latex(DString * out, const char * source, token * t, scrat
                        if (scratch->extensions & EXT_NO_LABELS) {
                                print_const("}");
                        } else {
-                               temp_token = manual_label_from_header(t, source);
-
-                               if (temp_token) {
-                                       temp_char = label_from_token(source, temp_token);
-                               } else {
-                                       temp_char = label_from_token(source, t);
-                               }
-
+                               temp_char = label_from_header(source, t, scratch);
                                printf("}\n\\label{%s}", temp_char);
                                free(temp_char);
                        }
index de742754c50f875b35448e98372431c987476e33..2f3622a08a58bc20fd57a415abd44a619a14a85e 100644 (file)
@@ -94,6 +94,8 @@ struct mmd_engine {
        short                                   quotes_lang;
 
        struct asset *                  asset_hash;
+
+       int                                             random_seed_base_labels;
 };
 
 
index 69d0be22b6ddcbdfb8a4107b5b2275037e3378db..539dc7e35d1bf6590a1b0e0742aedf73823ebf64 100644 (file)
@@ -643,7 +643,8 @@ void mmd_export_toc_entry_opendocument(DString * out, const char * source, scrat
 
                if (entry_level >= level) {
                        // This entry is a direct descendant of the parent
-                       temp_char = label_from_header(source, entry);
+                       scratch->label_counter = *counter;
+                       temp_char = label_from_header(source, entry, scratch);
                        printf("<text:p text:style-name=\"TOC_Item\"><text:a xlink:type=\"simple\" xlink:href=\"#%s\" text:style-name=\"Index_20_Link\" text:visited-style-name=\"Index_20_Link\">", temp_char);
                        mmd_export_token_tree_opendocument(out, source, entry->child, scratch);
                        print_const(" <text:tab/>1</text:a></text:p>\n");
@@ -688,6 +689,8 @@ void mmd_export_toc_opendocument(DString * out, const char * source, scratch_pad
        mmd_export_toc_entry_opendocument(out, source, scratch, &counter, 0);
 
        print_const("</text:index-body>\n</text:table-of-content>\n\n");
+
+       scratch->label_counter = 0;
 }
 
 
@@ -889,7 +892,7 @@ void mmd_export_token_opendocument(DString * out, const char * source, token * t
                        if (scratch->extensions & EXT_NO_LABELS) {
                                mmd_export_token_tree_opendocument(out, source, t->child, scratch);
                        } else {
-                               temp_char = label_from_header(source, t);
+                               temp_char = label_from_header(source, t, scratch);
                                printf("<text:bookmark text:name=\"%s\"/>", temp_char);
                                mmd_export_token_tree_opendocument(out, source, t->child, scratch);
                                //printf("<text:bookmark-end text:name=\"%s\"/>", temp_char);
index d8145f1dcc8641535bdd190069430002e4a6a9ae..ffd3d037841b79a629f4c4c6a95407c7f1d2eb52 100644 (file)
@@ -169,6 +169,14 @@ scratch_pad * scratch_pad_new(mmd_engine * e, short format) {
                        p->random_seed_base = 0;
                }
 
+               if (e->extensions & EXT_RANDOM_LABELS) {
+                       p->random_seed_base_labels = rand() % 32000;
+               } else {
+                       p->random_seed_base_labels = 0;
+               }
+
+               p->label_counter = 0;
+
                // Store links in a hash for rapid retrieval when exporting
                p->link_hash = NULL;
                link * l;
@@ -452,14 +460,25 @@ char * label_from_token(const char * source, token * t) {
 }
 
 
-char * label_from_header(const char * source, token * t) {
+char * label_from_header(const char * source, token * t, scratch_pad * scratch) {
        char * result;
+       short temp_short;
+
        token * temp_token = manual_label_from_header(t, source);
 
        if (temp_token) {
                result = label_from_token(source, temp_token);
        } else {
-               result = label_from_token(source, t);
+               if (scratch->extensions & EXT_RANDOM_LABELS) {
+                       srand(scratch->random_seed_base_labels + scratch->label_counter);
+                       temp_short = rand() % 32000 + 1;
+                       result = malloc(sizeof(char) * 6);
+                       sprintf(result, "%d", temp_short);
+
+                       scratch->label_counter++;
+               } else {
+                       result = label_from_token(source, t);
+               }
        }
 
        return result;
@@ -1968,6 +1987,9 @@ void mmd_engine_export_token_tree(DString * out, mmd_engine * e, short format) {
        // Preserve asset_hash for possible use in export
        e->asset_hash = scratch->asset_hash;
 
+       // Preserve random label seed
+       e->random_seed_base_labels = scratch->random_seed_base_labels;
+       
        scratch_pad_free(scratch);
 }
 
index 6922a9d28331572ad91061b20be12b3717597064..774956453ea30ec8167e034e2d9bd95a8aec9bb9 100644 (file)
@@ -90,6 +90,9 @@ typedef struct {
 
        int                             random_seed_base;
 
+       int                             random_seed_base_labels;
+       int                             label_counter;
+
        stack *                         used_citations;
        stack *                         inline_citations_to_free;
        struct fn_holder *      citation_hash;
@@ -219,7 +222,7 @@ void link_free(link * l);
 void footnote_free(footnote * f);
 
 char * label_from_token(const char * source, token * t);
-char * label_from_header(const char * source, token * t);
+char * label_from_header(const char * source, token * t, scratch_pad * scratch);
 
 void parse_brackets(const char * source, scratch_pad * scratch, token * bracket, link ** link, short * skip_token, bool * free_link);
 
index 5640d8d00c4059b8a52f8aa4c2025edba08bd137..5cdb764304ddc21f8155408f3869052754aede18 100644 (file)
@@ -75,7 +75,7 @@
 
 // argtable structs
 struct arg_lit *a_help, *a_version, *a_compatibility, *a_nolabels, *a_batch,
-                  *a_accept, *a_reject, *a_full, *a_snippet, *a_random, *a_meta,
+                  *a_accept, *a_reject, *a_full, *a_snippet, *a_random, *a_unique, *a_meta,
                   *a_notransclude, *a_nosmart, *a_opml, *a_itmz;
 struct arg_str *a_format, *a_lang, *a_extract;
 struct arg_file *a_file, *a_o;
@@ -147,6 +147,7 @@ int main(int argc, char** argv) {
                a_snippet               = arg_lit0("s", "snippet", "force a snippet"),
                a_compatibility = arg_lit0("c", "compatibility", "Markdown compatibility mode"),
                a_random                = arg_lit0(NULL, "random", "use random numbers for footnote anchors"),
+               a_unique                = arg_lit0(NULL, "unique", "use random numbers for header labels unless manually specified"),
                a_nosmart               = arg_lit0(NULL, "nosmart", "Disable smart typography"),
                a_nolabels              = arg_lit0(NULL, "nolabels", "Disable id attributes for headers"),
                a_notransclude  = arg_lit0(NULL, "notransclude", "Disable file transclusion"),
@@ -275,6 +276,11 @@ int main(int argc, char** argv) {
                extensions |= EXT_RANDOM_FOOT;
        }
 
+       if (a_unique->count > 0) {
+               // Use random header labels
+               extensions |= EXT_RANDOM_LABELS;
+       }
+
        if (a_format->count > 0) {
                if (strcmp(a_format->sval[0], "html") == 0) {
                        format = FORMAT_HTML;