aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 0e682596131f2f09a2df045d84b8af0a9569acdb (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. static void fix_parents(cmark_node *node) {
  242. cmark_node *cur = node->first_child;
  243. if (cur == NULL) {
  244. return;
  245. }
  246. while (cur->next != NULL) {
  247. cur->parent = node;
  248. cur = cur->next;
  249. }
  250. cur->parent = node;
  251. node->last_child = cur;
  252. }
  253. // Walk through cmark_node and all children, recursively, parsing
  254. // string content into inline content where appropriate.
  255. static void process_inlines(cmark_node* cur, reference_map *refmap)
  256. {
  257. block_stack* stack = NULL;
  258. block_stack* newstack = NULL;
  259. while (cur != NULL) {
  260. switch (cur->type) {
  261. case NODE_PARAGRAPH:
  262. case NODE_ATX_HEADER:
  263. case NODE_SETEXT_HEADER:
  264. cur->first_child = parse_inlines(&cur->string_content, refmap);
  265. fix_parents(cur);
  266. break;
  267. default:
  268. break;
  269. }
  270. if (cur->first_child) {
  271. newstack = (block_stack*)malloc(sizeof(block_stack));
  272. if (newstack == NULL) return;
  273. newstack->previous = stack;
  274. stack = newstack;
  275. stack->next_sibling = cur->next;
  276. cur = cur->first_child;
  277. } else {
  278. cur = cur->next;
  279. }
  280. while (cur == NULL && stack != NULL) {
  281. cur = stack->next_sibling;
  282. newstack = stack->previous;
  283. free(stack);
  284. stack = newstack;
  285. }
  286. }
  287. while (stack != NULL) {
  288. newstack = stack->previous;
  289. free(stack);
  290. stack = newstack;
  291. }
  292. }
  293. // Attempts to parse a list item marker (bullet or enumerated).
  294. // On success, returns length of the marker, and populates
  295. // data with the details. On failure, returns 0.
  296. static int parse_list_marker(chunk *input, int pos, cmark_list **dataptr)
  297. {
  298. unsigned char c;
  299. int startpos;
  300. cmark_list *data;
  301. startpos = pos;
  302. c = peek_at(input, pos);
  303. if ((c == '*' || c == '-' || c == '+') && !scan_hrule(input, pos)) {
  304. pos++;
  305. if (!isspace(peek_at(input, pos))) {
  306. return 0;
  307. }
  308. data = (cmark_list *)calloc(1, sizeof(*data));
  309. if(data == NULL) {
  310. return 0;
  311. } else {
  312. data->marker_offset = 0; // will be adjusted later
  313. data->list_type = CMARK_BULLET_LIST;
  314. data->bullet_char = c;
  315. data->start = 1;
  316. data->delimiter = CMARK_PERIOD_DELIM;
  317. data->tight = false;
  318. }
  319. } else if (isdigit(c)) {
  320. int start = 0;
  321. do {
  322. start = (10 * start) + (peek_at(input, pos) - '0');
  323. pos++;
  324. } while (isdigit(peek_at(input, pos)));
  325. c = peek_at(input, pos);
  326. if (c == '.' || c == ')') {
  327. pos++;
  328. if (!isspace(peek_at(input, pos))) {
  329. return 0;
  330. }
  331. data = (cmark_list *)calloc(1, sizeof(*data));
  332. if(data == NULL) {
  333. return 0;
  334. } else {
  335. data->marker_offset = 0; // will be adjusted later
  336. data->list_type = CMARK_ORDERED_LIST;
  337. data->bullet_char = 0;
  338. data->start = start;
  339. data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
  340. data->tight = false;
  341. }
  342. } else {
  343. return 0;
  344. }
  345. } else {
  346. return 0;
  347. }
  348. *dataptr = data;
  349. return (pos - startpos);
  350. }
  351. // Return 1 if list item belongs in list, else 0.
  352. static int lists_match(cmark_list *list_data, cmark_list *item_data)
  353. {
  354. return (list_data->list_type == item_data->list_type &&
  355. list_data->delimiter == item_data->delimiter &&
  356. // list_data->marker_offset == item_data.marker_offset &&
  357. list_data->bullet_char == item_data->bullet_char);
  358. }
  359. static cmark_node *finalize_document(cmark_doc_parser *parser)
  360. {
  361. while (parser->current != parser->root) {
  362. finalize(parser, parser->current, parser->line_number);
  363. parser->current = parser->current->parent;
  364. }
  365. finalize(parser, parser->root, parser->line_number);
  366. process_inlines(parser->root, parser->refmap);
  367. return parser->root;
  368. }
  369. extern cmark_node *cmark_parse_file(FILE *f)
  370. {
  371. unsigned char buffer[4096];
  372. cmark_doc_parser *parser = cmark_new_doc_parser();
  373. size_t offset;
  374. cmark_node *document;
  375. while (fgets((char *)buffer, sizeof(buffer), f)) {
  376. offset = strlen((char *)buffer);
  377. cmark_process_line(parser, buffer, offset);
  378. }
  379. document = cmark_finish(parser);
  380. cmark_free_doc_parser(parser);
  381. return document;
  382. }
  383. extern cmark_node *cmark_parse_document(const unsigned char *buffer, size_t len)
  384. {
  385. int linenum = 1;
  386. const unsigned char *end = buffer + len;
  387. size_t offset;
  388. cmark_doc_parser *parser = cmark_new_doc_parser();
  389. cmark_node *document;
  390. while (buffer < end) {
  391. const unsigned char *eol = (unsigned char *)memchr(buffer, '\n', end - buffer);
  392. offset = eol ? (eol - buffer) + 1 : eol - buffer;
  393. cmark_process_line(parser, buffer, offset);
  394. buffer += offset;
  395. linenum++;
  396. }
  397. document = cmark_finish(parser);
  398. cmark_free_doc_parser(parser);
  399. return document;
  400. }
  401. static void chop_trailing_hashtags(chunk *ch)
  402. {
  403. int n, orig_n;
  404. chunk_rtrim(ch);
  405. orig_n = n = ch->len - 1;
  406. // if string ends in space followed by #s, remove these:
  407. while (n >= 0 && peek_at(ch, n) == '#')
  408. n--;
  409. // Check for a be a space before the final #s:
  410. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  411. ch->len = n;
  412. chunk_rtrim(ch);
  413. }
  414. }
  415. void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer,
  416. size_t bytes)
  417. {
  418. cmark_node* last_matched_container;
  419. int offset = 0;
  420. int matched = 0;
  421. int lev = 0;
  422. int i;
  423. cmark_list *data = NULL;
  424. bool all_matched = true;
  425. cmark_node* container;
  426. cmark_node* cur = parser->current;
  427. bool blank = false;
  428. int first_nonspace;
  429. int indent;
  430. chunk input;
  431. utf8proc_detab(parser->curline, buffer, bytes);
  432. // Add a newline to the end if not present:
  433. // TODO this breaks abstraction:
  434. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  435. strbuf_putc(parser->curline, '\n');
  436. }
  437. input.data = parser->curline->ptr;
  438. input.len = parser->curline->size;
  439. // container starts at the document root.
  440. container = parser->root;
  441. parser->line_number++;
  442. // for each containing cmark_node, try to parse the associated line start.
  443. // bail out on failure: container will point to the last matching cmark_node.
  444. while (container->last_child && container->last_child->open) {
  445. container = container->last_child;
  446. first_nonspace = offset;
  447. while (peek_at(&input, first_nonspace) == ' ') {
  448. first_nonspace++;
  449. }
  450. indent = first_nonspace - offset;
  451. blank = peek_at(&input, first_nonspace) == '\n';
  452. if (container->type == NODE_BQUOTE) {
  453. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  454. if (matched) {
  455. offset = first_nonspace + 1;
  456. if (peek_at(&input, offset) == ' ')
  457. offset++;
  458. } else {
  459. all_matched = false;
  460. }
  461. } else if (container->type == NODE_LIST_ITEM) {
  462. if (indent >= container->as.list.marker_offset +
  463. container->as.list.padding) {
  464. offset += container->as.list.marker_offset +
  465. container->as.list.padding;
  466. } else if (blank) {
  467. offset = first_nonspace;
  468. } else {
  469. all_matched = false;
  470. }
  471. } else if (container->type == NODE_INDENTED_CODE) {
  472. if (indent >= CODE_INDENT) {
  473. offset += CODE_INDENT;
  474. } else if (blank) {
  475. offset = first_nonspace;
  476. } else {
  477. all_matched = false;
  478. }
  479. } else if (container->type == NODE_ATX_HEADER ||
  480. container->type == NODE_SETEXT_HEADER) {
  481. // a header can never contain more than one line
  482. all_matched = false;
  483. } else if (container->type == NODE_FENCED_CODE) {
  484. // skip optional spaces of fence offset
  485. i = container->as.code.fence_offset;
  486. while (i > 0 && peek_at(&input, offset) == ' ') {
  487. offset++;
  488. i--;
  489. }
  490. } else if (container->type == NODE_HTML) {
  491. if (blank) {
  492. all_matched = false;
  493. }
  494. } else if (container->type == NODE_PARAGRAPH) {
  495. if (blank) {
  496. container->last_line_blank = true;
  497. all_matched = false;
  498. }
  499. }
  500. if (!all_matched) {
  501. container = container->parent; // back up to last matching cmark_node
  502. break;
  503. }
  504. }
  505. last_matched_container = container;
  506. // check to see if we've hit 2nd blank line, break out of list:
  507. if (blank && container->last_line_blank) {
  508. break_out_of_lists(parser, &container, parser->line_number);
  509. }
  510. // unless last matched container is code cmark_node, try new container starts:
  511. while (container->type != NODE_FENCED_CODE && container->type != NODE_INDENTED_CODE &&
  512. container->type != NODE_HTML) {
  513. first_nonspace = offset;
  514. while (peek_at(&input, first_nonspace) == ' ')
  515. first_nonspace++;
  516. indent = first_nonspace - offset;
  517. blank = peek_at(&input, first_nonspace) == '\n';
  518. if (indent >= CODE_INDENT) {
  519. if (cur->type != NODE_PARAGRAPH && !blank) {
  520. offset += CODE_INDENT;
  521. container = add_child(parser, container, NODE_INDENTED_CODE, parser->line_number, offset + 1);
  522. } else { // indent > 4 in lazy line
  523. break;
  524. }
  525. } else if (peek_at(&input, first_nonspace) == '>') {
  526. offset = first_nonspace + 1;
  527. // optional following character
  528. if (peek_at(&input, offset) == ' ')
  529. offset++;
  530. container = add_child(parser, container, NODE_BQUOTE, parser->line_number, offset + 1);
  531. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  532. offset = first_nonspace + matched;
  533. container = add_child(parser, container, NODE_ATX_HEADER, parser->line_number, offset + 1);
  534. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  535. int level = 0;
  536. while (peek_at(&input, hashpos) == '#') {
  537. level++;
  538. hashpos++;
  539. }
  540. container->as.header.level = level;
  541. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  542. container = add_child(parser, container, NODE_FENCED_CODE, parser->line_number, first_nonspace + 1);
  543. container->as.code.fence_char = peek_at(&input, first_nonspace);
  544. container->as.code.fence_length = matched;
  545. container->as.code.fence_offset = first_nonspace - offset;
  546. offset = first_nonspace + matched;
  547. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  548. container = add_child(parser, container, NODE_HTML, parser->line_number, first_nonspace + 1);
  549. // note, we don't adjust offset because the tag is part of the text
  550. } else if (container->type == NODE_PARAGRAPH &&
  551. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  552. // check that there is only one line in the paragraph:
  553. strbuf_strrchr(&container->string_content, '\n',
  554. strbuf_len(&container->string_content) - 2) < 0) {
  555. container->type = NODE_SETEXT_HEADER;
  556. container->as.header.level = lev;
  557. offset = input.len - 1;
  558. } else if (!(container->type == NODE_PARAGRAPH && !all_matched) &&
  559. (matched = scan_hrule(&input, first_nonspace))) {
  560. // it's only now that we know the line is not part of a setext header:
  561. container = add_child(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
  562. finalize(parser, container, parser->line_number);
  563. container = container->parent;
  564. offset = input.len - 1;
  565. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  566. // compute padding:
  567. offset = first_nonspace + matched;
  568. i = 0;
  569. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  570. i++;
  571. }
  572. // i = number of spaces after marker, up to 5
  573. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  574. data->padding = matched + 1;
  575. if (i > 0) {
  576. offset += 1;
  577. }
  578. } else {
  579. data->padding = matched + i;
  580. offset += i;
  581. }
  582. // check container; if it's a list, see if this list item
  583. // can continue the list; otherwise, create a list container.
  584. data->marker_offset = indent;
  585. if (container->type != NODE_LIST ||
  586. !lists_match(&container->as.list, data)) {
  587. container = add_child(parser, container, NODE_LIST, parser->line_number,
  588. first_nonspace + 1);
  589. memcpy(&container->as.list, data, sizeof(*data));
  590. }
  591. // add the list item
  592. container = add_child(parser, container, NODE_LIST_ITEM, parser->line_number,
  593. first_nonspace + 1);
  594. /* TODO: static */
  595. memcpy(&container->as.list, data, sizeof(*data));
  596. free(data);
  597. } else {
  598. break;
  599. }
  600. if (accepts_lines(container->type)) {
  601. // if it's a line container, it can't contain other containers
  602. break;
  603. }
  604. }
  605. // what remains at offset is a text line. add the text to the
  606. // appropriate container.
  607. first_nonspace = offset;
  608. while (peek_at(&input, first_nonspace) == ' ')
  609. first_nonspace++;
  610. indent = first_nonspace - offset;
  611. blank = peek_at(&input, first_nonspace) == '\n';
  612. // cmark_node quote lines are never blank as they start with >
  613. // and we don't count blanks in fenced code for purposes of tight/loose
  614. // lists or breaking out of lists. we also don't set last_line_blank
  615. // on an empty list item.
  616. container->last_line_blank = (blank &&
  617. container->type != NODE_BQUOTE &&
  618. container->type != NODE_FENCED_CODE &&
  619. !(container->type == NODE_LIST_ITEM &&
  620. container->first_child == NULL &&
  621. container->start_line == parser->line_number));
  622. cmark_node *cont = container;
  623. while (cont->parent) {
  624. cont->parent->last_line_blank = false;
  625. cont = cont->parent;
  626. }
  627. if (cur != last_matched_container &&
  628. container == last_matched_container &&
  629. !blank &&
  630. cur->type == NODE_PARAGRAPH &&
  631. strbuf_len(&cur->string_content) > 0) {
  632. add_line(cur, &input, offset);
  633. } else { // not a lazy continuation
  634. // finalize any blocks that were not matched and set cur to container:
  635. while (cur != last_matched_container) {
  636. finalize(parser, cur, parser->line_number);
  637. cur = cur->parent;
  638. assert(cur != NULL);
  639. }
  640. if (container->type == NODE_INDENTED_CODE) {
  641. add_line(container, &input, offset);
  642. } else if (container->type == NODE_FENCED_CODE) {
  643. matched = 0;
  644. if (indent <= 3 &&
  645. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  646. int fence_len = scan_close_code_fence(&input, first_nonspace);
  647. if (fence_len > container->as.code.fence_length)
  648. matched = 1;
  649. }
  650. if (matched) {
  651. // if closing fence, don't add line to container; instead, close it:
  652. finalize(parser, container, parser->line_number);
  653. container = container->parent; // back up to parent
  654. } else {
  655. add_line(container, &input, offset);
  656. }
  657. } else if (container->type == NODE_HTML) {
  658. add_line(container, &input, offset);
  659. } else if (blank) {
  660. // ??? do nothing
  661. } else if (container->type == NODE_ATX_HEADER) {
  662. chop_trailing_hashtags(&input);
  663. add_line(container, &input, first_nonspace);
  664. finalize(parser, container, parser->line_number);
  665. container = container->parent;
  666. } else if (accepts_lines(container->type)) {
  667. add_line(container, &input, first_nonspace);
  668. } else if (container->type != NODE_HRULE && container->type != NODE_SETEXT_HEADER) {
  669. // create paragraph container for line
  670. container = add_child(parser, container, NODE_PARAGRAPH, parser->line_number, first_nonspace + 1);
  671. add_line(container, &input, first_nonspace);
  672. } else {
  673. assert(false);
  674. }
  675. parser->current = container;
  676. }
  677. strbuf_clear(parser->curline);
  678. }
  679. cmark_node *cmark_finish(cmark_doc_parser *parser)
  680. {
  681. finalize_document(parser);
  682. strbuf_free(parser->curline);
  683. #if CMARK_DEBUG_NODES
  684. if (cmark_node_check(parser->root)) {
  685. abort();
  686. }
  687. #endif
  688. return parser->root;
  689. }