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