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