static void md_convert_hashes(cmark_iter *const iter) { for(;;) { cmark_event_type const event = cmark_iter_next(iter); if(CMARK_EVENT_DONE == event) break; if(CMARK_EVENT_EXIT != event) continue; cmark_node *const node = cmark_iter_get_node(iter); cmark_node_type const type = cmark_node_get_type(node); if(CMARK_NODE_LINK != type && CMARK_NODE_IMAGE != type) continue; char const *const URI = cmark_node_get_url(node); if(!URI) continue; if(0 != strncasecmp(URI, STR_LEN("hash:"))) continue; cmark_node *sup = superscript("#", HASH_INFO_MSG, URI); cmark_node_insert_after(node, sup); cmark_iter_reset(iter, sup, CMARK_EVENT_EXIT); char *escaped = QSEscape(URI, strlen(URI), true); size_t const elen = strlen(escaped); cmark_strbuf rel[1]; char const qpfx[] = "/sources/"; cmark_strbuf_init(&DEFAULT_MEM_ALLOCATOR, rel, sizeof(qpfx)-1+elen); cmark_strbuf_put(rel, (unsigned char const *)qpfx, sizeof(qpfx)-1); cmark_strbuf_put(rel, (unsigned char const *)escaped, elen); free(escaped); escaped = NULL; cmark_node_set_url(node, cmark_strbuf_cstr(rel)); cmark_strbuf_free(rel); } }
void cmark_consolidate_text_nodes(cmark_node *root) { cmark_iter *iter = cmark_iter_new(root); cmark_strbuf buf = GH_BUF_INIT; cmark_event_type ev_type; cmark_node *cur, *tmp, *next; while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) { cur = cmark_iter_get_node(iter); if (ev_type == CMARK_EVENT_ENTER && cur->type == CMARK_NODE_TEXT && cur->next && cur->next->type == CMARK_NODE_TEXT) { cmark_strbuf_clear(&buf); cmark_strbuf_put(&buf, cur->as.literal.data, cur->as.literal.len); tmp = cur->next; while (tmp && tmp->type == CMARK_NODE_TEXT) { cmark_iter_next(iter); // advance pointer cmark_strbuf_put(&buf, tmp->as.literal.data, tmp->as.literal.len); next = tmp->next; cmark_node_free(tmp); tmp = next; } cmark_chunk_free(&cur->as.literal); cur->as.literal = cmark_chunk_buf_detach(&buf); } } cmark_strbuf_free(&buf); cmark_iter_free(iter); }
static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len, bool eof) { const unsigned char *end = buffer + len; while (buffer < end) { const unsigned char *eol = (const unsigned char *)memchr(buffer, '\n', end - buffer); size_t line_len; if (eol) { line_len = eol + 1 - buffer; } else if (eof) { line_len = end - buffer; } else { cmark_strbuf_put(parser->linebuf, buffer, end - buffer); break; } if (parser->linebuf->size > 0) { cmark_strbuf_put(parser->linebuf, buffer, line_len); S_process_line(parser, parser->linebuf->ptr, parser->linebuf->size); cmark_strbuf_clear(parser->linebuf); } else { S_process_line(parser, buffer, line_len); } buffer += line_len; } }
int houdini_escape_href(cmark_strbuf *ob, const uint8_t *src, bufsize_t size) { static const uint8_t hex_chars[] = "0123456789ABCDEF"; bufsize_t i = 0, org; uint8_t hex_str[3]; hex_str[0] = '%'; while (i < size) { org = i; while (i < size && HREF_SAFE[src[i]] != 0) i++; if (likely(i > org)) cmark_strbuf_put(ob, src + org, i - org); /* escaping */ if (i >= size) break; switch (src[i]) { /* amp appears all the time in URLs, but needs * HTML-entity escaping to be inside an href */ case '&': cmark_strbuf_puts(ob, "&"); break; /* the single quote is a valid URL character * according to the standard; it needs HTML * entity escaping too */ case '\'': cmark_strbuf_puts(ob, "'"); break; /* the space can be escaped to %20 or a plus * sign. we're going with the generic escape * for now. the plus thing is more commonly seen * when building GET strings */ #if 0 case ' ': cmark_strbuf_putc(ob, '+'); break; #endif /* every other character goes with a %XX escaping */ default: hex_str[1] = hex_chars[(src[i] >> 4) & 0xF]; hex_str[2] = hex_chars[src[i] & 0xF]; cmark_strbuf_put(ob, hex_str, 3); } i++; } return 1; }
static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len, bool eof) { const unsigned char *end = buffer + len; static const uint8_t repl[] = {239, 191, 189}; while (buffer < end) { const unsigned char *eol; bufsize_t chunk_len; bool process = false; for (eol = buffer; eol < end; ++eol) { if (S_is_line_end_char(*eol)) { process = true; break; } if (*eol == '\0' && eol < end) { break; } } if (eol >= end && eof) { process = true; } chunk_len = cmark_strbuf_check_bufsize(eol - buffer); if (process) { if (parser->linebuf->size > 0) { cmark_strbuf_put(parser->linebuf, buffer, chunk_len); S_process_line(parser, parser->linebuf->ptr, parser->linebuf->size); cmark_strbuf_clear(parser->linebuf); } else { S_process_line(parser, buffer, chunk_len); } } else { if (eol < end && *eol == '\0') { // omit NULL byte cmark_strbuf_put(parser->linebuf, buffer, chunk_len); // add replacement character cmark_strbuf_put(parser->linebuf, repl, 3); chunk_len += 1; // so we advance the buffer past NULL } else { cmark_strbuf_put(parser->linebuf, buffer, chunk_len); } } buffer += chunk_len; // skip over line ending characters: if (buffer < end && *buffer == '\r') buffer++; if (buffer < end && *buffer == '\n') buffer++; } }
static cmark_node *try_opening_code_block(cmark_syntax_extension *self, bool indented, cmark_parser *parser, cmark_node *parent, const char *input) { cmark_node *ret = NULL; cmark_bufsize_t matched = scan_open_gtkdoc_code_block(input, cmark_parser_get_first_nonspace(parser)); if (!indented && matched) { ret = cmark_parser_add_child(parser, parent, CMARK_NODE_CODE_BLOCK, cmark_parser_get_offset(parser)); cmark_node_set_syntax_extension(ret, self); cmark_node_set_fenced(ret, true, 2, cmark_parser_get_first_nonspace(parser) - cmark_parser_get_offset(parser), 0); cmark_parser_advance_offset(parser, input, matched, false); matched = scan_language_comment(input, matched); if (matched) { cmark_strbuf *lang = cmark_strbuf_new(32); cmark_strbuf_put(lang, (unsigned char *) input + 17, matched - 20); /* Will be transformed to fence info */ cmark_node_set_string_content(ret, cmark_strbuf_get(lang)); cmark_strbuf_free(lang); cmark_parser_advance_offset(parser, input, matched, false); } } return ret; }
int houdini_escape_html0(cmark_strbuf *ob, const uint8_t *src, size_t size, int secure) { size_t i = 0, org, esc = 0; while (i < size) { org = i; while (i < size && (esc = HTML_ESCAPE_TABLE[src[i]]) == 0) i++; if (i > org) cmark_strbuf_put(ob, src + org, i - org); /* escaping */ if (unlikely(i >= size)) break; /* The forward slash is only escaped in secure mode */ if ((src[i] == '/' || src[i] == '\'') && !secure) { cmark_strbuf_putc(ob, src[i]); } else { cmark_strbuf_puts(ob, HTML_ESCAPES[esc]); } i++; } return 1; }
static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len, bool eof) { const unsigned char *end = buffer + len; while (buffer < end) { const unsigned char *eol; size_t line_len; for (eol = buffer; eol < end; ++eol) { if (*eol == '\r' || *eol == '\n') break; } if (eol >= end) eol = NULL; if (eol) { if (eol < end && *eol == '\r') eol++; if (eol < end && *eol == '\n') eol++; line_len = eol - buffer; } else if (eof) { line_len = end - buffer; } else { cmark_strbuf_put(parser->linebuf, buffer, end - buffer); break; } if (parser->linebuf->size > 0) { cmark_strbuf_put(parser->linebuf, buffer, line_len); S_process_line(parser, parser->linebuf->ptr, parser->linebuf->size); cmark_strbuf_clear(parser->linebuf); } else { S_process_line(parser, buffer, line_len); } buffer += line_len; } }
static cmark_node *www_match(cmark_parser *parser, cmark_node *parent, cmark_inline_parser *inline_parser) { cmark_chunk *chunk = cmark_inline_parser_get_chunk(inline_parser); size_t max_rewind = cmark_inline_parser_get_offset(inline_parser); uint8_t *data = chunk->data + max_rewind; size_t size = chunk->len - max_rewind; int start = cmark_inline_parser_get_column(inline_parser); size_t link_end; if (max_rewind > 0 && strchr("*_~(", data[-1]) == NULL && !cmark_isspace(data[-1])) return 0; if (size < 4 || memcmp(data, "www.", strlen("www.")) != 0) return 0; link_end = check_domain(data, size, 0); if (link_end == 0) return NULL; while (link_end < size && !cmark_isspace(data[link_end])) link_end++; link_end = autolink_delim(data, link_end); if (link_end == 0) return NULL; cmark_inline_parser_set_offset(inline_parser, (int)(max_rewind + link_end)); cmark_node *node = cmark_node_new_with_mem(CMARK_NODE_LINK, parser->mem); cmark_strbuf buf; cmark_strbuf_init(parser->mem, &buf, 10); cmark_strbuf_puts(&buf, "http://"); cmark_strbuf_put(&buf, data, (bufsize_t)link_end); node->as.link.url = cmark_chunk_buf_detach(&buf); cmark_node *text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem); text->as.literal = cmark_chunk_dup(chunk, (bufsize_t)max_rewind, (bufsize_t)link_end); cmark_node_append_child(node, text); node->start_line = text->start_line = node->end_line = text->end_line = cmark_inline_parser_get_line(inline_parser); node->start_column = text->start_column = start - 1; node->end_column = text->end_column = cmark_inline_parser_get_column(inline_parser) - 1; return node; }
void utf8proc_detab(cmark_strbuf *ob, const uint8_t *line, size_t size) { static const uint8_t whitespace[] = " "; size_t i = 0, tab = 0; while (i < size) { size_t org = i; while (i < size && line[i] != '\t' && line[i] != '\0' && line[i] < 0x80) { i++; tab++; } if (i > org) cmark_strbuf_put(ob, line + org, i - org); if (i >= size) break; if (line[i] == '\t') { int numspaces = 4 - (tab % 4); cmark_strbuf_put(ob, whitespace, numspaces); i += 1; tab += numspaces; } else { int charlen = utf8proc_valid(line + i, size - i); if (charlen >= 0) { cmark_strbuf_put(ob, line + i, charlen); } else { encode_unknown(ob); charlen = -charlen; } i += charlen; tab += 1; } } }
static void filter_html_block(cmark_html_renderer *renderer, uint8_t *data, size_t len) { cmark_strbuf *html = renderer->html; cmark_llist *it; cmark_syntax_extension *ext; bool filtered; uint8_t *match; while (len) { match = (uint8_t *) memchr(data, '<', len); if (!match) break; if (match != data) { cmark_strbuf_put(html, data, (bufsize_t)(match - data)); len -= (match - data); data = match; } filtered = false; for (it = renderer->filter_extensions; it; it = it->next) { ext = ((cmark_syntax_extension *) it->data); if (!ext->html_filter_func(ext, data, len)) { filtered = true; break; } } if (!filtered) { cmark_strbuf_putc(html, '<'); } else { cmark_strbuf_puts(html, "<"); } ++data; --len; } if (len) cmark_strbuf_put(html, data, (bufsize_t)len); }
int houdini_unescape_html(cmark_strbuf *ob, const uint8_t *src, size_t size) { size_t i = 0, org, ent; while (i < size) { org = i; while (i < size && src[i] != '&') i++; if (likely(i > org)) { if (unlikely(org == 0)) { if (i >= size) return 0; cmark_strbuf_grow(ob, HOUDINI_UNESCAPED_SIZE(size)); } cmark_strbuf_put(ob, src + org, i - org); } /* escaping */ if (i >= size) break; i++; ent = houdini_unescape_ent(ob, src + i, size - i); i += ent; /* not really an entity */ if (ent == 0) cmark_strbuf_putc(ob, '&'); } return 1; }
static void add_line(cmark_node* node, cmark_chunk *ch, int offset) { assert(node->open); cmark_strbuf_put(&node->string_content, ch->data + offset, ch->len - offset); }
static void encode_unknown(cmark_strbuf *buf) { static const uint8_t repl[] = {239, 191, 189}; cmark_strbuf_put(buf, repl, 3); }
size_t houdini_unescape_ent(cmark_strbuf *ob, const uint8_t *src, size_t size) { size_t i = 0; if (size >= 3 && src[0] == '#') { int codepoint = 0; int num_digits = 0; if (_isdigit(src[1])) { for (i = 1; i < size && _isdigit(src[i]); ++i) { codepoint = (codepoint * 10) + (src[i] - '0'); if (codepoint >= 0x110000) { // Keep counting digits but // avoid integer overflow. codepoint = 0x110000; } } num_digits = i - 1; } else if (src[1] == 'x' || src[1] == 'X') { for (i = 2; i < size && _isxdigit(src[i]); ++i) { codepoint = (codepoint * 16) + ((src[i] | 32) % 39 - 9); if (codepoint >= 0x110000) { // Keep counting digits but // avoid integer overflow. codepoint = 0x110000; } } num_digits = i - 2; } if (num_digits >= 1 && num_digits <= 8 && i < size && src[i] == ';') { if (codepoint == 0 || (codepoint >= 0xD800 && codepoint < 0xE000) || codepoint >= 0x110000) { codepoint = 0xFFFD; } utf8proc_encode_char(codepoint, ob); return i + 1; } } else { if (size > MAX_WORD_LENGTH) size = MAX_WORD_LENGTH; for (i = MIN_WORD_LENGTH; i < size; ++i) { if (src[i] == ' ') break; if (src[i] == ';') { const struct html_ent *entity = find_entity((char *)src, i); if (entity != NULL) { int len = 0; while (len < 4 && entity->utf8[len] != '\0') { ++len; } cmark_strbuf_put(ob, entity->utf8, len); return i + 1; } break; } } } return 0; }
void houdini_unescape_html_f(cmark_strbuf *ob, const uint8_t *src, size_t size) { if (!houdini_unescape_html(ob, src, size)) cmark_strbuf_put(ob, src, size); }
static int S_render_node(cmark_node *node, cmark_event_type ev_type, struct render_state *state, int options) { cmark_node *parent; cmark_node *grandparent; cmark_strbuf *html = state->html; char start_header[] = "<h0"; char end_header[] = "</h0"; bool tight; bool entering = (ev_type == CMARK_EVENT_ENTER); if (state->plain == node) { // back at original node state->plain = NULL; } if (state->plain != NULL) { switch(node->type) { case CMARK_NODE_TEXT: case CMARK_NODE_CODE: case CMARK_NODE_INLINE_HTML: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: case CMARK_NODE_SOFTBREAK: cmark_strbuf_putc(html, ' '); break; default: break; } return 1; } switch (node->type) { case CMARK_NODE_DOCUMENT: break; case CMARK_NODE_BLOCK_QUOTE: if (entering) { cr(html); cmark_strbuf_puts(html, "<blockquote"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { cr(html); cmark_strbuf_puts(html, "</blockquote>\n"); } break; case CMARK_NODE_LIST: { cmark_list_type list_type = node->as.list.list_type; int start = node->as.list.start; if (entering) { cr(html); if (list_type == CMARK_BULLET_LIST) { cmark_strbuf_puts(html, "<ul"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else if (start == 1) { cmark_strbuf_puts(html, "<ol"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { cmark_strbuf_printf(html, "<ol start=\"%d\"", start); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } } else { cmark_strbuf_puts(html, list_type == CMARK_BULLET_LIST ? "</ul>\n" : "</ol>\n"); } break; } case CMARK_NODE_ITEM: if (entering) { cr(html); cmark_strbuf_puts(html, "<li"); S_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { cmark_strbuf_puts(html, "</li>\n"); } break; case CMARK_NODE_HEADER: if (entering) { cr(html); start_header[2] = '0' + node->as.header.level; cmark_strbuf_puts(html, start_header); S_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { end_header[3] = '0' + node->as.header.level; cmark_strbuf_puts(html, end_header); cmark_strbuf_puts(html, ">\n"); } break; case CMARK_NODE_CODE_BLOCK: cr(html); if (!node->as.code.fenced || node->as.code.info.len == 0) { cmark_strbuf_puts(html, "<pre"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code>"); } else { int first_tag = 0; while (first_tag < node->as.code.info.len && node->as.code.info.data[first_tag] != ' ') { first_tag += 1; } cmark_strbuf_puts(html, "<pre"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code class=\"language-"); escape_html(html, node->as.code.info.data, first_tag); cmark_strbuf_puts(html, "\">"); } escape_html(html, node->as.code.literal.data, node->as.code.literal.len); cmark_strbuf_puts(html, "</code></pre>\n"); break; case CMARK_NODE_HTML: cr(html); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_HRULE: cr(html); cmark_strbuf_puts(html, "<hr"); S_render_sourcepos(node, html, options); cmark_strbuf_puts(html, " />\n"); break; case CMARK_NODE_PARAGRAPH: parent = cmark_node_parent(node); grandparent = cmark_node_parent(parent); if (grandparent != NULL && grandparent->type == CMARK_NODE_LIST) { tight = grandparent->as.list.tight; } else { tight = false; } if (!tight) { if (entering) { cr(html); cmark_strbuf_puts(html, "<p"); S_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { cmark_strbuf_puts(html, "</p>\n"); } } break; case CMARK_NODE_TEXT: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: cmark_strbuf_puts(html, "<br />\n"); break; case CMARK_NODE_SOFTBREAK: if (options & CMARK_OPT_HARDBREAKS) { cmark_strbuf_puts(html, "<br />\n"); } else { cmark_strbuf_putc(html, '\n'); } break; case CMARK_NODE_CODE: cmark_strbuf_puts(html, "<code>"); escape_html(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "</code>"); break; case CMARK_NODE_INLINE_HTML: cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_STRONG: if (entering) { cmark_strbuf_puts(html, "<strong>"); } else { cmark_strbuf_puts(html, "</strong>"); } break; case CMARK_NODE_EMPH: if (entering) { cmark_strbuf_puts(html, "<em>"); } else { cmark_strbuf_puts(html, "</em>"); } break; case CMARK_NODE_LINK: if (entering) { cmark_strbuf_puts(html, "<a href=\""); if (node->as.link.url) escape_href(html, node->as.link.url, -1); if (node->as.link.title) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title, -1); } cmark_strbuf_puts(html, "\">"); } else { cmark_strbuf_puts(html, "</a>"); } break; case CMARK_NODE_IMAGE: if (entering) { cmark_strbuf_puts(html, "<img src=\""); if (node->as.link.url) escape_href(html, node->as.link.url, -1); cmark_strbuf_puts(html, "\" alt=\""); state->plain = node; } else { if (node->as.link.title) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title, -1); } cmark_strbuf_puts(html, "\" />"); } break; default: assert(false); break; } // cmark_strbuf_putc(html, 'x'); return 1; }
static void postprocess_text(cmark_parser *parser, cmark_node *text, int offset) { size_t link_end; uint8_t *data = text->as.literal.data, *at; size_t size = text->as.literal.len; int rewind, max_rewind, nb = 0, np = 0, ns = 0; if (offset < 0 || (size_t)offset >= size) return; data += offset; size -= offset; at = (uint8_t *)memchr(data, '@', size); if (!at) return; max_rewind = (int)(at - data); data += max_rewind; size -= max_rewind; for (rewind = 0; rewind < max_rewind; ++rewind) { uint8_t c = data[-rewind - 1]; if (cmark_isalnum(c)) continue; if (strchr(".+-_", c) != NULL) continue; if (c == '/') ns++; break; } if (rewind == 0 || ns > 0) { postprocess_text(parser, text, max_rewind + 1 + offset); return; } for (link_end = 0; link_end < size; ++link_end) { uint8_t c = data[link_end]; if (cmark_isalnum(c)) continue; if (c == '@') nb++; else if (c == '.' && link_end < size - 1 && cmark_isalnum(data[link_end + 1])) np++; else if (c != '-' && c != '_') break; } if (link_end < 2 || nb != 1 || np == 0 || (!cmark_isalpha(data[link_end - 1]) && data[link_end - 1] != '.')) { postprocess_text(parser, text, max_rewind + 1 + offset); return; } link_end = autolink_delim(data, link_end); if (link_end == 0) { postprocess_text(parser, text, max_rewind + 1 + offset); return; } cmark_chunk_to_cstr(parser->mem, &text->as.literal); cmark_node *link_node = cmark_node_new_with_mem(CMARK_NODE_LINK, parser->mem); cmark_strbuf buf; cmark_strbuf_init(parser->mem, &buf, 10); cmark_strbuf_puts(&buf, "mailto:"); cmark_strbuf_put(&buf, data - rewind, (bufsize_t)(link_end + rewind)); link_node->as.link.url = cmark_chunk_buf_detach(&buf); cmark_node *link_text = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem); cmark_chunk email = cmark_chunk_dup( &text->as.literal, offset + max_rewind - rewind, (bufsize_t)(link_end + rewind)); cmark_chunk_to_cstr(parser->mem, &email); link_text->as.literal = email; cmark_node_append_child(link_node, link_text); cmark_node_insert_after(text, link_node); cmark_node *post = cmark_node_new_with_mem(CMARK_NODE_TEXT, parser->mem); post->as.literal = cmark_chunk_dup(&text->as.literal, (bufsize_t)(offset + max_rewind + link_end), (bufsize_t)(size - link_end)); cmark_chunk_to_cstr(parser->mem, &post->as.literal); cmark_node_insert_after(link_node, post); text->as.literal.len = offset + max_rewind - rewind; text->as.literal.data[text->as.literal.len] = 0; postprocess_text(parser, post, 0); }
static void S_process_line(cmark_parser *parser, const unsigned char *buffer, bufsize_t bytes) { cmark_node *last_matched_container; bufsize_t matched = 0; int lev = 0; int i; cmark_list *data = NULL; bool all_matched = true; cmark_node *container; bool indented; cmark_chunk input; bool maybe_lazy; if (parser->options & CMARK_OPT_VALIDATE_UTF8) { cmark_utf8proc_check(parser->curline, buffer, bytes); } else { cmark_strbuf_put(parser->curline, buffer, bytes); } // ensure line ends with a newline: if (bytes == 0 || !S_is_line_end_char(parser->curline->ptr[bytes - 1])) { cmark_strbuf_putc(parser->curline, '\n'); } parser->offset = 0; parser->column = 0; parser->blank = false; input.data = parser->curline->ptr; input.len = parser->curline->size; // container starts at the document root. container = parser->root; parser->line_number++; // for each containing node, try to parse the associated line start. // bail out on failure: container will point to the last matching node. while (container->last_child && container->last_child->open) { container = container->last_child; S_find_first_nonspace(parser, &input); if (container->type == CMARK_NODE_BLOCK_QUOTE) { matched = parser->indent <= 3 && peek_at(&input, parser->first_nonspace) == '>'; if (matched) { S_advance_offset(parser, &input, parser->indent + 1, true); if (peek_at(&input, parser->offset) == ' ') parser->offset++; } else { all_matched = false; } } else if (container->type == CMARK_NODE_ITEM) { if (parser->indent >= container->as.list.marker_offset + container->as.list.padding) { S_advance_offset(parser, &input, container->as.list.marker_offset + container->as.list.padding, true); } else if (parser->blank && container->first_child != NULL) { // if container->first_child is NULL, then the opening line // of the list item was blank after the list marker; in this // case, we are done with the list item. S_advance_offset(parser, &input, parser->first_nonspace - parser->offset, false); } else { all_matched = false; } } else if (container->type == CMARK_NODE_CODE_BLOCK) { if (!container->as.code.fenced) { // indented if (parser->indent >= CODE_INDENT) { S_advance_offset(parser, &input, CODE_INDENT, true); } else if (parser->blank) { S_advance_offset(parser, &input, parser->first_nonspace - parser->offset, false); } else { all_matched = false; } } else { // fenced matched = 0; if (parser->indent <= 3 && (peek_at(&input, parser->first_nonspace) == container->as.code.fence_char)) { matched = scan_close_code_fence(&input, parser->first_nonspace); } if (matched >= container->as.code.fence_length) { // closing fence - and since we're at // the end of a line, we can return: all_matched = false; S_advance_offset(parser, &input, matched, false); parser->current = finalize(parser, container); goto finished; } else { // skip opt. spaces of fence parser->offset i = container->as.code.fence_offset; while (i > 0 && peek_at(&input, parser->offset) == ' ') { S_advance_offset(parser, &input, 1, false); i--; } } } } else if (container->type == CMARK_NODE_HEADING) { // a heading can never contain more than one line all_matched = false; } else if (container->type == CMARK_NODE_HTML_BLOCK) { switch (container->as.html_block_type) { case 1: case 2: case 3: case 4: case 5: // these types of blocks can accept blanks break; case 6: case 7: if (parser->blank) { all_matched = false; } break; default: fprintf(stderr, "Error (%s:%d): Unknown HTML block type %d\n", __FILE__, __LINE__, container->as.html_block_type); exit(1); } } else if (container->type == CMARK_NODE_PARAGRAPH) { if (parser->blank) { all_matched = false; } } if (!all_matched) { container = container->parent; // back up to last matching node break; } } last_matched_container = container; // check to see if we've hit 2nd blank line, break out of list: if (parser->blank && container->last_line_blank) { break_out_of_lists(parser, &container); } maybe_lazy = parser->current->type == CMARK_NODE_PARAGRAPH; // try new container starts: while (container->type != CMARK_NODE_CODE_BLOCK && container->type != CMARK_NODE_HTML_BLOCK) { S_find_first_nonspace(parser, &input); indented = parser->indent >= CODE_INDENT; if (!indented && peek_at(&input, parser->first_nonspace) == '>') { S_advance_offset(parser, &input, parser->first_nonspace + 1 - parser->offset, false); // optional following character if (peek_at(&input, parser->offset) == ' ') S_advance_offset(parser, &input, 1, false); container = add_child(parser, container, CMARK_NODE_BLOCK_QUOTE, parser->offset + 1); } else if (!indented && (matched = scan_atx_heading_start( &input, parser->first_nonspace))) { S_advance_offset(parser, &input, parser->first_nonspace + matched - parser->offset, false); container = add_child(parser, container, CMARK_NODE_HEADING, parser->offset + 1); bufsize_t hashpos = cmark_chunk_strchr(&input, '#', parser->first_nonspace); int level = 0; while (peek_at(&input, hashpos) == '#') { level++; hashpos++; } container->as.heading.level = level; container->as.heading.setext = false; } else if (!indented && (matched = scan_open_code_fence( &input, parser->first_nonspace))) { container = add_child(parser, container, CMARK_NODE_CODE_BLOCK, parser->first_nonspace + 1); container->as.code.fenced = true; container->as.code.fence_char = peek_at(&input, parser->first_nonspace); container->as.code.fence_length = matched; container->as.code.fence_offset = (int8_t)(parser->first_nonspace - parser->offset); container->as.code.info = cmark_chunk_literal(""); S_advance_offset(parser, &input, parser->first_nonspace + matched - parser->offset, false); } else if (!indented && ((matched = scan_html_block_start( &input, parser->first_nonspace)) || (container->type != CMARK_NODE_PARAGRAPH && (matched = scan_html_block_start_7( &input, parser->first_nonspace))))) { container = add_child(parser, container, CMARK_NODE_HTML_BLOCK, parser->first_nonspace + 1); container->as.html_block_type = matched; // note, we don't adjust parser->offset because the tag is part of the // text } else if (!indented && container->type == CMARK_NODE_PARAGRAPH && (lev = scan_setext_heading_line(&input, parser->first_nonspace))) { container->type = CMARK_NODE_HEADING; container->as.heading.level = lev; container->as.heading.setext = true; S_advance_offset(parser, &input, input.len - 1 - parser->offset, false); } else if (!indented && !(container->type == CMARK_NODE_PARAGRAPH && !all_matched) && (matched = scan_thematic_break(&input, parser->first_nonspace))) { // it's only now that we know the line is not part of a setext heading: container = add_child(parser, container, CMARK_NODE_THEMATIC_BREAK, parser->first_nonspace + 1); S_advance_offset(parser, &input, input.len - 1 - parser->offset, false); } else if ((matched = parse_list_marker(&input, parser->first_nonspace, &data)) && (!indented || container->type == CMARK_NODE_LIST)) { // Note that we can have new list items starting with >= 4 // spaces indent, as long as the list container is still open. // compute padding: S_advance_offset(parser, &input, parser->first_nonspace + matched - parser->offset, false); i = 0; while (i <= 5 && peek_at(&input, parser->offset + i) == ' ') { i++; } // i = number of spaces after marker, up to 5 if (i >= 5 || i < 1 || S_is_line_end_char(peek_at(&input, parser->offset))) { data->padding = matched + 1; if (i > 0) { S_advance_offset(parser, &input, 1, false); } } else { data->padding = matched + i; S_advance_offset(parser, &input, i, true); } // check container; if it's a list, see if this list item // can continue the list; otherwise, create a list container. data->marker_offset = parser->indent; if (container->type != CMARK_NODE_LIST || !lists_match(&container->as.list, data)) { container = add_child(parser, container, CMARK_NODE_LIST, parser->first_nonspace + 1); memcpy(&container->as.list, data, sizeof(*data)); } // add the list item container = add_child(parser, container, CMARK_NODE_ITEM, parser->first_nonspace + 1); /* TODO: static */ memcpy(&container->as.list, data, sizeof(*data)); free(data); } else if (indented && !maybe_lazy && !parser->blank) { S_advance_offset(parser, &input, CODE_INDENT, true); container = add_child(parser, container, CMARK_NODE_CODE_BLOCK, parser->offset + 1); container->as.code.fenced = false; container->as.code.fence_char = 0; container->as.code.fence_length = 0; container->as.code.fence_offset = 0; container->as.code.info = cmark_chunk_literal(""); } else { break; } if (accepts_lines(container->type)) { // if it's a line container, it can't contain other containers break; } maybe_lazy = false; } // what remains at parser->offset is a text line. add the text to the // appropriate container. S_find_first_nonspace(parser, &input); if (parser->blank && container->last_child) { container->last_child->last_line_blank = true; } // block quote lines are never blank as they start with > // and we don't count blanks in fenced code for purposes of tight/loose // lists or breaking out of lists. we also don't set last_line_blank // on an empty list item. container->last_line_blank = (parser->blank && container->type != CMARK_NODE_BLOCK_QUOTE && container->type != CMARK_NODE_HEADING && container->type != CMARK_NODE_THEMATIC_BREAK && !(container->type == CMARK_NODE_CODE_BLOCK && container->as.code.fenced) && !(container->type == CMARK_NODE_ITEM && container->first_child == NULL && container->start_line == parser->line_number)); cmark_node *cont = container; while (cont->parent) { cont->parent->last_line_blank = false; cont = cont->parent; } if (parser->current != last_matched_container && container == last_matched_container && !parser->blank && parser->current->type == CMARK_NODE_PARAGRAPH && cmark_strbuf_len(&parser->current->string_content) > 0) { add_line(parser->current, &input, parser->offset); } else { // not a lazy continuation // finalize any blocks that were not matched and set cur to container: while (parser->current != last_matched_container) { parser->current = finalize(parser, parser->current); assert(parser->current != NULL); } if (container->type == CMARK_NODE_CODE_BLOCK) { add_line(container, &input, parser->offset); } else if (container->type == CMARK_NODE_HTML_BLOCK) { add_line(container, &input, parser->offset); int matches_end_condition; switch (container->as.html_block_type) { case 1: // </script>, </style>, </pre> matches_end_condition = scan_html_block_end_1(&input, parser->first_nonspace); break; case 2: // --> matches_end_condition = scan_html_block_end_2(&input, parser->first_nonspace); break; case 3: // ?> matches_end_condition = scan_html_block_end_3(&input, parser->first_nonspace); break; case 4: // > matches_end_condition = scan_html_block_end_4(&input, parser->first_nonspace); break; case 5: // ]]> matches_end_condition = scan_html_block_end_5(&input, parser->first_nonspace); break; default: matches_end_condition = 0; break; } if (matches_end_condition) { container = finalize(parser, container); assert(parser->current != NULL); } } else if (parser->blank) { // ??? do nothing } else if (accepts_lines(container->type)) { if (container->type == CMARK_NODE_HEADING && container->as.heading.setext == false) { chop_trailing_hashtags(&input); } add_line(container, &input, parser->first_nonspace); } else { // create paragraph container for line container = add_child(parser, container, CMARK_NODE_PARAGRAPH, parser->first_nonspace + 1); add_line(container, &input, parser->first_nonspace); } parser->current = container; } finished: parser->last_line_length = input.len; if (parser->last_line_length && input.data[parser->last_line_length - 1] == '\n') parser->last_line_length -= 1; if (parser->last_line_length && input.data[parser->last_line_length - 1] == '\r') parser->last_line_length -= 1; cmark_strbuf_clear(parser->curline); }
static void S_out(cmark_renderer *renderer, const char *source, bool wrap, cmark_escaping escape) { int length = cmark_strbuf_safe_strlen(source); unsigned char nextc; int32_t c; int i = 0; int len; cmark_chunk remainder = cmark_chunk_literal(""); int k = renderer->buffer->size - 1; wrap = wrap && !renderer->no_wrap; if (renderer->in_tight_list_item && renderer->need_cr > 1) { renderer->need_cr = 1; } while (renderer->need_cr) { if (k < 0 || renderer->buffer->ptr[k] == '\n') { k -= 1; } else { cmark_strbuf_putc(renderer->buffer, '\n'); if (renderer->need_cr > 1) { cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, renderer->prefix->size); } } renderer->column = 0; renderer->begin_line = true; renderer->need_cr -= 1; } while (i < length) { if (renderer->begin_line) { cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, renderer->prefix->size); // note: this assumes prefix is ascii: renderer->column = renderer->prefix->size; } len = utf8proc_iterate((const uint8_t *)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 (!renderer->begin_line) { cmark_strbuf_putc(renderer->buffer, ' '); renderer->column += 1; renderer->begin_line = false; renderer->last_breakable = renderer->buffer->size - 1; // skip following spaces while (source[i + 1] == ' ') { i++; } } } else if (c == 10) { cmark_strbuf_putc(renderer->buffer, '\n'); renderer->column = 0; renderer->begin_line = true; renderer->last_breakable = 0; } else if (escape == LITERAL) { cmark_render_code_point(renderer, c); renderer->begin_line = false; } else { (renderer->outc)(renderer, escape, c, nextc); renderer->begin_line = false; } // If adding the character went beyond width, look for an // earlier place where the line could be broken: if (renderer->width > 0 && renderer->column > renderer->width && !renderer->begin_line && renderer->last_breakable > 0) { // copy from last_breakable to remainder cmark_chunk_set_cstr(&remainder, (char *) renderer->buffer->ptr + renderer->last_breakable + 1); // truncate at last_breakable cmark_strbuf_truncate(renderer->buffer, renderer->last_breakable); // add newline, prefix, and remainder cmark_strbuf_putc(renderer->buffer, '\n'); cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, renderer->prefix->size); cmark_strbuf_put(renderer->buffer, remainder.data, remainder.len); renderer->column = renderer->prefix->size + remainder.len; cmark_chunk_free(&remainder); renderer->last_breakable = 0; renderer->begin_line = false; } i += len; } }
static int S_render_node(cmark_html_renderer *renderer, cmark_node *node, cmark_event_type ev_type, int options) { cmark_node *parent; cmark_node *grandparent; cmark_strbuf *html = renderer->html; cmark_llist *it; cmark_syntax_extension *ext; char start_heading[] = "<h0"; char end_heading[] = "</h0"; bool tight; bool filtered; char buffer[BUFFER_SIZE]; bool entering = (ev_type == CMARK_EVENT_ENTER); if (renderer->plain == node) { // back at original node renderer->plain = NULL; } if (renderer->plain != NULL) { switch (node->type) { case CMARK_NODE_TEXT: case CMARK_NODE_CODE: case CMARK_NODE_HTML_INLINE: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: case CMARK_NODE_SOFTBREAK: cmark_strbuf_putc(html, ' '); break; default: break; } return 1; } if (node->extension && node->extension->html_render_func) { node->extension->html_render_func(node->extension, renderer, node, ev_type, options); return 1; } switch (node->type) { case CMARK_NODE_DOCUMENT: break; case CMARK_NODE_BLOCK_QUOTE: if (entering) { cmark_html_render_cr(html); cmark_strbuf_puts(html, "<blockquote"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { cmark_html_render_cr(html); cmark_strbuf_puts(html, "</blockquote>\n"); } break; case CMARK_NODE_LIST: { cmark_list_type list_type = node->as.list.list_type; int start = node->as.list.start; if (entering) { cmark_html_render_cr(html); if (list_type == CMARK_BULLET_LIST) { cmark_strbuf_puts(html, "<ul"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else if (start == 1) { cmark_strbuf_puts(html, "<ol"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } else { snprintf(buffer, BUFFER_SIZE, "<ol start=\"%d\"", start); cmark_strbuf_puts(html, buffer); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, ">\n"); } } else { cmark_strbuf_puts(html, list_type == CMARK_BULLET_LIST ? "</ul>\n" : "</ol>\n"); } break; } case CMARK_NODE_ITEM: if (entering) { cmark_html_render_cr(html); cmark_strbuf_puts(html, "<li"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { cmark_strbuf_puts(html, "</li>\n"); } break; case CMARK_NODE_HEADING: if (entering) { cmark_html_render_cr(html); start_heading[2] = (char)('0' + node->as.heading.level); cmark_strbuf_puts(html, start_heading); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { end_heading[3] = (char)('0' + node->as.heading.level); cmark_strbuf_puts(html, end_heading); cmark_strbuf_puts(html, ">\n"); } break; case CMARK_NODE_CODE_BLOCK: cmark_html_render_cr(html); if (node->as.code.info.len == 0) { cmark_strbuf_puts(html, "<pre"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code>"); } else { bufsize_t first_tag = 0; while (first_tag < node->as.code.info.len && !cmark_isspace(node->as.code.info.data[first_tag])) { first_tag += 1; } if (options & CMARK_OPT_GITHUB_PRE_LANG) { cmark_strbuf_puts(html, "<pre"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, " lang=\""); escape_html(html, node->as.code.info.data, first_tag); if (first_tag < node->as.code.info.len && (options & CMARK_OPT_FULL_INFO_STRING)) { cmark_strbuf_puts(html, "\" data-meta=\""); escape_html(html, node->as.code.info.data + first_tag + 1, node->as.code.info.len - first_tag - 1); } cmark_strbuf_puts(html, "\"><code>"); } else { cmark_strbuf_puts(html, "<pre"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, "><code class=\"language-"); escape_html(html, node->as.code.info.data, first_tag); if (first_tag < node->as.code.info.len && (options & CMARK_OPT_FULL_INFO_STRING)) { cmark_strbuf_puts(html, "\" data-meta=\""); escape_html(html, node->as.code.info.data + first_tag + 1, node->as.code.info.len - first_tag - 1); } cmark_strbuf_puts(html, "\">"); } } escape_html(html, node->as.code.literal.data, node->as.code.literal.len); cmark_strbuf_puts(html, "</code></pre>\n"); break; case CMARK_NODE_HTML_BLOCK: cmark_html_render_cr(html); if (!(options & CMARK_OPT_UNSAFE)) { cmark_strbuf_puts(html, "<!-- raw HTML omitted -->"); } else if (renderer->filter_extensions) { filter_html_block(renderer, node->as.literal.data, node->as.literal.len); } else { cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); } cmark_html_render_cr(html); break; case CMARK_NODE_CUSTOM_BLOCK: cmark_html_render_cr(html); if (entering) { cmark_strbuf_put(html, node->as.custom.on_enter.data, node->as.custom.on_enter.len); } else { cmark_strbuf_put(html, node->as.custom.on_exit.data, node->as.custom.on_exit.len); } cmark_html_render_cr(html); break; case CMARK_NODE_THEMATIC_BREAK: cmark_html_render_cr(html); cmark_strbuf_puts(html, "<hr"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_puts(html, " />\n"); break; case CMARK_NODE_PARAGRAPH: parent = cmark_node_parent(node); grandparent = cmark_node_parent(parent); if (grandparent != NULL && grandparent->type == CMARK_NODE_LIST) { tight = grandparent->as.list.tight; } else { tight = false; } if (!tight) { if (entering) { cmark_html_render_cr(html); cmark_strbuf_puts(html, "<p"); cmark_html_render_sourcepos(node, html, options); cmark_strbuf_putc(html, '>'); } else { if (parent->type == CMARK_NODE_FOOTNOTE_DEFINITION && node->next == NULL) { cmark_strbuf_putc(html, ' '); S_put_footnote_backref(renderer, html); } cmark_strbuf_puts(html, "</p>\n"); } } break; case CMARK_NODE_TEXT: escape_html(html, node->as.literal.data, node->as.literal.len); break; case CMARK_NODE_LINEBREAK: cmark_strbuf_puts(html, "<br />\n"); break; case CMARK_NODE_SOFTBREAK: if (options & CMARK_OPT_HARDBREAKS) { cmark_strbuf_puts(html, "<br />\n"); } else if (options & CMARK_OPT_NOBREAKS) { cmark_strbuf_putc(html, ' '); } else { cmark_strbuf_putc(html, '\n'); } break; case CMARK_NODE_CODE: cmark_strbuf_puts(html, "<code>"); escape_html(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "</code>"); break; case CMARK_NODE_HTML_INLINE: if (!(options & CMARK_OPT_UNSAFE)) { cmark_strbuf_puts(html, "<!-- raw HTML omitted -->"); } else { filtered = false; for (it = renderer->filter_extensions; it; it = it->next) { ext = (cmark_syntax_extension *) it->data; if (!ext->html_filter_func(ext, node->as.literal.data, node->as.literal.len)) { filtered = true; break; } } if (!filtered) { cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); } else { cmark_strbuf_puts(html, "<"); cmark_strbuf_put(html, node->as.literal.data + 1, node->as.literal.len - 1); } } break; case CMARK_NODE_CUSTOM_INLINE: if (entering) { cmark_strbuf_put(html, node->as.custom.on_enter.data, node->as.custom.on_enter.len); } else { cmark_strbuf_put(html, node->as.custom.on_exit.data, node->as.custom.on_exit.len); } break; case CMARK_NODE_STRONG: if (entering) { cmark_strbuf_puts(html, "<strong>"); } else { cmark_strbuf_puts(html, "</strong>"); } break; case CMARK_NODE_EMPH: if (entering) { cmark_strbuf_puts(html, "<em>"); } else { cmark_strbuf_puts(html, "</em>"); } break; case CMARK_NODE_LINK: if (entering) { cmark_strbuf_puts(html, "<a href=\""); if (!(!(options & CMARK_OPT_UNSAFE) && scan_dangerous_url(&node->as.link.url, 0))) { houdini_escape_href(html, node->as.link.url.data, node->as.link.url.len); } if (node->as.link.title.len) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title.data, node->as.link.title.len); } cmark_strbuf_puts(html, "\">"); } else { cmark_strbuf_puts(html, "</a>"); } break; case CMARK_NODE_IMAGE: if (entering) { cmark_strbuf_puts(html, "<img src=\""); if (!(!(options & CMARK_OPT_UNSAFE) && scan_dangerous_url(&node->as.link.url, 0))) { houdini_escape_href(html, node->as.link.url.data, node->as.link.url.len); } cmark_strbuf_puts(html, "\" alt=\""); renderer->plain = node; } else { if (node->as.link.title.len) { cmark_strbuf_puts(html, "\" title=\""); escape_html(html, node->as.link.title.data, node->as.link.title.len); } cmark_strbuf_puts(html, "\" />"); } break; case CMARK_NODE_FOOTNOTE_DEFINITION: if (entering) { if (renderer->footnote_ix == 0) { cmark_strbuf_puts(html, "<section class=\"footnotes\">\n<ol>\n"); } ++renderer->footnote_ix; cmark_strbuf_puts(html, "<li id=\"fn"); char n[32]; snprintf(n, sizeof(n), "%d", renderer->footnote_ix); cmark_strbuf_puts(html, n); cmark_strbuf_puts(html, "\">\n"); } else { if (S_put_footnote_backref(renderer, html)) { cmark_strbuf_putc(html, '\n'); } cmark_strbuf_puts(html, "</li>\n"); } break; case CMARK_NODE_FOOTNOTE_REFERENCE: if (entering) { cmark_strbuf_puts(html, "<sup class=\"footnote-ref\"><a href=\"#fn"); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "\" id=\"fnref"); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "\">"); cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len); cmark_strbuf_puts(html, "</a></sup>"); } break; default: assert(false); break; } return 1; }
void cmark_strbuf_puts(cmark_strbuf *buf, const char *string) { cmark_strbuf_put(buf, (const unsigned char *)string, cmark_strbuf_safe_strlen(string)); }
static void S_out(cmark_renderer *renderer, cmark_node *node, const char *source, bool wrap, cmark_escaping escape) { int length = (int)strlen(source); unsigned char nextc; int32_t c; int i = 0; int last_nonspace; int len; cmark_chunk remainder = cmark_chunk_literal(""); int k = renderer->buffer->size - 1; cmark_syntax_extension *ext = NULL; cmark_node *n = node; while (n && !ext) { ext = n->extension; if (!ext) n = n->parent; } if (ext && !ext->commonmark_escape_func) ext = NULL; wrap = wrap && !renderer->no_linebreaks; if (renderer->in_tight_list_item && renderer->need_cr > 1) { renderer->need_cr = 1; } while (renderer->need_cr) { if (k < 0 || renderer->buffer->ptr[k] == '\n') { k -= 1; } else { cmark_strbuf_putc(renderer->buffer, '\n'); if (renderer->need_cr > 1) { cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, renderer->prefix->size); } } renderer->column = 0; renderer->begin_line = true; renderer->begin_content = true; renderer->need_cr -= 1; } while (i < length) { if (renderer->begin_line) { cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, renderer->prefix->size); // note: this assumes prefix is ascii: renderer->column = renderer->prefix->size; } len = cmark_utf8proc_iterate((const uint8_t *)source + i, length - i, &c); if (len == -1) { // error condition return; // return without rendering rest of string } if (ext && ext->commonmark_escape_func(ext, node, c)) cmark_strbuf_putc(renderer->buffer, '\\'); nextc = source[i + len]; if (c == 32 && wrap) { if (!renderer->begin_line) { last_nonspace = renderer->buffer->size; cmark_strbuf_putc(renderer->buffer, ' '); renderer->column += 1; renderer->begin_line = false; renderer->begin_content = false; // skip following spaces while (source[i + 1] == ' ') { i++; } // We don't allow breaks that make a digit the first character // because this causes problems with commonmark output. if (!cmark_isdigit(source[i + 1])) { renderer->last_breakable = last_nonspace; } } } else if (c == 10) { cmark_strbuf_putc(renderer->buffer, '\n'); renderer->column = 0; renderer->begin_line = true; renderer->begin_content = true; renderer->last_breakable = 0; } else if (escape == LITERAL) { cmark_render_code_point(renderer, c); renderer->begin_line = false; // we don't set 'begin_content' to false til we've // finished parsing a digit. Reason: in commonmark // we need to escape a potential list marker after // a digit: renderer->begin_content = renderer->begin_content && cmark_isdigit((char)c) == 1; } else { (renderer->outc)(renderer, node, escape, c, nextc); renderer->begin_line = false; renderer->begin_content = renderer->begin_content && cmark_isdigit((char)c) == 1; } // If adding the character went beyond width, look for an // earlier place where the line could be broken: if (renderer->width > 0 && renderer->column > renderer->width && !renderer->begin_line && renderer->last_breakable > 0) { // copy from last_breakable to remainder cmark_chunk_set_cstr(renderer->mem, &remainder, (char *)renderer->buffer->ptr + renderer->last_breakable + 1); // truncate at last_breakable cmark_strbuf_truncate(renderer->buffer, renderer->last_breakable); // add newline, prefix, and remainder cmark_strbuf_putc(renderer->buffer, '\n'); cmark_strbuf_put(renderer->buffer, renderer->prefix->ptr, renderer->prefix->size); cmark_strbuf_put(renderer->buffer, remainder.data, remainder.len); renderer->column = renderer->prefix->size + remainder.len; cmark_chunk_free(renderer->mem, &remainder); renderer->last_breakable = 0; renderer->begin_line = false; renderer->begin_content = false; } i += len; } }