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