From: Fletcher T. Penney Date: Fri, 10 Feb 2017 03:37:05 +0000 (-0500) Subject: ADDED: Add support for manual labels on ATX Headers X-Git-Tag: 0.3.0a^2~30 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=40857bcd11353193e0e09a42acc900c43f1f888e;p=multimarkdown ADDED: Add support for manual labels on ATX Headers --- diff --git a/src/html.c b/src/html.c index 29001e1..11769a6 100644 --- a/src/html.c +++ b/src/html.c @@ -415,7 +415,12 @@ void mmd_export_token_html(DString * out, const char * source, token * t, size_t if (scratch->extensions & EXT_NO_LABELS) { printf("", temp_short + scratch->base_header_level - 1); } else { - temp_char = label_from_token(source, t); + 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); + } printf("", temp_short + scratch->base_header_level - 1, temp_char); free(temp_char); } @@ -1267,6 +1272,7 @@ void mmd_export_token_html(DString * out, const char * source, token * t, size_t break; case CODE_FENCE: case TEXT_EMPTY: + case MANUAL_LABEL: break; case TEXT_NL: if (t->next) diff --git a/src/libMultiMarkdown.h b/src/libMultiMarkdown.h index 17d58ea..c1bcf3d 100644 --- a/src/libMultiMarkdown.h +++ b/src/libMultiMarkdown.h @@ -270,6 +270,8 @@ enum token_types { TEXT_NUMBER_POSS_LIST, TEXT_PERIOD, TEXT_PLAIN, + + MANUAL_LABEL, }; diff --git a/src/writer.c b/src/writer.c index 432ccf6..7f8e53f 100644 --- a/src/writer.c +++ b/src/writer.c @@ -1110,10 +1110,69 @@ void process_definition_stack(mmd_engine * e) { } } +token * manual_label_from_header(token * h, const char * source) { + token * walker = h->child->tail; + token * label = NULL; + short count = 0; + + while (walker) { + switch (walker->type) { + case MANUAL_LABEL: + // Already identified + label = walker; + walker = NULL; + break; + case INDENT_TAB: + case INDENT_SPACE: + case NON_INDENT_SPACE: + case TEXT_NL: + case TEXT_LINEBREAK: + case TEXT_EMPTY: + walker = walker->prev; + break; + case TEXT_PLAIN: + if (walker->len == 1) { + if (source[walker->start] == ' ') { + walker = walker->prev; + break; + } + } + walker = NULL; + break; + case PAIR_BRACKET: + label = walker; + while(walker->type == PAIR_BRACKET) { + walker = walker->prev; + count++; + } + if (count % 2 == 0) { + // Even count + label = NULL; + } else { + // Odd count + label->type = MANUAL_LABEL; + } + default: + walker = NULL; + } + } + + return label; +} + void process_header_to_links(mmd_engine * e, token * h) { char * label = label_from_token(e->dstr->str, h); + // See if we have a manual label + token * manual = manual_label_from_header(h, e->dstr->str); + + if (manual) { + free(label); + label = label_from_token(e->dstr->str, manual); + h = manual; + } + DString * url = d_string_new("#"); d_string_append(url, label); diff --git a/src/writer.h b/src/writer.h index faf8d7c..fa3f5b4 100644 --- a/src/writer.h +++ b/src/writer.h @@ -207,5 +207,7 @@ bool table_has_caption(token * table); char * get_fence_language_specifier(token * fence, const char * source); +token * manual_label_from_header(token * h, const char * source); + #endif diff --git a/tests/MMD6Tests/Advanced Headers.html b/tests/MMD6Tests/Advanced Headers.html new file mode 100644 index 0000000..b3bb99a --- /dev/null +++ b/tests/MMD6Tests/Advanced Headers.html @@ -0,0 +1,11 @@ +

foo

+ +

foo

+ +

foo bar

+ +

foo bar

+ +

foo bar

+ +

5

diff --git a/tests/MMD6Tests/Advanced Headers.htmlc b/tests/MMD6Tests/Advanced Headers.htmlc new file mode 100644 index 0000000..0a8e69e --- /dev/null +++ b/tests/MMD6Tests/Advanced Headers.htmlc @@ -0,0 +1,11 @@ +

foo [bar]

+ +

foo[bar]

+ +

foo [bar][bat]

+ +

foo [bar] [bat]

+ +

foo [bar][bat][baz]

+ +

5

diff --git a/tests/MMD6Tests/Advanced Headers.text b/tests/MMD6Tests/Advanced Headers.text new file mode 100644 index 0000000..949dab6 --- /dev/null +++ b/tests/MMD6Tests/Advanced Headers.text @@ -0,0 +1,11 @@ +# foo [bar] # + +# foo[bar] + +# foo [bar][bat] + +# foo [bar] [bat] + +# foo [bar][bat][baz] + +5