aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 58162b5620552c27245e1a1bc48e41aef1b0204b (plain)
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include "config.h"
  6. #include "parser.h"
  7. #include "cmark.h"
  8. #include "node.h"
  9. #include "references.h"
  10. #include "utf8.h"
  11. #include "scanners.h"
  12. #include "inlines.h"
  13. #include "html/houdini.h"
  14. #include "buffer.h"
  15. #include "debug.h"
  16. #define CODE_INDENT 4
  17. #define peek_at(i, n) (i)->data[n]
  18. static cmark_node* make_block(cmark_node_type tag, int start_line, int start_column)
  19. {
  20. cmark_node* e;
  21. e = (cmark_node *)calloc(1, sizeof(*e));
  22. if(e != NULL) {
  23. e->type = tag;
  24. e->open = true;
  25. e->start_line = start_line;
  26. e->start_column = start_column;
  27. e->end_line = start_line;
  28. strbuf_init(&e->string_content, 32);
  29. }
  30. return e;
  31. }
  32. // Create a root document cmark_node.
  33. static cmark_node* make_document()
  34. {
  35. cmark_node *e = make_block(NODE_DOCUMENT, 1, 1);
  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. cmark_node *document = make_document();
  42. strbuf *line = (strbuf*)malloc(sizeof(strbuf));
  43. cmark_strbuf_init(line, 256);
  44. parser->refmap = reference_map_new();
  45. parser->root = 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. cmark_reference_map_free(parser->refmap);
  56. free(parser);
  57. }
  58. static void finalize(cmark_doc_parser *parser, cmark_node* b, int line_number);
  59. // Returns true if line has only space characters, else false.
  60. static bool is_blank(strbuf *s, int offset)
  61. {
  62. while (offset < s->size) {
  63. switch (s->ptr[offset]) {
  64. case '\n':
  65. return true;
  66. case ' ':
  67. offset++;
  68. break;
  69. default:
  70. return false;
  71. }
  72. }
  73. return true;
  74. }
  75. static inline bool can_contain(cmark_node_type parent_type, cmark_node_type child_type)
  76. {
  77. return ( parent_type == NODE_DOCUMENT ||
  78. parent_type == NODE_BQUOTE ||
  79. parent_type == NODE_LIST_ITEM ||
  80. (parent_type == NODE_LIST && child_type == NODE_LIST_ITEM) );
  81. }
  82. static inline bool accepts_lines(cmark_node_type block_type)
  83. {
  84. return (block_type == NODE_PARAGRAPH ||
  85. block_type == NODE_ATX_HEADER ||
  86. block_type == NODE_INDENTED_CODE ||
  87. block_type == NODE_FENCED_CODE);
  88. }
  89. static void add_line(cmark_node* cmark_node, chunk *ch, int offset)
  90. {
  91. assert(cmark_node->open);
  92. strbuf_put(&cmark_node->string_content, ch->data + offset, ch->len - offset);
  93. }
  94. static void remove_trailing_blank_lines(strbuf *ln)
  95. {
  96. int i;
  97. for (i = ln->size - 1; i >= 0; --i) {
  98. unsigned char c = ln->ptr[i];
  99. if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
  100. break;
  101. }
  102. if (i < 0) {
  103. strbuf_clear(ln);
  104. return;
  105. }
  106. i = strbuf_strchr(ln, '\n', i);
  107. if (i >= 0)
  108. strbuf_truncate(ln, i);
  109. }
  110. // Check to see if a cmark_node ends with a blank line, descending
  111. // if needed into lists and sublists.
  112. static bool ends_with_blank_line(cmark_node* cmark_node)
  113. {
  114. if (cmark_node->last_line_blank) {
  115. return true;
  116. }
  117. if ((cmark_node->type == NODE_LIST || cmark_node->type == NODE_LIST_ITEM) && cmark_node->last_child) {
  118. return ends_with_blank_line(cmark_node->last_child);
  119. } else {
  120. return false;
  121. }
  122. }
  123. // Break out of all containing lists
  124. static int break_out_of_lists(cmark_doc_parser *parser, cmark_node ** bptr, int line_number)
  125. {
  126. cmark_node *container = *bptr;
  127. cmark_node *b = parser->root;
  128. // find first containing NODE_LIST:
  129. while (b && b->type != NODE_LIST) {
  130. b = b->last_child;
  131. }
  132. if (b) {
  133. while (container && container != b) {
  134. finalize(parser, container, line_number);
  135. container = container->parent;
  136. }
  137. finalize(parser, b, line_number);
  138. *bptr = b->parent;
  139. }
  140. return 0;
  141. }
  142. static void finalize(cmark_doc_parser *parser, cmark_node* b, int line_number)
  143. {
  144. int firstlinelen;
  145. int pos;
  146. cmark_node* item;
  147. cmark_node* subitem;
  148. if (!b->open)
  149. return; // don't do anything if the cmark_node is already closed
  150. b->open = false;
  151. if (line_number > b->start_line) {
  152. b->end_line = line_number - 1;
  153. } else {
  154. b->end_line = line_number;
  155. }
  156. switch (b->type) {
  157. case NODE_PARAGRAPH:
  158. pos = 0;
  159. while (strbuf_at(&b->string_content, 0) == '[' &&
  160. (pos = parse_reference_inline(&b->string_content, parser->refmap))) {
  161. strbuf_drop(&b->string_content, pos);
  162. }
  163. if (is_blank(&b->string_content, 0)) {
  164. b->type = NODE_REFERENCE_DEF;
  165. }
  166. break;
  167. case NODE_INDENTED_CODE:
  168. remove_trailing_blank_lines(&b->string_content);
  169. strbuf_putc(&b->string_content, '\n');
  170. break;
  171. case NODE_FENCED_CODE:
  172. // first line of contents becomes info
  173. firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
  174. strbuf_init(&b->as.code.info, 0);
  175. houdini_unescape_html_f(
  176. &b->as.code.info,
  177. b->string_content.ptr,
  178. firstlinelen
  179. );
  180. strbuf_drop(&b->string_content, firstlinelen + 1);
  181. strbuf_trim(&b->as.code.info);
  182. strbuf_unescape(&b->as.code.info);
  183. break;
  184. case NODE_LIST: // determine tight/loose status
  185. b->as.list.tight = true; // tight by default
  186. item = b->first_child;
  187. while (item) {
  188. // check for non-final non-empty list item ending with blank line:
  189. if (item->last_line_blank && item->next) {
  190. b->as.list.tight = false;
  191. break;
  192. }
  193. // recurse into children of list item, to see if there are
  194. // spaces between them:
  195. subitem = item->first_child;
  196. while (subitem) {
  197. if (ends_with_blank_line(subitem) &&
  198. (item->next || subitem->next)) {
  199. b->as.list.tight = false;
  200. break;
  201. }
  202. subitem = subitem->next;
  203. }
  204. if (!(b->as.list.tight)) {
  205. break;
  206. }
  207. item = item->next;
  208. }
  209. break;
  210. default:
  211. break;
  212. }
  213. }
  214. // Add a cmark_node as child of another. Return pointer to child.
  215. static cmark_node* add_child(cmark_doc_parser *parser, cmark_node* parent,
  216. cmark_node_type block_type, int start_line, int start_column)
  217. {
  218. assert(parent);
  219. // if 'parent' isn't the kind of cmark_node that can accept this child,
  220. // then back up til we hit a cmark_node that can.
  221. while (!can_contain(parent->type, block_type)) {
  222. finalize(parser, parent, start_line);
  223. parent = parent->parent;
  224. }
  225. cmark_node* child = make_block(block_type, start_line, start_column);
  226. child->parent = parent;
  227. if (parent->last_child) {
  228. parent->last_child->next = child;
  229. child->prev = parent->last_child;
  230. } else {
  231. parent->first_child = child;
  232. child->prev = NULL;
  233. }
  234. parent->last_child = child;
  235. return child;
  236. }
  237. typedef struct BlockStack {
  238. struct BlockStack *previous;
  239. cmark_node *next_sibling;
  240. } block_stack;
  241. // Walk through cmark_node and all children, recursively, parsing
  242. // string content into inline content where appropriate.
  243. static void process_inlines(cmark_node* cur, reference_map *refmap)
  244. {
  245. block_stack* stack = NULL;
  246. block_stack* newstack = NULL;
  247. while (cur != NULL) {
  248. switch (cur->type) {
  249. case NODE_PARAGRAPH:
  250. case NODE_ATX_HEADER:
  251. case NODE_SETEXT_HEADER:
  252. parse_inlines(cur, refmap);
  253. break;
  254. default:
  255. break;
  256. }
  257. if (cur->first_child) {
  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->first_child;
  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, cmark_list **dataptr)
  284. {
  285. unsigned char c;
  286. int startpos;
  287. cmark_list *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 = (cmark_list *)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 = (cmark_list *)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(cmark_list *list_data, cmark_list *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 cmark_node *finalize_document(cmark_doc_parser *parser)
  347. {
  348. while (parser->current != parser->root) {
  349. finalize(parser, parser->current, parser->line_number);
  350. parser->current = parser->current->parent;
  351. }
  352. finalize(parser, parser->root, parser->line_number);
  353. process_inlines(parser->root, parser->refmap);
  354. return parser->root;
  355. }
  356. extern cmark_node *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. cmark_node *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 cmark_node *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. cmark_node *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. cmark_node* last_matched_container;
  406. int offset = 0;
  407. int matched = 0;
  408. int lev = 0;
  409. int i;
  410. cmark_list *data = NULL;
  411. bool all_matched = true;
  412. cmark_node* container;
  413. cmark_node* 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 = parser->root;
  428. parser->line_number++;
  429. // for each containing cmark_node, try to parse the associated line start.
  430. // bail out on failure: container will point to the last matching cmark_node.
  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->type == NODE_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->type == NODE_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->type == NODE_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->type == NODE_ATX_HEADER ||
  467. container->type == NODE_SETEXT_HEADER) {
  468. // a header can never contain more than one line
  469. all_matched = false;
  470. } else if (container->type == NODE_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->type == NODE_HTML) {
  478. if (blank) {
  479. all_matched = false;
  480. }
  481. } else if (container->type == NODE_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 cmark_node
  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(parser, &container, parser->line_number);
  496. }
  497. // unless last matched container is code cmark_node, try new container starts:
  498. while (container->type != NODE_FENCED_CODE && container->type != NODE_INDENTED_CODE &&
  499. container->type != NODE_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->type != NODE_PARAGRAPH && !blank) {
  507. offset += CODE_INDENT;
  508. container = add_child(parser, container, NODE_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(parser, container, NODE_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(parser, container, NODE_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(parser, container, NODE_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(parser, container, NODE_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->type == NODE_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->type = NODE_SETEXT_HEADER;
  543. container->as.header.level = lev;
  544. offset = input.len - 1;
  545. } else if (!(container->type == NODE_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(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
  549. finalize(parser, 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->type != NODE_LIST ||
  573. !lists_match(&container->as.list, data)) {
  574. container = add_child(parser, container, NODE_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(parser, container, NODE_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->type)) {
  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. // cmark_node 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->type != NODE_BQUOTE &&
  605. container->type != NODE_FENCED_CODE &&
  606. !(container->type == NODE_LIST_ITEM &&
  607. container->first_child == NULL &&
  608. container->start_line == parser->line_number));
  609. cmark_node *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->type == NODE_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(parser, cur, parser->line_number);
  624. cur = cur->parent;
  625. assert(cur != NULL);
  626. }
  627. if (container->type == NODE_INDENTED_CODE) {
  628. add_line(container, &input, offset);
  629. } else if (container->type == NODE_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(parser, 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->type == NODE_HTML) {
  645. add_line(container, &input, offset);
  646. } else if (blank) {
  647. // ??? do nothing
  648. } else if (container->type == NODE_ATX_HEADER) {
  649. chop_trailing_hashtags(&input);
  650. add_line(container, &input, first_nonspace);
  651. finalize(parser, container, parser->line_number);
  652. container = container->parent;
  653. } else if (accepts_lines(container->type)) {
  654. add_line(container, &input, first_nonspace);
  655. } else if (container->type != NODE_HRULE && container->type != NODE_SETEXT_HEADER) {
  656. // create paragraph container for line
  657. container = add_child(parser, container, NODE_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. cmark_node *cmark_finish(cmark_doc_parser *parser)
  667. {
  668. finalize_document(parser);
  669. strbuf_free(parser->curline);
  670. #if CMARK_DEBUG_NODES
  671. if (cmark_node_check(parser->root)) {
  672. abort();
  673. }
  674. #endif
  675. return parser->root;
  676. }