aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 57f6756b04b67c41708f65f9a9a75b92847a77a8 (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_BLOCK_QUOTE ||
  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_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_HEADER:
  251. parse_inlines(cur, refmap);
  252. break;
  253. default:
  254. break;
  255. }
  256. if (cur->first_child) {
  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->first_child;
  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, cmark_list **dataptr)
  283. {
  284. unsigned char c;
  285. int startpos;
  286. cmark_list *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 = (cmark_list *)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 = CMARK_BULLET_LIST;
  300. data->bullet_char = c;
  301. data->start = 1;
  302. data->delimiter = CMARK_PERIOD_DELIM;
  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 = (cmark_list *)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 = CMARK_ORDERED_LIST;
  323. data->bullet_char = 0;
  324. data->start = start;
  325. data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
  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(cmark_list *list_data, cmark_list *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 cmark_node *finalize_document(cmark_doc_parser *parser)
  346. {
  347. while (parser->current != parser->root) {
  348. finalize(parser, parser->current, parser->line_number);
  349. parser->current = parser->current->parent;
  350. }
  351. finalize(parser, parser->root, parser->line_number);
  352. process_inlines(parser->root, parser->refmap);
  353. return parser->root;
  354. }
  355. extern cmark_node *cmark_parse_file(FILE *f)
  356. {
  357. char buffer[4096];
  358. cmark_doc_parser *parser = cmark_new_doc_parser();
  359. size_t offset;
  360. cmark_node *document;
  361. while (fgets(buffer, sizeof(buffer), f)) {
  362. offset = strlen(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 cmark_node *cmark_parse_document(const char *buffer, size_t len)
  370. {
  371. int linenum = 1;
  372. const char *end = buffer + len;
  373. size_t offset;
  374. cmark_doc_parser *parser = cmark_new_doc_parser();
  375. cmark_node *document;
  376. while (buffer < end) {
  377. const char *eol = 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 char *buffer,
  402. size_t bytes)
  403. {
  404. cmark_node* last_matched_container;
  405. int offset = 0;
  406. int matched = 0;
  407. int lev = 0;
  408. int i;
  409. cmark_list *data = NULL;
  410. bool all_matched = true;
  411. cmark_node* container;
  412. cmark_node* cur = parser->current;
  413. bool blank = false;
  414. int first_nonspace;
  415. int indent;
  416. chunk input;
  417. utf8proc_detab(parser->curline, (unsigned char *)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 = parser->root;
  427. parser->line_number++;
  428. // for each containing cmark_node, try to parse the associated line start.
  429. // bail out on failure: container will point to the last matching cmark_node.
  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->type == NODE_BLOCK_QUOTE) {
  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->type == NODE_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->type == NODE_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->type == NODE_HEADER) {
  466. // a header can never contain more than one line
  467. all_matched = false;
  468. if (blank) {
  469. container->last_line_blank = true;
  470. }
  471. } else if (container->type == NODE_FENCED_CODE) {
  472. // skip optional spaces of fence offset
  473. i = container->as.code.fence_offset;
  474. while (i > 0 && peek_at(&input, offset) == ' ') {
  475. offset++;
  476. i--;
  477. }
  478. } else if (container->type == NODE_HTML) {
  479. if (blank) {
  480. container->last_line_blank = true;
  481. all_matched = false;
  482. }
  483. } else if (container->type == NODE_PARAGRAPH) {
  484. if (blank) {
  485. container->last_line_blank = true;
  486. all_matched = false;
  487. }
  488. }
  489. if (!all_matched) {
  490. container = container->parent; // back up to last matching cmark_node
  491. break;
  492. }
  493. }
  494. last_matched_container = container;
  495. // check to see if we've hit 2nd blank line, break out of list:
  496. if (blank && container->last_line_blank) {
  497. break_out_of_lists(parser, &container, parser->line_number);
  498. }
  499. // unless last matched container is code cmark_node, try new container starts:
  500. while (container->type != NODE_FENCED_CODE && container->type != NODE_INDENTED_CODE &&
  501. container->type != NODE_HTML) {
  502. first_nonspace = offset;
  503. while (peek_at(&input, first_nonspace) == ' ')
  504. first_nonspace++;
  505. indent = first_nonspace - offset;
  506. blank = peek_at(&input, first_nonspace) == '\n';
  507. if (indent >= CODE_INDENT) {
  508. if (cur->type != NODE_PARAGRAPH && !blank) {
  509. offset += CODE_INDENT;
  510. container = add_child(parser, container, NODE_INDENTED_CODE, parser->line_number, offset + 1);
  511. } else { // indent > 4 in lazy line
  512. break;
  513. }
  514. } else if (peek_at(&input, first_nonspace) == '>') {
  515. offset = first_nonspace + 1;
  516. // optional following character
  517. if (peek_at(&input, offset) == ' ')
  518. offset++;
  519. container = add_child(parser, container, NODE_BLOCK_QUOTE, parser->line_number, offset + 1);
  520. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  521. offset = first_nonspace + matched;
  522. container = add_child(parser, container, NODE_HEADER, parser->line_number, offset + 1);
  523. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  524. int level = 0;
  525. while (peek_at(&input, hashpos) == '#') {
  526. level++;
  527. hashpos++;
  528. }
  529. container->as.header.level = level;
  530. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  531. container = add_child(parser, container, NODE_FENCED_CODE, parser->line_number, first_nonspace + 1);
  532. container->as.code.fence_char = peek_at(&input, first_nonspace);
  533. container->as.code.fence_length = matched;
  534. container->as.code.fence_offset = first_nonspace - offset;
  535. offset = first_nonspace + matched;
  536. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  537. container = add_child(parser, container, NODE_HTML, parser->line_number, first_nonspace + 1);
  538. // note, we don't adjust offset because the tag is part of the text
  539. } else if (container->type == NODE_PARAGRAPH &&
  540. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  541. // check that there is only one line in the paragraph:
  542. strbuf_strrchr(&container->string_content, '\n',
  543. strbuf_len(&container->string_content) - 2) < 0) {
  544. container->type = NODE_HEADER;
  545. container->as.header.level = lev;
  546. offset = input.len - 1;
  547. } else if (!(container->type == NODE_PARAGRAPH && !all_matched) &&
  548. (matched = scan_hrule(&input, first_nonspace))) {
  549. // it's only now that we know the line is not part of a setext header:
  550. container = add_child(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
  551. finalize(parser, container, parser->line_number);
  552. container = container->parent;
  553. offset = input.len - 1;
  554. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  555. // compute padding:
  556. offset = first_nonspace + matched;
  557. i = 0;
  558. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  559. i++;
  560. }
  561. // i = number of spaces after marker, up to 5
  562. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  563. data->padding = matched + 1;
  564. if (i > 0) {
  565. offset += 1;
  566. }
  567. } else {
  568. data->padding = matched + i;
  569. offset += i;
  570. }
  571. // check container; if it's a list, see if this list item
  572. // can continue the list; otherwise, create a list container.
  573. data->marker_offset = indent;
  574. if (container->type != NODE_LIST ||
  575. !lists_match(&container->as.list, data)) {
  576. container = add_child(parser, container, NODE_LIST, parser->line_number,
  577. first_nonspace + 1);
  578. memcpy(&container->as.list, data, sizeof(*data));
  579. }
  580. // add the list item
  581. container = add_child(parser, container, NODE_LIST_ITEM, parser->line_number,
  582. first_nonspace + 1);
  583. /* TODO: static */
  584. memcpy(&container->as.list, data, sizeof(*data));
  585. free(data);
  586. } else {
  587. break;
  588. }
  589. if (accepts_lines(container->type)) {
  590. // if it's a line container, it can't contain other containers
  591. break;
  592. }
  593. }
  594. // what remains at offset is a text line. add the text to the
  595. // appropriate container.
  596. first_nonspace = offset;
  597. while (peek_at(&input, first_nonspace) == ' ')
  598. first_nonspace++;
  599. indent = first_nonspace - offset;
  600. blank = peek_at(&input, first_nonspace) == '\n';
  601. // cmark_node quote lines are never blank as they start with >
  602. // and we don't count blanks in fenced code for purposes of tight/loose
  603. // lists or breaking out of lists. we also don't set last_line_blank
  604. // on an empty list item.
  605. container->last_line_blank = (blank &&
  606. container->type != NODE_BLOCK_QUOTE &&
  607. container->type != NODE_HEADER &&
  608. container->type != NODE_FENCED_CODE &&
  609. !(container->type == NODE_LIST_ITEM &&
  610. container->first_child == NULL &&
  611. container->start_line == parser->line_number));
  612. cmark_node *cont = container;
  613. while (cont->parent) {
  614. cont->parent->last_line_blank = false;
  615. cont = cont->parent;
  616. }
  617. if (cur != last_matched_container &&
  618. container == last_matched_container &&
  619. !blank &&
  620. cur->type == NODE_PARAGRAPH &&
  621. strbuf_len(&cur->string_content) > 0) {
  622. add_line(cur, &input, offset);
  623. } else { // not a lazy continuation
  624. // finalize any blocks that were not matched and set cur to container:
  625. while (cur != last_matched_container) {
  626. finalize(parser, cur, parser->line_number);
  627. cur = cur->parent;
  628. assert(cur != NULL);
  629. }
  630. if (container->type == NODE_INDENTED_CODE) {
  631. add_line(container, &input, offset);
  632. } else if (container->type == NODE_FENCED_CODE) {
  633. matched = 0;
  634. if (indent <= 3 &&
  635. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  636. int fence_len = scan_close_code_fence(&input, first_nonspace);
  637. if (fence_len > container->as.code.fence_length)
  638. matched = 1;
  639. }
  640. if (matched) {
  641. // if closing fence, don't add line to container; instead, close it:
  642. finalize(parser, container, parser->line_number);
  643. container = container->parent; // back up to parent
  644. } else {
  645. add_line(container, &input, offset);
  646. }
  647. } else if (container->type == NODE_HTML) {
  648. add_line(container, &input, offset);
  649. } else if (blank) {
  650. // ??? do nothing
  651. } else if (container->type == NODE_HEADER) {
  652. chop_trailing_hashtags(&input);
  653. add_line(container, &input, first_nonspace);
  654. finalize(parser, container, parser->line_number);
  655. container = container->parent;
  656. } else if (accepts_lines(container->type)) {
  657. add_line(container, &input, first_nonspace);
  658. } else if (container->type != NODE_HRULE &&
  659. container->type != NODE_HEADER) {
  660. // create paragraph container for line
  661. container = add_child(parser, container, NODE_PARAGRAPH, parser->line_number, first_nonspace + 1);
  662. add_line(container, &input, first_nonspace);
  663. } else {
  664. assert(false);
  665. }
  666. parser->current = container;
  667. }
  668. strbuf_clear(parser->curline);
  669. }
  670. cmark_node *cmark_finish(cmark_doc_parser *parser)
  671. {
  672. finalize_document(parser);
  673. strbuf_free(parser->curline);
  674. #if CMARK_DEBUG_NODES
  675. if (cmark_node_check(parser->root, stderr)) {
  676. abort();
  677. }
  678. #endif
  679. return parser->root;
  680. }