ext/redcarpet/markdown.c in redcarpet-3.2.3 vs ext/redcarpet/markdown.c in redcarpet-3.3.0

- old
+ new

@@ -1117,11 +1117,11 @@ title_e = i - 1; while (title_e > title_b && _isspace(data[title_e])) title_e--; /* checking for closing quote presence */ - if (data[title_e] != '\'' && data[title_e] != '"') { + if (data[title_e] != '\'' && data[title_e] != '"') { title_b = title_e = 0; link_e = i; } } @@ -1546,11 +1546,11 @@ return 0; return i + 2; } -/* prefix_uli • returns ordered list item prefix */ +/* prefix_uli • returns unordered list item prefix */ static size_t prefix_uli(uint8_t *data, size_t size) { size_t i = 0; @@ -1618,11 +1618,11 @@ } static size_t parse_htmlblock(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size, int do_render); -/* parse_blockquote • handles parsing of a regular paragraph */ +/* parse_paragraph • handles parsing of a regular paragraph */ static size_t parse_paragraph(struct buf *ob, struct sd_markdown *rndr, uint8_t *data, size_t size) { size_t i = 0, end = 0; int level = 0, last_is_empty = 1; @@ -2799,10 +2799,11 @@ #define MARKDOWN_GROW(x) ((x) + ((x) >> 1)) static const char UTF8_BOM[] = {0xEF, 0xBB, 0xBF}; struct buf *text; size_t beg, end; + int in_fence = 0; text = bufnew(64); if (!text) return; @@ -2810,11 +2811,12 @@ bufgrow(text, doc_size); /* reset the references table */ memset(&md->refs, 0x0, REF_TABLE_SIZE * sizeof(void *)); - int footnotes_enabled = md->ext_flags & MKDEXT_FOOTNOTES; + int footnotes_enabled = md->ext_flags & MKDEXT_FOOTNOTES; + int codefences_enabled = md->ext_flags & MKDEXT_FENCED_CODE; /* reset the footnotes lists */ if (footnotes_enabled) { memset(&md->footnotes_found, 0x0, sizeof(md->footnotes_found)); memset(&md->footnotes_used, 0x0, sizeof(md->footnotes_used)); @@ -2826,14 +2828,17 @@ /* Skip a possible UTF-8 BOM, even though the Unicode standard * discourages having these in UTF-8 documents */ if (doc_size >= 3 && memcmp(document, UTF8_BOM, 3) == 0) beg += 3; - while (beg < doc_size) /* iterating over lines */ - if (footnotes_enabled && is_footnote(document, beg, doc_size, &end, &md->footnotes_found)) + while (beg < doc_size) { /* iterating over lines */ + if (codefences_enabled && (is_codefence(document + beg, doc_size - beg, NULL) != 0)) + in_fence = !in_fence; + + if (!in_fence && footnotes_enabled && is_footnote(document, beg, doc_size, &end, &md->footnotes_found)) beg = end; - else if (is_ref(document, beg, doc_size, &end, md->refs)) + else if (!in_fence && is_ref(document, beg, doc_size, &end, md->refs)) beg = end; else { /* skipping to the next line */ end = beg; while (end < doc_size && document[end] != '\n' && document[end] != '\r') end++; @@ -2849,21 +2854,22 @@ end++; } beg = end; } + } /* pre-grow the output buffer to minimize allocations */ bufgrow(ob, MARKDOWN_GROW(text->size)); /* second pass: actual rendering */ if (md->cb.doc_header) md->cb.doc_header(ob, md->opaque); if (text->size) { /* adding a final newline if not already present */ - if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') + if (text->data[text->size - 1] != '\n' && text->data[text->size - 1] != '\r') bufputc(text, '\n'); parse_block(ob, md, text->data, text->size); } @@ -2871,9 +2877,12 @@ if (footnotes_enabled) parse_footnote_list(ob, md, &md->footnotes_used); if (md->cb.doc_footer) md->cb.doc_footer(ob, md->opaque); + + /* Null-terminate the buffer */ + bufcstr(ob); /* clean-up */ bufrelease(text); free_link_refs(md->refs); if (footnotes_enabled) {