aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 806f7a63d08b820de78d59c28c6e14305ccbb7c4 (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. assert(b->open); // shouldn't call finalize on closed blocks
  167. b->open = false;
  168. if (parser->curline->size == 0) {
  169. // end of input - line number has not been incremented
  170. b->end_line = parser->line_number;
  171. b->end_column = parser->last_line_length;
  172. } else if (b->type == NODE_DOCUMENT ||
  173. (b->type == NODE_CODE_BLOCK && b->as.code.fenced) ||
  174. (b->type == NODE_HEADER && b->as.header.setext)) {
  175. b->end_line = parser->line_number;
  176. b->end_column = parser->curline->size -
  177. (parser->curline->ptr[parser->curline->size - 1] == '\n' ?
  178. 1 : 0);
  179. } else {
  180. b->end_line = parser->line_number - 1;
  181. b->end_column = parser->last_line_length;
  182. }
  183. switch (b->type) {
  184. case NODE_PARAGRAPH:
  185. while (cmark_strbuf_at(&b->string_content, 0) == '[' &&
  186. (pos = cmark_parse_reference_inline(&b->string_content, parser->refmap))) {
  187. cmark_strbuf_drop(&b->string_content, pos);
  188. }
  189. if (is_blank(&b->string_content, 0)) {
  190. // remove blank node (former reference def)
  191. cmark_node_free(b);
  192. }
  193. break;
  194. case NODE_CODE_BLOCK:
  195. if (!b->as.code.fenced) { // indented code
  196. remove_trailing_blank_lines(&b->string_content);
  197. cmark_strbuf_putc(&b->string_content, '\n');
  198. } else {
  199. // first line of contents becomes info
  200. firstlinelen = cmark_strbuf_strchr(&b->string_content, '\n', 0);
  201. cmark_strbuf tmp = GH_BUF_INIT;
  202. houdini_unescape_html_f(
  203. &tmp,
  204. b->string_content.ptr,
  205. firstlinelen
  206. );
  207. cmark_strbuf_trim(&tmp);
  208. cmark_strbuf_unescape(&tmp);
  209. b->as.code.info = cmark_chunk_buf_detach(&tmp);
  210. cmark_strbuf_drop(&b->string_content, firstlinelen + 1);
  211. }
  212. b->as.code.literal = cmark_chunk_buf_detach(&b->string_content);
  213. break;
  214. case NODE_HTML:
  215. b->as.literal = cmark_chunk_buf_detach(&b->string_content);
  216. break;
  217. case NODE_LIST: // determine tight/loose status
  218. b->as.list.tight = true; // tight by default
  219. item = b->first_child;
  220. while (item) {
  221. // check for non-final non-empty list item ending with blank line:
  222. if (item->last_line_blank && item->next) {
  223. b->as.list.tight = false;
  224. break;
  225. }
  226. // recurse into children of list item, to see if there are
  227. // spaces between them:
  228. subitem = item->first_child;
  229. while (subitem) {
  230. if (ends_with_blank_line(subitem) &&
  231. (item->next || subitem->next)) {
  232. b->as.list.tight = false;
  233. break;
  234. }
  235. subitem = subitem->next;
  236. }
  237. if (!(b->as.list.tight)) {
  238. break;
  239. }
  240. item = item->next;
  241. }
  242. break;
  243. default:
  244. break;
  245. }
  246. return parent;
  247. }
  248. // Add a cmark_node as child of another. Return pointer to child.
  249. static cmark_node* add_child(cmark_parser *parser, cmark_node* parent,
  250. cmark_node_type block_type, int start_column)
  251. {
  252. assert(parent);
  253. // if 'parent' isn't the kind of cmark_node that can accept this child,
  254. // then back up til we hit a cmark_node that can.
  255. while (!can_contain(parent->type, block_type)) {
  256. parent = finalize(parser, parent);
  257. }
  258. cmark_node* child = make_block(block_type, parser->line_number, start_column);
  259. child->parent = parent;
  260. if (parent->last_child) {
  261. parent->last_child->next = child;
  262. child->prev = parent->last_child;
  263. } else {
  264. parent->first_child = child;
  265. child->prev = NULL;
  266. }
  267. parent->last_child = child;
  268. return child;
  269. }
  270. // Walk through cmark_node and all children, recursively, parsing
  271. // string content into inline content where appropriate.
  272. static void process_inlines(cmark_node* root, cmark_reference_map *refmap)
  273. {
  274. cmark_iter *iter = cmark_iter_new(root);
  275. cmark_node *cur;
  276. cmark_event_type ev_type;
  277. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  278. cur = cmark_iter_get_node(iter);
  279. if (ev_type == CMARK_EVENT_ENTER) {
  280. if (cur->type == NODE_PARAGRAPH ||
  281. cur->type == NODE_HEADER) {
  282. cmark_parse_inlines(cur, refmap);
  283. }
  284. }
  285. }
  286. cmark_iter_free(iter);
  287. }
  288. // Attempts to parse a list item marker (bullet or enumerated).
  289. // On success, returns length of the marker, and populates
  290. // data with the details. On failure, returns 0.
  291. static int parse_list_marker(cmark_chunk *input, int pos, cmark_list **dataptr)
  292. {
  293. unsigned char c;
  294. int startpos;
  295. cmark_list *data;
  296. startpos = pos;
  297. c = peek_at(input, pos);
  298. if ((c == '*' || c == '-' || c == '+') && !scan_hrule(input, pos)) {
  299. pos++;
  300. if (!cmark_isspace(peek_at(input, pos))) {
  301. return 0;
  302. }
  303. data = (cmark_list *)calloc(1, sizeof(*data));
  304. if(data == NULL) {
  305. return 0;
  306. } else {
  307. data->marker_offset = 0; // will be adjusted later
  308. data->list_type = CMARK_BULLET_LIST;
  309. data->bullet_char = c;
  310. data->start = 1;
  311. data->delimiter = CMARK_PERIOD_DELIM;
  312. data->tight = false;
  313. }
  314. } else if (cmark_isdigit(c)) {
  315. int start = 0;
  316. do {
  317. start = (10 * start) + (peek_at(input, pos) - '0');
  318. pos++;
  319. } while (cmark_isdigit(peek_at(input, pos)));
  320. c = peek_at(input, pos);
  321. if (c == '.' || c == ')') {
  322. pos++;
  323. if (!cmark_isspace(peek_at(input, pos))) {
  324. return 0;
  325. }
  326. data = (cmark_list *)calloc(1, sizeof(*data));
  327. if(data == NULL) {
  328. return 0;
  329. } else {
  330. data->marker_offset = 0; // will be adjusted later
  331. data->list_type = CMARK_ORDERED_LIST;
  332. data->bullet_char = 0;
  333. data->start = start;
  334. data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
  335. data->tight = false;
  336. }
  337. } else {
  338. return 0;
  339. }
  340. } else {
  341. return 0;
  342. }
  343. *dataptr = data;
  344. return (pos - startpos);
  345. }
  346. // Return 1 if list item belongs in list, else 0.
  347. static int lists_match(cmark_list *list_data, cmark_list *item_data)
  348. {
  349. return (list_data->list_type == item_data->list_type &&
  350. list_data->delimiter == item_data->delimiter &&
  351. // list_data->marker_offset == item_data.marker_offset &&
  352. list_data->bullet_char == item_data->bullet_char);
  353. }
  354. static cmark_node *finalize_document(cmark_parser *parser)
  355. {
  356. while (parser->current != parser->root) {
  357. parser->current = finalize(parser, parser->current);
  358. }
  359. finalize(parser, parser->root);
  360. process_inlines(parser->root, parser->refmap);
  361. return parser->root;
  362. }
  363. cmark_node *cmark_parse_file(FILE *f)
  364. {
  365. unsigned char buffer[4096];
  366. cmark_parser *parser = cmark_parser_new();
  367. size_t bytes;
  368. cmark_node *document;
  369. while ((bytes = fread(buffer, 1, sizeof(buffer), f)) > 0) {
  370. bool eof = bytes < sizeof(buffer);
  371. S_parser_feed(parser, buffer, bytes, eof);
  372. if (eof) {
  373. break;
  374. }
  375. }
  376. document = cmark_parser_finish(parser);
  377. cmark_parser_free(parser);
  378. return document;
  379. }
  380. cmark_node *cmark_parse_document(const char *buffer, size_t len)
  381. {
  382. cmark_parser *parser = cmark_parser_new();
  383. cmark_node *document;
  384. S_parser_feed(parser, (const unsigned char *)buffer, len, true);
  385. document = cmark_parser_finish(parser);
  386. cmark_parser_free(parser);
  387. return document;
  388. }
  389. void
  390. cmark_parser_feed(cmark_parser *parser, const char *buffer, size_t len)
  391. {
  392. S_parser_feed(parser, (const unsigned char *)buffer, len, false);
  393. }
  394. static void
  395. S_parser_feed(cmark_parser *parser, const unsigned char *buffer, size_t len,
  396. bool eof)
  397. {
  398. const unsigned char *end = buffer + len;
  399. while (buffer < end) {
  400. const unsigned char *eol
  401. = (const unsigned char *)memchr(buffer, '\n',
  402. end - buffer);
  403. size_t line_len;
  404. if (eol) {
  405. line_len = eol + 1 - buffer;
  406. } else if (eof) {
  407. line_len = end - buffer;
  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. } else {
  418. S_process_line(parser, buffer, line_len);
  419. }
  420. buffer += line_len;
  421. }
  422. }
  423. static void chop_trailing_hashtags(cmark_chunk *ch)
  424. {
  425. int n, orig_n;
  426. cmark_chunk_rtrim(ch);
  427. orig_n = n = ch->len - 1;
  428. // if string ends in space followed by #s, remove these:
  429. while (n >= 0 && peek_at(ch, n) == '#')
  430. n--;
  431. // Check for a be a space before the final #s:
  432. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  433. ch->len = n;
  434. cmark_chunk_rtrim(ch);
  435. }
  436. }
  437. static void
  438. S_process_line(cmark_parser *parser, const unsigned char *buffer, size_t bytes)
  439. {
  440. cmark_node* last_matched_container;
  441. int offset = 0;
  442. int matched = 0;
  443. int lev = 0;
  444. int i;
  445. cmark_list *data = NULL;
  446. bool all_matched = true;
  447. cmark_node* container;
  448. cmark_node* cur = parser->current;
  449. bool blank = false;
  450. int first_nonspace;
  451. int indent;
  452. cmark_chunk input;
  453. utf8proc_detab(parser->curline, buffer, bytes);
  454. // Add a newline to the end if not present:
  455. // TODO this breaks abstraction:
  456. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  457. cmark_strbuf_putc(parser->curline, '\n');
  458. }
  459. input.data = parser->curline->ptr;
  460. input.len = parser->curline->size;
  461. // container starts at the document root.
  462. container = parser->root;
  463. parser->line_number++;
  464. // for each containing cmark_node, try to parse the associated line start.
  465. // bail out on failure: container will point to the last matching cmark_node.
  466. while (container->last_child && container->last_child->open) {
  467. container = container->last_child;
  468. first_nonspace = offset;
  469. while (peek_at(&input, first_nonspace) == ' ') {
  470. first_nonspace++;
  471. }
  472. indent = first_nonspace - offset;
  473. blank = peek_at(&input, first_nonspace) == '\n';
  474. if (container->type == NODE_BLOCK_QUOTE) {
  475. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  476. if (matched) {
  477. offset = first_nonspace + 1;
  478. if (peek_at(&input, offset) == ' ')
  479. offset++;
  480. } else {
  481. all_matched = false;
  482. }
  483. } else if (container->type == NODE_ITEM) {
  484. if (indent >= container->as.list.marker_offset +
  485. container->as.list.padding) {
  486. offset += container->as.list.marker_offset +
  487. container->as.list.padding;
  488. } else if (blank) {
  489. offset = first_nonspace;
  490. } else {
  491. all_matched = false;
  492. }
  493. } else if (container->type == NODE_CODE_BLOCK) {
  494. if (!container->as.code.fenced) { // indented
  495. if (indent >= CODE_INDENT) {
  496. offset += CODE_INDENT;
  497. } else if (blank) {
  498. offset = first_nonspace;
  499. } else {
  500. all_matched = false;
  501. }
  502. } else { // fenced
  503. matched = 0;
  504. if (indent <= 3 &&
  505. (peek_at(&input, first_nonspace) ==
  506. container->as.code.fence_char)) {
  507. matched = scan_close_code_fence(&input,
  508. first_nonspace);
  509. }
  510. if (matched >= container->as.code.fence_length) {
  511. // closing fence - and since we're at
  512. // the end of a line, we can return:
  513. all_matched = false;
  514. offset += matched;
  515. parser->current = finalize(parser, container);
  516. goto finished;
  517. } else {
  518. // skip opt. spaces of fence offset
  519. i = container->as.code.fence_offset;
  520. while (i > 0 &&
  521. peek_at(&input, offset) == ' ') {
  522. offset++;
  523. i--;
  524. }
  525. }
  526. }
  527. } else if (container->type == NODE_HEADER) {
  528. // a header can never contain more than one line
  529. all_matched = false;
  530. } else if (container->type == NODE_HTML) {
  531. if (blank) {
  532. all_matched = false;
  533. }
  534. } else if (container->type == NODE_PARAGRAPH) {
  535. if (blank) {
  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. if (blank && container->last_child) {
  660. container->last_child->last_line_blank = true;
  661. }
  662. // block quote lines are never blank as they start with >
  663. // and we don't count blanks in fenced code for purposes of tight/loose
  664. // lists or breaking out of lists. we also don't set last_line_blank
  665. // on an empty list item.
  666. container->last_line_blank = (blank &&
  667. container->type != NODE_BLOCK_QUOTE &&
  668. container->type != NODE_HEADER &&
  669. !(container->type == NODE_CODE_BLOCK &&
  670. container->as.code.fenced) &&
  671. !(container->type == NODE_ITEM &&
  672. container->first_child == NULL &&
  673. container->start_line == parser->line_number));
  674. cmark_node *cont = container;
  675. while (cont->parent) {
  676. cont->parent->last_line_blank = false;
  677. cont = cont->parent;
  678. }
  679. if (cur != last_matched_container &&
  680. container == last_matched_container &&
  681. !blank &&
  682. cur->type == NODE_PARAGRAPH &&
  683. cmark_strbuf_len(&cur->string_content) > 0) {
  684. add_line(cur, &input, offset);
  685. } else { // not a lazy continuation
  686. // finalize any blocks that were not matched and set cur to container:
  687. while (cur != last_matched_container) {
  688. cur = finalize(parser, cur);
  689. assert(cur != NULL);
  690. }
  691. if (container->type == NODE_CODE_BLOCK ||
  692. container->type == NODE_HTML) {
  693. add_line(container, &input, offset);
  694. } else if (blank) {
  695. // ??? do nothing
  696. } else if (accepts_lines(container->type)) {
  697. if (container->type == NODE_HEADER &&
  698. container->as.header.setext == false) {
  699. chop_trailing_hashtags(&input);
  700. }
  701. add_line(container, &input, first_nonspace);
  702. } else {
  703. // create paragraph container for line
  704. container = add_child(parser, container, NODE_PARAGRAPH, first_nonspace + 1);
  705. add_line(container, &input, first_nonspace);
  706. }
  707. parser->current = container;
  708. }
  709. finished:
  710. parser->last_line_length = parser->curline->size -
  711. (parser->curline->ptr[parser->curline->size - 1] == '\n' ?
  712. 1 : 0);
  713. ;
  714. cmark_strbuf_clear(parser->curline);
  715. }
  716. cmark_node *cmark_parser_finish(cmark_parser *parser)
  717. {
  718. if (parser->linebuf->size) {
  719. S_process_line(parser, parser->linebuf->ptr,
  720. parser->linebuf->size);
  721. cmark_strbuf_clear(parser->linebuf);
  722. }
  723. finalize_document(parser);
  724. cmark_strbuf_free(parser->curline);
  725. #if CMARK_DEBUG_NODES
  726. if (cmark_node_check(parser->root, stderr)) {
  727. abort();
  728. }
  729. #endif
  730. return parser->root;
  731. }