Skip to content

Commit

Permalink
Preserve whitespace for inline parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
MathieuDuponchelle committed Nov 29, 2016
1 parent b8f993c commit c6b706a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
19 changes: 11 additions & 8 deletions src/blocks.c
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ static CMARK_INLINE bool contains_inlines(cmark_node_type block_type) {
static void add_line(cmark_node *node, cmark_chunk *ch, cmark_parser *parser) {
int chars_to_tab;
int i;

assert(node->flags & CMARK_NODE__OPEN);
if (parser->partially_consumed_tab) {
parser->offset += 1; // skip over tab
Expand Down Expand Up @@ -263,11 +264,17 @@ static cmark_node *finalize(cmark_parser *parser, cmark_node *b) {

switch (S_type(b)) {
case CMARK_NODE_PARAGRAPH:
while (cmark_strbuf_at(node_content, 0) == '[' &&
(pos = cmark_parse_reference_inline(parser->mem, node_content,
parser->refmap))) {
{
bufsize_t first_nonspace = 0;

cmark_strbuf_drop(node_content, pos);
while (S_is_space_or_tab(cmark_strbuf_at(node_content, first_nonspace))) {
first_nonspace++;
}
while (cmark_strbuf_at(node_content, first_nonspace) == '[' &&
(pos = cmark_parse_reference_inline(parser->mem, node_content,
parser->refmap))) {
cmark_strbuf_drop(node_content, pos);
}
}
if (is_blank(node_content, 0)) {
// remove blank node (former reference def)
Expand Down Expand Up @@ -1167,15 +1174,11 @@ static void add_text_to_container(cmark_parser *parser, cmark_node *container,
container->as.heading.setext == false) {
chop_trailing_hashtags(input);
}
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
false);
add_line(container, input, parser);
} else {
// create paragraph container for line
container = add_child(parser, container, CMARK_NODE_PARAGRAPH,
parser->first_nonspace + 1);
S_advance_offset(parser, input, parser->first_nonspace - parser->offset,
false);
add_line(container, input, parser);
}

Expand Down
5 changes: 5 additions & 0 deletions src/inlines.c
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ static cmark_node *handle_backslash(subject *subj) {
advance(subj);
return make_str(subj->mem, cmark_chunk_dup(&subj->input, subj->pos - 1, 1));
} else if (!is_eof(subj) && skip_line_end(subj)) {
skip_spaces(subj);
return make_linebreak(subj->mem);
} else {
return make_str(subj->mem, cmark_chunk_literal("\\"));
Expand Down Expand Up @@ -952,6 +953,7 @@ static cmark_node *handle_close_bracket(subject *subj) {
// Assumes the subject has a cr or newline at the current position.
static cmark_node *handle_newline(subject *subj) {
bufsize_t nlpos = subj->pos;

// skip over cr, crlf, or lf:
if (peek_at(subj, subj->pos) == '\r') {
advance(subj);
Expand Down Expand Up @@ -1094,6 +1096,7 @@ extern void cmark_parse_inlines(cmark_mem *mem, cmark_node *parent,
cmark_reference_map *refmap, int options) {
subject subj;
subject_from_buf(mem, &subj, &parent->content, refmap);
skip_spaces(&subj);
cmark_chunk_rtrim(&subj.input);

while (!is_eof(&subj) && parse_inline(&subj, parent, options))
Expand Down Expand Up @@ -1131,6 +1134,8 @@ bufsize_t cmark_parse_reference_inline(cmark_mem *mem, cmark_strbuf *input,

subject_from_buf(mem, &subj, input, NULL);

skip_spaces(&subj);

// parse label:
if (!link_label(&subj, &lab) || lab.len == 0)
return 0;
Expand Down

0 comments on commit c6b706a

Please sign in to comment.