diff --git a/src/document.c b/src/document.c index a9cb42c8..69da5bcc 100644 --- a/src/document.c +++ b/src/document.c @@ -79,6 +79,7 @@ static size_t char_autolink_www(hoedown_buffer *ob, hoedown_document *doc, uint8 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_subscript(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); enum markdown_char_t { @@ -95,6 +96,7 @@ enum markdown_char_t { MD_CHAR_AUTOLINK_EMAIL, MD_CHAR_AUTOLINK_WWW, MD_CHAR_SUPERSCRIPT, + MD_CHAR_SUBSCRIPT, MD_CHAR_QUOTE, MD_CHAR_MATH }; @@ -113,6 +115,7 @@ static char_trigger markdown_char_ptrs[] = { &char_autolink_email, &char_autolink_www, &char_superscript, + &char_subscript, &char_quote, &char_math }; @@ -1368,6 +1371,42 @@ char_superscript(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_ return (sup_start == 2) ? sup_len + 1 : sup_len; } +static size_t +char_subscript(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size) +{ + size_t sup_start, sup_len; + hoedown_buffer *sup; + + if (!doc->md.subscript) + return 0; + + if (size < 2) + return 0; + + if (data[1] == '(') { + sup_start = 2; + sup_len = find_emph_char(data + 2, size - 2, ')') + 2; + + if (sup_len == size) + return 0; + } else { + sup_start = sup_len = 1; + + while (sup_len < size && !_isspace(data[sup_len])) + sup_len++; + } + + if (sup_len - sup_start == 0) + return (sup_start == 2) ? 3 : 0; + + sup = newbuf(doc, BUFFER_SPAN); + parse_inline(sup, doc, data + sup_start, sup_len - sup_start); + doc->md.subscript(ob, sup, &doc->data); + popbuf(doc, BUFFER_SPAN); + + return (sup_start == 2) ? sup_len + 1 : sup_len; +} + static size_t char_math(hoedown_buffer *ob, hoedown_document *doc, uint8_t *data, size_t offset, size_t size) { @@ -2840,6 +2879,7 @@ hoedown_document_new( if (extensions & HOEDOWN_EXT_SUPERSCRIPT) doc->active_char['^'] = MD_CHAR_SUPERSCRIPT; + doc->active_char['~'] = MD_CHAR_SUBSCRIPT; if (extensions & HOEDOWN_EXT_QUOTE) doc->active_char['"'] = MD_CHAR_QUOTE; diff --git a/src/document.h b/src/document.h index a8178fec..a2e0570e 100644 --- a/src/document.h +++ b/src/document.h @@ -129,6 +129,7 @@ struct hoedown_renderer { int (*triple_emphasis)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data); int (*strikethrough)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data); int (*superscript)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data); + int (*subscript)(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data); int (*footnote_ref)(hoedown_buffer *ob, unsigned int num, const hoedown_renderer_data *data); int (*math)(hoedown_buffer *ob, const hoedown_buffer *text, int displaymode, const hoedown_renderer_data *data); int (*raw_html)(hoedown_buffer *ob, const hoedown_buffer *text, const hoedown_renderer_data *data); diff --git a/src/html.c b/src/html.c index b5101c1a..d3615b54 100644 --- a/src/html.c +++ b/src/html.c @@ -488,6 +488,16 @@ rndr_superscript(hoedown_buffer *ob, const hoedown_buffer *content, const hoedow return 1; } +static int +rndr_subscript(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data) +{ + if (!content || !content->size) return 0; + HOEDOWN_BUFPUTSL(ob, ""); + hoedown_buffer_put(ob, content->data, content->size); + HOEDOWN_BUFPUTSL(ob, ""); + return 1; +} + static void rndr_normal_text(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data) { @@ -651,6 +661,7 @@ hoedown_html_toc_renderer_new(int nesting_level) rndr_triple_emphasis, rndr_strikethrough, rndr_superscript, + rndr_subscript, NULL, NULL, NULL, @@ -714,6 +725,7 @@ hoedown_html_renderer_new(hoedown_html_flags render_flags, int nesting_level) rndr_triple_emphasis, rndr_strikethrough, rndr_superscript, + rndr_subscript, rndr_footnote_ref, rndr_math, rndr_raw_html,