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