Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clumsy attempt to add subscript support #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions src/document.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
};
Expand All @@ -113,6 +115,7 @@ static char_trigger markdown_char_ptrs[] = {
&char_autolink_email,
&char_autolink_www,
&char_superscript,
&char_subscript,
&char_quote,
&char_math
};
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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;
Expand Down
1 change: 1 addition & 0 deletions src/document.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 12 additions & 0 deletions src/html.c
Original file line number Diff line number Diff line change
Expand Up @@ -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, "<sub>");
hoedown_buffer_put(ob, content->data, content->size);
HOEDOWN_BUFPUTSL(ob, "</sub>");
return 1;
}

static void
rndr_normal_text(hoedown_buffer *ob, const hoedown_buffer *content, const hoedown_renderer_data *data)
{
Expand Down Expand Up @@ -651,6 +661,7 @@ hoedown_html_toc_renderer_new(int nesting_level)
rndr_triple_emphasis,
rndr_strikethrough,
rndr_superscript,
rndr_subscript,
NULL,
NULL,
NULL,
Expand Down Expand Up @@ -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,
Expand Down