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