aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 1bdf67b00e1ae08c4dacfbc63946611d7d43a48c (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_parser *cmark_parser_new()
  39. {
  40. cmark_parser *parser = (cmark_parser*)malloc(sizeof(cmark_parser));
  41. cmark_node *document = make_document();
  42. strbuf *line = (strbuf*)malloc(sizeof(strbuf));
  43. cmark_strbuf_init(line, 256);
  44. parser->refmap = cmark_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_parser_free(cmark_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_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_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_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. while (strbuf_at(&b->string_content, 0) == '[' &&
  158. (pos = cmark_parse_reference_inline(&b->string_content, parser->refmap))) {
  159. strbuf_drop(&b->string_content, pos);
  160. }
  161. if (is_blank(&b->string_content, 0)) {
  162. b->type = NODE_REFERENCE_DEF;
  163. }
  164. break;
  165. case NODE_CODE_BLOCK:
  166. if (!b->as.code.fenced) { // indented code
  167. remove_trailing_blank_lines(&b->string_content);
  168. strbuf_putc(&b->string_content, '\n');
  169. break;
  170. } else {
  171. // first line of contents becomes info
  172. firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
  173. houdini_unescape_html_f(
  174. &b->as.code.info,
  175. b->string_content.ptr,
  176. firstlinelen
  177. );
  178. strbuf_drop(&b->string_content, firstlinelen + 1);
  179. strbuf_trim(&b->as.code.info);
  180. strbuf_unescape(&b->as.code.info);
  181. break;
  182. }
  183. case NODE_LIST: // determine tight/loose status
  184. b->as.list.tight = true; // tight by default
  185. item = b->first_child;
  186. while (item) {
  187. // check for non-final non-empty list item ending with blank line:
  188. if (item->last_line_blank && item->next) {
  189. b->as.list.tight = false;
  190. break;
  191. }
  192. // recurse into children of list item, to see if there are
  193. // spaces between them:
  194. subitem = item->first_child;
  195. while (subitem) {
  196. if (ends_with_blank_line(subitem) &&
  197. (item->next || subitem->next)) {
  198. b->as.list.tight = false;
  199. break;
  200. }
  201. subitem = subitem->next;
  202. }
  203. if (!(b->as.list.tight)) {
  204. break;
  205. }
  206. item = item->next;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. // Add a cmark_node as child of another. Return pointer to child.
  214. static cmark_node* add_child(cmark_parser *parser, cmark_node* parent,
  215. cmark_node_type block_type, int start_line, int start_column)
  216. {
  217. assert(parent);
  218. // if 'parent' isn't the kind of cmark_node that can accept this child,
  219. // then back up til we hit a cmark_node that can.
  220. while (!can_contain(parent->type, block_type)) {
  221. finalize(parser, parent, start_line);
  222. parent = parent->parent;
  223. }
  224. cmark_node* child = make_block(block_type, start_line, start_column);
  225. child->parent = parent;
  226. if (parent->last_child) {
  227. parent->last_child->next = child;
  228. child->prev = parent->last_child;
  229. } else {
  230. parent->first_child = child;
  231. child->prev = NULL;
  232. }
  233. parent->last_child = child;
  234. return child;
  235. }
  236. typedef struct BlockStack {
  237. struct BlockStack *previous;
  238. cmark_node *next_sibling;
  239. } block_stack;
  240. // Walk through cmark_node and all children, recursively, parsing
  241. // string content into inline content where appropriate.
  242. static void process_inlines(cmark_node* cur, cmark_reference_map *refmap)
  243. {
  244. block_stack* stack = NULL;
  245. block_stack* newstack = NULL;
  246. while (cur != NULL) {
  247. switch (cur->type) {
  248. case NODE_PARAGRAPH:
  249. case NODE_HEADER:
  250. cmark_parse_inlines(cur, refmap);
  251. break;
  252. default:
  253. break;
  254. }
  255. if (cur->first_child) {
  256. newstack = (block_stack*)malloc(sizeof(block_stack));
  257. if (newstack == NULL) break;
  258. newstack->previous = stack;
  259. stack = newstack;
  260. stack->next_sibling = cur->next;
  261. cur = cur->first_child;
  262. } else {
  263. cur = cur->next;
  264. }
  265. while (cur == NULL && stack != NULL) {
  266. cur = stack->next_sibling;
  267. newstack = stack->previous;
  268. free(stack);
  269. stack = newstack;
  270. }
  271. }
  272. while (stack != NULL) {
  273. newstack = stack->previous;
  274. free(stack);
  275. stack = newstack;
  276. }
  277. }
  278. // Attempts to parse a list item marker (bullet or enumerated).
  279. // On success, returns length of the marker, and populates
  280. // data with the details. On failure, returns 0.
  281. static int parse_list_marker(chunk *input, int pos, cmark_list **dataptr)
  282. {
  283. unsigned char c;
  284. int startpos;
  285. cmark_list *data;
  286. startpos = pos;
  287. c = peek_at(input, pos);
  288. if ((c == '*' || c == '-' || c == '+') && !scan_hrule(input, pos)) {
  289. pos++;
  290. if (!isspace(peek_at(input, pos))) {
  291. return 0;
  292. }
  293. data = (cmark_list *)calloc(1, sizeof(*data));
  294. if(data == NULL) {
  295. return 0;
  296. } else {
  297. data->marker_offset = 0; // will be adjusted later
  298. data->list_type = CMARK_BULLET_LIST;
  299. data->bullet_char = c;
  300. data->start = 1;
  301. data->delimiter = CMARK_PERIOD_DELIM;
  302. data->tight = false;
  303. }
  304. } else if (isdigit(c)) {
  305. int start = 0;
  306. do {
  307. start = (10 * start) + (peek_at(input, pos) - '0');
  308. pos++;
  309. } while (isdigit(peek_at(input, pos)));
  310. c = peek_at(input, pos);
  311. if (c == '.' || c == ')') {
  312. pos++;
  313. if (!isspace(peek_at(input, pos))) {
  314. return 0;
  315. }
  316. data = (cmark_list *)calloc(1, sizeof(*data));
  317. if(data == NULL) {
  318. return 0;
  319. } else {
  320. data->marker_offset = 0; // will be adjusted later
  321. data->list_type = CMARK_ORDERED_LIST;
  322. data->bullet_char = 0;
  323. data->start = start;
  324. data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
  325. data->tight = false;
  326. }
  327. } else {
  328. return 0;
  329. }
  330. } else {
  331. return 0;
  332. }
  333. *dataptr = data;
  334. return (pos - startpos);
  335. }
  336. // Return 1 if list item belongs in list, else 0.
  337. static int lists_match(cmark_list *list_data, cmark_list *item_data)
  338. {
  339. return (list_data->list_type == item_data->list_type &&
  340. list_data->delimiter == item_data->delimiter &&
  341. // list_data->marker_offset == item_data.marker_offset &&
  342. list_data->bullet_char == item_data->bullet_char);
  343. }
  344. static cmark_node *finalize_document(cmark_parser *parser)
  345. {
  346. while (parser->current != parser->root) {
  347. finalize(parser, parser->current, parser->line_number);
  348. parser->current = parser->current->parent;
  349. }
  350. finalize(parser, parser->root, parser->line_number);
  351. process_inlines(parser->root, parser->refmap);
  352. return parser->root;
  353. }
  354. extern cmark_node *cmark_parse_file(FILE *f)
  355. {
  356. char buffer[4096];
  357. cmark_parser *parser = cmark_parser_new();
  358. size_t offset;
  359. cmark_node *document;
  360. while (fgets(buffer, sizeof(buffer), f)) {
  361. offset = strlen(buffer);
  362. cmark_parser_process_line(parser, buffer, offset);
  363. }
  364. document = cmark_parser_finish(parser);
  365. cmark_parser_free(parser);
  366. return document;
  367. }
  368. extern cmark_node *cmark_parse_document(const char *buffer, size_t len)
  369. {
  370. int linenum = 1;
  371. const char *end = buffer + len;
  372. size_t offset;
  373. cmark_parser *parser = cmark_parser_new();
  374. cmark_node *document;
  375. while (buffer < end) {
  376. const char *eol
  377. = (const char *)memchr(buffer, '\n', end - buffer);
  378. offset = eol ? (eol - buffer) + 1 : end - buffer;
  379. cmark_parser_process_line(parser, buffer, offset);
  380. buffer += offset;
  381. linenum++;
  382. }
  383. document = cmark_parser_finish(parser);
  384. cmark_parser_free(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_parser_process_line(cmark_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_CODE_BLOCK) {
  458. if (!container->as.code.fenced) { // indented
  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 {
  467. // skip optional spaces of fence offset
  468. i = container->as.code.fence_offset;
  469. while (i > 0 && peek_at(&input, offset) == ' ') {
  470. offset++;
  471. i--;
  472. }
  473. }
  474. } else if (container->type == NODE_HEADER) {
  475. // a header can never contain more than one line
  476. all_matched = false;
  477. if (blank) {
  478. container->last_line_blank = true;
  479. }
  480. } else if (container->type == NODE_HTML) {
  481. if (blank) {
  482. container->last_line_blank = true;
  483. all_matched = false;
  484. }
  485. } else if (container->type == NODE_PARAGRAPH) {
  486. if (blank) {
  487. container->last_line_blank = true;
  488. all_matched = false;
  489. }
  490. }
  491. if (!all_matched) {
  492. container = container->parent; // back up to last matching cmark_node
  493. break;
  494. }
  495. }
  496. last_matched_container = container;
  497. // check to see if we've hit 2nd blank line, break out of list:
  498. if (blank && container->last_line_blank) {
  499. break_out_of_lists(parser, &container, parser->line_number);
  500. }
  501. // unless last matched container is code cmark_node, try new container starts:
  502. while (container->type != NODE_CODE_BLOCK &&
  503. container->type != NODE_HTML) {
  504. first_nonspace = offset;
  505. while (peek_at(&input, first_nonspace) == ' ')
  506. first_nonspace++;
  507. indent = first_nonspace - offset;
  508. blank = peek_at(&input, first_nonspace) == '\n';
  509. if (indent >= CODE_INDENT) {
  510. if (cur->type != NODE_PARAGRAPH && !blank) {
  511. offset += CODE_INDENT;
  512. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, offset + 1);
  513. container->as.code.fenced = false;
  514. container->as.code.fence_char = 0;
  515. container->as.code.fence_length = 0;
  516. container->as.code.fence_offset = 0;
  517. strbuf_init(&container->as.code.info, 0);
  518. } else { // indent > 4 in lazy line
  519. break;
  520. }
  521. } else if (peek_at(&input, first_nonspace) == '>') {
  522. offset = first_nonspace + 1;
  523. // optional following character
  524. if (peek_at(&input, offset) == ' ')
  525. offset++;
  526. container = add_child(parser, container, NODE_BLOCK_QUOTE, parser->line_number, offset + 1);
  527. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  528. offset = first_nonspace + matched;
  529. container = add_child(parser, container, NODE_HEADER, parser->line_number, offset + 1);
  530. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  531. int level = 0;
  532. while (peek_at(&input, hashpos) == '#') {
  533. level++;
  534. hashpos++;
  535. }
  536. container->as.header.level = level;
  537. container->as.header.setext = false;
  538. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  539. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, first_nonspace + 1);
  540. container->as.code.fenced = true;
  541. container->as.code.fence_char = peek_at(&input, first_nonspace);
  542. container->as.code.fence_length = matched;
  543. container->as.code.fence_offset = first_nonspace - offset;
  544. strbuf_init(&container->as.code.info, 0);
  545. offset = first_nonspace + matched;
  546. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  547. container = add_child(parser, container, NODE_HTML, parser->line_number, first_nonspace + 1);
  548. // note, we don't adjust offset because the tag is part of the text
  549. } else if (container->type == NODE_PARAGRAPH &&
  550. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  551. // check that there is only one line in the paragraph:
  552. strbuf_strrchr(&container->string_content, '\n',
  553. strbuf_len(&container->string_content) - 2) < 0) {
  554. container->type = NODE_HEADER;
  555. container->as.header.level = lev;
  556. container->as.header.setext = true;
  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_BLOCK_QUOTE &&
  618. container->type != NODE_HEADER &&
  619. (container->type != NODE_CODE_BLOCK &&
  620. container->as.code.fenced) &&
  621. !(container->type == NODE_LIST_ITEM &&
  622. container->first_child == NULL &&
  623. container->start_line == parser->line_number));
  624. cmark_node *cont = container;
  625. while (cont->parent) {
  626. cont->parent->last_line_blank = false;
  627. cont = cont->parent;
  628. }
  629. if (cur != last_matched_container &&
  630. container == last_matched_container &&
  631. !blank &&
  632. cur->type == NODE_PARAGRAPH &&
  633. strbuf_len(&cur->string_content) > 0) {
  634. add_line(cur, &input, offset);
  635. } else { // not a lazy continuation
  636. // finalize any blocks that were not matched and set cur to container:
  637. while (cur != last_matched_container) {
  638. finalize(parser, cur, parser->line_number);
  639. cur = cur->parent;
  640. assert(cur != NULL);
  641. }
  642. if (container->type == NODE_CODE_BLOCK &&
  643. !container->as.code.fenced) {
  644. add_line(container, &input, offset);
  645. } else if (container->type == NODE_CODE_BLOCK &&
  646. container->as.code.fenced) {
  647. matched = 0;
  648. if (indent <= 3 &&
  649. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  650. int fence_len = scan_close_code_fence(&input, first_nonspace);
  651. if (fence_len > container->as.code.fence_length)
  652. matched = 1;
  653. }
  654. if (matched) {
  655. // if closing fence, don't add line to container; instead, close it:
  656. finalize(parser, container, parser->line_number);
  657. container = container->parent; // back up to parent
  658. } else {
  659. add_line(container, &input, offset);
  660. }
  661. } else if (container->type == NODE_HTML) {
  662. add_line(container, &input, offset);
  663. } else if (blank) {
  664. // ??? do nothing
  665. } else if (container->type == NODE_HEADER) {
  666. chop_trailing_hashtags(&input);
  667. add_line(container, &input, first_nonspace);
  668. finalize(parser, container, parser->line_number);
  669. container = container->parent;
  670. } else if (accepts_lines(container->type)) {
  671. add_line(container, &input, first_nonspace);
  672. } else if (container->type != NODE_HRULE &&
  673. container->type != NODE_HEADER) {
  674. // create paragraph container for line
  675. container = add_child(parser, container, NODE_PARAGRAPH, parser->line_number, first_nonspace + 1);
  676. add_line(container, &input, first_nonspace);
  677. } else {
  678. assert(false);
  679. }
  680. parser->current = container;
  681. }
  682. strbuf_clear(parser->curline);
  683. }
  684. cmark_node *cmark_parser_finish(cmark_parser *parser)
  685. {
  686. finalize_document(parser);
  687. strbuf_free(parser->curline);
  688. #if CMARK_DEBUG_NODES
  689. if (cmark_node_check(parser->root, stderr)) {
  690. abort();
  691. }
  692. #endif
  693. return parser->root;
  694. }