aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 6a89312d560460816e55ef6f1f1d58a191d8abd4 (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. }
  405. else if (eof) {
  406. line_len = end - buffer;
  407. }
  408. else {
  409. cmark_strbuf_put(parser->linebuf, buffer, end - buffer);
  410. break;
  411. }
  412. if (parser->linebuf->size > 0) {
  413. cmark_strbuf_put(parser->linebuf, buffer, line_len);
  414. S_process_line(parser, parser->linebuf->ptr,
  415. parser->linebuf->size);
  416. cmark_strbuf_clear(parser->linebuf);
  417. }
  418. else {
  419. S_process_line(parser, buffer, line_len);
  420. }
  421. buffer += line_len;
  422. }
  423. }
  424. static void chop_trailing_hashtags(cmark_chunk *ch)
  425. {
  426. int n, orig_n;
  427. cmark_chunk_rtrim(ch);
  428. orig_n = n = ch->len - 1;
  429. // if string ends in space followed by #s, remove these:
  430. while (n >= 0 && peek_at(ch, n) == '#')
  431. n--;
  432. // Check for a be a space before the final #s:
  433. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  434. ch->len = n;
  435. cmark_chunk_rtrim(ch);
  436. }
  437. }
  438. static void
  439. S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
  440. {
  441. cmark_node* last_matched_container;
  442. int offset = 0;
  443. int matched = 0;
  444. int lev = 0;
  445. int i;
  446. cmark_list *data = NULL;
  447. bool all_matched = true;
  448. cmark_node* container;
  449. cmark_node* cur = parser->current;
  450. bool blank = false;
  451. int first_nonspace;
  452. int indent;
  453. cmark_chunk input;
  454. utf8proc_detab(parser->curline, buffer, bytes);
  455. // Add a newline to the end if not present:
  456. // TODO this breaks abstraction:
  457. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  458. cmark_strbuf_putc(parser->curline, '\n');
  459. }
  460. input.data = parser->curline->ptr;
  461. input.len = parser->curline->size;
  462. // container starts at the document root.
  463. container = parser->root;
  464. parser->line_number++;
  465. // for each containing cmark_node, try to parse the associated line start.
  466. // bail out on failure: container will point to the last matching cmark_node.
  467. while (container->last_child && container->last_child->open) {
  468. container = container->last_child;
  469. first_nonspace = offset;
  470. while (peek_at(&input, first_nonspace) == ' ') {
  471. first_nonspace++;
  472. }
  473. indent = first_nonspace - offset;
  474. blank = peek_at(&input, first_nonspace) == '\n';
  475. if (container->type == NODE_BLOCK_QUOTE) {
  476. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  477. if (matched) {
  478. offset = first_nonspace + 1;
  479. if (peek_at(&input, offset) == ' ')
  480. offset++;
  481. } else {
  482. all_matched = false;
  483. }
  484. } else if (container->type == NODE_ITEM) {
  485. if (indent >= container->as.list.marker_offset +
  486. container->as.list.padding) {
  487. offset += container->as.list.marker_offset +
  488. container->as.list.padding;
  489. } else if (blank) {
  490. offset = first_nonspace;
  491. } else {
  492. all_matched = false;
  493. }
  494. } else if (container->type == NODE_CODE_BLOCK) {
  495. if (!container->as.code.fenced) { // indented
  496. if (indent >= CODE_INDENT) {
  497. offset += CODE_INDENT;
  498. } else if (blank) {
  499. offset = first_nonspace;
  500. } else {
  501. all_matched = false;
  502. }
  503. } else {
  504. // skip optional spaces of fence offset
  505. i = container->as.code.fence_offset;
  506. while (i > 0 && peek_at(&input, offset) == ' ') {
  507. offset++;
  508. i--;
  509. }
  510. }
  511. } else if (container->type == NODE_HEADER) {
  512. // a header can never contain more than one line
  513. all_matched = false;
  514. if (blank) {
  515. container->last_line_blank = true;
  516. }
  517. } else if (container->type == NODE_HTML) {
  518. if (blank) {
  519. container->last_line_blank = true;
  520. all_matched = false;
  521. }
  522. } else if (container->type == NODE_PARAGRAPH) {
  523. if (blank) {
  524. container->last_line_blank = true;
  525. all_matched = false;
  526. }
  527. }
  528. if (!all_matched) {
  529. container = container->parent; // back up to last matching cmark_node
  530. break;
  531. }
  532. }
  533. last_matched_container = container;
  534. // check to see if we've hit 2nd blank line, break out of list:
  535. if (blank && container->last_line_blank) {
  536. break_out_of_lists(parser, &container);
  537. }
  538. // unless last matched container is code cmark_node, try new container starts:
  539. while (container->type != NODE_CODE_BLOCK &&
  540. container->type != NODE_HTML) {
  541. first_nonspace = offset;
  542. while (peek_at(&input, first_nonspace) == ' ')
  543. first_nonspace++;
  544. indent = first_nonspace - offset;
  545. blank = peek_at(&input, first_nonspace) == '\n';
  546. if (indent >= CODE_INDENT) {
  547. if (cur->type != NODE_PARAGRAPH && !blank) {
  548. offset += CODE_INDENT;
  549. container = add_child(parser, container, NODE_CODE_BLOCK, offset + 1);
  550. container->as.code.fenced = false;
  551. container->as.code.fence_char = 0;
  552. container->as.code.fence_length = 0;
  553. container->as.code.fence_offset = 0;
  554. container->as.code.info = cmark_chunk_literal("");
  555. } else { // indent > 4 in lazy line
  556. break;
  557. }
  558. } else if (peek_at(&input, first_nonspace) == '>') {
  559. offset = first_nonspace + 1;
  560. // optional following character
  561. if (peek_at(&input, offset) == ' ')
  562. offset++;
  563. container = add_child(parser, container, NODE_BLOCK_QUOTE, offset + 1);
  564. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  565. offset = first_nonspace + matched;
  566. container = add_child(parser, container, NODE_HEADER, offset + 1);
  567. int hashpos = cmark_chunk_strchr(&input, '#', first_nonspace);
  568. int level = 0;
  569. while (peek_at(&input, hashpos) == '#') {
  570. level++;
  571. hashpos++;
  572. }
  573. container->as.header.level = level;
  574. container->as.header.setext = false;
  575. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  576. container = add_child(parser, container, NODE_CODE_BLOCK, first_nonspace + 1);
  577. container->as.code.fenced = true;
  578. container->as.code.fence_char = peek_at(&input, first_nonspace);
  579. container->as.code.fence_length = matched;
  580. container->as.code.fence_offset = first_nonspace - offset;
  581. container->as.code.info = cmark_chunk_literal("");
  582. offset = first_nonspace + matched;
  583. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  584. container = add_child(parser, container, NODE_HTML, first_nonspace + 1);
  585. // note, we don't adjust offset because the tag is part of the text
  586. } else if (container->type == NODE_PARAGRAPH &&
  587. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  588. // check that there is only one line in the paragraph:
  589. cmark_strbuf_strrchr(&container->string_content, '\n',
  590. cmark_strbuf_len(&container->string_content) - 2) < 0) {
  591. container->type = NODE_HEADER;
  592. container->as.header.level = lev;
  593. container->as.header.setext = true;
  594. offset = input.len - 1;
  595. } else if (!(container->type == NODE_PARAGRAPH && !all_matched) &&
  596. (matched = scan_hrule(&input, first_nonspace))) {
  597. // it's only now that we know the line is not part of a setext header:
  598. container = add_child(parser, container, NODE_HRULE, first_nonspace + 1);
  599. container = finalize(parser, container);
  600. offset = input.len - 1;
  601. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  602. // compute padding:
  603. offset = first_nonspace + matched;
  604. i = 0;
  605. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  606. i++;
  607. }
  608. // i = number of spaces after marker, up to 5
  609. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  610. data->padding = matched + 1;
  611. if (i > 0) {
  612. offset += 1;
  613. }
  614. } else {
  615. data->padding = matched + i;
  616. offset += i;
  617. }
  618. // check container; if it's a list, see if this list item
  619. // can continue the list; otherwise, create a list container.
  620. data->marker_offset = indent;
  621. if (container->type != NODE_LIST ||
  622. !lists_match(&container->as.list, data)) {
  623. container = add_child(parser, container, NODE_LIST,
  624. first_nonspace + 1);
  625. memcpy(&container->as.list, data, sizeof(*data));
  626. }
  627. // add the list item
  628. container = add_child(parser, container, NODE_ITEM,
  629. first_nonspace + 1);
  630. /* TODO: static */
  631. memcpy(&container->as.list, data, sizeof(*data));
  632. free(data);
  633. } else {
  634. break;
  635. }
  636. if (accepts_lines(container->type)) {
  637. // if it's a line container, it can't contain other containers
  638. break;
  639. }
  640. }
  641. // what remains at offset is a text line. add the text to the
  642. // appropriate container.
  643. first_nonspace = offset;
  644. while (peek_at(&input, first_nonspace) == ' ')
  645. first_nonspace++;
  646. indent = first_nonspace - offset;
  647. blank = peek_at(&input, first_nonspace) == '\n';
  648. // cmark_node quote lines are never blank as they start with >
  649. // and we don't count blanks in fenced code for purposes of tight/loose
  650. // lists or breaking out of lists. we also don't set last_line_blank
  651. // on an empty list item.
  652. container->last_line_blank = (blank &&
  653. container->type != NODE_BLOCK_QUOTE &&
  654. container->type != NODE_HEADER &&
  655. !(container->type == NODE_CODE_BLOCK &&
  656. container->as.code.fenced) &&
  657. !(container->type == NODE_ITEM &&
  658. container->first_child == NULL &&
  659. container->start_line == parser->line_number));
  660. cmark_node *cont = container;
  661. while (cont->parent) {
  662. cont->parent->last_line_blank = false;
  663. cont = cont->parent;
  664. }
  665. if (cur != last_matched_container &&
  666. container == last_matched_container &&
  667. !blank &&
  668. cur->type == NODE_PARAGRAPH &&
  669. cmark_strbuf_len(&cur->string_content) > 0) {
  670. add_line(cur, &input, offset);
  671. } else { // not a lazy continuation
  672. // finalize any blocks that were not matched and set cur to container:
  673. while (cur != last_matched_container) {
  674. cur = finalize(parser, cur);
  675. assert(cur != NULL);
  676. }
  677. if (container->type == NODE_CODE_BLOCK &&
  678. !container->as.code.fenced) {
  679. add_line(container, &input, offset);
  680. } else if (container->type == NODE_CODE_BLOCK &&
  681. container->as.code.fenced) {
  682. matched = 0;
  683. if (indent <= 3 &&
  684. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  685. int fence_len = scan_close_code_fence(&input, first_nonspace);
  686. if (fence_len > container->as.code.fence_length)
  687. matched = 1;
  688. }
  689. if (matched) {
  690. // if closing fence, don't add line to container; instead, close it:
  691. container = finalize(parser, container);
  692. } else {
  693. add_line(container, &input, offset);
  694. }
  695. } else if (container->type == NODE_HTML) {
  696. add_line(container, &input, offset);
  697. } else if (blank) {
  698. // ??? do nothing
  699. } else if (container->type == NODE_HEADER) {
  700. chop_trailing_hashtags(&input);
  701. add_line(container, &input, first_nonspace);
  702. container = finalize(parser, container);
  703. } else if (accepts_lines(container->type)) {
  704. add_line(container, &input, first_nonspace);
  705. } else if (container->type != NODE_HRULE &&
  706. container->type != NODE_HEADER) {
  707. // create paragraph container for line
  708. container = add_child(parser, container, NODE_PARAGRAPH, first_nonspace + 1);
  709. add_line(container, &input, first_nonspace);
  710. } else {
  711. assert(false);
  712. }
  713. parser->current = container;
  714. }
  715. parser->last_line_length = parser->curline->size -
  716. (parser->curline->ptr[parser->curline->size - 1] == '\n' ?
  717. 1 : 0);
  718. ;
  719. cmark_strbuf_clear(parser->curline);
  720. }
  721. cmark_node *cmark_parser_finish(cmark_parser *parser)
  722. {
  723. if (parser->linebuf->size) {
  724. S_process_line(parser, parser->linebuf->ptr,
  725. parser->linebuf->size);
  726. cmark_strbuf_clear(parser->linebuf);
  727. }
  728. finalize_document(parser);
  729. cmark_strbuf_free(parser->curline);
  730. #if CMARK_DEBUG_NODES
  731. if (cmark_node_check(parser->root, stderr)) {
  732. abort();
  733. }
  734. #endif
  735. return parser->root;
  736. }