]> granicus.if.org Git - multimarkdown/commitdiff
ADDED: Add support for reference image id attributes
authorFletcher T. Penney <fletcher@fletcherpenney.net>
Wed, 8 Feb 2017 15:53:33 +0000 (10:53 -0500)
committerFletcher T. Penney <fletcher@fletcherpenney.net>
Wed, 8 Feb 2017 15:53:33 +0000 (10:53 -0500)
src/html.c
src/writer.c
src/writer.h
tests/MMD6Tests/Figure Images.html [new file with mode: 0644]
tests/MMD6Tests/Figure Images.htmlc [new file with mode: 0644]
tests/MMD6Tests/Figure Images.text [new file with mode: 0644]
tests/MMD6Tests/Link Attributes.html
tests/MMD6Tests/Reference Images.html

index 698f89bebb14395f0e0242558453fd3a32a11559..8acf7cf28cc5399cbfbbc5e8e7137c46cd961fd6 100644 (file)
@@ -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 <p> markers
+               d_string_erase(out, out->currentStringLength - 3, 3);
+               print("<figure>\n");
+               scratch->close_para = false;
+       }
+
        if (link->url)
                printf("<img src=\"%s\"", link->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<figcaption>");
+                       mmd_export_token_tree_html(out, source, text->child, offset, scratch);
+                       print("</figcaption>");
+               }
+               print("\n</figure>");
+       }
 }
 
 
@@ -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("</p>");
+                       if (scratch->close_para) {
+                               if (!scratch->list_is_tight)
+                                       print("</p>");
+                       } else {
+                               scratch->close_para = true;
+                       }
 
                        print("</li>");
                        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("</p>");
+                       if (scratch->close_para) {
+                               if (!scratch->list_is_tight)
+                                       print("</p>");
+                       } 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) {
index 9810ec755a74aa86efc0e60af4ba882b8633e1d2..197b8bfda22714c172b3e1ee834f32aa726e5713 100644 (file)
@@ -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);
index 7f2260d6cb7af3f5cd1c8098c98dc62c11da2fc3..2e62fa63f7bbb4ffdaf6687dbceee9cd4a226a7e 100644 (file)
@@ -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 (file)
index 0000000..d7bd33a
--- /dev/null
@@ -0,0 +1,16 @@
+<figure>
+<img src="http://foo.bar/" alt="foo" id="foo" title="foo" width="40px" />
+<figcaption>foo</figcaption>
+</figure>
+
+<figure>
+<img src="http://foo.bar/" alt="bar" id="foo" title="foo" width="40px" />
+<figcaption><em>bar</em></figcaption>
+</figure>
+
+<p><img src="http://foo.bar/" alt="bar" id="foo" title="foo" width="40px" /> bar</p>
+
+<figure>
+<img src="http://foo.bar/" alt="foo" title="foo" width="40px" />
+<figcaption><em>foo</em></figcaption>
+</figure>
diff --git a/tests/MMD6Tests/Figure Images.htmlc b/tests/MMD6Tests/Figure Images.htmlc
new file mode 100644 (file)
index 0000000..6190c81
--- /dev/null
@@ -0,0 +1,9 @@
+<p>![foo]</p>
+
+<p>![<em>bar</em>][foo]</p>
+
+<p>![<em>bar</em>][foo] bar</p>
+
+<p>![<em>foo</em>](http://foo.bar/ &quot;foo&quot; width=&quot;40px&quot;)</p>
+
+<p>[foo]: http://foo.bar/      &quot;foo&quot; width=&quot;40px&quot;</p>
\ No newline at end of file
diff --git a/tests/MMD6Tests/Figure Images.text b/tests/MMD6Tests/Figure Images.text
new file mode 100644 (file)
index 0000000..b4437f2
--- /dev/null
@@ -0,0 +1,9 @@
+![foo]
+
+![*bar*][foo]
+
+![*bar*][foo] bar
+
+![*foo*](http://foo.bar/ "foo" width="40px")
+
+[foo]: http://foo.bar/ "foo" width="40px"
index 8afed8df3e3ac643ac29be4c1ffcdbab5ab80f54..fd7087c156500e26e7f5a8db18273a03bd9361cb 100644 (file)
@@ -1,4 +1,4 @@
-<p>foo <img src="http://foo.bar/" alt="image" title="title" width="40px" height="400px" /></p>
+<p>foo <img src="http://foo.bar/" alt="image" id="image" title="title" width="40px" height="400px" /></p>
 
 <p>foo <a href="http://foo.bar/1" class="external" style="border: solid black 1px;">link</a></p>
 
@@ -6,6 +6,9 @@
 
 <p>foo <a href="http://foo.bar/3" class="external" style="border: solid black 1px;">link3</a></p>
 
-<p><img src="http://foo.bar/" alt="test" title="title" width="40px" height="400px" /></p>
+<figure>
+<img src="http://foo.bar/" alt="test" title="title" width="40px" height="400px" />
+<figcaption>test</figcaption>
+</figure>
 
 <p>5</p>
index c277d09f5c8f422cc323fad74e563749946fbe89..2b856df785cf7081f06dd22ab0eb08cf37bc6ccc 100644 (file)
@@ -1,17 +1,17 @@
-<p>Test <img src="http://test.1/" alt="foo" />.</p>
+<p>Test <img src="http://test.1/" alt="foo" id="bar" />.</p>
 
-<p>Test <img src="http://test.1/" alt="foo" />.</p>
+<p>Test <img src="http://test.1/" alt="foo" id="bar" />.</p>
 
-<p>Test <img src="http://test.3/" alt="foo" title="title" />.</p>
+<p>Test <img src="http://test.3/" alt="foo" id="foobar" title="title" />.</p>
 
-<p>Test <img src="http://test.4/" alt="foo" />.</p>
+<p>Test <img src="http://test.4/" alt="foo" id="foobar" />.</p>
 
-<p>Test <img src="http://test.4/" alt="foo" />.</p>
+<p>Test <img src="http://test.4/" alt="foo" id="foobar" />.</p>
 
 <p>5</p>
 
-<p>Test <img src="http://test.6/" alt="foo" title="title" />.</p>
+<p>Test <img src="http://test.6/" alt="foo" id="long" title="title" />.</p>
 
-<p>Test <img src="http://test.0/" alt="foo" />.</p>
+<p>Test <img src="http://test.0/" alt="foo" id="foo" />.</p>
 
-<p>Test <img src="http://test.0/" alt="foo" />.</p>
+<p>Test <img src="http://test.0/" alt="foo" id="foo" />.</p>