Commit 09ae86095f902c672e408ffba710138612ad4f1d

Martin Mitas 2016-12-07T23:56:47

Handle images more like links. Remove MD_SPAN_IMG_DETAIL::alt. Instead, the contents of the image is propagated to the renderer via MD_RENDERER::text() callback. * This fixes handling of entities inside the image text (issue #4). * It simplifies parsing and, more importantly, it better distingusshes what is responsibility of parser or renderer respectively. * This allows more flexibility on renderers side. Renderer who do not * really support images can just output the image content as any other text. The cost is a renderer into HTML (if it wants to render image contents into the attribute ALT of the IMG tag), has to handle images with more care. Typically such renderer has to track whether it is inside an image, and if so, then render span enter/leave as an empty string.

diff --git a/md2html/md2html.c b/md2html/md2html.c
index b75840e..3869d0c 100644
--- a/md2html/md2html.c
+++ b/md2html/md2html.c
@@ -180,6 +180,8 @@ membuf_append_url_escaped(struct membuffer* buf, const char* data, MD_SIZE size)
  ***  HTML rendering helper functions  ***
  *****************************************/
 
+static int image_nesting_level = 0;
+
 static void
 open_ol_block(struct membuffer* out, const MD_BLOCK_OL_DETAIL* det)
 {
@@ -244,14 +246,21 @@ open_img_span(struct membuffer* out, const MD_SPAN_IMG_DETAIL* det)
     membuf_append_url_escaped(out, det->src, det->src_size);
 
     MEMBUF_APPEND_LITERAL(out, "\" alt=\"");
-    membuf_append_escaped(out, det->alt, det->alt_size);
 
+    image_nesting_level++;
+}
+
+static void
+close_img_span(struct membuffer* out, const MD_SPAN_IMG_DETAIL* det)
+{
     if(det->title != NULL) {
         MEMBUF_APPEND_LITERAL(out, "\" title=\"");
         membuf_append_escaped(out, det->title, det->title_size);
     }
 
     MEMBUF_APPEND_LITERAL(out, "\">");
+
+    image_nesting_level--;
 }
 
 static unsigned
@@ -407,6 +416,12 @@ enter_span_callback(MD_SPANTYPE type, void* detail, void* userdata)
 {
     struct membuffer* out = (struct membuffer*) userdata;
 
+    if(image_nesting_level > 0) {
+        /* We are inside an image, i.e. rendering the ALT attribute of
+         * <IMG> tag. */
+        return 0;
+    }
+
     switch(type) {
         case MD_SPAN_EM:        MEMBUF_APPEND_LITERAL(out, "<em>"); break;
         case MD_SPAN_STRONG:    MEMBUF_APPEND_LITERAL(out, "<strong>"); break;
@@ -423,11 +438,19 @@ leave_span_callback(MD_SPANTYPE type, void* detail, void* userdata)
 {
     struct membuffer* out = (struct membuffer*) userdata;
 
+    if(image_nesting_level > 0) {
+        /* We are inside an image, i.e. rendering the ALT attribute of
+         * <IMG> tag. */
+        if(image_nesting_level == 1  &&  type == MD_SPAN_IMG)
+            close_img_span(out, (MD_SPAN_IMG_DETAIL*) detail);
+        return 0;
+    }
+
     switch(type) {
         case MD_SPAN_EM:        MEMBUF_APPEND_LITERAL(out, "</em>"); break;
         case MD_SPAN_STRONG:    MEMBUF_APPEND_LITERAL(out, "</strong>"); break;
         case MD_SPAN_A:         MEMBUF_APPEND_LITERAL(out, "</a>"); break;
-        case MD_SPAN_IMG:       /* noop */ break;
+        case MD_SPAN_IMG:       /*noop, handled above*/ break;
         case MD_SPAN_CODE:      MEMBUF_APPEND_LITERAL(out, "</code>"); break;
     }
 
@@ -441,8 +464,8 @@ text_callback(MD_TEXTTYPE type, const MD_CHAR* text, MD_SIZE size, void* userdat
 
     switch(type) {
         case MD_TEXT_NULLCHAR:  render_utf8_codepoint(out, 0x0000); break;
-        case MD_TEXT_BR:        MEMBUF_APPEND_LITERAL(out, "<br>\n"); break;
-        case MD_TEXT_SOFTBR:    MEMBUF_APPEND_LITERAL(out, "\n"); break;
+        case MD_TEXT_BR:        MEMBUF_APPEND_LITERAL(out, (image_nesting_level == 0 ? "<br>\n" : " ")); break;
+        case MD_TEXT_SOFTBR:    MEMBUF_APPEND_LITERAL(out, (image_nesting_level == 0 ? "\n" : " ")); break;
         case MD_TEXT_HTML:      membuf_append(out, text, size); break;
         case MD_TEXT_ENTITY:    render_entity(out, text, size); break;
         default:                membuf_append_escaped(out, text, size); break;
diff --git a/md4c/md4c.c b/md4c/md4c.c
index bdb4c58..aae2a63 100644
--- a/md4c/md4c.c
+++ b/md4c/md4c.c
@@ -2890,10 +2890,6 @@ md_resolve_links(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
             }
 
             md_analyze_link_contents(ctx, lines, n_lines, opener->end, closer->beg);
-
-            /* If image, eat everything by the opener. */
-            if(opener->ch == '!')
-                opener->end = closer->end;
         }
 
         opener_index = next_index;
@@ -3289,84 +3285,11 @@ abort:
     return ret;
 }
 
-static void
-md_build_img_alt(MD_CTX* ctx, MD_MARK* mark, const MD_LINE* lines, int n_lines,
-                 OFF beg, OFF end, CHAR* buffer, SZ* p_size)
-{
-    MD_MARK* inner_mark;
-    int line_index = 0;
-    OFF off = beg;
-    CHAR* ptr = buffer;
-
-    /* Revive the contents of any inner image so we include its ALT. */
-    for(inner_mark = mark; inner_mark < ctx->marks + mark->next; inner_mark++) {
-        if(inner_mark->ch == '!')
-            inner_mark->end = inner_mark->beg + 2;
-    }
-
-    while(lines[line_index].end <= beg)
-        line_index++;
-
-    while(!(mark->flags & MD_MARK_RESOLVED))
-        mark++;
-
-    while(line_index < n_lines) {
-        OFF line_end = lines[line_index].end;
-        if(line_end > end)
-            line_end = end;
-
-        while(off < line_end) {
-            OFF tmp = (line_end < mark->beg) ? line_end : mark->beg;
-            memcpy(ptr, STR(off), (tmp - off) * sizeof(CHAR));
-            ptr += (tmp - off);
-            off = tmp;
-
-            if(off >= mark->beg) {
-                off = mark->end;
-
-                mark++;
-                while(!(mark->flags & MD_MARK_RESOLVED) || mark->beg < off)
-                    mark++;
-            }
-        }
-
-        if(off >= end)
-            break;
-
-        line_index++;
-        off = lines[line_index].beg;
-        *ptr = _T(' ');
-        ptr++;
-   }
-
-   *p_size = ptr - buffer;
-}
-
-static int
-md_setup_span_img_detail(MD_CTX* ctx, MD_MARK* mark, const MD_LINE* lines, int n_lines,
-                         OFF alt_beg, OFF alt_end, MD_SPAN_IMG_DETAIL* det)
+static inline int
+md_setup_span_img_detail(MD_CTX* ctx, MD_MARK* mark, MD_SPAN_IMG_DETAIL* det)
 {
-    const MD_MARK* dest_mark = mark+1;
-    const MD_MARK* title_mark = mark+2;
-    int ret = 0;
-
-    MD_ASSERT(dest_mark->ch == 'D');
-    if(dest_mark->beg < dest_mark->end)
-        det->src = STR(dest_mark->beg);
-    else
-        det->src = NULL;
-    det->src_size = dest_mark->end - dest_mark->beg;
-
-    MD_TEMP_BUFFER((alt_end - alt_beg) * sizeof(CHAR));
-    det->alt = ctx->buffer;
-    md_build_img_alt(ctx, mark+3, lines, n_lines, alt_beg, alt_end, ctx->buffer, &det->alt_size);
-
-    MD_ASSERT(title_mark->ch == 'D');
-    det->title = md_mark_get_ptr(ctx, title_mark - ctx->marks);
-    det->title_size = title_mark->prev;
-
-abort:
-    return ret;
+    /* MD_SPAN_A_DETAIL and MD_SPAN_IMG_DETAIL are binary-compatible. */
+    return md_setup_span_a_detail(ctx, mark, (MD_SPAN_A_DETAIL*) det);
 }
 
 /* Render the output, accordingly to the analyzed ctx->marks. */
@@ -3375,7 +3298,6 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
 {
     union {
         MD_SPAN_A_DETAIL a;
-        MD_SPAN_IMG_DETAIL img;
     } det;
     MD_TEXTTYPE text_type;
     const MD_LINE* line = lines;
@@ -3449,21 +3371,16 @@ md_process_inlines(MD_CTX* ctx, const MD_LINE* lines, int n_lines)
                     }
                     break;
 
-                case '[':       /* Link . */
+                case '[':       /* Link, image. */
+                case '!':
+                    /* Note we here rely on fact that MD_SPAN_A_DETAIL and
+                     * MD_SPAN_IMG_DETAIL are binary-compatible. */
                     MD_CHECK(md_setup_span_a_detail(ctx, mark, &det.a));
-                    MD_ENTER_SPAN(MD_SPAN_A, &det.a);
+                    MD_ENTER_SPAN((mark->ch == '!' ? MD_SPAN_IMG : MD_SPAN_A), &det.a);
                     break;
                 case ']':
                     MD_CHECK(md_setup_span_a_detail(ctx, &ctx->marks[mark->prev], &det.a));
-                    MD_LEAVE_SPAN(MD_SPAN_A, &det.a);
-                    break;
-
-                case '!':       /* Image */
-                    MD_CHECK(md_setup_span_img_detail(ctx, mark, lines, n_lines,
-                                    mark->beg+2, ctx->marks[mark->next].beg, &det.img));
-                    MD_ENTER_SPAN(MD_SPAN_IMG, &det.img);
-                    MD_LEAVE_SPAN(MD_SPAN_IMG, &det.img);
-                    mark = &ctx->marks[mark->next];
+                    MD_LEAVE_SPAN((ctx->marks[mark->prev].ch == '!' ? MD_SPAN_IMG : MD_SPAN_A), &det.a);
                     break;
 
                 case '<':
diff --git a/md4c/md4c.h b/md4c/md4c.h
index efecc1f..ec5fbb2 100644
--- a/md4c/md4c.h
+++ b/md4c/md4c.h
@@ -114,7 +114,11 @@ enum MD_SPANTYPE_tag {
     MD_SPAN_A,
 
     /* <img src="xxx">...</a>
-     * Detail: Structure MD_SPAN_IMG_DETAIL. */
+     * Detail: Structure MD_SPAN_IMG_DETAIL.
+     * Note: Image text can contain nested spans and even nested images.
+     * If rendered into ALT attribute of HTML <IMG> tag, it's responsibility
+     * of the renderer to deal with it.
+     */
     MD_SPAN_IMG,
 
     /* <code>...</code> */
@@ -218,9 +222,6 @@ struct MD_SPAN_IMG_DETAIL_tag {
     const MD_CHAR* src;
     MD_SIZE src_size;
 
-    const MD_CHAR* alt;
-    MD_SIZE alt_size;
-
     const MD_CHAR* title;
     MD_SIZE title_size;
 };
diff --git a/test/coverage.txt b/test/coverage.txt
index b720b45..e575e98 100644
--- a/test/coverage.txt
+++ b/test/coverage.txt
@@ -39,3 +39,12 @@ att2=tok2> bar
 <p>foo <gi att1=tok1
 att2=tok2> bar</p>
 ````````````````````````````````
+
+
+### [Issue 4](https://github.com/mity/md4c/issues/4)
+
+```````````````````````````````` example
+![alt text with *entity* &copy;](img.png 'title')
+.
+<p><img src="img.png" alt="alt text with entity ©" title="title"></p>
+````````````````````````````````