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