aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 8ca10b974e12b25f6bcadce00186dd4f5f78ba9b (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 "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 void
  19. S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len,
  20. bool eof);
  21. static void
  22. S_process_line(cmark_parser *parser, const unsigned char *buffer,
  23. size_t bytes);
  24. static cmark_node* make_block(cmark_node_type tag, int start_line, int start_column)
  25. {
  26. cmark_node* e;
  27. e = (cmark_node *)calloc(1, sizeof(*e));
  28. if(e != NULL) {
  29. e->type = tag;
  30. e->open = true;
  31. e->start_line = start_line;
  32. e->start_column = start_column;
  33. e->end_line = start_line;
  34. strbuf_init(&e->string_content, 32);
  35. }
  36. return e;
  37. }
  38. // Create a root document cmark_node.
  39. static cmark_node* make_document()
  40. {
  41. cmark_node *e = make_block(NODE_DOCUMENT, 1, 1);
  42. return e;
  43. }
  44. cmark_parser *cmark_parser_new()
  45. {
  46. cmark_parser *parser = (cmark_parser*)malloc(sizeof(cmark_parser));
  47. cmark_node *document = make_document();
  48. strbuf *line = (strbuf*)malloc(sizeof(strbuf));
  49. strbuf *buf = (strbuf*)malloc(sizeof(strbuf));
  50. cmark_strbuf_init(line, 256);
  51. cmark_strbuf_init(buf, 0);
  52. parser->refmap = cmark_reference_map_new();
  53. parser->root = document;
  54. parser->current = document;
  55. parser->line_number = 0;
  56. parser->curline = line;
  57. parser->linebuf = buf;
  58. return parser;
  59. }
  60. void cmark_parser_free(cmark_parser *parser)
  61. {
  62. cmark_strbuf_free(parser->curline);
  63. free(parser->curline);
  64. cmark_strbuf_free(parser->linebuf);
  65. free(parser->linebuf);
  66. cmark_reference_map_free(parser->refmap);
  67. free(parser);
  68. }
  69. static void finalize(cmark_parser *parser, cmark_node* b, int line_number);
  70. // Returns true if line has only space characters, else false.
  71. static bool is_blank(strbuf *s, int offset)
  72. {
  73. while (offset < s->size) {
  74. switch (s->ptr[offset]) {
  75. case '\n':
  76. return true;
  77. case ' ':
  78. offset++;
  79. break;
  80. default:
  81. return false;
  82. }
  83. }
  84. return true;
  85. }
  86. static inline bool can_contain(cmark_node_type parent_type, cmark_node_type child_type)
  87. {
  88. return ( parent_type == NODE_DOCUMENT ||
  89. parent_type == NODE_BLOCK_QUOTE ||
  90. parent_type == NODE_LIST_ITEM ||
  91. (parent_type == NODE_LIST && child_type == NODE_LIST_ITEM) );
  92. }
  93. static inline bool accepts_lines(cmark_node_type block_type)
  94. {
  95. return (block_type == NODE_PARAGRAPH ||
  96. block_type == NODE_HEADER ||
  97. block_type == NODE_CODE_BLOCK);
  98. }
  99. static void add_line(cmark_node* cmark_node, chunk *ch, int offset)
  100. {
  101. assert(cmark_node->open);
  102. strbuf_put(&cmark_node->string_content, ch->data + offset, ch->len - offset);
  103. }
  104. static void remove_trailing_blank_lines(strbuf *ln)
  105. {
  106. int i;
  107. for (i = ln->size - 1; i >= 0; --i) {
  108. unsigned char c = ln->ptr[i];
  109. if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
  110. break;
  111. }
  112. if (i < 0) {
  113. strbuf_clear(ln);
  114. return;
  115. }
  116. i = strbuf_strchr(ln, '\n', i);
  117. if (i >= 0)
  118. strbuf_truncate(ln, i);
  119. }
  120. // Check to see if a cmark_node ends with a blank line, descending
  121. // if needed into lists and sublists.
  122. static bool ends_with_blank_line(cmark_node* cmark_node)
  123. {
  124. if (cmark_node->last_line_blank) {
  125. return true;
  126. }
  127. if ((cmark_node->type == NODE_LIST || cmark_node->type == NODE_LIST_ITEM) && cmark_node->last_child) {
  128. return ends_with_blank_line(cmark_node->last_child);
  129. } else {
  130. return false;
  131. }
  132. }
  133. // Break out of all containing lists
  134. static int break_out_of_lists(cmark_parser *parser, cmark_node ** bptr, int line_number)
  135. {
  136. cmark_node *container = *bptr;
  137. cmark_node *b = parser->root;
  138. // find first containing NODE_LIST:
  139. while (b && b->type != NODE_LIST) {
  140. b = b->last_child;
  141. }
  142. if (b) {
  143. while (container && container != b) {
  144. finalize(parser, container, line_number);
  145. container = container->parent;
  146. }
  147. finalize(parser, b, line_number);
  148. *bptr = b->parent;
  149. }
  150. return 0;
  151. }
  152. static void finalize(cmark_parser *parser, cmark_node* b, int line_number)
  153. {
  154. int firstlinelen;
  155. int pos;
  156. cmark_node* item;
  157. cmark_node* subitem;
  158. if (!b->open)
  159. return; // don't do anything if the cmark_node is already closed
  160. b->open = false;
  161. if (line_number > b->start_line) {
  162. b->end_line = line_number - 1;
  163. } else {
  164. b->end_line = line_number;
  165. }
  166. switch (b->type) {
  167. case NODE_PARAGRAPH:
  168. while (strbuf_at(&b->string_content, 0) == '[' &&
  169. (pos = cmark_parse_reference_inline(&b->string_content, parser->refmap))) {
  170. strbuf_drop(&b->string_content, pos);
  171. }
  172. if (is_blank(&b->string_content, 0)) {
  173. // remove blank node (former reference def)
  174. cmark_node_free(b);
  175. }
  176. break;
  177. case NODE_CODE_BLOCK:
  178. if (!b->as.code.fenced) { // indented code
  179. remove_trailing_blank_lines(&b->string_content);
  180. strbuf_putc(&b->string_content, '\n');
  181. break;
  182. } else {
  183. // first line of contents becomes info
  184. firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
  185. houdini_unescape_html_f(
  186. &b->as.code.info,
  187. b->string_content.ptr,
  188. firstlinelen
  189. );
  190. strbuf_drop(&b->string_content, firstlinelen + 1);
  191. strbuf_trim(&b->as.code.info);
  192. strbuf_unescape(&b->as.code.info);
  193. break;
  194. }
  195. case NODE_LIST: // determine tight/loose status
  196. b->as.list.tight = true; // tight by default
  197. item = b->first_child;
  198. while (item) {
  199. // check for non-final non-empty list item ending with blank line:
  200. if (item->last_line_blank && item->next) {
  201. b->as.list.tight = false;
  202. break;
  203. }
  204. // recurse into children of list item, to see if there are
  205. // spaces between them:
  206. subitem = item->first_child;
  207. while (subitem) {
  208. if (ends_with_blank_line(subitem) &&
  209. (item->next || subitem->next)) {
  210. b->as.list.tight = false;
  211. break;
  212. }
  213. subitem = subitem->next;
  214. }
  215. if (!(b->as.list.tight)) {
  216. break;
  217. }
  218. item = item->next;
  219. }
  220. break;
  221. default:
  222. break;
  223. }
  224. }
  225. // Add a cmark_node as child of another. Return pointer to child.
  226. static cmark_node* add_child(cmark_parser *parser, cmark_node* parent,
  227. cmark_node_type block_type, int start_line, int start_column)
  228. {
  229. assert(parent);
  230. // if 'parent' isn't the kind of cmark_node that can accept this child,
  231. // then back up til we hit a cmark_node that can.
  232. while (!can_contain(parent->type, block_type)) {
  233. finalize(parser, parent, start_line);
  234. parent = parent->parent;
  235. }
  236. cmark_node* child = make_block(block_type, start_line, start_column);
  237. child->parent = parent;
  238. if (parent->last_child) {
  239. parent->last_child->next = child;
  240. child->prev = parent->last_child;
  241. } else {
  242. parent->first_child = child;
  243. child->prev = NULL;
  244. }
  245. parent->last_child = child;
  246. return child;
  247. }
  248. typedef struct BlockStack {
  249. struct BlockStack *previous;
  250. cmark_node *next_sibling;
  251. } block_stack;
  252. // Walk through cmark_node and all children, recursively, parsing
  253. // string content into inline content where appropriate.
  254. static void process_inlines(cmark_node* cur, cmark_reference_map *refmap)
  255. {
  256. block_stack* stack = NULL;
  257. block_stack* newstack = NULL;
  258. while (cur != NULL) {
  259. switch (cur->type) {
  260. case NODE_PARAGRAPH:
  261. case NODE_HEADER:
  262. cmark_parse_inlines(cur, refmap);
  263. break;
  264. default:
  265. break;
  266. }
  267. if (cur->first_child) {
  268. newstack = (block_stack*)malloc(sizeof(block_stack));
  269. if (newstack == NULL) break;
  270. newstack->previous = stack;
  271. stack = newstack;
  272. stack->next_sibling = cur->next;
  273. cur = cur->first_child;
  274. } else {
  275. cur = cur->next;
  276. }
  277. while (cur == NULL && stack != NULL) {
  278. cur = stack->next_sibling;
  279. newstack = stack->previous;
  280. free(stack);
  281. stack = newstack;
  282. }
  283. }
  284. while (stack != NULL) {
  285. newstack = stack->previous;
  286. free(stack);
  287. stack = newstack;
  288. }
  289. }
  290. // Attempts to parse a list item marker (bullet or enumerated).
  291. // On success, returns length of the marker, and populates
  292. // data with the details. On failure, returns 0.
  293. static int parse_list_marker(chunk *input, int pos, cmark_list **dataptr)
  294. {
  295. unsigned char c;
  296. int startpos;
  297. cmark_list *data;
  298. startpos = pos;
  299. c = peek_at(input, pos);
  300. if ((c == '*' || c == '-' || c == '+') && !scan_hrule(input, pos)) {
  301. pos++;
  302. if (!isspace(peek_at(input, pos))) {
  303. return 0;
  304. }
  305. data = (cmark_list *)calloc(1, sizeof(*data));
  306. if(data == NULL) {
  307. return 0;
  308. } else {
  309. data->marker_offset = 0; // will be adjusted later
  310. data->list_type = CMARK_BULLET_LIST;
  311. data->bullet_char = c;
  312. data->start = 1;
  313. data->delimiter = CMARK_PERIOD_DELIM;
  314. data->tight = false;
  315. }
  316. } else if (isdigit(c)) {
  317. int start = 0;
  318. do {
  319. start = (10 * start) + (peek_at(input, pos) - '0');
  320. pos++;
  321. } while (isdigit(peek_at(input, pos)));
  322. c = peek_at(input, pos);
  323. if (c == '.' || c == ')') {
  324. pos++;
  325. if (!isspace(peek_at(input, pos))) {
  326. return 0;
  327. }
  328. data = (cmark_list *)calloc(1, sizeof(*data));
  329. if(data == NULL) {
  330. return 0;
  331. } else {
  332. data->marker_offset = 0; // will be adjusted later
  333. data->list_type = CMARK_ORDERED_LIST;
  334. data->bullet_char = 0;
  335. data->start = start;
  336. data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
  337. data->tight = false;
  338. }
  339. } else {
  340. return 0;
  341. }
  342. } else {
  343. return 0;
  344. }
  345. *dataptr = data;
  346. return (pos - startpos);
  347. }
  348. // Return 1 if list item belongs in list, else 0.
  349. static int lists_match(cmark_list *list_data, cmark_list *item_data)
  350. {
  351. return (list_data->list_type == item_data->list_type &&
  352. list_data->delimiter == item_data->delimiter &&
  353. // list_data->marker_offset == item_data.marker_offset &&
  354. list_data->bullet_char == item_data->bullet_char);
  355. }
  356. static cmark_node *finalize_document(cmark_parser *parser)
  357. {
  358. while (parser->current != parser->root) {
  359. finalize(parser, parser->current, parser->line_number);
  360. parser->current = parser->current->parent;
  361. }
  362. finalize(parser, parser->root, parser->line_number);
  363. process_inlines(parser->root, parser->refmap);
  364. return parser->root;
  365. }
  366. cmark_node *cmark_parse_file(FILE *f)
  367. {
  368. unsigned char buffer[4096];
  369. cmark_parser *parser = cmark_parser_new();
  370. size_t bytes;
  371. cmark_node *document;
  372. while ((bytes = fread(buffer, 1, sizeof(buffer), f)) > 0) {
  373. bool eof = bytes < sizeof(buffer);
  374. S_parser_feed(parser, buffer, bytes, eof);
  375. if (eof) {
  376. break;
  377. }
  378. }
  379. document = cmark_parser_finish(parser);
  380. cmark_parser_free(parser);
  381. return document;
  382. }
  383. cmark_node *cmark_parse_document(const char *buffer, size_t len)
  384. {
  385. cmark_parser *parser = cmark_parser_new();
  386. cmark_node *document;
  387. S_parser_feed(parser, (const unsigned char *)buffer, len, true);
  388. document = cmark_parser_finish(parser);
  389. cmark_parser_free(parser);
  390. return document;
  391. }
  392. void
  393. cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len)
  394. {
  395. S_parser_feed(parser, (const unsigned char *)buffer, len, false);
  396. }
  397. static void
  398. S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len,
  399. bool eof)
  400. {
  401. const unsigned char *end = buffer + len;
  402. while (buffer < end) {
  403. const unsigned char *eol
  404. = (const unsigned char *)memchr(buffer, '\n',
  405. end - buffer);
  406. size_t line_len;
  407. if (eol) {
  408. line_len = eol + 1 - buffer;
  409. }
  410. else if (eof) {
  411. line_len = end - buffer;
  412. }
  413. else {
  414. strbuf_put(parser->linebuf, buffer, end - buffer);
  415. break;
  416. }
  417. if (parser->linebuf->size > 0) {
  418. strbuf_put(parser->linebuf, buffer, line_len);
  419. S_process_line(parser, parser->linebuf->ptr,
  420. parser->linebuf->size);
  421. strbuf_clear(parser->linebuf);
  422. }
  423. else {
  424. S_process_line(parser, buffer, line_len);
  425. }
  426. buffer += line_len;
  427. }
  428. }
  429. static void chop_trailing_hashtags(chunk *ch)
  430. {
  431. int n, orig_n;
  432. chunk_rtrim(ch);
  433. orig_n = n = ch->len - 1;
  434. // if string ends in space followed by #s, remove these:
  435. while (n >= 0 && peek_at(ch, n) == '#')
  436. n--;
  437. // Check for a be a space before the final #s:
  438. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  439. ch->len = n;
  440. chunk_rtrim(ch);
  441. }
  442. }
  443. static void
  444. S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
  445. {
  446. cmark_node* last_matched_container;
  447. int offset = 0;
  448. int matched = 0;
  449. int lev = 0;
  450. int i;
  451. cmark_list *data = NULL;
  452. bool all_matched = true;
  453. cmark_node* container;
  454. cmark_node* cur = parser->current;
  455. bool blank = false;
  456. int first_nonspace;
  457. int indent;
  458. chunk input;
  459. utf8proc_detab(parser->curline, buffer, bytes);
  460. // Add a newline to the end if not present:
  461. // TODO this breaks abstraction:
  462. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  463. strbuf_putc(parser->curline, '\n');
  464. }
  465. input.data = parser->curline->ptr;
  466. input.len = parser->curline->size;
  467. // container starts at the document root.
  468. container = parser->root;
  469. parser->line_number++;
  470. // for each containing cmark_node, try to parse the associated line start.
  471. // bail out on failure: container will point to the last matching cmark_node.
  472. while (container->last_child && container->last_child->open) {
  473. container = container->last_child;
  474. first_nonspace = offset;
  475. while (peek_at(&input, first_nonspace) == ' ') {
  476. first_nonspace++;
  477. }
  478. indent = first_nonspace - offset;
  479. blank = peek_at(&input, first_nonspace) == '\n';
  480. if (container->type == NODE_BLOCK_QUOTE) {
  481. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  482. if (matched) {
  483. offset = first_nonspace + 1;
  484. if (peek_at(&input, offset) == ' ')
  485. offset++;
  486. } else {
  487. all_matched = false;
  488. }
  489. } else if (container->type == NODE_LIST_ITEM) {
  490. if (indent >= container->as.list.marker_offset +
  491. container->as.list.padding) {
  492. offset += container->as.list.marker_offset +
  493. container->as.list.padding;
  494. } else if (blank) {
  495. offset = first_nonspace;
  496. } else {
  497. all_matched = false;
  498. }
  499. } else if (container->type == NODE_CODE_BLOCK) {
  500. if (!container->as.code.fenced) { // indented
  501. if (indent >= CODE_INDENT) {
  502. offset += CODE_INDENT;
  503. } else if (blank) {
  504. offset = first_nonspace;
  505. } else {
  506. all_matched = false;
  507. }
  508. } else {
  509. // skip optional spaces of fence offset
  510. i = container->as.code.fence_offset;
  511. while (i > 0 && peek_at(&input, offset) == ' ') {
  512. offset++;
  513. i--;
  514. }
  515. }
  516. } else if (container->type == NODE_HEADER) {
  517. // a header can never contain more than one line
  518. all_matched = false;
  519. if (blank) {
  520. container->last_line_blank = true;
  521. }
  522. } else if (container->type == NODE_HTML) {
  523. if (blank) {
  524. container->last_line_blank = true;
  525. all_matched = false;
  526. }
  527. } else if (container->type == NODE_PARAGRAPH) {
  528. if (blank) {
  529. container->last_line_blank = true;
  530. all_matched = false;
  531. }
  532. }
  533. if (!all_matched) {
  534. container = container->parent; // back up to last matching cmark_node
  535. break;
  536. }
  537. }
  538. last_matched_container = container;
  539. // check to see if we've hit 2nd blank line, break out of list:
  540. if (blank && container->last_line_blank) {
  541. break_out_of_lists(parser, &container, parser->line_number);
  542. }
  543. // unless last matched container is code cmark_node, try new container starts:
  544. while (container->type != NODE_CODE_BLOCK &&
  545. container->type != NODE_HTML) {
  546. first_nonspace = offset;
  547. while (peek_at(&input, first_nonspace) == ' ')
  548. first_nonspace++;
  549. indent = first_nonspace - offset;
  550. blank = peek_at(&input, first_nonspace) == '\n';
  551. if (indent >= CODE_INDENT) {
  552. if (cur->type != NODE_PARAGRAPH && !blank) {
  553. offset += CODE_INDENT;
  554. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, offset + 1);
  555. container->as.code.fenced = false;
  556. container->as.code.fence_char = 0;
  557. container->as.code.fence_length = 0;
  558. container->as.code.fence_offset = 0;
  559. strbuf_init(&container->as.code.info, 0);
  560. } else { // indent > 4 in lazy line
  561. break;
  562. }
  563. } else if (peek_at(&input, first_nonspace) == '>') {
  564. offset = first_nonspace + 1;
  565. // optional following character
  566. if (peek_at(&input, offset) == ' ')
  567. offset++;
  568. container = add_child(parser, container, NODE_BLOCK_QUOTE, parser->line_number, offset + 1);
  569. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  570. offset = first_nonspace + matched;
  571. container = add_child(parser, container, NODE_HEADER, parser->line_number, offset + 1);
  572. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  573. int level = 0;
  574. while (peek_at(&input, hashpos) == '#') {
  575. level++;
  576. hashpos++;
  577. }
  578. container->as.header.level = level;
  579. container->as.header.setext = false;
  580. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  581. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, first_nonspace + 1);
  582. container->as.code.fenced = true;
  583. container->as.code.fence_char = peek_at(&input, first_nonspace);
  584. container->as.code.fence_length = matched;
  585. container->as.code.fence_offset = first_nonspace - offset;
  586. strbuf_init(&container->as.code.info, 0);
  587. offset = first_nonspace + matched;
  588. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  589. container = add_child(parser, container, NODE_HTML, parser->line_number, first_nonspace + 1);
  590. // note, we don't adjust offset because the tag is part of the text
  591. } else if (container->type == NODE_PARAGRAPH &&
  592. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  593. // check that there is only one line in the paragraph:
  594. strbuf_strrchr(&container->string_content, '\n',
  595. strbuf_len(&container->string_content) - 2) < 0) {
  596. container->type = NODE_HEADER;
  597. container->as.header.level = lev;
  598. container->as.header.setext = true;
  599. offset = input.len - 1;
  600. } else if (!(container->type == NODE_PARAGRAPH && !all_matched) &&
  601. (matched = scan_hrule(&input, first_nonspace))) {
  602. // it's only now that we know the line is not part of a setext header:
  603. container = add_child(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
  604. finalize(parser, container, parser->line_number);
  605. container = container->parent;
  606. offset = input.len - 1;
  607. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  608. // compute padding:
  609. offset = first_nonspace + matched;
  610. i = 0;
  611. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  612. i++;
  613. }
  614. // i = number of spaces after marker, up to 5
  615. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  616. data->padding = matched + 1;
  617. if (i > 0) {
  618. offset += 1;
  619. }
  620. } else {
  621. data->padding = matched + i;
  622. offset += i;
  623. }
  624. // check container; if it's a list, see if this list item
  625. // can continue the list; otherwise, create a list container.
  626. data->marker_offset = indent;
  627. if (container->type != NODE_LIST ||
  628. !lists_match(&container->as.list, data)) {
  629. container = add_child(parser, container, NODE_LIST, parser->line_number,
  630. first_nonspace + 1);
  631. memcpy(&container->as.list, data, sizeof(*data));
  632. }
  633. // add the list item
  634. container = add_child(parser, container, NODE_LIST_ITEM, parser->line_number,
  635. first_nonspace + 1);
  636. /* TODO: static */
  637. memcpy(&container->as.list, data, sizeof(*data));
  638. free(data);
  639. } else {
  640. break;
  641. }
  642. if (accepts_lines(container->type)) {
  643. // if it's a line container, it can't contain other containers
  644. break;
  645. }
  646. }
  647. // what remains at offset is a text line. add the text to the
  648. // appropriate container.
  649. first_nonspace = offset;
  650. while (peek_at(&input, first_nonspace) == ' ')
  651. first_nonspace++;
  652. indent = first_nonspace - offset;
  653. blank = peek_at(&input, first_nonspace) == '\n';
  654. // cmark_node quote lines are never blank as they start with >
  655. // and we don't count blanks in fenced code for purposes of tight/loose
  656. // lists or breaking out of lists. we also don't set last_line_blank
  657. // on an empty list item.
  658. container->last_line_blank = (blank &&
  659. container->type != NODE_BLOCK_QUOTE &&
  660. container->type != NODE_HEADER &&
  661. !(container->type == NODE_CODE_BLOCK &&
  662. container->as.code.fenced) &&
  663. !(container->type == NODE_LIST_ITEM &&
  664. container->first_child == NULL &&
  665. container->start_line == parser->line_number));
  666. cmark_node *cont = container;
  667. while (cont->parent) {
  668. cont->parent->last_line_blank = false;
  669. cont = cont->parent;
  670. }
  671. if (cur != last_matched_container &&
  672. container == last_matched_container &&
  673. !blank &&
  674. cur->type == NODE_PARAGRAPH &&
  675. strbuf_len(&cur->string_content) > 0) {
  676. add_line(cur, &input, offset);
  677. } else { // not a lazy continuation
  678. // finalize any blocks that were not matched and set cur to container:
  679. while (cur != last_matched_container) {
  680. finalize(parser, cur, parser->line_number);
  681. cur = cur->parent;
  682. assert(cur != NULL);
  683. }
  684. if (container->type == NODE_CODE_BLOCK &&
  685. !container->as.code.fenced) {
  686. add_line(container, &input, offset);
  687. } else if (container->type == NODE_CODE_BLOCK &&
  688. container->as.code.fenced) {
  689. matched = 0;
  690. if (indent <= 3 &&
  691. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  692. int fence_len = scan_close_code_fence(&input, first_nonspace);
  693. if (fence_len > container->as.code.fence_length)
  694. matched = 1;
  695. }
  696. if (matched) {
  697. // if closing fence, don't add line to container; instead, close it:
  698. finalize(parser, container, parser->line_number);
  699. container = container->parent; // back up to parent
  700. } else {
  701. add_line(container, &input, offset);
  702. }
  703. } else if (container->type == NODE_HTML) {
  704. add_line(container, &input, offset);
  705. } else if (blank) {
  706. // ??? do nothing
  707. } else if (container->type == NODE_HEADER) {
  708. chop_trailing_hashtags(&input);
  709. add_line(container, &input, first_nonspace);
  710. finalize(parser, container, parser->line_number);
  711. container = container->parent;
  712. } else if (accepts_lines(container->type)) {
  713. add_line(container, &input, first_nonspace);
  714. } else if (container->type != NODE_HRULE &&
  715. container->type != NODE_HEADER) {
  716. // create paragraph container for line
  717. container = add_child(parser, container, NODE_PARAGRAPH, parser->line_number, first_nonspace + 1);
  718. add_line(container, &input, first_nonspace);
  719. } else {
  720. assert(false);
  721. }
  722. parser->current = container;
  723. }
  724. strbuf_clear(parser->curline);
  725. }
  726. cmark_node *cmark_parser_finish(cmark_parser *parser)
  727. {
  728. if (parser->linebuf->size) {
  729. S_process_line(parser, parser->linebuf->ptr,
  730. parser->linebuf->size);
  731. strbuf_clear(parser->linebuf);
  732. }
  733. finalize_document(parser);
  734. strbuf_free(parser->curline);
  735. #if CMARK_DEBUG_NODES
  736. if (cmark_node_check(parser->root, stderr)) {
  737. abort();
  738. }
  739. #endif
  740. return parser->root;
  741. }