aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 568af0a4a1000269081758ca7e61af64823345d6 (plain)
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6. #include "cmark.h"
  7. #include "utf8.h"
  8. #include "scanners.h"
  9. #include "inlines.h"
  10. #include "html/houdini.h"
  11. #include "buffer.h"
  12. #include "debug.h"
  13. #define peek_at(i, n) (i)->data[n]
  14. static node_block* make_block(int tag, int start_line, int start_column)
  15. {
  16. node_block* e;
  17. e = calloc(1, sizeof(*e));
  18. if(e != NULL) {
  19. e->tag = tag;
  20. e->open = true;
  21. e->start_line = start_line;
  22. e->start_column = start_column;
  23. e->end_line = start_line;
  24. strbuf_init(&e->string_content, 32);
  25. }
  26. return e;
  27. }
  28. // Create a root document node_block.
  29. static node_block* make_document()
  30. {
  31. node_block *e = make_block(BLOCK_DOCUMENT, 1, 1);
  32. e->as.document.refmap = reference_map_new();
  33. e->top = e;
  34. return e;
  35. }
  36. cmark_doc_parser *cmark_new_doc_parser()
  37. {
  38. cmark_doc_parser *parser = (cmark_doc_parser*)malloc(sizeof(cmark_doc_parser));
  39. node_block *document = make_document();
  40. strbuf *line = (strbuf*)malloc(sizeof(strbuf));
  41. cmark_strbuf_init(line, 256);
  42. parser->head = document;
  43. parser->current = document;
  44. parser->line_number = 0;
  45. parser->curline = line;
  46. return parser;
  47. }
  48. void cmark_free_doc_parser(cmark_doc_parser *parser)
  49. {
  50. cmark_strbuf_free(parser->curline);
  51. free(parser->curline);
  52. free(parser);
  53. }
  54. static void finalize(node_block* b, int line_number);
  55. // Returns true if line has only space characters, else false.
  56. static bool is_blank(strbuf *s, int offset)
  57. {
  58. while (offset < s->size) {
  59. switch (s->ptr[offset]) {
  60. case '\n':
  61. return true;
  62. case ' ':
  63. offset++;
  64. break;
  65. default:
  66. return false;
  67. }
  68. }
  69. return true;
  70. }
  71. static inline bool can_contain(int parent_type, int child_type)
  72. {
  73. return ( parent_type == BLOCK_DOCUMENT ||
  74. parent_type == BLOCK_BQUOTE ||
  75. parent_type == BLOCK_LIST_ITEM ||
  76. (parent_type == BLOCK_LIST && child_type == BLOCK_LIST_ITEM) );
  77. }
  78. static inline bool accepts_lines(int block_type)
  79. {
  80. return (block_type == BLOCK_PARAGRAPH ||
  81. block_type == BLOCK_ATX_HEADER ||
  82. block_type == BLOCK_INDENTED_CODE ||
  83. block_type == BLOCK_FENCED_CODE);
  84. }
  85. static void add_line(node_block* node_block, chunk *ch, int offset)
  86. {
  87. assert(node_block->open);
  88. strbuf_put(&node_block->string_content, ch->data + offset, ch->len - offset);
  89. }
  90. static void remove_trailing_blank_lines(strbuf *ln)
  91. {
  92. int i;
  93. for (i = ln->size - 1; i >= 0; --i) {
  94. unsigned char c = ln->ptr[i];
  95. if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
  96. break;
  97. }
  98. if (i < 0) {
  99. strbuf_clear(ln);
  100. return;
  101. }
  102. i = strbuf_strchr(ln, '\n', i);
  103. if (i >= 0)
  104. strbuf_truncate(ln, i);
  105. }
  106. // Check to see if a node_block ends with a blank line, descending
  107. // if needed into lists and sublists.
  108. static bool ends_with_blank_line(node_block* node_block)
  109. {
  110. if (node_block->last_line_blank) {
  111. return true;
  112. }
  113. if ((node_block->tag == BLOCK_LIST || node_block->tag == BLOCK_LIST_ITEM) && node_block->last_child) {
  114. return ends_with_blank_line(node_block->last_child);
  115. } else {
  116. return false;
  117. }
  118. }
  119. // Break out of all containing lists
  120. static int break_out_of_lists(node_block ** bptr, int line_number)
  121. {
  122. node_block *container = *bptr;
  123. node_block *b = container->top;
  124. // find first containing BLOCK_LIST:
  125. while (b && b->tag != BLOCK_LIST) {
  126. b = b->last_child;
  127. }
  128. if (b) {
  129. while (container && container != b) {
  130. finalize(container, line_number);
  131. container = container->parent;
  132. }
  133. finalize(b, line_number);
  134. *bptr = b->parent;
  135. }
  136. return 0;
  137. }
  138. static void finalize(node_block* b, int line_number)
  139. {
  140. int firstlinelen;
  141. int pos;
  142. node_block* item;
  143. node_block* subitem;
  144. if (!b->open)
  145. return; // don't do anything if the node_block is already closed
  146. b->open = false;
  147. if (line_number > b->start_line) {
  148. b->end_line = line_number - 1;
  149. } else {
  150. b->end_line = line_number;
  151. }
  152. switch (b->tag) {
  153. case BLOCK_PARAGRAPH:
  154. pos = 0;
  155. while (strbuf_at(&b->string_content, 0) == '[' &&
  156. (pos = parse_reference_inline(&b->string_content, b->top->as.document.refmap))) {
  157. strbuf_drop(&b->string_content, pos);
  158. }
  159. if (is_blank(&b->string_content, 0)) {
  160. b->tag = BLOCK_REFERENCE_DEF;
  161. }
  162. break;
  163. case BLOCK_INDENTED_CODE:
  164. remove_trailing_blank_lines(&b->string_content);
  165. strbuf_putc(&b->string_content, '\n');
  166. break;
  167. case BLOCK_FENCED_CODE:
  168. // first line of contents becomes info
  169. firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
  170. strbuf_init(&b->as.code.info, 0);
  171. houdini_unescape_html_f(
  172. &b->as.code.info,
  173. b->string_content.ptr,
  174. firstlinelen
  175. );
  176. strbuf_drop(&b->string_content, firstlinelen + 1);
  177. strbuf_trim(&b->as.code.info);
  178. strbuf_unescape(&b->as.code.info);
  179. break;
  180. case BLOCK_LIST: // determine tight/loose status
  181. b->as.list.tight = true; // tight by default
  182. item = b->children;
  183. while (item) {
  184. // check for non-final non-empty list item ending with blank line:
  185. if (item->last_line_blank && item->next) {
  186. b->as.list.tight = false;
  187. break;
  188. }
  189. // recurse into children of list item, to see if there are
  190. // spaces between them:
  191. subitem = item->children;
  192. while (subitem) {
  193. if (ends_with_blank_line(subitem) &&
  194. (item->next || subitem->next)) {
  195. b->as.list.tight = false;
  196. break;
  197. }
  198. subitem = subitem->next;
  199. }
  200. if (!(b->as.list.tight)) {
  201. break;
  202. }
  203. item = item->next;
  204. }
  205. break;
  206. default:
  207. break;
  208. }
  209. }
  210. // Add a node_block as child of another. Return pointer to child.
  211. static node_block* add_child(node_block* parent,
  212. int block_type, int start_line, int start_column)
  213. {
  214. assert(parent);
  215. // if 'parent' isn't the kind of node_block that can accept this child,
  216. // then back up til we hit a node_block that can.
  217. while (!can_contain(parent->tag, block_type)) {
  218. finalize(parent, start_line);
  219. parent = parent->parent;
  220. }
  221. node_block* child = make_block(block_type, start_line, start_column);
  222. child->parent = parent;
  223. child->top = parent->top;
  224. if (parent->last_child) {
  225. parent->last_child->next = child;
  226. child->prev = parent->last_child;
  227. } else {
  228. parent->children = child;
  229. child->prev = NULL;
  230. }
  231. parent->last_child = child;
  232. return child;
  233. }
  234. typedef struct BlockStack {
  235. struct BlockStack *previous;
  236. node_block *next_sibling;
  237. } block_stack;
  238. // Walk through node_block and all children, recursively, parsing
  239. // string content into inline content where appropriate.
  240. static void process_inlines(node_block* cur, reference_map *refmap)
  241. {
  242. block_stack* stack = NULL;
  243. block_stack* newstack = NULL;
  244. while (cur != NULL) {
  245. switch (cur->tag) {
  246. case BLOCK_PARAGRAPH:
  247. case BLOCK_ATX_HEADER:
  248. case BLOCK_SETEXT_HEADER:
  249. cur->inline_content = parse_inlines(&cur->string_content, refmap);
  250. break;
  251. default:
  252. break;
  253. }
  254. if (cur->children) {
  255. newstack = (block_stack*)malloc(sizeof(block_stack));
  256. if (newstack == NULL) return;
  257. newstack->previous = stack;
  258. stack = newstack;
  259. stack->next_sibling = cur->next;
  260. cur = cur->children;
  261. } else {
  262. cur = cur->next;
  263. }
  264. while (cur == NULL && stack != NULL) {
  265. cur = stack->next_sibling;
  266. newstack = stack->previous;
  267. free(stack);
  268. stack = newstack;
  269. }
  270. }
  271. while (stack != NULL) {
  272. newstack = stack->previous;
  273. free(stack);
  274. stack = newstack;
  275. }
  276. }
  277. // Attempts to parse a list item marker (bullet or enumerated).
  278. // On success, returns length of the marker, and populates
  279. // data with the details. On failure, returns 0.
  280. static int parse_list_marker(chunk *input, int pos, struct ListData ** dataptr)
  281. {
  282. unsigned char c;
  283. int startpos;
  284. struct ListData * data;
  285. startpos = pos;
  286. c = peek_at(input, pos);
  287. if ((c == '*' || c == '-' || c == '+') && !scan_hrule(input, pos)) {
  288. pos++;
  289. if (!isspace(peek_at(input, pos))) {
  290. return 0;
  291. }
  292. data = calloc(1, sizeof(*data));
  293. if(data == NULL) {
  294. return 0;
  295. } else {
  296. data->marker_offset = 0; // will be adjusted later
  297. data->list_type = bullet;
  298. data->bullet_char = c;
  299. data->start = 1;
  300. data->delimiter = period;
  301. data->tight = false;
  302. }
  303. } else if (isdigit(c)) {
  304. int start = 0;
  305. do {
  306. start = (10 * start) + (peek_at(input, pos) - '0');
  307. pos++;
  308. } while (isdigit(peek_at(input, pos)));
  309. c = peek_at(input, pos);
  310. if (c == '.' || c == ')') {
  311. pos++;
  312. if (!isspace(peek_at(input, pos))) {
  313. return 0;
  314. }
  315. data = calloc(1, sizeof(*data));
  316. if(data == NULL) {
  317. return 0;
  318. } else {
  319. data->marker_offset = 0; // will be adjusted later
  320. data->list_type = ordered;
  321. data->bullet_char = 0;
  322. data->start = start;
  323. data->delimiter = (c == '.' ? period : parens);
  324. data->tight = false;
  325. }
  326. } else {
  327. return 0;
  328. }
  329. } else {
  330. return 0;
  331. }
  332. *dataptr = data;
  333. return (pos - startpos);
  334. }
  335. // Return 1 if list item belongs in list, else 0.
  336. static int lists_match(struct ListData *list_data, struct ListData *item_data)
  337. {
  338. return (list_data->list_type == item_data->list_type &&
  339. list_data->delimiter == item_data->delimiter &&
  340. // list_data->marker_offset == item_data.marker_offset &&
  341. list_data->bullet_char == item_data->bullet_char);
  342. }
  343. static node_block *finalize_document(node_block *document, int linenum)
  344. {
  345. while (document != document->top) {
  346. finalize(document, linenum);
  347. document = document->parent;
  348. }
  349. finalize(document, linenum);
  350. process_inlines(document, document->as.document.refmap);
  351. return document;
  352. }
  353. extern node_block *cmark_parse_file(FILE *f)
  354. {
  355. unsigned char buffer[4096];
  356. cmark_doc_parser *parser = cmark_new_doc_parser();
  357. size_t offset;
  358. node_block *document;
  359. while (fgets((char *)buffer, sizeof(buffer), f)) {
  360. offset = strlen((char *)buffer);
  361. cmark_process_line(parser, buffer, offset);
  362. }
  363. document = cmark_finish(parser);
  364. cmark_free_doc_parser(parser);
  365. return document;
  366. }
  367. extern node_block *cmark_parse_document(const unsigned char *buffer, size_t len)
  368. {
  369. int linenum = 1;
  370. const unsigned char *end = buffer + len;
  371. size_t offset;
  372. cmark_doc_parser *parser = cmark_new_doc_parser();
  373. node_block *document;
  374. while (buffer < end) {
  375. const unsigned char *eol = memchr(buffer, '\n', end - buffer);
  376. offset = eol ? (eol - buffer) + 1 : eol - buffer;
  377. cmark_process_line(parser, buffer, offset);
  378. buffer += offset;
  379. linenum++;
  380. }
  381. document = cmark_finish(parser);
  382. cmark_free_doc_parser(parser);
  383. return document;
  384. }
  385. static void chop_trailing_hashtags(chunk *ch)
  386. {
  387. int n, orig_n;
  388. chunk_rtrim(ch);
  389. orig_n = n = ch->len - 1;
  390. // if string ends in space followed by #s, remove these:
  391. while (n >= 0 && peek_at(ch, n) == '#')
  392. n--;
  393. // Check for a be a space before the final #s:
  394. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  395. ch->len = n;
  396. chunk_rtrim(ch);
  397. }
  398. }
  399. void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer,
  400. size_t bytes)
  401. {
  402. node_block* last_matched_container;
  403. int offset = 0;
  404. int matched = 0;
  405. int lev = 0;
  406. int i;
  407. struct ListData * data = NULL;
  408. bool all_matched = true;
  409. node_block* container;
  410. node_block* cur = parser->current;
  411. bool blank = false;
  412. int first_nonspace;
  413. int indent;
  414. chunk input;
  415. utf8proc_detab(parser->curline, buffer, bytes);
  416. // Add a newline to the end if not present:
  417. // TODO this breaks abstraction:
  418. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  419. strbuf_putc(parser->curline, '\n');
  420. }
  421. input.data = parser->curline->ptr;
  422. input.len = parser->curline->size;
  423. // container starts at the document root.
  424. container = cur->top;
  425. parser->line_number++;
  426. // for each containing node_block, try to parse the associated line start.
  427. // bail out on failure: container will point to the last matching node_block.
  428. while (container->last_child && container->last_child->open) {
  429. container = container->last_child;
  430. first_nonspace = offset;
  431. while (peek_at(&input, first_nonspace) == ' ') {
  432. first_nonspace++;
  433. }
  434. indent = first_nonspace - offset;
  435. blank = peek_at(&input, first_nonspace) == '\n';
  436. if (container->tag == BLOCK_BQUOTE) {
  437. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  438. if (matched) {
  439. offset = first_nonspace + 1;
  440. if (peek_at(&input, offset) == ' ')
  441. offset++;
  442. } else {
  443. all_matched = false;
  444. }
  445. } else if (container->tag == BLOCK_LIST_ITEM) {
  446. if (indent >= container->as.list.marker_offset +
  447. container->as.list.padding) {
  448. offset += container->as.list.marker_offset +
  449. container->as.list.padding;
  450. } else if (blank) {
  451. offset = first_nonspace;
  452. } else {
  453. all_matched = false;
  454. }
  455. } else if (container->tag == BLOCK_INDENTED_CODE) {
  456. if (indent >= CODE_INDENT) {
  457. offset += CODE_INDENT;
  458. } else if (blank) {
  459. offset = first_nonspace;
  460. } else {
  461. all_matched = false;
  462. }
  463. } else if (container->tag == BLOCK_ATX_HEADER ||
  464. container->tag == BLOCK_SETEXT_HEADER) {
  465. // a header can never contain more than one line
  466. all_matched = false;
  467. } else if (container->tag == BLOCK_FENCED_CODE) {
  468. // skip optional spaces of fence offset
  469. i = container->as.code.fence_offset;
  470. while (i > 0 && peek_at(&input, offset) == ' ') {
  471. offset++;
  472. i--;
  473. }
  474. } else if (container->tag == BLOCK_HTML) {
  475. if (blank) {
  476. all_matched = false;
  477. }
  478. } else if (container->tag == BLOCK_PARAGRAPH) {
  479. if (blank) {
  480. container->last_line_blank = true;
  481. all_matched = false;
  482. }
  483. }
  484. if (!all_matched) {
  485. container = container->parent; // back up to last matching node_block
  486. break;
  487. }
  488. }
  489. last_matched_container = container;
  490. // check to see if we've hit 2nd blank line, break out of list:
  491. if (blank && container->last_line_blank) {
  492. break_out_of_lists(&container, parser->line_number);
  493. }
  494. // unless last matched container is code node_block, try new container starts:
  495. while (container->tag != BLOCK_FENCED_CODE && container->tag != BLOCK_INDENTED_CODE &&
  496. container->tag != BLOCK_HTML) {
  497. first_nonspace = offset;
  498. while (peek_at(&input, first_nonspace) == ' ')
  499. first_nonspace++;
  500. indent = first_nonspace - offset;
  501. blank = peek_at(&input, first_nonspace) == '\n';
  502. if (indent >= CODE_INDENT) {
  503. if (cur->tag != BLOCK_PARAGRAPH && !blank) {
  504. offset += CODE_INDENT;
  505. container = add_child(container, BLOCK_INDENTED_CODE, parser->line_number, offset + 1);
  506. } else { // indent > 4 in lazy line
  507. break;
  508. }
  509. } else if (peek_at(&input, first_nonspace) == '>') {
  510. offset = first_nonspace + 1;
  511. // optional following character
  512. if (peek_at(&input, offset) == ' ')
  513. offset++;
  514. container = add_child(container, BLOCK_BQUOTE, parser->line_number, offset + 1);
  515. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  516. offset = first_nonspace + matched;
  517. container = add_child(container, BLOCK_ATX_HEADER, parser->line_number, offset + 1);
  518. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  519. int level = 0;
  520. while (peek_at(&input, hashpos) == '#') {
  521. level++;
  522. hashpos++;
  523. }
  524. container->as.header.level = level;
  525. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  526. container = add_child(container, BLOCK_FENCED_CODE, parser->line_number, first_nonspace + 1);
  527. container->as.code.fence_char = peek_at(&input, first_nonspace);
  528. container->as.code.fence_length = matched;
  529. container->as.code.fence_offset = first_nonspace - offset;
  530. offset = first_nonspace + matched;
  531. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  532. container = add_child(container, BLOCK_HTML, parser->line_number, first_nonspace + 1);
  533. // note, we don't adjust offset because the tag is part of the text
  534. } else if (container->tag == BLOCK_PARAGRAPH &&
  535. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  536. // check that there is only one line in the paragraph:
  537. strbuf_strrchr(&container->string_content, '\n',
  538. strbuf_len(&container->string_content) - 2) < 0) {
  539. container->tag = BLOCK_SETEXT_HEADER;
  540. container->as.header.level = lev;
  541. offset = input.len - 1;
  542. } else if (!(container->tag == BLOCK_PARAGRAPH && !all_matched) &&
  543. (matched = scan_hrule(&input, first_nonspace))) {
  544. // it's only now that we know the line is not part of a setext header:
  545. container = add_child(container, BLOCK_HRULE, parser->line_number, first_nonspace + 1);
  546. finalize(container, parser->line_number);
  547. container = container->parent;
  548. offset = input.len - 1;
  549. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  550. // compute padding:
  551. offset = first_nonspace + matched;
  552. i = 0;
  553. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  554. i++;
  555. }
  556. // i = number of spaces after marker, up to 5
  557. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  558. data->padding = matched + 1;
  559. if (i > 0) {
  560. offset += 1;
  561. }
  562. } else {
  563. data->padding = matched + i;
  564. offset += i;
  565. }
  566. // check container; if it's a list, see if this list item
  567. // can continue the list; otherwise, create a list container.
  568. data->marker_offset = indent;
  569. if (container->tag != BLOCK_LIST ||
  570. !lists_match(&container->as.list, data)) {
  571. container = add_child(container, BLOCK_LIST, parser->line_number,
  572. first_nonspace + 1);
  573. memcpy(&container->as.list, data, sizeof(*data));
  574. }
  575. // add the list item
  576. container = add_child(container, BLOCK_LIST_ITEM, parser->line_number,
  577. first_nonspace + 1);
  578. /* TODO: static */
  579. memcpy(&container->as.list, data, sizeof(*data));
  580. free(data);
  581. } else {
  582. break;
  583. }
  584. if (accepts_lines(container->tag)) {
  585. // if it's a line container, it can't contain other containers
  586. break;
  587. }
  588. }
  589. // what remains at offset is a text line. add the text to the
  590. // appropriate container.
  591. first_nonspace = offset;
  592. while (peek_at(&input, first_nonspace) == ' ')
  593. first_nonspace++;
  594. indent = first_nonspace - offset;
  595. blank = peek_at(&input, first_nonspace) == '\n';
  596. // node_block quote lines are never blank as they start with >
  597. // and we don't count blanks in fenced code for purposes of tight/loose
  598. // lists or breaking out of lists. we also don't set last_line_blank
  599. // on an empty list item.
  600. container->last_line_blank = (blank &&
  601. container->tag != BLOCK_BQUOTE &&
  602. container->tag != BLOCK_FENCED_CODE &&
  603. !(container->tag == BLOCK_LIST_ITEM &&
  604. container->children == NULL &&
  605. container->start_line == parser->line_number));
  606. node_block *cont = container;
  607. while (cont->parent) {
  608. cont->parent->last_line_blank = false;
  609. cont = cont->parent;
  610. }
  611. if (cur != last_matched_container &&
  612. container == last_matched_container &&
  613. !blank &&
  614. cur->tag == BLOCK_PARAGRAPH &&
  615. strbuf_len(&cur->string_content) > 0) {
  616. add_line(cur, &input, offset);
  617. } else { // not a lazy continuation
  618. // finalize any blocks that were not matched and set cur to container:
  619. while (cur != last_matched_container) {
  620. finalize(cur, parser->line_number);
  621. cur = cur->parent;
  622. assert(cur != NULL);
  623. }
  624. if (container->tag == BLOCK_INDENTED_CODE) {
  625. add_line(container, &input, offset);
  626. } else if (container->tag == BLOCK_FENCED_CODE) {
  627. matched = 0;
  628. if (indent <= 3 &&
  629. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  630. int fence_len = scan_close_code_fence(&input, first_nonspace);
  631. if (fence_len > container->as.code.fence_length)
  632. matched = 1;
  633. }
  634. if (matched) {
  635. // if closing fence, don't add line to container; instead, close it:
  636. finalize(container, parser->line_number);
  637. container = container->parent; // back up to parent
  638. } else {
  639. add_line(container, &input, offset);
  640. }
  641. } else if (container->tag == BLOCK_HTML) {
  642. add_line(container, &input, offset);
  643. } else if (blank) {
  644. // ??? do nothing
  645. } else if (container->tag == BLOCK_ATX_HEADER) {
  646. chop_trailing_hashtags(&input);
  647. add_line(container, &input, first_nonspace);
  648. finalize(container, parser->line_number);
  649. container = container->parent;
  650. } else if (accepts_lines(container->tag)) {
  651. add_line(container, &input, first_nonspace);
  652. } else if (container->tag != BLOCK_HRULE && container->tag != BLOCK_SETEXT_HEADER) {
  653. // create paragraph container for line
  654. container = add_child(container, BLOCK_PARAGRAPH, parser->line_number, first_nonspace + 1);
  655. add_line(container, &input, first_nonspace);
  656. } else {
  657. assert(false);
  658. }
  659. parser->current = container;
  660. }
  661. strbuf_clear(parser->curline);
  662. }
  663. node_block *cmark_finish(cmark_doc_parser *parser)
  664. {
  665. finalize_document(parser->current, parser->line_number);
  666. strbuf_free(parser->curline);
  667. return parser->head;
  668. }