Merge pull request #187 from jasharpe/saner_image_parsing

Make image parsing a little more sane, by having an explicit char_image function (that delegates most work to char_link).
This commit is contained in:
Xavier Mendez 2015-12-07 21:04:43 +01:00
commit 980b9c549b
4 changed files with 42 additions and 4 deletions

View file

@ -77,6 +77,7 @@ static size_t char_autolink_url(hoedown_buffer *ob, hoedown_document *doc, uint8
static size_t char_autolink_email(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size);
static size_t char_autolink_www(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size);
static size_t char_link(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size);
static size_t char_image(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size);
static size_t char_superscript(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size);
static size_t char_math(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size);
@ -86,6 +87,7 @@ enum markdown_char_t {
MD_CHAR_CODESPAN,
MD_CHAR_LINEBREAK,
MD_CHAR_LINK,
MD_CHAR_IMAGE,
MD_CHAR_LANGLE,
MD_CHAR_ESCAPE,
MD_CHAR_ENTITY,
@ -103,6 +105,7 @@ static char_trigger markdown_char_ptrs[] = {
&char_codespan,
&char_linebreak,
&char_link,
&char_image,
&char_langle_tag,
&char_escape,
&char_entity,
@ -1095,6 +1098,17 @@ char_autolink_url(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size
return link_len;
}
static size_t
char_image(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size) {
size_t ret;
if (size < 2 || data[1] != '[') return 0;
ret = char_link(ob, doc, data + 1, offset + 1, size - 1);
if (!ret) return 0;
return ret + 1;
}
/* char_link • '[': parsing a link, a footnote or an image */
static size_t
char_link(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size)
@ -1307,9 +1321,6 @@ char_link(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offse
/* calling the relevant rendering function */
if (is_img) {
if (ob->size && ob->data[ob->size - 1] == '!')
ob->size -= 1;
ret = doc->md.image(ob, u_link, title, content, &doc->data);
} else {
ret = doc->md.link(ob, content, u_link, title, &doc->data);
@ -2812,8 +2823,10 @@ hoedown_document_new(
if (doc->md.linebreak)
doc->active_char['\n'] = MD_CHAR_LINEBREAK;
if (doc->md.image || doc->md.link || doc->md.footnotes || doc->md.footnote_ref)
if (doc->md.image || doc->md.link || doc->md.footnotes || doc->md.footnote_ref) {
doc->active_char['['] = MD_CHAR_LINK;
doc->active_char['!'] = MD_CHAR_IMAGE;
}
doc->active_char['<'] = MD_CHAR_LANGLE;
doc->active_char['\\'] = MD_CHAR_ESCAPE;

9
test/Tests/Images.html Normal file
View file

@ -0,0 +1,9 @@
<p>This is an <img src="/url" alt="image">.</p>
<p>This is a tricky !<img src="/url" alt="image">.</p>
<p>This is another tricky case: !<a href="/url">not image</a>.</p>
<p>This is a reference <img src="/url2" alt="image">.</p>
<p>Terminating !</p>

11
test/Tests/Images.text Normal file
View file

@ -0,0 +1,11 @@
This is an ![image](/url).
This is a tricky !![image](/url).
This is another tricky case: \![not image](/url).
This is a reference ![image][ref].
[ref]: /url2
Terminating !

View file

@ -115,6 +115,11 @@
"input": "Tests/Table.text",
"output": "Tests/Table.html",
"flags": ["--tables"]
},
{
"input": "Tests/Images.text",
"output": "Tests/Images.html",
"flags": []
}
]
}