aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 6f995b3d4257e7139580f2dee75592d8d13022c5 (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_CODE_BLOCK);
  87. }
  88. static void add_line(cmark_node* cmark_node, chunk *ch, int offset)
  89. {
  90. assert(cmark_node->open);
  91. strbuf_put(&cmark_node->string_content, ch->data + offset, ch->len - offset);
  92. }
  93. static void remove_trailing_blank_lines(strbuf *ln)
  94. {
  95. int i;
  96. for (i = ln->size - 1; i >= 0; --i) {
  97. unsigned char c = ln->ptr[i];
  98. if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
  99. break;
  100. }
  101. if (i < 0) {
  102. strbuf_clear(ln);
  103. return;
  104. }
  105. i = strbuf_strchr(ln, '\n', i);
  106. if (i >= 0)
  107. strbuf_truncate(ln, i);
  108. }
  109. // Check to see if a cmark_node ends with a blank line, descending
  110. // if needed into lists and sublists.
  111. static bool ends_with_blank_line(cmark_node* cmark_node)
  112. {
  113. if (cmark_node->last_line_blank) {
  114. return true;
  115. }
  116. if ((cmark_node->type == NODE_LIST || cmark_node->type == NODE_LIST_ITEM) && cmark_node->last_child) {
  117. return ends_with_blank_line(cmark_node->last_child);
  118. } else {
  119. return false;
  120. }
  121. }
  122. // Break out of all containing lists
  123. static int break_out_of_lists(cmark_doc_parser *parser, cmark_node ** bptr, int line_number)
  124. {
  125. cmark_node *container = *bptr;
  126. cmark_node *b = parser->root;
  127. // find first containing NODE_LIST:
  128. while (b && b->type != NODE_LIST) {
  129. b = b->last_child;
  130. }
  131. if (b) {
  132. while (container && container != b) {
  133. finalize(parser, container, line_number);
  134. container = container->parent;
  135. }
  136. finalize(parser, b, line_number);
  137. *bptr = b->parent;
  138. }
  139. return 0;
  140. }
  141. static void finalize(cmark_doc_parser *parser, cmark_node* b, int line_number)
  142. {
  143. int firstlinelen;
  144. int pos;
  145. cmark_node* item;
  146. cmark_node* subitem;
  147. if (!b->open)
  148. return; // don't do anything if the cmark_node is already closed
  149. b->open = false;
  150. if (line_number > b->start_line) {
  151. b->end_line = line_number - 1;
  152. } else {
  153. b->end_line = line_number;
  154. }
  155. switch (b->type) {
  156. case NODE_PARAGRAPH:
  157. pos = 0;
  158. while (strbuf_at(&b->string_content, 0) == '[' &&
  159. (pos = parse_reference_inline(&b->string_content, parser->refmap))) {
  160. strbuf_drop(&b->string_content, pos);
  161. }
  162. if (is_blank(&b->string_content, 0)) {
  163. b->type = NODE_REFERENCE_DEF;
  164. }
  165. break;
  166. case NODE_CODE_BLOCK:
  167. if (!b->as.code.fenced) { // indented code
  168. remove_trailing_blank_lines(&b->string_content);
  169. strbuf_putc(&b->string_content, '\n');
  170. break;
  171. } else {
  172. // first line of contents becomes info
  173. firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
  174. houdini_unescape_html_f(
  175. &b->as.code.info,
  176. b->string_content.ptr,
  177. firstlinelen
  178. );
  179. strbuf_drop(&b->string_content, firstlinelen + 1);
  180. strbuf_trim(&b->as.code.info);
  181. strbuf_unescape(&b->as.code.info);
  182. break;
  183. }
  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
  378. = (const char *)memchr(buffer, '\n', end - buffer);
  379. offset = eol ? (eol - buffer) + 1 : end - 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 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, (unsigned char *)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_BLOCK_QUOTE) {
  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_CODE_BLOCK) {
  459. if (!container->as.code.fenced) { // indented
  460. if (indent >= CODE_INDENT) {
  461. offset += CODE_INDENT;
  462. } else if (blank) {
  463. offset = first_nonspace;
  464. } else {
  465. all_matched = false;
  466. }
  467. } else {
  468. // skip optional spaces of fence offset
  469. i = container->as.code.fence_offset;
  470. while (i > 0 && peek_at(&input, offset) == ' ') {
  471. offset++;
  472. i--;
  473. }
  474. }
  475. } else if (container->type == NODE_HEADER) {
  476. // a header can never contain more than one line
  477. all_matched = false;
  478. if (blank) {
  479. container->last_line_blank = true;
  480. }
  481. } else if (container->type == NODE_HTML) {
  482. if (blank) {
  483. container->last_line_blank = true;
  484. all_matched = false;
  485. }
  486. } else if (container->type == NODE_PARAGRAPH) {
  487. if (blank) {
  488. container->last_line_blank = true;
  489. all_matched = false;
  490. }
  491. }
  492. if (!all_matched) {
  493. container = container->parent; // back up to last matching cmark_node
  494. break;
  495. }
  496. }
  497. last_matched_container = container;
  498. // check to see if we've hit 2nd blank line, break out of list:
  499. if (blank && container->last_line_blank) {
  500. break_out_of_lists(parser, &container, parser->line_number);
  501. }
  502. // unless last matched container is code cmark_node, try new container starts:
  503. while (container->type != NODE_CODE_BLOCK &&
  504. container->type != NODE_HTML) {
  505. first_nonspace = offset;
  506. while (peek_at(&input, first_nonspace) == ' ')
  507. first_nonspace++;
  508. indent = first_nonspace - offset;
  509. blank = peek_at(&input, first_nonspace) == '\n';
  510. if (indent >= CODE_INDENT) {
  511. if (cur->type != NODE_PARAGRAPH && !blank) {
  512. offset += CODE_INDENT;
  513. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, offset + 1);
  514. container->as.code.fenced = false;
  515. container->as.code.fence_char = 0;
  516. container->as.code.fence_length = 0;
  517. container->as.code.fence_offset = 0;
  518. strbuf_init(&container->as.code.info, 0);
  519. } else { // indent > 4 in lazy line
  520. break;
  521. }
  522. } else if (peek_at(&input, first_nonspace) == '>') {
  523. offset = first_nonspace + 1;
  524. // optional following character
  525. if (peek_at(&input, offset) == ' ')
  526. offset++;
  527. container = add_child(parser, container, NODE_BLOCK_QUOTE, parser->line_number, offset + 1);
  528. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  529. offset = first_nonspace + matched;
  530. container = add_child(parser, container, NODE_HEADER, parser->line_number, offset + 1);
  531. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  532. int level = 0;
  533. while (peek_at(&input, hashpos) == '#') {
  534. level++;
  535. hashpos++;
  536. }
  537. container->as.header.level = level;
  538. container->as.header.setext = false;
  539. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  540. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, first_nonspace + 1);
  541. container->as.code.fenced = true;
  542. container->as.code.fence_char = peek_at(&input, first_nonspace);
  543. container->as.code.fence_length = matched;
  544. container->as.code.fence_offset = first_nonspace - offset;
  545. strbuf_init(&container->as.code.info, 0);
  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_HEADER;
  556. container->as.header.level = lev;
  557. container->as.header.setext = true;
  558. offset = input.len - 1;
  559. } else if (!(container->type == NODE_PARAGRAPH && !all_matched) &&
  560. (matched = scan_hrule(&input, first_nonspace))) {
  561. // it's only now that we know the line is not part of a setext header:
  562. container = add_child(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
  563. finalize(parser, container, parser->line_number);
  564. container = container->parent;
  565. offset = input.len - 1;
  566. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  567. // compute padding:
  568. offset = first_nonspace + matched;
  569. i = 0;
  570. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  571. i++;
  572. }
  573. // i = number of spaces after marker, up to 5
  574. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  575. data->padding = matched + 1;
  576. if (i > 0) {
  577. offset += 1;
  578. }
  579. } else {
  580. data->padding = matched + i;
  581. offset += i;
  582. }
  583. // check container; if it's a list, see if this list item
  584. // can continue the list; otherwise, create a list container.
  585. data->marker_offset = indent;
  586. if (container->type != NODE_LIST ||
  587. !lists_match(&container->as.list, data)) {
  588. container = add_child(parser, container, NODE_LIST, parser->line_number,
  589. first_nonspace + 1);
  590. memcpy(&container->as.list, data, sizeof(*data));
  591. }
  592. // add the list item
  593. container = add_child(parser, container, NODE_LIST_ITEM, parser->line_number,
  594. first_nonspace + 1);
  595. /* TODO: static */
  596. memcpy(&container->as.list, data, sizeof(*data));
  597. free(data);
  598. } else {
  599. break;
  600. }
  601. if (accepts_lines(container->type)) {
  602. // if it's a line container, it can't contain other containers
  603. break;
  604. }
  605. }
  606. // what remains at offset is a text line. add the text to the
  607. // appropriate container.
  608. first_nonspace = offset;
  609. while (peek_at(&input, first_nonspace) == ' ')
  610. first_nonspace++;
  611. indent = first_nonspace - offset;
  612. blank = peek_at(&input, first_nonspace) == '\n';
  613. // cmark_node quote lines are never blank as they start with >
  614. // and we don't count blanks in fenced code for purposes of tight/loose
  615. // lists or breaking out of lists. we also don't set last_line_blank
  616. // on an empty list item.
  617. container->last_line_blank = (blank &&
  618. container->type != NODE_BLOCK_QUOTE &&
  619. container->type != NODE_HEADER &&
  620. (container->type != NODE_CODE_BLOCK &&
  621. container->as.code.fenced) &&
  622. !(container->type == NODE_LIST_ITEM &&
  623. container->first_child == NULL &&
  624. container->start_line == parser->line_number));
  625. cmark_node *cont = container;
  626. while (cont->parent) {
  627. cont->parent->last_line_blank = false;
  628. cont = cont->parent;
  629. }
  630. if (cur != last_matched_container &&
  631. container == last_matched_container &&
  632. !blank &&
  633. cur->type == NODE_PARAGRAPH &&
  634. strbuf_len(&cur->string_content) > 0) {
  635. add_line(cur, &input, offset);
  636. } else { // not a lazy continuation
  637. // finalize any blocks that were not matched and set cur to container:
  638. while (cur != last_matched_container) {
  639. finalize(parser, cur, parser->line_number);
  640. cur = cur->parent;
  641. assert(cur != NULL);
  642. }
  643. if (container->type == NODE_CODE_BLOCK &&
  644. !container->as.code.fenced) {
  645. add_line(container, &input, offset);
  646. } else if (container->type == NODE_CODE_BLOCK &&
  647. container->as.code.fenced) {
  648. matched = 0;
  649. if (indent <= 3 &&
  650. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  651. int fence_len = scan_close_code_fence(&input, first_nonspace);
  652. if (fence_len > container->as.code.fence_length)
  653. matched = 1;
  654. }
  655. if (matched) {
  656. // if closing fence, don't add line to container; instead, close it:
  657. finalize(parser, container, parser->line_number);
  658. container = container->parent; // back up to parent
  659. } else {
  660. add_line(container, &input, offset);
  661. }
  662. } else if (container->type == NODE_HTML) {
  663. add_line(container, &input, offset);
  664. } else if (blank) {
  665. // ??? do nothing
  666. } else if (container->type == NODE_HEADER) {
  667. chop_trailing_hashtags(&input);
  668. add_line(container, &input, first_nonspace);
  669. finalize(parser, container, parser->line_number);
  670. container = container->parent;
  671. } else if (accepts_lines(container->type)) {
  672. add_line(container, &input, first_nonspace);
  673. } else if (container->type != NODE_HRULE &&
  674. container->type != NODE_HEADER) {
  675. // create paragraph container for line
  676. container = add_child(parser, container, NODE_PARAGRAPH, parser->line_number, first_nonspace + 1);
  677. add_line(container, &input, first_nonspace);
  678. } else {
  679. assert(false);
  680. }
  681. parser->current = container;
  682. }
  683. strbuf_clear(parser->curline);
  684. }
  685. cmark_node *cmark_finish(cmark_doc_parser *parser)
  686. {
  687. finalize_document(parser);
  688. strbuf_free(parser->curline);
  689. #if CMARK_DEBUG_NODES
  690. if (cmark_node_check(parser->root, stderr)) {
  691. abort();
  692. }
  693. #endif
  694. return parser->root;
  695. }