diff options
author | John MacFarlane <jgm@berkeley.edu> | 2015-03-29 16:24:15 -0700 |
---|---|---|
committer | John MacFarlane <jgm@berkeley.edu> | 2015-03-29 16:24:15 -0700 |
commit | 464b21b11b17009b09379e7edd6dac9c6479db98 (patch) | |
tree | ed61d479afdc1afd9fa2e9f4f940410d05407316 | |
parent | 53a48ee09495c15ba27aea06fb6621211bde831f (diff) |
commonmark renderer - better inline code.
Use the minimum number of backticks as delimiter, and include
a space only if needed.
-rw-r--r-- | src/commonmark.c | 37 |
1 files changed, 32 insertions, 5 deletions
diff --git a/src/commonmark.c b/src/commonmark.c index fc508c6..6994ed1 100644 --- a/src/commonmark.c +++ b/src/commonmark.c @@ -215,10 +215,37 @@ longest_backtick_sequence(cmark_chunk *code) } static int +shortest_unused_backtick_sequence(cmark_chunk *code) +{ + int32_t used = 1; + int current = 0; + int i = 0; + while (i <= code->len) { + if (code->data[i] == '`') { + current++; + } else { + if (current) { + used |= (1 << current); + } + current = 0; + } + i++; + } + // return number of first bit that is 0: + i = 0; + while (used & 1) { + used = used >> 1; + i++; + } + return i; +} + +static int S_render_node(cmark_node *node, cmark_event_type ev_type, struct render_state *state) { cmark_node *tmp; + cmark_chunk *code; int list_number; cmark_delim_type list_delim; int numticks; @@ -324,8 +351,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, cmark_strbuf_truncate(state->prefix, state->prefix->size - 4); } else { - numticks = longest_backtick_sequence(&node->as.code.literal) - + 1; + numticks = longest_backtick_sequence(&node->as.code.literal) + 1; if (numticks < 3) { numticks = 3; } @@ -378,15 +404,16 @@ S_render_node(cmark_node *node, cmark_event_type ev_type, break; case CMARK_NODE_CODE: - numticks = longest_backtick_sequence(&node->as.literal) + 1; + code = &node->as.literal; + numticks = shortest_unused_backtick_sequence(code); for (i = 0; i < numticks; i++) { lit(state, "`", false); } - if (numticks > 1) { + if (code->data[0] == '`') { lit(state, " ", false); } out(state, node->as.literal, true, LITERAL); - if (numticks > 1) { + if (code->data[code->len - 1] == '`') { lit(state, " ", false); } for (i = 0; i < numticks; i++) { |