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