aboutsummaryrefslogtreecommitdiff
path: root/src/inlines.c
blob: 49dc7810e0c275d1b048b8ae34805d855f02730d (plain)
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <ctype.h>
  5. #include "config.h"
  6. #include "node.h"
  7. #include "parser.h"
  8. #include "references.h"
  9. #include "cmark.h"
  10. #include "html/houdini.h"
  11. #include "utf8.h"
  12. #include "scanners.h"
  13. #include "inlines.h"
  14. // Macros for creating various kinds of simple.
  15. #define make_str(s) make_literal(CMARK_NODE_TEXT, s)
  16. #define make_code(s) make_literal(CMARK_NODE_INLINE_CODE, s)
  17. #define make_raw_html(s) make_literal(CMARK_NODE_INLINE_HTML, s)
  18. #define make_linebreak() make_simple(CMARK_NODE_LINEBREAK)
  19. #define make_softbreak() make_simple(CMARK_NODE_SOFTBREAK)
  20. #define make_emph(contents) make_inlines(CMARK_NODE_EMPH, contents)
  21. #define make_strong(contents) make_inlines(CMARK_NODE_STRONG, contents)
  22. typedef struct DelimiterStack {
  23. struct DelimiterStack *previous;
  24. struct DelimiterStack *next;
  25. cmark_node *first_inline;
  26. int delim_count;
  27. unsigned char delim_char;
  28. int position;
  29. bool can_open;
  30. bool can_close;
  31. } delimiter_stack;
  32. typedef struct Subject {
  33. chunk input;
  34. int pos;
  35. cmark_reference_map *refmap;
  36. delimiter_stack *delimiters;
  37. } subject;
  38. static int parse_inline(subject* subj, cmark_node * parent);
  39. static void subject_from_buf(subject *e, strbuf *buffer,
  40. cmark_reference_map *refmap);
  41. static int subject_find_special_char(subject *subj);
  42. static unsigned char *cmark_clean_autolink(chunk *url, int is_email)
  43. {
  44. strbuf buf = GH_BUF_INIT;
  45. chunk_trim(url);
  46. if (url->len == 0)
  47. return NULL;
  48. if (is_email)
  49. strbuf_puts(&buf, "mailto:");
  50. houdini_unescape_html_f(&buf, url->data, url->len);
  51. return strbuf_detach(&buf);
  52. }
  53. static inline cmark_node *make_link(cmark_node *label, unsigned char *url, unsigned char *title)
  54. {
  55. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  56. if(e != NULL) {
  57. e->type = CMARK_NODE_LINK;
  58. e->first_child = label;
  59. e->last_child = label;
  60. e->as.link.url = url;
  61. e->as.link.title = title;
  62. e->next = NULL;
  63. label->parent = e;
  64. }
  65. return e;
  66. }
  67. static inline cmark_node* make_autolink(cmark_node* label, cmark_chunk url, int is_email)
  68. {
  69. return make_link(label, cmark_clean_autolink(&url, is_email), NULL);
  70. }
  71. // Setting 'last_child' and the parent of 'contents' is up to the caller.
  72. static inline cmark_node* make_inlines(cmark_node_type t, cmark_node* contents)
  73. {
  74. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  75. if(e != NULL) {
  76. e->type = t;
  77. e->first_child = contents;
  78. e->next = NULL;
  79. e->prev = NULL;
  80. e->parent = NULL;
  81. // These fields aren't used for inlines:
  82. e->start_line = 0;
  83. e->start_column = 0;
  84. e->end_line = 0;
  85. }
  86. return e;
  87. }
  88. // Create an inline with a literal string value.
  89. static inline cmark_node* make_literal(cmark_node_type t, cmark_chunk s)
  90. {
  91. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  92. if(e != NULL) {
  93. e->type = t;
  94. e->as.literal = s;
  95. e->next = NULL;
  96. e->prev = NULL;
  97. e->parent = NULL;
  98. e->first_child = NULL;
  99. e->last_child = NULL;
  100. // These fields aren't used for inlines:
  101. e->start_line = 0;
  102. e->start_column = 0;
  103. e->end_line = 0;
  104. }
  105. return e;
  106. }
  107. // Create an inline with no value.
  108. static inline cmark_node* make_simple(cmark_node_type t)
  109. {
  110. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  111. if(e != NULL) {
  112. e->type = t;
  113. e->next = NULL;
  114. e->prev = NULL;
  115. e->parent = NULL;
  116. e->first_child = NULL;
  117. e->last_child = NULL;
  118. // These fields aren't used for inlines:
  119. e->start_line = 0;
  120. e->start_column = 0;
  121. e->end_line = 0;
  122. }
  123. return e;
  124. }
  125. static unsigned char *bufdup(const unsigned char *buf)
  126. {
  127. unsigned char *new_buf = NULL;
  128. if (buf) {
  129. int len = strlen((char *)buf);
  130. new_buf = (unsigned char *)calloc(len + 1, sizeof(*new_buf));
  131. if(new_buf != NULL) {
  132. memcpy(new_buf, buf, len + 1);
  133. }
  134. }
  135. return new_buf;
  136. }
  137. static void subject_from_buf(subject *e, strbuf *buffer,
  138. cmark_reference_map *refmap)
  139. {
  140. e->input.data = buffer->ptr;
  141. e->input.len = buffer->size;
  142. e->input.alloc = 0;
  143. e->pos = 0;
  144. e->refmap = refmap;
  145. e->delimiters = NULL;
  146. chunk_rtrim(&e->input);
  147. }
  148. static inline int isbacktick(int c)
  149. {
  150. return (c == '`');
  151. }
  152. static inline unsigned char peek_char(subject *subj)
  153. {
  154. return (subj->pos < subj->input.len) ? subj->input.data[subj->pos] : 0;
  155. }
  156. static inline unsigned char peek_at(subject *subj, int pos)
  157. {
  158. return subj->input.data[pos];
  159. }
  160. // Return true if there are more characters in the subject.
  161. static inline int is_eof(subject* subj)
  162. {
  163. return (subj->pos >= subj->input.len);
  164. }
  165. // Advance the subject. Doesn't check for eof.
  166. #define advance(subj) (subj)->pos += 1
  167. // Take characters while a predicate holds, and return a string.
  168. static inline chunk take_while(subject* subj, int (*f)(int))
  169. {
  170. unsigned char c;
  171. int startpos = subj->pos;
  172. int len = 0;
  173. while ((c = peek_char(subj)) && (*f)(c)) {
  174. advance(subj);
  175. len++;
  176. }
  177. return chunk_dup(&subj->input, startpos, len);
  178. }
  179. // Try to process a backtick code span that began with a
  180. // span of ticks of length openticklength length (already
  181. // parsed). Return 0 if you don't find matching closing
  182. // backticks, otherwise return the position in the subject
  183. // after the closing backticks.
  184. static int scan_to_closing_backticks(subject* subj, int openticklength)
  185. {
  186. // read non backticks
  187. unsigned char c;
  188. while ((c = peek_char(subj)) && c != '`') {
  189. advance(subj);
  190. }
  191. if (is_eof(subj)) {
  192. return 0; // did not find closing ticks, return 0
  193. }
  194. int numticks = 0;
  195. while (peek_char(subj) == '`') {
  196. advance(subj);
  197. numticks++;
  198. }
  199. if (numticks != openticklength){
  200. return(scan_to_closing_backticks(subj, openticklength));
  201. }
  202. return (subj->pos);
  203. }
  204. // Parse backtick code section or raw backticks, return an inline.
  205. // Assumes that the subject has a backtick at the current position.
  206. static cmark_node* handle_backticks(subject *subj)
  207. {
  208. chunk openticks = take_while(subj, isbacktick);
  209. int startpos = subj->pos;
  210. int endpos = scan_to_closing_backticks(subj, openticks.len);
  211. if (endpos == 0) { // not found
  212. subj->pos = startpos; // rewind
  213. return make_str(openticks);
  214. } else {
  215. strbuf buf = GH_BUF_INIT;
  216. strbuf_set(&buf, subj->input.data + startpos, endpos - startpos - openticks.len);
  217. strbuf_trim(&buf);
  218. strbuf_normalize_whitespace(&buf);
  219. return make_code(chunk_buf_detach(&buf));
  220. }
  221. }
  222. // Scan ***, **, or * and return number scanned, or 0.
  223. // Advances position.
  224. static int scan_delims(subject* subj, unsigned char c, bool * can_open, bool * can_close)
  225. {
  226. int numdelims = 0;
  227. unsigned char char_before, char_after;
  228. char_before = subj->pos == 0 ? '\n' : peek_at(subj, subj->pos - 1);
  229. while (peek_char(subj) == c) {
  230. numdelims++;
  231. advance(subj);
  232. }
  233. char_after = peek_char(subj);
  234. *can_open = numdelims > 0 && !isspace(char_after);
  235. *can_close = numdelims > 0 && !isspace(char_before);
  236. if (c == '_') {
  237. *can_open = *can_open && !isalnum(char_before);
  238. *can_close = *can_close && !isalnum(char_after);
  239. }
  240. return numdelims;
  241. }
  242. /*
  243. static void print_delimiters(subject *subj)
  244. {
  245. delimiter_stack *tempstack;
  246. tempstack = subj->delimiters;
  247. while (tempstack != NULL) {
  248. printf("Item at %p: %d %d %d %d next(%p) prev(%p)\n",
  249. tempstack, tempstack->delim_count, tempstack->delim_char,
  250. tempstack->can_open, tempstack->can_close,
  251. tempstack->next, tempstack->previous);
  252. tempstack = tempstack->previous;
  253. }
  254. }
  255. */
  256. static void remove_delimiter(subject *subj, delimiter_stack *stack)
  257. {
  258. if (stack->previous != NULL) {
  259. stack->previous->next = stack->next;
  260. }
  261. if (stack->next == NULL) {
  262. // top of stack
  263. subj->delimiters = stack->previous;
  264. } else {
  265. stack->next->previous = stack->previous;
  266. }
  267. free(stack);
  268. }
  269. static delimiter_stack * push_delimiter(subject *subj,
  270. int numdelims,
  271. unsigned char c,
  272. bool can_open,
  273. bool can_close,
  274. cmark_node *inl_text)
  275. {
  276. delimiter_stack *istack =
  277. (delimiter_stack*)malloc(sizeof(delimiter_stack));
  278. if (istack == NULL) {
  279. return NULL;
  280. }
  281. istack->delim_count = numdelims;
  282. istack->delim_char = c;
  283. istack->can_open = can_open;
  284. istack->can_close = can_close;
  285. istack->first_inline = inl_text;
  286. istack->previous = subj->delimiters;
  287. istack->next = NULL;
  288. if (istack->previous != NULL) {
  289. istack->previous->next = istack;
  290. }
  291. istack->position = subj->pos;
  292. return istack;
  293. }
  294. // Parse strong/emph or a fallback.
  295. // Assumes the subject has '_' or '*' at the current position.
  296. static cmark_node* handle_strong_emph(subject* subj, unsigned char c)
  297. {
  298. int numdelims;
  299. cmark_node * inl_text;
  300. bool can_open, can_close;
  301. numdelims = scan_delims(subj, c, &can_open, &can_close);
  302. inl_text = make_str(chunk_dup(&subj->input, subj->pos - numdelims, numdelims));
  303. if (can_open || can_close) {
  304. subj->delimiters = push_delimiter(subj, numdelims, c, can_open, can_close,
  305. inl_text);
  306. }
  307. return inl_text;
  308. }
  309. static void process_emphasis(subject *subj, delimiter_stack *stack_bottom)
  310. {
  311. delimiter_stack *closer = subj->delimiters;
  312. delimiter_stack *opener, *tempstack, *nextstack;
  313. int use_delims;
  314. cmark_node *inl, *tmp, *emph;
  315. // move back to first relevant delim.
  316. while (closer != NULL && closer->previous != stack_bottom) {
  317. closer = closer->previous;
  318. }
  319. // now move forward, looking for closers, and handling each
  320. while (closer != NULL) {
  321. if (closer->can_close &&
  322. (closer->delim_char == '*' || closer->delim_char == '_')) {
  323. // Now look backwards for first matching opener:
  324. opener = closer->previous;
  325. while (opener != NULL && opener != stack_bottom) {
  326. if (opener->delim_char == closer->delim_char &&
  327. opener->can_open) {
  328. break;
  329. }
  330. opener = opener->previous;
  331. }
  332. if (opener != NULL && opener != stack_bottom) {
  333. // calculate the actual number of delimeters used from this closer
  334. if (closer->delim_count < 3 || opener->delim_count < 3) {
  335. use_delims = closer->delim_count <= opener->delim_count ?
  336. closer->delim_count : opener->delim_count;
  337. } else { // closer and opener both have >= 3 delims
  338. use_delims = closer->delim_count % 2 == 0 ? 2 : 1;
  339. }
  340. inl = opener->first_inline;
  341. // remove used delimiters from stack elements and associated inlines.
  342. opener->delim_count -= use_delims;
  343. closer->delim_count -= use_delims;
  344. inl->as.literal.len = opener->delim_count;
  345. closer->first_inline->as.literal.len = closer->delim_count;
  346. // free delimiters between opener and closer
  347. tempstack = closer->previous;
  348. while (tempstack != NULL && tempstack != opener) {
  349. nextstack = tempstack->previous;
  350. remove_delimiter(subj, tempstack);
  351. tempstack = nextstack;
  352. }
  353. // create new emph or strong, and splice it in to our inlines
  354. // between the opener and closer
  355. emph = use_delims == 1 ? make_emph(inl->next) : make_strong(inl->next);
  356. emph->next = closer->first_inline;
  357. emph->prev = inl;
  358. emph->parent = inl->parent;
  359. inl->next = emph;
  360. // if opener has 0 delims, remove it and its associated inline
  361. if (opener->delim_count == 0) {
  362. // replace empty opener inline with emph
  363. chunk_free(&(inl->as.literal));
  364. inl->type = emph->type;
  365. inl->next = emph->next;
  366. inl->first_child = emph->first_child;
  367. free(emph);
  368. emph = inl;
  369. // remove opener from stack
  370. remove_delimiter(subj, opener);
  371. }
  372. // fix tree structure
  373. tmp = emph->first_child;
  374. while (tmp->next != NULL && tmp->next != closer->first_inline) {
  375. tmp->parent = emph;
  376. tmp = tmp->next;
  377. }
  378. tmp->parent = emph;
  379. if (tmp->next) {
  380. tmp->next->prev = emph;
  381. }
  382. tmp->next = NULL;
  383. emph->last_child = tmp;
  384. // if closer has 0 delims, remove it and its associated inline
  385. if (closer->delim_count == 0) {
  386. // remove empty closer inline
  387. tmp = closer->first_inline;
  388. emph->next = tmp->next;
  389. if (tmp->next) {
  390. tmp->next->prev = emph;
  391. }
  392. cmark_node_unlink(tmp);
  393. cmark_free_nodes(tmp);
  394. // remove closer from stack
  395. tempstack = closer->next;
  396. remove_delimiter(subj, closer);
  397. closer = tempstack;
  398. }
  399. } else {
  400. closer = closer->next;
  401. }
  402. } else {
  403. closer = closer->next;
  404. }
  405. }
  406. // free all delimiters in stack down to stack_bottom:
  407. while (subj->delimiters != stack_bottom) {
  408. remove_delimiter(subj, subj->delimiters);
  409. }
  410. }
  411. // Parse backslash-escape or just a backslash, returning an inline.
  412. static cmark_node* handle_backslash(subject *subj)
  413. {
  414. advance(subj);
  415. unsigned char nextchar = peek_char(subj);
  416. if (ispunct(nextchar)) { // only ascii symbols and newline can be escaped
  417. advance(subj);
  418. return make_str(chunk_dup(&subj->input, subj->pos - 1, 1));
  419. } else if (nextchar == '\n') {
  420. advance(subj);
  421. return make_linebreak();
  422. } else {
  423. return make_str(chunk_literal("\\"));
  424. }
  425. }
  426. // Parse an entity or a regular "&" string.
  427. // Assumes the subject has an '&' character at the current position.
  428. static cmark_node* handle_entity(subject* subj)
  429. {
  430. strbuf ent = GH_BUF_INIT;
  431. size_t len;
  432. advance(subj);
  433. len = houdini_unescape_ent(&ent,
  434. subj->input.data + subj->pos,
  435. subj->input.len - subj->pos
  436. );
  437. if (len == 0)
  438. return make_str(chunk_literal("&"));
  439. subj->pos += len;
  440. return make_str(chunk_buf_detach(&ent));
  441. }
  442. // Like make_str, but parses entities.
  443. // Returns an inline sequence consisting of str and entity elements.
  444. static cmark_node *make_str_with_entities(chunk *content)
  445. {
  446. strbuf unescaped = GH_BUF_INIT;
  447. if (houdini_unescape_html(&unescaped, content->data, (size_t)content->len)) {
  448. return make_str(chunk_buf_detach(&unescaped));
  449. } else {
  450. return make_str(*content);
  451. }
  452. }
  453. // Clean a URL: remove surrounding whitespace and surrounding <>,
  454. // and remove \ that escape punctuation.
  455. unsigned char *cmark_clean_url(chunk *url)
  456. {
  457. strbuf buf = GH_BUF_INIT;
  458. chunk_trim(url);
  459. if (url->len == 0)
  460. return NULL;
  461. if (url->data[0] == '<' && url->data[url->len - 1] == '>') {
  462. houdini_unescape_html_f(&buf, url->data + 1, url->len - 2);
  463. } else {
  464. houdini_unescape_html_f(&buf, url->data, url->len);
  465. }
  466. strbuf_unescape(&buf);
  467. return strbuf_detach(&buf);
  468. }
  469. unsigned char *cmark_clean_title(chunk *title)
  470. {
  471. strbuf buf = GH_BUF_INIT;
  472. unsigned char first, last;
  473. if (title->len == 0)
  474. return NULL;
  475. first = title->data[0];
  476. last = title->data[title->len - 1];
  477. // remove surrounding quotes if any:
  478. if ((first == '\'' && last == '\'') ||
  479. (first == '(' && last == ')') ||
  480. (first == '"' && last == '"')) {
  481. houdini_unescape_html_f(&buf, title->data + 1, title->len - 2);
  482. } else {
  483. houdini_unescape_html_f(&buf, title->data, title->len);
  484. }
  485. strbuf_unescape(&buf);
  486. return strbuf_detach(&buf);
  487. }
  488. // Parse an autolink or HTML tag.
  489. // Assumes the subject has a '<' character at the current position.
  490. static cmark_node* handle_pointy_brace(subject* subj)
  491. {
  492. int matchlen = 0;
  493. chunk contents;
  494. advance(subj); // advance past first <
  495. // first try to match a URL autolink
  496. matchlen = scan_autolink_uri(&subj->input, subj->pos);
  497. if (matchlen > 0) {
  498. contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
  499. subj->pos += matchlen;
  500. return make_autolink(
  501. make_str_with_entities(&contents),
  502. contents, 0
  503. );
  504. }
  505. // next try to match an email autolink
  506. matchlen = scan_autolink_email(&subj->input, subj->pos);
  507. if (matchlen > 0) {
  508. contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
  509. subj->pos += matchlen;
  510. return make_autolink(
  511. make_str_with_entities(&contents),
  512. contents, 1
  513. );
  514. }
  515. // finally, try to match an html tag
  516. matchlen = scan_html_tag(&subj->input, subj->pos);
  517. if (matchlen > 0) {
  518. contents = chunk_dup(&subj->input, subj->pos - 1, matchlen + 1);
  519. subj->pos += matchlen;
  520. return make_raw_html(contents);
  521. }
  522. // if nothing matches, just return the opening <:
  523. return make_str(chunk_literal("<"));
  524. }
  525. // Parse a link label. Returns 1 if successful.
  526. // Note: unescaped brackets are not allowed in labels.
  527. // The label begins with `[` and ends with the first `]` character
  528. // encountered. Backticks in labels do not start code spans.
  529. static int link_label(subject* subj, chunk *raw_label)
  530. {
  531. int startpos = subj->pos;
  532. int length = 0;
  533. advance(subj); // advance past [
  534. unsigned char c;
  535. while ((c = peek_char(subj)) && c != '[' && c != ']') {
  536. if (c == '\\') {
  537. advance(subj);
  538. length++;
  539. if (ispunct(peek_char(subj))) {
  540. advance(subj);
  541. length++;
  542. }
  543. } else {
  544. advance(subj);
  545. length++;
  546. }
  547. if (length > MAX_LINK_LABEL_LENGTH) {
  548. goto noMatch;
  549. }
  550. }
  551. if (c == ']') { // match found
  552. *raw_label = chunk_dup(&subj->input, startpos + 1, subj->pos - (startpos + 1));
  553. advance(subj); // advance past ]
  554. return 1;
  555. }
  556. noMatch:
  557. subj->pos = startpos; // rewind
  558. return 0;
  559. }
  560. // Return a link, an image, or a literal close bracket.
  561. static cmark_node* handle_close_bracket(subject* subj, cmark_node *parent)
  562. {
  563. int initial_pos;
  564. int starturl, endurl, starttitle, endtitle, endall;
  565. int n;
  566. int sps;
  567. cmark_reference *ref;
  568. bool is_image = false;
  569. chunk urlchunk, titlechunk;
  570. unsigned char *url, *title;
  571. delimiter_stack *opener;
  572. delimiter_stack *tempstack;
  573. cmark_node *link_text;
  574. cmark_node *inl;
  575. chunk raw_label;
  576. int found_label;
  577. advance(subj); // advance past ]
  578. initial_pos = subj->pos;
  579. // look through stack of delimiters for a [ or !
  580. opener = subj->delimiters;
  581. while (opener) {
  582. if (opener->delim_char == '[' || opener->delim_char == '!') {
  583. break;
  584. }
  585. opener = opener->previous;
  586. }
  587. if (opener == NULL) {
  588. return make_str(chunk_literal("]"));
  589. }
  590. // If we got here, we matched a potential link/image text.
  591. is_image = opener->delim_char == '!';
  592. link_text = opener->first_inline->next;
  593. // Now we check to see if it's a link/image.
  594. // First, look for an inline link.
  595. if (peek_char(subj) == '(' &&
  596. ((sps = scan_spacechars(&subj->input, subj->pos + 1)) > -1) &&
  597. ((n = scan_link_url(&subj->input, subj->pos + 1 + sps)) > -1)) {
  598. // try to parse an explicit link:
  599. starturl = subj->pos + 1 + sps; // after (
  600. endurl = starturl + n;
  601. starttitle = endurl + scan_spacechars(&subj->input, endurl);
  602. // ensure there are spaces btw url and title
  603. endtitle = (starttitle == endurl) ? starttitle :
  604. starttitle + scan_link_title(&subj->input, starttitle);
  605. endall = endtitle + scan_spacechars(&subj->input, endtitle);
  606. if (peek_at(subj, endall) == ')') {
  607. subj->pos = endall + 1;
  608. urlchunk = chunk_dup(&subj->input, starturl, endurl - starturl);
  609. titlechunk = chunk_dup(&subj->input, starttitle, endtitle - starttitle);
  610. url = cmark_clean_url(&urlchunk);
  611. title = cmark_clean_title(&titlechunk);
  612. chunk_free(&urlchunk);
  613. chunk_free(&titlechunk);
  614. goto match;
  615. } else {
  616. goto noMatch;
  617. }
  618. }
  619. // Next, look for a following [link label] that matches in refmap.
  620. // skip spaces
  621. subj->pos = subj->pos + scan_spacechars(&subj->input, subj->pos);
  622. raw_label = chunk_literal("");
  623. found_label = link_label(subj, &raw_label);
  624. if (!found_label || raw_label.len == 0) {
  625. chunk_free(&raw_label);
  626. raw_label = chunk_dup(&subj->input, opener->position,
  627. initial_pos - opener->position - 1);
  628. }
  629. if (!found_label) {
  630. // If we have a shortcut reference link, back up
  631. // to before the spacse we skipped.
  632. subj->pos = initial_pos;
  633. }
  634. ref = cmark_reference_lookup(subj->refmap, &raw_label);
  635. chunk_free(&raw_label);
  636. if (ref != NULL) { // found
  637. url = bufdup(ref->url);
  638. title = bufdup(ref->title);
  639. goto match;
  640. } else {
  641. goto noMatch;
  642. }
  643. noMatch:
  644. // If we fall through to here, it means we didn't match a link:
  645. remove_delimiter(subj, opener); // remove this opener from delimiter stack
  646. subj->pos = initial_pos;
  647. return make_str(chunk_literal("]"));
  648. match:
  649. inl = opener->first_inline;
  650. inl->type = is_image ? NODE_IMAGE : NODE_LINK;
  651. chunk_free(&inl->as.literal);
  652. inl->first_child = link_text;
  653. process_emphasis(subj, opener->previous);
  654. inl->as.link.url = url;
  655. inl->as.link.title = title;
  656. inl->next = NULL;
  657. if (link_text) {
  658. cmark_node *tmp;
  659. link_text->prev = NULL;
  660. for (tmp = link_text; tmp->next != NULL; tmp = tmp->next) {
  661. tmp->parent = inl;
  662. }
  663. tmp->parent = inl;
  664. inl->last_child = tmp;
  665. }
  666. parent->last_child = inl;
  667. // process_emphasis will remove this delimiter and all later ones.
  668. // Now, if we have a link, we also want to remove earlier link
  669. // delimiters. (This code can be removed if we decide to allow links
  670. // inside links.)
  671. if (!is_image) {
  672. opener = subj->delimiters;
  673. while (opener != NULL) {
  674. tempstack = opener->previous;
  675. if (opener->delim_char == '[') {
  676. remove_delimiter(subj, opener);
  677. }
  678. opener = tempstack;
  679. }
  680. }
  681. return NULL;
  682. }
  683. // Parse a hard or soft linebreak, returning an inline.
  684. // Assumes the subject has a newline at the current position.
  685. static cmark_node* handle_newline(subject *subj)
  686. {
  687. int nlpos = subj->pos;
  688. // skip over newline
  689. advance(subj);
  690. // skip spaces at beginning of line
  691. while (peek_char(subj) == ' ') {
  692. advance(subj);
  693. }
  694. if (nlpos > 1 &&
  695. peek_at(subj, nlpos - 1) == ' ' &&
  696. peek_at(subj, nlpos - 2) == ' ') {
  697. return make_linebreak();
  698. } else {
  699. return make_softbreak();
  700. }
  701. }
  702. static int subject_find_special_char(subject *subj)
  703. {
  704. // "\n\\`&_*[]<!"
  705. static const int8_t SPECIAL_CHARS[256] = {
  706. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  707. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  708. 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  709. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
  710. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  711. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
  712. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  713. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  714. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  715. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  716. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  717. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  718. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  719. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  720. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  721. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  722. int n = subj->pos + 1;
  723. while (n < subj->input.len) {
  724. if (SPECIAL_CHARS[subj->input.data[n]])
  725. return n;
  726. n++;
  727. }
  728. return subj->input.len;
  729. }
  730. // Parse an inline, advancing subject, and add it as a child of parent.
  731. // Return 0 if no inline can be parsed, 1 otherwise.
  732. static int parse_inline(subject* subj, cmark_node * parent)
  733. {
  734. cmark_node* new_inl = NULL;
  735. chunk contents;
  736. unsigned char c;
  737. int endpos;
  738. c = peek_char(subj);
  739. if (c == 0) {
  740. return 0;
  741. }
  742. switch(c){
  743. case '\n':
  744. new_inl = handle_newline(subj);
  745. break;
  746. case '`':
  747. new_inl = handle_backticks(subj);
  748. break;
  749. case '\\':
  750. new_inl = handle_backslash(subj);
  751. break;
  752. case '&':
  753. new_inl = handle_entity(subj);
  754. break;
  755. case '<':
  756. new_inl = handle_pointy_brace(subj);
  757. break;
  758. case '*':
  759. case '_':
  760. new_inl = handle_strong_emph(subj, c);
  761. break;
  762. case '[':
  763. advance(subj);
  764. new_inl = make_str(chunk_literal("["));
  765. subj->delimiters = push_delimiter(subj, 1, '[', true, false, new_inl);
  766. break;
  767. case ']':
  768. new_inl = handle_close_bracket(subj, parent);
  769. break;
  770. case '!':
  771. advance(subj);
  772. if (peek_char(subj) == '[') {
  773. advance(subj);
  774. new_inl = make_str(chunk_literal("!["));
  775. subj->delimiters = push_delimiter(subj, 1, '!', false, true, new_inl);
  776. } else {
  777. new_inl = make_str(chunk_literal("!"));
  778. }
  779. break;
  780. default:
  781. endpos = subject_find_special_char(subj);
  782. contents = chunk_dup(&subj->input, subj->pos, endpos - subj->pos);
  783. subj->pos = endpos;
  784. // if we're at a newline, strip trailing spaces.
  785. if (peek_char(subj) == '\n') {
  786. chunk_rtrim(&contents);
  787. }
  788. new_inl = make_str(contents);
  789. }
  790. if (new_inl != NULL) {
  791. cmark_node_append_child(parent, new_inl);
  792. }
  793. return 1;
  794. }
  795. // Parse inlines from parent's string_content, adding as children of parent.
  796. extern void cmark_parse_inlines(cmark_node* parent, cmark_reference_map *refmap)
  797. {
  798. subject subj;
  799. subject_from_buf(&subj, &parent->string_content, refmap);
  800. while (!is_eof(&subj) && parse_inline(&subj, parent)) ;
  801. process_emphasis(&subj, NULL);
  802. }
  803. // Parse zero or more space characters, including at most one newline.
  804. static void spnl(subject* subj)
  805. {
  806. bool seen_newline = false;
  807. while (peek_char(subj) == ' ' ||
  808. (!seen_newline &&
  809. (seen_newline = peek_char(subj) == '\n'))) {
  810. advance(subj);
  811. }
  812. }
  813. // Parse reference. Assumes string begins with '[' character.
  814. // Modify refmap if a reference is encountered.
  815. // Return 0 if no reference found, otherwise position of subject
  816. // after reference is parsed.
  817. int cmark_parse_reference_inline(strbuf *input, cmark_reference_map *refmap)
  818. {
  819. subject subj;
  820. chunk lab;
  821. chunk url;
  822. chunk title;
  823. int matchlen = 0;
  824. int beforetitle;
  825. subject_from_buf(&subj, input, NULL);
  826. // parse label:
  827. if (!link_label(&subj, &lab))
  828. return 0;
  829. // colon:
  830. if (peek_char(&subj) == ':') {
  831. advance(&subj);
  832. } else {
  833. return 0;
  834. }
  835. // parse link url:
  836. spnl(&subj);
  837. matchlen = scan_link_url(&subj.input, subj.pos);
  838. if (matchlen) {
  839. url = chunk_dup(&subj.input, subj.pos, matchlen);
  840. subj.pos += matchlen;
  841. } else {
  842. return 0;
  843. }
  844. // parse optional link_title
  845. beforetitle = subj.pos;
  846. spnl(&subj);
  847. matchlen = scan_link_title(&subj.input, subj.pos);
  848. if (matchlen) {
  849. title = chunk_dup(&subj.input, subj.pos, matchlen);
  850. subj.pos += matchlen;
  851. } else {
  852. subj.pos = beforetitle;
  853. title = chunk_literal("");
  854. }
  855. // parse final spaces and newline:
  856. while (peek_char(&subj) == ' ') {
  857. advance(&subj);
  858. }
  859. if (peek_char(&subj) == '\n') {
  860. advance(&subj);
  861. } else if (peek_char(&subj) != 0) {
  862. return 0;
  863. }
  864. // insert reference into refmap
  865. cmark_reference_create(refmap, &lab, &url, &title);
  866. return subj.pos;
  867. }