aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 4dcd3f41a6a4d87f9234806fe5af0df088d7b8ab (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, int line_number);
  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_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, 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_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 (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, 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. // 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. parser->line_number);
  346. }
  347. finalize(parser, parser->root, parser->line_number);
  348. process_inlines(parser->root, parser->refmap);
  349. return parser->root;
  350. }
  351. cmark_node *cmark_parse_file(FILE *f)
  352. {
  353. unsigned char buffer[4096];
  354. cmark_parser *parser = cmark_parser_new();
  355. size_t bytes;
  356. cmark_node *document;
  357. while ((bytes = fread(buffer, 1, sizeof(buffer), f)) > 0) {
  358. bool eof = bytes < sizeof(buffer);
  359. S_parser_feed(parser, buffer, bytes, eof);
  360. if (eof) {
  361. break;
  362. }
  363. }
  364. document = cmark_parser_finish(parser);
  365. cmark_parser_free(parser);
  366. return document;
  367. }
  368. cmark_node *cmark_parse_document(const char *buffer, size_t len)
  369. {
  370. cmark_parser *parser = cmark_parser_new();
  371. cmark_node *document;
  372. S_parser_feed(parser, (const unsigned char *)buffer, len, true);
  373. document = cmark_parser_finish(parser);
  374. cmark_parser_free(parser);
  375. return document;
  376. }
  377. void
  378. cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len)
  379. {
  380. S_parser_feed(parser, (const unsigned char *)buffer, len, false);
  381. }
  382. static void
  383. S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len,
  384. bool eof)
  385. {
  386. const unsigned char *end = buffer + len;
  387. while (buffer < end) {
  388. const unsigned char *eol
  389. = (const unsigned char *)memchr(buffer, '\n',
  390. end - buffer);
  391. size_t line_len;
  392. if (eol) {
  393. line_len = eol + 1 - buffer;
  394. }
  395. else if (eof) {
  396. line_len = end - buffer;
  397. }
  398. else {
  399. cmark_strbuf_put(parser->linebuf, buffer, end - buffer);
  400. break;
  401. }
  402. if (parser->linebuf->size > 0) {
  403. cmark_strbuf_put(parser->linebuf, buffer, line_len);
  404. S_process_line(parser, parser->linebuf->ptr,
  405. parser->linebuf->size);
  406. cmark_strbuf_clear(parser->linebuf);
  407. }
  408. else {
  409. S_process_line(parser, buffer, line_len);
  410. }
  411. buffer += line_len;
  412. }
  413. }
  414. static void chop_trailing_hashtags(cmark_chunk *ch)
  415. {
  416. int n, orig_n;
  417. cmark_chunk_rtrim(ch);
  418. orig_n = n = ch->len - 1;
  419. // if string ends in space followed by #s, remove these:
  420. while (n >= 0 && peek_at(ch, n) == '#')
  421. n--;
  422. // Check for a be a space before the final #s:
  423. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  424. ch->len = n;
  425. cmark_chunk_rtrim(ch);
  426. }
  427. }
  428. static void
  429. S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
  430. {
  431. cmark_node* last_matched_container;
  432. int offset = 0;
  433. int matched = 0;
  434. int lev = 0;
  435. int i;
  436. cmark_list *data = NULL;
  437. bool all_matched = true;
  438. cmark_node* container;
  439. cmark_node* cur = parser->current;
  440. bool blank = false;
  441. int first_nonspace;
  442. int indent;
  443. cmark_chunk input;
  444. utf8proc_detab(parser->curline, buffer, bytes);
  445. // Add a newline to the end if not present:
  446. // TODO this breaks abstraction:
  447. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  448. cmark_strbuf_putc(parser->curline, '\n');
  449. }
  450. input.data = parser->curline->ptr;
  451. input.len = parser->curline->size;
  452. // container starts at the document root.
  453. container = parser->root;
  454. parser->line_number++;
  455. // for each containing cmark_node, try to parse the associated line start.
  456. // bail out on failure: container will point to the last matching cmark_node.
  457. while (container->last_child && container->last_child->open) {
  458. container = container->last_child;
  459. first_nonspace = offset;
  460. while (peek_at(&input, first_nonspace) == ' ') {
  461. first_nonspace++;
  462. }
  463. indent = first_nonspace - offset;
  464. blank = peek_at(&input, first_nonspace) == '\n';
  465. if (container->type == NODE_BLOCK_QUOTE) {
  466. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  467. if (matched) {
  468. offset = first_nonspace + 1;
  469. if (peek_at(&input, offset) == ' ')
  470. offset++;
  471. } else {
  472. all_matched = false;
  473. }
  474. } else if (container->type == NODE_LIST_ITEM) {
  475. if (indent >= container->as.list.marker_offset +
  476. container->as.list.padding) {
  477. offset += container->as.list.marker_offset +
  478. container->as.list.padding;
  479. } else if (blank) {
  480. offset = first_nonspace;
  481. } else {
  482. all_matched = false;
  483. }
  484. } else if (container->type == NODE_CODE_BLOCK) {
  485. if (!container->as.code.fenced) { // indented
  486. if (indent >= CODE_INDENT) {
  487. offset += CODE_INDENT;
  488. } else if (blank) {
  489. offset = first_nonspace;
  490. } else {
  491. all_matched = false;
  492. }
  493. } else {
  494. // skip optional spaces of fence offset
  495. i = container->as.code.fence_offset;
  496. while (i > 0 && peek_at(&input, offset) == ' ') {
  497. offset++;
  498. i--;
  499. }
  500. }
  501. } else if (container->type == NODE_HEADER) {
  502. // a header can never contain more than one line
  503. all_matched = false;
  504. if (blank) {
  505. container->last_line_blank = true;
  506. }
  507. } else if (container->type == NODE_HTML) {
  508. if (blank) {
  509. container->last_line_blank = true;
  510. all_matched = false;
  511. }
  512. } else if (container->type == NODE_PARAGRAPH) {
  513. if (blank) {
  514. container->last_line_blank = true;
  515. all_matched = false;
  516. }
  517. }
  518. if (!all_matched) {
  519. container = container->parent; // back up to last matching cmark_node
  520. break;
  521. }
  522. }
  523. last_matched_container = container;
  524. // check to see if we've hit 2nd blank line, break out of list:
  525. if (blank && container->last_line_blank) {
  526. break_out_of_lists(parser, &container, parser->line_number);
  527. }
  528. // unless last matched container is code cmark_node, try new container starts:
  529. while (container->type != NODE_CODE_BLOCK &&
  530. container->type != NODE_HTML) {
  531. first_nonspace = offset;
  532. while (peek_at(&input, first_nonspace) == ' ')
  533. first_nonspace++;
  534. indent = first_nonspace - offset;
  535. blank = peek_at(&input, first_nonspace) == '\n';
  536. if (indent >= CODE_INDENT) {
  537. if (cur->type != NODE_PARAGRAPH && !blank) {
  538. offset += CODE_INDENT;
  539. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, offset + 1);
  540. container->as.code.fenced = false;
  541. container->as.code.fence_char = 0;
  542. container->as.code.fence_length = 0;
  543. container->as.code.fence_offset = 0;
  544. container->as.code.info = cmark_chunk_literal("");
  545. } else { // indent > 4 in lazy line
  546. break;
  547. }
  548. } else if (peek_at(&input, first_nonspace) == '>') {
  549. offset = first_nonspace + 1;
  550. // optional following character
  551. if (peek_at(&input, offset) == ' ')
  552. offset++;
  553. container = add_child(parser, container, NODE_BLOCK_QUOTE, parser->line_number, offset + 1);
  554. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  555. offset = first_nonspace + matched;
  556. container = add_child(parser, container, NODE_HEADER, parser->line_number, offset + 1);
  557. int hashpos = cmark_chunk_strchr(&input, '#', first_nonspace);
  558. int level = 0;
  559. while (peek_at(&input, hashpos) == '#') {
  560. level++;
  561. hashpos++;
  562. }
  563. container->as.header.level = level;
  564. container->as.header.setext = false;
  565. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  566. container = add_child(parser, container, NODE_CODE_BLOCK, parser->line_number, first_nonspace + 1);
  567. container->as.code.fenced = true;
  568. container->as.code.fence_char = peek_at(&input, first_nonspace);
  569. container->as.code.fence_length = matched;
  570. container->as.code.fence_offset = first_nonspace - offset;
  571. container->as.code.info = cmark_chunk_literal("");
  572. offset = first_nonspace + matched;
  573. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  574. container = add_child(parser, container, NODE_HTML, parser->line_number, first_nonspace + 1);
  575. // note, we don't adjust offset because the tag is part of the text
  576. } else if (container->type == NODE_PARAGRAPH &&
  577. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  578. // check that there is only one line in the paragraph:
  579. cmark_strbuf_strrchr(&container->string_content, '\n',
  580. cmark_strbuf_len(&container->string_content) - 2) < 0) {
  581. container->type = NODE_HEADER;
  582. container->as.header.level = lev;
  583. container->as.header.setext = true;
  584. offset = input.len - 1;
  585. } else if (!(container->type == NODE_PARAGRAPH && !all_matched) &&
  586. (matched = scan_hrule(&input, first_nonspace))) {
  587. // it's only now that we know the line is not part of a setext header:
  588. container = add_child(parser, container, NODE_HRULE, parser->line_number, first_nonspace + 1);
  589. container = finalize(parser, container,
  590. parser->line_number);
  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, parser->line_number,
  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_LIST_ITEM, parser->line_number,
  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_LIST_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, parser->line_number);
  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. parser->line_number);
  684. } else {
  685. add_line(container, &input, offset);
  686. }
  687. } else if (container->type == NODE_HTML) {
  688. add_line(container, &input, offset);
  689. } else if (blank) {
  690. // ??? do nothing
  691. } else if (container->type == NODE_HEADER) {
  692. chop_trailing_hashtags(&input);
  693. add_line(container, &input, first_nonspace);
  694. container = finalize(parser, container,
  695. parser->line_number);
  696. } else if (accepts_lines(container->type)) {
  697. add_line(container, &input, first_nonspace);
  698. } else if (container->type != NODE_HRULE &&
  699. container->type != NODE_HEADER) {
  700. // create paragraph container for line
  701. container = add_child(parser, container, NODE_PARAGRAPH, parser->line_number, first_nonspace + 1);
  702. add_line(container, &input, first_nonspace);
  703. } else {
  704. assert(false);
  705. }
  706. parser->current = container;
  707. }
  708. cmark_strbuf_clear(parser->curline);
  709. }
  710. cmark_node *cmark_parser_finish(cmark_parser *parser)
  711. {
  712. if (parser->linebuf->size) {
  713. S_process_line(parser, parser->linebuf->ptr,
  714. parser->linebuf->size);
  715. cmark_strbuf_clear(parser->linebuf);
  716. }
  717. finalize_document(parser);
  718. cmark_strbuf_free(parser->curline);
  719. #if CMARK_DEBUG_NODES
  720. if (cmark_node_check(parser->root, stderr)) {
  721. abort();
  722. }
  723. #endif
  724. return parser->root;
  725. }