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