Skip to content

Commit

Permalink
cmark_parser: implement and expose reentrant feed function.
Browse files Browse the repository at this point in the history
This can be useful for transclusion extensions for example,
http://talk.commonmark.org/t/transclusion-or-including-sub-documents-for-reuse/270
  • Loading branch information
MathieuDuponchelle committed Apr 27, 2016
1 parent 51f76f3 commit 4cee305
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -566,6 +566,18 @@ void cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len) {
S_parser_feed(parser, (const unsigned char *)buffer, len, false);
}

void cmark_parser_feed_reentrant(cmark_parser *parser, const char *buffer, size_t len) {
cmark_strbuf *saved_linebuf = parser->linebuf;
cmark_strbuf *buf = (cmark_strbuf *)malloc(sizeof(cmark_strbuf));
cmark_strbuf_init(buf, 0);

parser->linebuf = buf;
S_parser_feed(parser, (const unsigned char *)buffer, len, true);
cmark_strbuf_free(buf);

parser->linebuf = saved_linebuf;
}

static void S_parser_feed(cmark_parser *parser, const unsigned char *buffer,
size_t len, bool eof) {
const unsigned char *end = buffer + len;
Expand Down Expand Up @@ -1211,6 +1223,9 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
bool all_matched = true;
cmark_node *container;
cmark_chunk input;
cmark_node *current;

cmark_strbuf_clear(parser->curline);

if (parser->options & CMARK_OPT_VALIDATE_UTF8)
cmark_utf8proc_check(parser->curline, buffer, bytes);
Expand Down Expand Up @@ -1242,9 +1257,13 @@ static void S_process_line(cmark_parser *parser, const unsigned char *buffer,
if (parser->blank && container->last_line_blank)
break_out_of_lists(parser, &container);

current = parser->current;

open_new_blocks(parser, &container, &input, all_matched);

add_text_to_container(parser, container, last_matched_container, &input);
/* parser->current might have changed if feed_reentrant was called */
if (current == parser->current)
add_text_to_container(parser, container, last_matched_container, &input);

finished:
parser->last_line_length = input.len;
Expand Down
4 changes: 4 additions & 0 deletions src/cmark_extension_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,6 +416,10 @@ void cmark_parser_advance_offset(cmark_parser *parser,
int count,
int columns);


CMARK_EXPORT
void cmark_parser_feed_reentrant(cmark_parser *parser, const char *buffer, size_t len);

/** Attach the syntax 'extension' to the 'parser', to provide extra syntax
* rules.
* See the documentation for cmark_syntax_extension for more information.
Expand Down

0 comments on commit 4cee305

Please sign in to comment.