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