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