From ad1b6bda84a35dda5490ed738aac68f3ad86c2f8 Mon Sep 17 00:00:00 2001 From: "Fletcher T. Penney" Date: Wed, 8 Feb 2017 10:53:33 -0500 Subject: [PATCH] ADDED: Add support for reference image id attributes --- src/html.c | 62 +++++++++++++++++++++++---- src/writer.c | 14 ++++-- src/writer.h | 1 + tests/MMD6Tests/Figure Images.html | 16 +++++++ tests/MMD6Tests/Figure Images.htmlc | 9 ++++ tests/MMD6Tests/Figure Images.text | 9 ++++ tests/MMD6Tests/Link Attributes.html | 7 ++- tests/MMD6Tests/Reference Images.html | 16 +++---- 8 files changed, 112 insertions(+), 22 deletions(-) create mode 100644 tests/MMD6Tests/Figure Images.html create mode 100644 tests/MMD6Tests/Figure Images.htmlc create mode 100644 tests/MMD6Tests/Figure Images.text diff --git a/src/html.c b/src/html.c index 698f89b..8acf7cf 100644 --- a/src/html.c +++ b/src/html.c @@ -236,9 +236,20 @@ void mmd_export_link_html(DString * out, const char * source, token * text, link } -void mmd_export_image_html(DString * out, const char * source, token * text, link * link, size_t offset, scratch_pad * scratch) { +void mmd_export_image_html(DString * out, const char * source, token * text, link * link, size_t offset, scratch_pad * scratch, bool is_figure) { attr * a = link->attributes; + // Compatibility mode doesn't allow figures + if (scratch->extensions & EXT_COMPATIBILITY) + is_figure = false; + + if (is_figure) { + // Remove wrapping

markers + d_string_erase(out, out->currentStringLength - 3, 3); + print("

\n"); + scratch->close_para = false; + } + if (link->url) printf("url); else @@ -250,7 +261,7 @@ void mmd_export_image_html(DString * out, const char * source, token * text, lin print("\""); } - if (0 && link->label) { + if (link->label && !(scratch->extensions & EXT_COMPATIBILITY)) { // \todo: Need to decide on approach to id's char * label = label_from_token(source, link->label); printf(" id=\"%s\"", label); @@ -270,6 +281,15 @@ void mmd_export_image_html(DString * out, const char * source, token * text, lin } print(" />"); + + if (is_figure) { + if (text) { + print("\n
"); + mmd_export_token_tree_html(out, source, text->child, offset, scratch); + print("
"); + } + print("\n
"); + } } @@ -457,8 +477,12 @@ void mmd_export_token_html(DString * out, const char * source, token * t, size_t scratch->padded = 2; mmd_export_token_tree_html(out, source, t->child, offset, scratch); - if (!scratch->list_is_tight) - print("

"); + if (scratch->close_para) { + if (!scratch->list_is_tight) + print("

"); + } else { + scratch->close_para = true; + } print(""); scratch->padded = 0; @@ -491,8 +515,12 @@ void mmd_export_token_html(DString * out, const char * source, token * t, size_t } } - if (!scratch->list_is_tight) - print("

"); + if (scratch->close_para) { + if (!scratch->list_is_tight) + print("

"); + } else { + scratch->close_para = true; + } scratch->padded = 0; break; case BLOCK_TABLE: @@ -859,8 +887,26 @@ void mmd_export_token_html(DString * out, const char * source, token * t, size_t // Link mmd_export_link_html(out, source, t, temp_link, offset, scratch); } else { - // Image - mmd_export_image_html(out, source, t, temp_link, offset, scratch); + // Image -- should it be a figure (e.g. image is only thing in paragraph)? + temp_token = t->next; + + if (temp_token && + ((temp_token->type == PAIR_BRACKET) || + (temp_token->type == PAIR_PAREN))) { + temp_token = temp_token->next; + } + + if (temp_token && temp_token->type == TEXT_NL) + temp_token = temp_token->next; + + if (temp_token && temp_token->type == TEXT_LINEBREAK) + temp_token = temp_token->next; + + if (t->prev || temp_token) { + mmd_export_image_html(out, source, t, temp_link, offset, scratch, false); + } else { + mmd_export_image_html(out, source, t, temp_link, offset, scratch, true); + } } if (temp_bool) { diff --git a/src/writer.c b/src/writer.c index 9810ec7..197b8bf 100644 --- a/src/writer.c +++ b/src/writer.c @@ -85,6 +85,7 @@ scratch_pad * scratch_pad_new(mmd_engine * e) { p->padded = 2; // Prevent unnecessary leading space p->list_is_tight = false; // Tight vs Loose list p->skip_token = 0; // Skip over next n tokens + p->close_para = true; p->extensions = e->extensions; p->quotes_lang = e->quotes_lang; @@ -452,8 +453,13 @@ link * link_new(const char * source, token * label, char * url, char * title, ch if (l) { l->label = label; - l->clean_text = clean_inside_pair(source, label, true); - l->label_text = label_from_token(source, label); + if (label) { + l->clean_text = clean_inside_pair(source, label, true); + l->label_text = label_from_token(source, label); + } else { + l->clean_text = NULL; + l->label_text = NULL; + } l->url = clean_string(url, false); l->title = (title == NULL) ? NULL : strdup(title); l->attributes = (attributes == NULL) ? NULL : parse_attributes(attributes); @@ -808,9 +814,9 @@ link * explicit_link(scratch_pad * scratch, token * bracket, token * paren, cons if (attr_char) { if (!(scratch->extensions & EXT_COMPATIBILITY)) - l = link_new(source, bracket, url_char, title_char, attr_char); + l = link_new(source, NULL, url_char, title_char, attr_char); } else { - l = link_new(source, bracket, url_char, title_char, attr_char); + l = link_new(source, NULL, url_char, title_char, attr_char); } free(url_char); diff --git a/src/writer.h b/src/writer.h index 7f2260d..2e62fa6 100644 --- a/src/writer.h +++ b/src/writer.h @@ -78,6 +78,7 @@ typedef struct { unsigned long extensions; short padded; //!< How many empty lines at end output buffer short list_is_tight; + short close_para; short skip_token; short footnote_para_counter; diff --git a/tests/MMD6Tests/Figure Images.html b/tests/MMD6Tests/Figure Images.html new file mode 100644 index 0000000..d7bd33a --- /dev/null +++ b/tests/MMD6Tests/Figure Images.html @@ -0,0 +1,16 @@ +
+foo +
foo
+
+ +
+bar +
bar
+
+ +

bar bar

+ +
+foo +
foo
+
diff --git a/tests/MMD6Tests/Figure Images.htmlc b/tests/MMD6Tests/Figure Images.htmlc new file mode 100644 index 0000000..6190c81 --- /dev/null +++ b/tests/MMD6Tests/Figure Images.htmlc @@ -0,0 +1,9 @@ +

![foo]

+ +

![bar][foo]

+ +

![bar][foo] bar

+ +

![foo](http://foo.bar/ "foo" width="40px")

+ +

[foo]: http://foo.bar/ "foo" width="40px"

\ No newline at end of file diff --git a/tests/MMD6Tests/Figure Images.text b/tests/MMD6Tests/Figure Images.text new file mode 100644 index 0000000..b4437f2 --- /dev/null +++ b/tests/MMD6Tests/Figure Images.text @@ -0,0 +1,9 @@ +![foo] + +![*bar*][foo] + +![*bar*][foo] bar + +![*foo*](http://foo.bar/ "foo" width="40px") + +[foo]: http://foo.bar/ "foo" width="40px" diff --git a/tests/MMD6Tests/Link Attributes.html b/tests/MMD6Tests/Link Attributes.html index 8afed8d..fd7087c 100644 --- a/tests/MMD6Tests/Link Attributes.html +++ b/tests/MMD6Tests/Link Attributes.html @@ -1,4 +1,4 @@ -

foo image

+

foo image

foo link

@@ -6,6 +6,9 @@

foo link3

-

test

+
+test +
test
+

5

diff --git a/tests/MMD6Tests/Reference Images.html b/tests/MMD6Tests/Reference Images.html index c277d09..2b856df 100644 --- a/tests/MMD6Tests/Reference Images.html +++ b/tests/MMD6Tests/Reference Images.html @@ -1,17 +1,17 @@ -

Test foo.

+

Test foo.

-

Test foo.

+

Test foo.

-

Test foo.

+

Test foo.

-

Test foo.

+

Test foo.

-

Test foo.

+

Test foo.

5

-

Test foo.

+

Test foo.

-

Test foo.

+

Test foo.

-

Test foo.

+

Test foo.

-- 2.50.1