Skip to content

Commit

Permalink
updated to cmark 0.19.0
Browse files Browse the repository at this point in the history
  • Loading branch information
rhinoman committed Apr 30, 2015
1 parent 49686ff commit 0d388ea
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 49 deletions.
92 changes: 48 additions & 44 deletions commonmark.c
Original file line number Diff line number Diff line change
Expand Up @@ -49,35 +49,35 @@ typedef enum {

static inline bool
needs_escaping(escaping escape,
int32_t c,
unsigned char next_c,
struct render_state *state)
int32_t c,
unsigned char next_c,
struct render_state *state)
{
if (escape == NORMAL) {
return (c == '*' || c == '_' || c == '[' || c == ']' ||
c == '<' || c == '>' || c == '\\' || c == '`' ||
(c == '&' && isalpha(next_c)) ||
(c == '!' && next_c == '[') ||
(state->begin_line &&
(c == '-' || c == '+' || c == '#' || c == '=')) ||
(c == '#' && (isspace(next_c) || next_c == '\0')) ||
((c == '.' || c == ')') &&
isdigit(state->buffer->ptr[state->buffer->size - 1])));
c == '<' || c == '>' || c == '\\' || c == '`' ||
(c == '&' && isalpha(next_c)) ||
(c == '!' && next_c == '[') ||
(state->begin_line &&
(c == '-' || c == '+' || c == '#' || c == '=')) ||
(c == '#' && (isspace(next_c) || next_c == '\0')) ||
((c == '.' || c == ')') &&
isdigit(state->buffer->ptr[state->buffer->size - 1])));
} else if (escape == TITLE) {
return (c == '`' || c == '<' || c == '>' || c == '"' ||
c == '\\');
c == '\\');
} else if (escape == URL) {
return (c == '`' || c == '<' || c == '>' || isspace(c) ||
c == '\\' || c == ')' || c == '(');
c == '\\' || c == ')' || c == '(');
} else {
return false;
}
}

static inline void out(struct render_state *state,
cmark_chunk str,
bool wrap,
escaping escape)
cmark_chunk str,
bool wrap,
escaping escape)
{
unsigned char* source = str.data;
int length = str.len;
Expand All @@ -100,7 +100,7 @@ static inline void out(struct render_state *state,
cmark_strbuf_putc(state->buffer, '\n');
if (state->need_cr > 1) {
cmark_strbuf_put(state->buffer, state->prefix->ptr,
state->prefix->size);
state->prefix->size);
}
}
state->column = 0;
Expand All @@ -111,20 +111,23 @@ static inline void out(struct render_state *state,
while (i < length) {
if (state->begin_line) {
cmark_strbuf_put(state->buffer, state->prefix->ptr,
state->prefix->size);
state->prefix->size);
// note: this assumes prefix is ascii:
state->column = state->prefix->size;
}

len = utf8proc_iterate(source + i, length - i, &c);
if (len == -1) { // error condition
return; // return without rendering rest of string
}
nextc = source[i + len];
if (c == 32 && wrap) {
if (!state->begin_line) {
cmark_strbuf_putc(state->buffer, ' ');
state->column += 1;
state->begin_line = false;
state->last_breakable = state->buffer->size -
1;
1;
// skip following spaces
while (source[i + 1] == ' ') {
i++;
Expand Down Expand Up @@ -167,7 +170,7 @@ static inline void out(struct render_state *state,
// add newline, prefix, and remainder
cmark_strbuf_putc(state->buffer, '\n');
cmark_strbuf_put(state->buffer, state->prefix->ptr,
state->prefix->size);
state->prefix->size);
cmark_strbuf_put(state->buffer, remainder.data, remainder.len);
state->column = state->prefix->size + remainder.len;
cmark_chunk_free(&remainder);
Expand Down Expand Up @@ -254,8 +257,8 @@ is_autolink(cmark_node *node)
}
cmark_consolidate_text_nodes(node);
return (strncmp(url,
(char*)node->as.literal.data,
node->as.literal.len) == 0);
(char*)node->as.literal.data,
node->as.literal.len) == 0);
}

// if node is a block node, returns node.
Expand All @@ -265,7 +268,7 @@ get_containing_block(cmark_node *node)
{
while (node &&
(node->type < CMARK_NODE_FIRST_BLOCK ||
node->type > CMARK_NODE_LAST_BLOCK)) {
node->type > CMARK_NODE_LAST_BLOCK)) {
node = node->parent;
}
return node;
Expand Down Expand Up @@ -293,14 +296,14 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
// a following list.
if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL &&
entering)) {
tmp = get_containing_block(node);
state->in_tight_list_item =
(tmp->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent)) ||
(tmp &&
tmp->parent &&
tmp->parent->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent->parent));
tmp = get_containing_block(node);
state->in_tight_list_item =
(tmp->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent)) ||
(tmp &&
tmp->parent &&
tmp->parent->type == CMARK_NODE_ITEM &&
cmark_node_get_list_tight(tmp->parent->parent));
}

