aboutsummaryrefslogtreecommitdiff
path: root/src/blocks.c
blob: 45d43ecae653a5df78a69386ff8fe5c4aa0af788 (plain)
  1. #include <stdlib.h>
  2. #include <assert.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include "config.h"
  6. #include "ast.h"
  7. #include "cmark.h"
  8. #include "references.h"
  9. #include "utf8.h"
  10. #include "scanners.h"
  11. #include "inlines.h"
  12. #include "html/houdini.h"
  13. #include "buffer.h"
  14. #include "debug.h"
  15. #define CODE_INDENT 4
  16. #define peek_at(i, n) (i)->data[n]
  17. static node_block* make_block(cmark_block_tag tag, int start_line, int start_column)
  18. {
  19. node_block* e;
  20. e = (node_block *)calloc(1, sizeof(*e));
  21. if(e != NULL) {
  22. e->tag = tag;
  23. e->open = true;
  24. e->start_line = start_line;
  25. e->start_column = start_column;
  26. e->end_line = start_line;
  27. strbuf_init(&e->string_content, 32);
  28. }
  29. return e;
  30. }
  31. // Create a root document node_block.
  32. static node_block* make_document()
  33. {
  34. node_block *e = make_block(BLOCK_DOCUMENT, 1, 1);
  35. return e;
  36. }
  37. cmark_doc_parser *cmark_new_doc_parser()
  38. {
  39. cmark_doc_parser *parser = (cmark_doc_parser*)malloc(sizeof(cmark_doc_parser));
  40. node_block *document = make_document();
  41. strbuf *line = (strbuf*)malloc(sizeof(strbuf));
  42. cmark_strbuf_init(line, 256);
  43. parser->refmap = reference_map_new();
  44. parser->root = document;
  45. parser->current = document;
  46. parser->line_number = 0;
  47. parser->curline = line;
  48. return parser;
  49. }
  50. void cmark_free_doc_parser(cmark_doc_parser *parser)
  51. {
  52. cmark_strbuf_free(parser->curline);
  53. free(parser->curline);
  54. cmark_reference_map_free(parser->refmap);
  55. free(parser);
  56. }
  57. static void finalize(cmark_doc_parser *parser, node_block* b, int line_number);
  58. // Returns true if line has only space characters, else false.
  59. static bool is_blank(strbuf *s, int offset)
  60. {
  61. while (offset < s->size) {
  62. switch (s->ptr[offset]) {
  63. case '\n':
  64. return true;
  65. case ' ':
  66. offset++;
  67. break;
  68. default:
  69. return false;
  70. }
  71. }
  72. return true;
  73. }
  74. static inline bool can_contain(cmark_block_tag parent_type, cmark_block_tag child_type)
  75. {
  76. return ( parent_type == BLOCK_DOCUMENT ||
  77. parent_type == BLOCK_BQUOTE ||
  78. parent_type == BLOCK_LIST_ITEM ||
  79. (parent_type == BLOCK_LIST && child_type == BLOCK_LIST_ITEM) );
  80. }
  81. static inline bool accepts_lines(int block_type)
  82. {
  83. return (block_type == BLOCK_PARAGRAPH ||
  84. block_type == BLOCK_ATX_HEADER ||
  85. block_type == BLOCK_INDENTED_CODE ||
  86. block_type == BLOCK_FENCED_CODE);
  87. }
  88. static void add_line(node_block* node_block, chunk *ch, int offset)
  89. {
  90. assert(node_block->open);
  91. strbuf_put(&node_block->string_content, ch->data + offset, ch->len - offset);
  92. }
  93. static void remove_trailing_blank_lines(strbuf *ln)
  94. {
  95. int i;
  96. for (i = ln->size - 1; i >= 0; --i) {
  97. unsigned char c = ln->ptr[i];
  98. if (c != ' ' && c != '\t' && c != '\r' && c != '\n')
  99. break;
  100. }
  101. if (i < 0) {
  102. strbuf_clear(ln);
  103. return;
  104. }
  105. i = strbuf_strchr(ln, '\n', i);
  106. if (i >= 0)
  107. strbuf_truncate(ln, i);
  108. }
  109. // Check to see if a node_block ends with a blank line, descending
  110. // if needed into lists and sublists.
  111. static bool ends_with_blank_line(node_block* node_block)
  112. {
  113. if (node_block->last_line_blank) {
  114. return true;
  115. }
  116. if ((node_block->tag == BLOCK_LIST || node_block->tag == BLOCK_LIST_ITEM) && node_block->last_child) {
  117. return ends_with_blank_line(node_block->last_child);
  118. } else {
  119. return false;
  120. }
  121. }
  122. // Break out of all containing lists
  123. static int break_out_of_lists(cmark_doc_parser *parser, node_block ** bptr, int line_number)
  124. {
  125. node_block *container = *bptr;
  126. node_block *b = parser->root;
  127. // find first containing BLOCK_LIST:
  128. while (b && b->tag != BLOCK_LIST) {
  129. b = b->last_child;
  130. }
  131. if (b) {
  132. while (container && container != b) {
  133. finalize(parser, container, line_number);
  134. container = container->parent;
  135. }
  136. finalize(parser, b, line_number);
  137. *bptr = b->parent;
  138. }
  139. return 0;
  140. }
  141. static void finalize(cmark_doc_parser *parser, node_block* b, int line_number)
  142. {
  143. int firstlinelen;
  144. int pos;
  145. node_block* item;
  146. node_block* subitem;
  147. if (!b->open)
  148. return; // don't do anything if the node_block is already closed
  149. b->open = false;
  150. if (line_number > b->start_line) {
  151. b->end_line = line_number - 1;
  152. } else {
  153. b->end_line = line_number;
  154. }
  155. switch (b->tag) {
  156. case BLOCK_PARAGRAPH:
  157. pos = 0;
  158. while (strbuf_at(&b->string_content, 0) == '[' &&
  159. (pos = parse_reference_inline(&b->string_content, parser->refmap))) {
  160. strbuf_drop(&b->string_content, pos);
  161. }
  162. if (is_blank(&b->string_content, 0)) {
  163. b->tag = BLOCK_REFERENCE_DEF;
  164. }
  165. break;
  166. case BLOCK_INDENTED_CODE:
  167. remove_trailing_blank_lines(&b->string_content);
  168. strbuf_putc(&b->string_content, '\n');
  169. break;
  170. case BLOCK_FENCED_CODE:
  171. // first line of contents becomes info
  172. firstlinelen = strbuf_strchr(&b->string_content, '\n', 0);
  173. strbuf_init(&b->as.code.info, 0);
  174. houdini_unescape_html_f(
  175. &b->as.code.info,
  176. b->string_content.ptr,
  177. firstlinelen
  178. );
  179. strbuf_drop(&b->string_content, firstlinelen + 1);
  180. strbuf_trim(&b->as.code.info);
  181. strbuf_unescape(&b->as.code.info);
  182. break;
  183. case BLOCK_LIST: // determine tight/loose status
  184. b->as.list.tight = true; // tight by default
  185. item = b->children;
  186. while (item) {
  187. // check for non-final non-empty list item ending with blank line:
  188. if (item->last_line_blank && item->next) {
  189. b->as.list.tight = false;
  190. break;
  191. }
  192. // recurse into children of list item, to see if there are
  193. // spaces between them:
  194. subitem = item->children;
  195. while (subitem) {
  196. if (ends_with_blank_line(subitem) &&
  197. (item->next || subitem->next)) {
  198. b->as.list.tight = false;
  199. break;
  200. }
  201. subitem = subitem->next;
  202. }
  203. if (!(b->as.list.tight)) {
  204. break;
  205. }
  206. item = item->next;
  207. }
  208. break;
  209. default:
  210. break;
  211. }
  212. }
  213. // Add a node_block as child of another. Return pointer to child.
  214. static node_block* add_child(cmark_doc_parser *parser, node_block* parent,
  215. cmark_block_tag block_type, int start_line, int start_column)
  216. {
  217. assert(parent);
  218. // if 'parent' isn't the kind of node_block that can accept this child,
  219. // then back up til we hit a node_block that can.
  220. while (!can_contain(parent->tag, block_type)) {
  221. finalize(parser, parent, start_line);
  222. parent = parent->parent;
  223. }
  224. node_block* child = make_block(block_type, start_line, start_column);
  225. child->parent = parent;
  226. if (parent->last_child) {
  227. parent->last_child->next = child;
  228. child->prev = parent->last_child;
  229. } else {
  230. parent->children = child;
  231. child->prev = NULL;
  232. }
  233. parent->last_child = child;
  234. return child;
  235. }
  236. typedef struct BlockStack {
  237. struct BlockStack *previous;
  238. node_block *next_sibling;
  239. } block_stack;
  240. // Walk through node_block and all children, recursively, parsing
  241. // string content into inline content where appropriate.
  242. static void process_inlines(node_block* cur, reference_map *refmap)
  243. {
  244. block_stack* stack = NULL;
  245. block_stack* newstack = NULL;
  246. while (cur != NULL) {
  247. switch (cur->tag) {
  248. case BLOCK_PARAGRAPH:
  249. case BLOCK_ATX_HEADER:
  250. case BLOCK_SETEXT_HEADER:
  251. cur->inline_content = parse_inlines(&cur->string_content, refmap);
  252. break;
  253. default:
  254. break;
  255. }
  256. if (cur->children) {
  257. newstack = (block_stack*)malloc(sizeof(block_stack));
  258. if (newstack == NULL) return;
  259. newstack->previous = stack;
  260. stack = newstack;
  261. stack->next_sibling = cur->next;
  262. cur = cur->children;
  263. } else {
  264. cur = cur->next;
  265. }
  266. while (cur == NULL && stack != NULL) {
  267. cur = stack->next_sibling;
  268. newstack = stack->previous;
  269. free(stack);
  270. stack = newstack;
  271. }
  272. }
  273. while (stack != NULL) {
  274. newstack = stack->previous;
  275. free(stack);
  276. stack = newstack;
  277. }
  278. }
  279. // Attempts to parse a list item marker (bullet or enumerated).
  280. // On success, returns length of the marker, and populates
  281. // data with the details. On failure, returns 0.
  282. static int parse_list_marker(chunk *input, int pos, struct ListData ** dataptr)
  283. {
  284. unsigned char c;
  285. int startpos;
  286. struct ListData * data;
  287. startpos = pos;
  288. c = peek_at(input, pos);
  289. if ((c == '*' || c == '-' || c == '+') && !scan_hrule(input, pos)) {
  290. pos++;
  291. if (!isspace(peek_at(input, pos))) {
  292. return 0;
  293. }
  294. data = (struct ListData *)calloc(1, sizeof(*data));
  295. if(data == NULL) {
  296. return 0;
  297. } else {
  298. data->marker_offset = 0; // will be adjusted later
  299. data->list_type = CMARK_BULLET_LIST;
  300. data->bullet_char = c;
  301. data->start = 1;
  302. data->delimiter = CMARK_PERIOD_DELIM;
  303. data->tight = false;
  304. }
  305. } else if (isdigit(c)) {
  306. int start = 0;
  307. do {
  308. start = (10 * start) + (peek_at(input, pos) - '0');
  309. pos++;
  310. } while (isdigit(peek_at(input, pos)));
  311. c = peek_at(input, pos);
  312. if (c == '.' || c == ')') {
  313. pos++;
  314. if (!isspace(peek_at(input, pos))) {
  315. return 0;
  316. }
  317. data = (struct ListData *)calloc(1, sizeof(*data));
  318. if(data == NULL) {
  319. return 0;
  320. } else {
  321. data->marker_offset = 0; // will be adjusted later
  322. data->list_type = CMARK_ORDERED_LIST;
  323. data->bullet_char = 0;
  324. data->start = start;
  325. data->delimiter = (c == '.' ? CMARK_PERIOD_DELIM : CMARK_PAREN_DELIM);
  326. data->tight = false;
  327. }
  328. } else {
  329. return 0;
  330. }
  331. } else {
  332. return 0;
  333. }
  334. *dataptr = data;
  335. return (pos - startpos);
  336. }
  337. // Return 1 if list item belongs in list, else 0.
  338. static int lists_match(struct ListData *list_data, struct ListData *item_data)
  339. {
  340. return (list_data->list_type == item_data->list_type &&
  341. list_data->delimiter == item_data->delimiter &&
  342. // list_data->marker_offset == item_data.marker_offset &&
  343. list_data->bullet_char == item_data->bullet_char);
  344. }
  345. static node_block *finalize_document(cmark_doc_parser *parser)
  346. {
  347. while (parser->current != parser->root) {
  348. finalize(parser, parser->current, parser->line_number);
  349. parser->current = parser->current->parent;
  350. }
  351. finalize(parser, parser->root, parser->line_number);
  352. process_inlines(parser->root, parser->refmap);
  353. return parser->root;
  354. }
  355. extern node_block *cmark_parse_file(FILE *f)
  356. {
  357. unsigned char buffer[4096];
  358. cmark_doc_parser *parser = cmark_new_doc_parser();
  359. size_t offset;
  360. node_block *document;
  361. while (fgets((char *)buffer, sizeof(buffer), f)) {
  362. offset = strlen((char *)buffer);
  363. cmark_process_line(parser, buffer, offset);
  364. }
  365. document = cmark_finish(parser);
  366. cmark_free_doc_parser(parser);
  367. return document;
  368. }
  369. extern node_block *cmark_parse_document(const unsigned char *buffer, size_t len)
  370. {
  371. int linenum = 1;
  372. const unsigned char *end = buffer + len;
  373. size_t offset;
  374. cmark_doc_parser *parser = cmark_new_doc_parser();
  375. node_block *document;
  376. while (buffer < end) {
  377. const unsigned char *eol = (unsigned char *)memchr(buffer, '\n', end - buffer);
  378. offset = eol ? (eol - buffer) + 1 : eol - buffer;
  379. cmark_process_line(parser, buffer, offset);
  380. buffer += offset;
  381. linenum++;
  382. }
  383. document = cmark_finish(parser);
  384. cmark_free_doc_parser(parser);
  385. return document;
  386. }
  387. static void chop_trailing_hashtags(chunk *ch)
  388. {
  389. int n, orig_n;
  390. chunk_rtrim(ch);
  391. orig_n = n = ch->len - 1;
  392. // if string ends in space followed by #s, remove these:
  393. while (n >= 0 && peek_at(ch, n) == '#')
  394. n--;
  395. // Check for a be a space before the final #s:
  396. if (n != orig_n && n >= 0 && peek_at(ch, n) == ' ') {
  397. ch->len = n;
  398. chunk_rtrim(ch);
  399. }
  400. }
  401. void cmark_process_line(cmark_doc_parser *parser, const unsigned char *buffer,
  402. size_t bytes)
  403. {
  404. node_block* last_matched_container;
  405. int offset = 0;
  406. int matched = 0;
  407. int lev = 0;
  408. int i;
  409. struct ListData * data = NULL;
  410. bool all_matched = true;
  411. node_block* container;
  412. node_block* cur = parser->current;
  413. bool blank = false;
  414. int first_nonspace;
  415. int indent;
  416. chunk input;
  417. utf8proc_detab(parser->curline, buffer, bytes);
  418. // Add a newline to the end if not present:
  419. // TODO this breaks abstraction:
  420. if (parser->curline->ptr[parser->curline->size - 1] != '\n') {
  421. strbuf_putc(parser->curline, '\n');
  422. }
  423. input.data = parser->curline->ptr;
  424. input.len = parser->curline->size;
  425. // container starts at the document root.
  426. container = parser->root;
  427. parser->line_number++;
  428. // for each containing node_block, try to parse the associated line start.
  429. // bail out on failure: container will point to the last matching node_block.
  430. while (container->last_child && container->last_child->open) {
  431. container = container->last_child;
  432. first_nonspace = offset;
  433. while (peek_at(&input, first_nonspace) == ' ') {
  434. first_nonspace++;
  435. }
  436. indent = first_nonspace - offset;
  437. blank = peek_at(&input, first_nonspace) == '\n';
  438. if (container->tag == BLOCK_BQUOTE) {
  439. matched = indent <= 3 && peek_at(&input, first_nonspace) == '>';
  440. if (matched) {
  441. offset = first_nonspace + 1;
  442. if (peek_at(&input, offset) == ' ')
  443. offset++;
  444. } else {
  445. all_matched = false;
  446. }
  447. } else if (container->tag == BLOCK_LIST_ITEM) {
  448. if (indent >= container->as.list.marker_offset +
  449. container->as.list.padding) {
  450. offset += container->as.list.marker_offset +
  451. container->as.list.padding;
  452. } else if (blank) {
  453. offset = first_nonspace;
  454. } else {
  455. all_matched = false;
  456. }
  457. } else if (container->tag == BLOCK_INDENTED_CODE) {
  458. if (indent >= CODE_INDENT) {
  459. offset += CODE_INDENT;
  460. } else if (blank) {
  461. offset = first_nonspace;
  462. } else {
  463. all_matched = false;
  464. }
  465. } else if (container->tag == BLOCK_ATX_HEADER ||
  466. container->tag == BLOCK_SETEXT_HEADER) {
  467. // a header can never contain more than one line
  468. all_matched = false;
  469. } else if (container->tag == BLOCK_FENCED_CODE) {
  470. // skip optional spaces of fence offset
  471. i = container->as.code.fence_offset;
  472. while (i > 0 && peek_at(&input, offset) == ' ') {
  473. offset++;
  474. i--;
  475. }
  476. } else if (container->tag == BLOCK_HTML) {
  477. if (blank) {
  478. all_matched = false;
  479. }
  480. } else if (container->tag == BLOCK_PARAGRAPH) {
  481. if (blank) {
  482. container->last_line_blank = true;
  483. all_matched = false;
  484. }
  485. }
  486. if (!all_matched) {
  487. container = container->parent; // back up to last matching node_block
  488. break;
  489. }
  490. }
  491. last_matched_container = container;
  492. // check to see if we've hit 2nd blank line, break out of list:
  493. if (blank && container->last_line_blank) {
  494. break_out_of_lists(parser, &container, parser->line_number);
  495. }
  496. // unless last matched container is code node_block, try new container starts:
  497. while (container->tag != BLOCK_FENCED_CODE && container->tag != BLOCK_INDENTED_CODE &&
  498. container->tag != BLOCK_HTML) {
  499. first_nonspace = offset;
  500. while (peek_at(&input, first_nonspace) == ' ')
  501. first_nonspace++;
  502. indent = first_nonspace - offset;
  503. blank = peek_at(&input, first_nonspace) == '\n';
  504. if (indent >= CODE_INDENT) {
  505. if (cur->tag != BLOCK_PARAGRAPH && !blank) {
  506. offset += CODE_INDENT;
  507. container = add_child(parser, container, BLOCK_INDENTED_CODE, parser->line_number, offset + 1);
  508. } else { // indent > 4 in lazy line
  509. break;
  510. }
  511. } else if (peek_at(&input, first_nonspace) == '>') {
  512. offset = first_nonspace + 1;
  513. // optional following character
  514. if (peek_at(&input, offset) == ' ')
  515. offset++;
  516. container = add_child(parser, container, BLOCK_BQUOTE, parser->line_number, offset + 1);
  517. } else if ((matched = scan_atx_header_start(&input, first_nonspace))) {
  518. offset = first_nonspace + matched;
  519. container = add_child(parser, container, BLOCK_ATX_HEADER, parser->line_number, offset + 1);
  520. int hashpos = chunk_strchr(&input, '#', first_nonspace);
  521. int level = 0;
  522. while (peek_at(&input, hashpos) == '#') {
  523. level++;
  524. hashpos++;
  525. }
  526. container->as.header.level = level;
  527. } else if ((matched = scan_open_code_fence(&input, first_nonspace))) {
  528. container = add_child(parser, container, BLOCK_FENCED_CODE, parser->line_number, first_nonspace + 1);
  529. container->as.code.fence_char = peek_at(&input, first_nonspace);
  530. container->as.code.fence_length = matched;
  531. container->as.code.fence_offset = first_nonspace - offset;
  532. offset = first_nonspace + matched;
  533. } else if ((matched = scan_html_block_tag(&input, first_nonspace))) {
  534. container = add_child(parser, container, BLOCK_HTML, parser->line_number, first_nonspace + 1);
  535. // note, we don't adjust offset because the tag is part of the text
  536. } else if (container->tag == BLOCK_PARAGRAPH &&
  537. (lev = scan_setext_header_line(&input, first_nonspace)) &&
  538. // check that there is only one line in the paragraph:
  539. strbuf_strrchr(&container->string_content, '\n',
  540. strbuf_len(&container->string_content) - 2) < 0) {
  541. container->tag = BLOCK_SETEXT_HEADER;
  542. container->as.header.level = lev;
  543. offset = input.len - 1;
  544. } else if (!(container->tag == BLOCK_PARAGRAPH && !all_matched) &&
  545. (matched = scan_hrule(&input, first_nonspace))) {
  546. // it's only now that we know the line is not part of a setext header:
  547. container = add_child(parser, container, BLOCK_HRULE, parser->line_number, first_nonspace + 1);
  548. finalize(parser, container, parser->line_number);
  549. container = container->parent;
  550. offset = input.len - 1;
  551. } else if ((matched = parse_list_marker(&input, first_nonspace, &data))) {
  552. // compute padding:
  553. offset = first_nonspace + matched;
  554. i = 0;
  555. while (i <= 5 && peek_at(&input, offset + i) == ' ') {
  556. i++;
  557. }
  558. // i = number of spaces after marker, up to 5
  559. if (i >= 5 || i < 1 || peek_at(&input, offset) == '\n') {
  560. data->padding = matched + 1;
  561. if (i > 0) {
  562. offset += 1;
  563. }
  564. } else {
  565. data->padding = matched + i;
  566. offset += i;
  567. }
  568. // check container; if it's a list, see if this list item
  569. // can continue the list; otherwise, create a list container.
  570. data->marker_offset = indent;
  571. if (container->tag != BLOCK_LIST ||
  572. !lists_match(&container->as.list, data)) {
  573. container = add_child(parser, container, BLOCK_LIST, parser->line_number,
  574. first_nonspace + 1);
  575. memcpy(&container->as.list, data, sizeof(*data));
  576. }
  577. // add the list item
  578. container = add_child(parser, container, BLOCK_LIST_ITEM, parser->line_number,
  579. first_nonspace + 1);
  580. /* TODO: static */
  581. memcpy(&container->as.list, data, sizeof(*data));
  582. free(data);
  583. } else {
  584. break;
  585. }
  586. if (accepts_lines(container->tag)) {
  587. // if it's a line container, it can't contain other containers
  588. break;
  589. }
  590. }
  591. // what remains at offset is a text line. add the text to the
  592. // appropriate container.
  593. first_nonspace = offset;
  594. while (peek_at(&input, first_nonspace) == ' ')
  595. first_nonspace++;
  596. indent = first_nonspace - offset;
  597. blank = peek_at(&input, first_nonspace) == '\n';
  598. // node_block quote lines are never blank as they start with >
  599. // and we don't count blanks in fenced code for purposes of tight/loose
  600. // lists or breaking out of lists. we also don't set last_line_blank
  601. // on an empty list item.
  602. container->last_line_blank = (blank &&
  603. container->tag != BLOCK_BQUOTE &&
  604. container->tag != BLOCK_FENCED_CODE &&
  605. !(container->tag == BLOCK_LIST_ITEM &&
  606. container->children == NULL &&
  607. container->start_line == parser->line_number));
  608. node_block *cont = container;
  609. while (cont->parent) {
  610. cont->parent->last_line_blank = false;
  611. cont = cont->parent;
  612. }
  613. if (cur != last_matched_container &&
  614. container == last_matched_container &&
  615. !blank &&
  616. cur->tag == BLOCK_PARAGRAPH &&
  617. strbuf_len(&cur->string_content) > 0) {
  618. add_line(cur, &input, offset);
  619. } else { // not a lazy continuation
  620. // finalize any blocks that were not matched and set cur to container:
  621. while (cur != last_matched_container) {
  622. finalize(parser, cur, parser->line_number);
  623. cur = cur->parent;
  624. assert(cur != NULL);
  625. }
  626. if (container->tag == BLOCK_INDENTED_CODE) {
  627. add_line(container, &input, offset);
  628. } else if (container->tag == BLOCK_FENCED_CODE) {
  629. matched = 0;
  630. if (indent <= 3 &&
  631. peek_at(&input, first_nonspace) == container->as.code.fence_char) {
  632. int fence_len = scan_close_code_fence(&input, first_nonspace);
  633. if (fence_len > container->as.code.fence_length)
  634. matched = 1;
  635. }
  636. if (matched) {
  637. // if closing fence, don't add line to container; instead, close it:
  638. finalize(parser, container, parser->line_number);
  639. container = container->parent; // back up to parent
  640. } else {
  641. add_line(container, &input, offset);
  642. }
  643. } else if (container->tag == BLOCK_HTML) {
  644. add_line(container, &input, offset);
  645. } else if (blank) {
  646. // ??? do nothing
  647. } else if (container->tag == BLOCK_ATX_HEADER) {
  648. chop_trailing_hashtags(&input);
  649. add_line(container, &input, first_nonspace);
  650. finalize(parser, container, parser->line_number);
  651. container = container->parent;
  652. } else if (accepts_lines(container->tag)) {
  653. add_line(container, &input, first_nonspace);
  654. } else if (container->tag != BLOCK_HRULE && container->tag != BLOCK_SETEXT_HEADER) {
  655. // create paragraph container for line
  656. container = add_child(parser, container, BLOCK_PARAGRAPH, parser->line_number, first_nonspace + 1);
  657. add_line(container, &input, first_nonspace);
  658. } else {
  659. assert(false);
  660. }
  661. parser->current = container;
  662. }
  663. strbuf_clear(parser->curline);
  664. }
  665. node_block *cmark_finish(cmark_doc_parser *parser)
  666. {
  667. finalize_document(parser);
  668. strbuf_free(parser->curline);
  669. return parser->root;
  670. }