Example #1
0
static void remove_trailing_blank_lines(cmark_strbuf *ln)
{
	int i;
	unsigned char c;

	for (i = ln->size - 1; i >= 0; --i) {
		c = ln->ptr[i];

		if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
			break;
	}

	if (i < 0) {
		cmark_strbuf_clear(ln);
		return;
	}


	for(; i < ln->size; ++i) {
		c = ln->ptr[i];

		if (c != '\r' && c != '\n')
			continue;

		cmark_strbuf_truncate(ln, i);
		break;
	}
}
Example #2
0
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;
	}
}
Example #3
0
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);
}
Example #4
0
File: blocks.c Project: gcgft/cmark
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++;
  }
}
Example #5
0
void cmark_strbuf_set(cmark_strbuf *buf, const unsigned char *data,
                      bufsize_t len) {
  if (len <= 0 || data == NULL) {
    cmark_strbuf_clear(buf);
  } else {
    if (data != buf->ptr) {
      if (len >= buf->asize)
        cmark_strbuf_grow(buf, len);
      memmove(buf->ptr, data, len);
    }
    buf->size = len;
    buf->ptr[buf->size] = '\0';
  }
}
Example #6
0
cmark_node *cmark_parser_finish(cmark_parser *parser)
{
	if (parser->linebuf->size) {
		S_process_line(parser, parser->linebuf->ptr,
		               parser->linebuf->size);
		cmark_strbuf_clear(parser->linebuf);
	}

	finalize_document(parser);
	cmark_strbuf_free(parser->curline);
#if CMARK_DEBUG_NODES
	if (cmark_node_check(parser->root, stderr)) {
		abort();
	}
#endif
	return parser->root;
}
Example #7
0
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;
	}
}
Example #8
0
static void remove_trailing_blank_lines(cmark_strbuf *ln)
{
	int i;

	for (i = ln->size - 1; i >= 0; --i) {
		unsigned char c = ln->ptr[i];

		if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
			break;
	}

	if (i < 0) {
		cmark_strbuf_clear(ln);
		return;
	}

	i = cmark_strbuf_strchr(ln, '\n', i);
	if (i >= 0)
		cmark_strbuf_truncate(ln, i);
}
Example #9
0
File: blocks.c Project: gcgft/cmark
cmark_node *cmark_parser_finish(cmark_parser *parser) {
  if (parser->linebuf->size) {
    S_process_line(parser, parser->linebuf->ptr, parser->linebuf->size);
    cmark_strbuf_clear(parser->linebuf);
  }

  finalize_document(parser);

  if (parser->options & CMARK_OPT_NORMALIZE) {
    cmark_consolidate_text_nodes(parser->root);
  }

  cmark_strbuf_free(parser->curline);

#if CMARK_DEBUG_NODES
  if (cmark_node_check(parser->root, stderr)) {
    abort();
  }
#endif
  return parser->root;
}
Example #10
0
static void
S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
{
	cmark_node* last_matched_container;
	int offset = 0;
	int matched = 0;
	int lev = 0;
	int i;
	cmark_list *data = NULL;
	bool all_matched = true;
	cmark_node* container;
	bool blank = false;
	int first_nonspace;
	int indent;
	bool indented;
	cmark_chunk input;
	bool maybe_lazy;
	int trim = 0;
	bool cr = false;
	bool lf = false;

	utf8proc_detab(parser->curline, buffer, bytes);

	// Add a newline to the end if not present:
	// TODO this breaks abstraction:
	if (parser->curline->size > trim &&
	    parser->curline->ptr[parser->curline->size - 1 - trim] == '\n') {
		trim += 1;
		lf = true;
	}
	if (parser->curline->size > trim &&
	    parser->curline->ptr[parser->curline->size - 1 - trim] == '\r') {
		trim += 1;
		cr = true;
	}
	if (cr) {
		cmark_strbuf_truncate(parser->curline, parser->curline->size - trim);
	}
	if (cr || !lf) {
		cmark_strbuf_putc(parser->curline, '\n');
	}

	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;

		first_nonspace = offset;
		while (peek_at(&input, first_nonspace) == ' ') {
			first_nonspace++;
		}

		indent = first_nonspace - offset;
		blank = peek_at(&input, first_nonspace) == '\n' ||
		        peek_at(&input, first_nonspace) == '\r';

		if (container->type == NODE_BLOCK_QUOTE) {
			matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
			if (matched) {
				offset = first_nonspace + 1;
				if (peek_at(&input, offset) == ' ')
					offset++;
			} else {
				all_matched = false;
			}

		} else if (container->type == NODE_ITEM) {

			if (indent >= container->as.list.marker_offset +
			    container->as.list.padding) {
				offset += container->as.list.marker_offset +
				          container->as.list.padding;
			} else if (blank) {
				offset = first_nonspace;
			} else {
				all_matched = false;
			}

		} else if (container->type == NODE_CODE_BLOCK) {

			if (!container->as.code.fenced) { // indented
				if (indent >= CODE_INDENT) {
					offset += CODE_INDENT;
				} else if (blank) {
					offset = first_nonspace;
				} else {
					all_matched = false;
				}
			} else { // fenced
				matched = 0;
				if (indent <= 3 &&
				    (peek_at(&input, first_nonspace) ==
				     container->as.code.fence_char)) {
					matched = scan_close_code_fence(&input,
					                                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;
					offset += matched;
					parser->current = finalize(parser, container);
					goto finished;
				} else {
					// skip opt. spaces of fence offset
					i = container->as.code.fence_offset;
					while (i > 0 &&
					       peek_at(&input, offset) == ' ') {
						offset++;
						i--;
					}
				}
			}
		} else if (container->type == NODE_HEADER) {

			// a header can never contain more than one line
			all_matched = false;

		} else if (container->type == NODE_HTML) {

			if (blank) {
				all_matched = false;
			}

		} else if (container->type == NODE_PARAGRAPH) {

			if (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 (blank && container->last_line_blank) {
		break_out_of_lists(parser, &container);
	}

	maybe_lazy = parser->current->type == NODE_PARAGRAPH;
	// try new container starts:
	while (container->type != NODE_CODE_BLOCK &&
	       container->type != NODE_HTML) {

		first_nonspace = offset;
		while (peek_at(&input, first_nonspace) == ' ')
			first_nonspace++;

		indent = first_nonspace - offset;
		indented = indent >= CODE_INDENT;
		blank = peek_at(&input, first_nonspace) == '\n' ||
		        peek_at(&input, first_nonspace) == '\r';

		if (indented && !maybe_lazy && !blank) {
				offset += CODE_INDENT;
				container = add_child(parser, container, NODE_CODE_BLOCK, 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 if (!indented && peek_at(&input, first_nonspace) == '>') {

			offset = first_nonspace + 1;
			// optional following character
			if (peek_at(&input, offset) == ' ')
				offset++;
			container = add_child(parser, container, NODE_BLOCK_QUOTE, offset + 1);

		} else if (!indented && (matched = scan_atx_header_start(&input, first_nonspace))) {

			offset = first_nonspace + matched;
			container = add_child(parser, container, NODE_HEADER, offset + 1);

			int hashpos = cmark_chunk_strchr(&input, '#', first_nonspace);
			int level = 0;

			while (peek_at(&input, hashpos) == '#') {
				level++;
				hashpos++;
			}
			container->as.header.level = level;
			container->as.header.setext = false;

		} else if (!indented && (matched = scan_open_code_fence(&input, first_nonspace))) {

			container = add_child(parser, container, NODE_CODE_BLOCK, first_nonspace + 1);
			container->as.code.fenced = true;
			container->as.code.fence_char = peek_at(&input, first_nonspace);
			container->as.code.fence_length = matched;
			container->as.code.fence_offset = first_nonspace - offset;
			container->as.code.info = cmark_chunk_literal("");
			offset = first_nonspace + matched;

		} else if (!indented && (matched = scan_html_block_tag(&input, first_nonspace))) {

			container = add_child(parser, container, NODE_HTML, first_nonspace + 1);
			// note, we don't adjust offset because the tag is part of the text

		} else if (!indented &&
			   container->type == NODE_PARAGRAPH &&
		           (lev = scan_setext_header_line(&input, first_nonspace)) &&
		           // check that there is only one line in the paragraph:
		           (cmark_strbuf_strrchr(&container->string_content, '\n',
		                                 cmark_strbuf_len(&container->string_content) - 2) < 0 &&
		           cmark_strbuf_strrchr(&container->string_content, '\r',
		                                cmark_strbuf_len(&container->string_content) - 2) < 0)) {

			container->type = NODE_HEADER;
			container->as.header.level = lev;
			container->as.header.setext = true;
			offset = input.len - 1;

		} else if (!indented &&
			   !(container->type == NODE_PARAGRAPH &&
			     !all_matched) &&
		           (matched = scan_hrule(&input, first_nonspace))) {

			// it's only now that we know the line is not part of a setext header:
			container = add_child(parser, container, NODE_HRULE, first_nonspace + 1);
			container = finalize(parser, container);
			offset = input.len - 1;

		} else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {

			// compute padding:
			offset = first_nonspace + matched;
			i = 0;
			while (i <= 5 && peek_at(&input, offset + i) == ' ') {
				i++;
			}
			// i = number of spaces after marker, up to 5
			if (i >= 5 || i < 1 ||
			    peek_at(&input, offset) == '\n' ||
			    peek_at(&input, offset) == '\r') {
				data->padding = matched + 1;
				if (i > 0) {
					offset += 1;
				}
			} else {
				data->padding = matched + i;
				offset += i;
			}

			// check container; if it's a list, see if this list item
			// can continue the list; otherwise, create a list container.

			data->marker_offset = indent;

			if (container->type != NODE_LIST ||
			    !lists_match(&container->as.list, data)) {
				container = add_child(parser, container, NODE_LIST,
				                      first_nonspace + 1);

				memcpy(&container->as.list, data, sizeof(*data));
			}

			// add the list item
			container = add_child(parser, container, NODE_ITEM,
			                      first_nonspace + 1);
			/* TODO: static */
			memcpy(&container->as.list, data, sizeof(*data));
			free(data);
		} 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 offset is a text line.  add the text to the
	// appropriate container.

	first_nonspace = offset;
	while (peek_at(&input, first_nonspace) == ' ')
		first_nonspace++;

	indent = first_nonspace - offset;
	blank = peek_at(&input, first_nonspace) == '\n' ||
	        peek_at(&input, first_nonspace) == '\r';

	if (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 = (blank &&
	                              container->type != NODE_BLOCK_QUOTE &&
	                              container->type != NODE_HEADER &&
	                              !(container->type == NODE_CODE_BLOCK &&
	                                container->as.code.fenced) &&
	                              !(container->type == 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 &&
	    !blank &&
	    parser->current->type == NODE_PARAGRAPH &&
	    cmark_strbuf_len(&parser->current->string_content) > 0) {

		add_line(parser->current, &input, 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 == NODE_CODE_BLOCK ||
		    container->type == NODE_HTML) {

			add_line(container, &input, offset);

		} else if (blank) {

			// ??? do nothing

		} else if (accepts_lines(container->type)) {

			if (container->type == NODE_HEADER &&
			    container->as.header.setext == false) {
				chop_trailing_hashtags(&input);
			}
			add_line(container, &input, first_nonspace);

		} else {
			// create paragraph container for line
			container = add_child(parser, container, NODE_PARAGRAPH, first_nonspace + 1);
			add_line(container, &input, first_nonspace);

		}

		parser->current = container;
	}
finished:
	parser->last_line_length = parser->curline->size;
	if (parser->last_line_length &&
	    parser->curline->ptr[parser->last_line_length - 1] == '\n')
		parser->last_line_length--;
	if (parser->last_line_length &&
	    parser->curline->ptr[parser->last_line_length - 1] == '\r')
		parser->last_line_length--;

	cmark_strbuf_clear(parser->curline);

}
Example #11
0
File: blocks.c Project: gcgft/cmark
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);
}