Skip to content

Commit

Permalink
ADDED: Add support for random header labels when not manually specifi…
Browse files Browse the repository at this point in the history
…ed (Addresses #157)
  • Loading branch information
fletcher committed Oct 9, 2018
1 parent 08fbc7c commit 1e870f4
Show file tree
Hide file tree
Showing 9 changed files with 60 additions and 35 deletions.
7 changes: 5 additions & 2 deletions Sources/libMultiMarkdown/epub.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>");
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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;
Expand Down
25 changes: 7 additions & 18 deletions Sources/libMultiMarkdown/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -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>");
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions Sources/libMultiMarkdown/include/libMultiMarkdown.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
};

Expand Down
14 changes: 5 additions & 9 deletions Sources/libMultiMarkdown/latex.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
}
Expand Down
2 changes: 2 additions & 0 deletions Sources/libMultiMarkdown/mmd.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ struct mmd_engine {
short quotes_lang;

struct asset * asset_hash;

int random_seed_base_labels;
};


Expand Down
7 changes: 5 additions & 2 deletions Sources/libMultiMarkdown/opendocument-content.c
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down Expand Up @@ -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;
}


Expand Down Expand Up @@ -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);
Expand Down
26 changes: 24 additions & 2 deletions Sources/libMultiMarkdown/writer.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}

Expand Down
5 changes: 4 additions & 1 deletion Sources/libMultiMarkdown/writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down
8 changes: 7 additions & 1 deletion Sources/multimarkdown/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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"),
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit 1e870f4

Please sign in to comment.