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