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.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
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* ©](img.png 'title')
+.
+<p><img src="img.png" alt="alt text with entity ©" title="title"></p>
+````````````````````````````````