switch (node->type) {
Expand All @@ -316,7 +319,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
cmark_strbuf_puts(state->prefix, "> ");
} else {
cmark_strbuf_truncate(state->prefix,
state->prefix->size - 2);
state->prefix->size - 2);
blankline(state);
}
break;
Expand Down Expand Up @@ -348,10 +351,10 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
// we get nice transition from single digits
// to double
cmark_strbuf_printf(&listmarker,
"%d%s%s", list_number,
list_delim == CMARK_PAREN_DELIM ?
")" : ".",
list_number < 10 ? " " : " ");
"%d%s%s", list_number,
list_delim == CMARK_PAREN_DELIM ?
")" : ".",
list_number < 10 ? " " : " ");
marker_width = listmarker.size;
}
if (entering) {
Expand All @@ -361,14 +364,14 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
cmark_strbuf_puts(state->prefix, " ");
} else {
lit(state, (char *)listmarker.ptr, false);
for (i=marker_width; i--;) {
for (i = marker_width; i--;) {
cmark_strbuf_putc(state->prefix, ' ');
}
}
} else {
cmark_strbuf_truncate(state->prefix,
state->prefix->size -
marker_width);
state->prefix->size -
marker_width);
cr(state);
}
cmark_strbuf_free(&listmarker);
Expand Down Expand Up @@ -405,7 +408,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
cmark_strbuf_puts(state->prefix, " ");
out(state, node->as.code.literal, false, LITERAL);
cmark_strbuf_truncate(state->prefix,
state->prefix->size - 4);
state->prefix->size - 4);
} else {
numticks = longest_backtick_sequence(code) + 1;
if (numticks < 3) {
Expand Down Expand Up @@ -514,7 +517,7 @@ S_render_node(cmark_node *node, cmark_event_type ev_type,
if (entering) {
lit(state, "<", false);
if (strncmp(cmark_node_get_url(node),
"mailto:", 7) == 0) {
"mailto:", 7) == 0) {
lit(state,
(char *)cmark_node_get_url(node) + 7,
false);
Expand Down Expand Up @@ -579,9 +582,10 @@ char *cmark_render_commonmark(cmark_node *root, int options, int width)
if (CMARK_OPT_HARDBREAKS & options) {
width = 0;
}
struct render_state state =
{ options, &commonmark, &prefix, 0, width,
0, 0, true, false, false};
struct render_state state = {
options, &commonmark, &prefix, 0, width,
0, 0, true, false, false
};
cmark_node *cur;
cmark_event_type ev_type;
cmark_iter *iter = cmark_iter_new(root);
Expand Down
6 changes: 4 additions & 2 deletions inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,8 +301,10 @@ scan_delims(subject* subj, unsigned char c, bool * can_open, bool * can_close)
!utf8proc_is_space(after_char) &&
!utf8proc_is_punctuation(after_char));
if (c == '_') {
*can_open = left_flanking && !right_flanking;
*can_close = right_flanking && !left_flanking;
*can_open = left_flanking &&
(!right_flanking || utf8proc_is_punctuation(before_char));
*can_close = right_flanking &&
(!left_flanking || utf8proc_is_punctuation(after_char));
} else if (c == '\'' || c == '"') {
*can_open = left_flanking && !right_flanking;
*can_close = right_flanking;
Expand Down
3 changes: 3 additions & 0 deletions man.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ static void escape_man(cmark_strbuf *dest, const unsigned char *source, int leng

while (i < length) {
len = utf8proc_iterate(source + i, length - i, &c);
if (len == -1) { // error condition
return; // return without rendering anything
}
switch(c) {
case 46:
if (beginLine) {
Expand Down
5 changes: 2 additions & 3 deletions utf8.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,7 @@ int utf8proc_iterate(const uint8_t *str, int str_len, int32_t *dst)
case 3:
uc = ((str[0] & 0x0F) << 12) + ((str[1] & 0x3F) << 6)
+ (str[2] & 0x3F);
if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000) ||
(uc >= 0xFDD0 && uc < 0xFDF0)) uc = -1;
if (uc < 0x800 || (uc >= 0xD800 && uc < 0xE000)) uc = -1;
break;
case 4:
uc = ((str[0] & 0x07) << 18) + ((str[1] & 0x3F) << 12)
Expand All @@ -182,7 +181,7 @@ int utf8proc_iterate(const uint8_t *str, int str_len, int32_t *dst)
break;
}

if (uc < 0 || ((uc & 0xFFFF) >= 0xFFFE))
if (uc < 0)
return -1;

*dst = uc;
Expand Down

0 comments on commit 0d388ea

Please sign in to comment.