aboutsummaryrefslogtreecommitdiff
path: root/src/inlines.c
blob: 265234225c41805c155645c988a1a1fec25daa1b (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. // if opener has 0 delims, remove it and its associated inline
  379. if (opener->delim_count == 0) {
  380. // replace empty opener inline with emph
  381. chunk_free(&(inl->as.literal));
  382. emph = inl;
  383. emph->type = use_delims == 1 ? NODE_EMPH : NODE_STRONG;
  384. emph->first_child = inl->next;
  385. // remove opener from stack
  386. remove_delimiter(subj, opener);
  387. }
  388. else {
  389. // create new emph or strong, and splice it in to our inlines
  390. // between the opener and closer
  391. emph = use_delims == 1 ? make_emph(inl->next) : make_strong(inl->next);
  392. emph->parent = inl->parent;
  393. emph->prev = inl;
  394. inl->next = emph;
  395. }
  396. emph->next = closer->first_inline;
  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. cmark_node_free(closer->first_inline);
  414. // remove closer from stack
  415. tempstack = closer->next;
  416. remove_delimiter(subj, closer);
  417. closer = tempstack;
  418. }
  419. return closer;
  420. }
  421. // Parse backslash-escape or just a backslash, returning an inline.
  422. static cmark_node* handle_backslash(subject *subj)
  423. {
  424. advance(subj);
  425. unsigned char nextchar = peek_char(subj);
  426. if (ispunct(nextchar)) { // only ascii symbols and newline can be escaped
  427. advance(subj);
  428. return make_str(chunk_dup(&subj->input, subj->pos - 1, 1));
  429. } else if (nextchar == '\n') {
  430. advance(subj);
  431. return make_linebreak();
  432. } else {
  433. return make_str(chunk_literal("\\"));
  434. }
  435. }
  436. // Parse an entity or a regular "&" string.
  437. // Assumes the subject has an '&' character at the current position.
  438. static cmark_node* handle_entity(subject* subj)
  439. {
  440. strbuf ent = GH_BUF_INIT;
  441. size_t len;
  442. advance(subj);
  443. len = houdini_unescape_ent(&ent,
  444. subj->input.data + subj->pos,
  445. subj->input.len - subj->pos
  446. );
  447. if (len == 0)
  448. return make_str(chunk_literal("&"));
  449. subj->pos += len;
  450. return make_str(chunk_buf_detach(&ent));
  451. }
  452. // Like make_str, but parses entities.
  453. // Returns an inline sequence consisting of str and entity elements.
  454. static cmark_node *make_str_with_entities(chunk *content)
  455. {
  456. strbuf unescaped = GH_BUF_INIT;
  457. if (houdini_unescape_html(&unescaped, content->data, (size_t)content->len)) {
  458. return make_str(chunk_buf_detach(&unescaped));
  459. } else {
  460. return make_str(*content);
  461. }
  462. }
  463. // Clean a URL: remove surrounding whitespace and surrounding <>,
  464. // and remove \ that escape punctuation.
  465. unsigned char *cmark_clean_url(chunk *url)
  466. {
  467. strbuf buf = GH_BUF_INIT;
  468. chunk_trim(url);
  469. if (url->len == 0)
  470. return NULL;
  471. if (url->data[0] == '<' && url->data[url->len - 1] == '>') {
  472. houdini_unescape_html_f(&buf, url->data + 1, url->len - 2);
  473. } else {
  474. houdini_unescape_html_f(&buf, url->data, url->len);
  475. }
  476. strbuf_unescape(&buf);
  477. return strbuf_detach(&buf);
  478. }
  479. unsigned char *cmark_clean_title(chunk *title)
  480. {
  481. strbuf buf = GH_BUF_INIT;
  482. unsigned char first, last;
  483. if (title->len == 0)
  484. return NULL;
  485. first = title->data[0];
  486. last = title->data[title->len - 1];
  487. // remove surrounding quotes if any:
  488. if ((first == '\'' && last == '\'') ||
  489. (first == '(' && last == ')') ||
  490. (first == '"' && last == '"')) {
  491. houdini_unescape_html_f(&buf, title->data + 1, title->len - 2);
  492. } else {
  493. houdini_unescape_html_f(&buf, title->data, title->len);
  494. }
  495. strbuf_unescape(&buf);
  496. return strbuf_detach(&buf);
  497. }
  498. // Parse an autolink or HTML tag.
  499. // Assumes the subject has a '<' character at the current position.
  500. static cmark_node* handle_pointy_brace(subject* subj)
  501. {
  502. int matchlen = 0;
  503. chunk contents;
  504. advance(subj); // advance past first <
  505. // first try to match a URL autolink
  506. matchlen = scan_autolink_uri(&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, 0
  513. );
  514. }
  515. // next try to match an email autolink
  516. matchlen = scan_autolink_email(&subj->input, subj->pos);
  517. if (matchlen > 0) {
  518. contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
  519. subj->pos += matchlen;
  520. return make_autolink(
  521. make_str_with_entities(&contents),
  522. contents, 1
  523. );
  524. }
  525. // finally, try to match an html tag
  526. matchlen = scan_html_tag(&subj->input, subj->pos);
  527. if (matchlen > 0) {
  528. contents = chunk_dup(&subj->input, subj->pos - 1, matchlen + 1);
  529. subj->pos += matchlen;
  530. return make_raw_html(contents);
  531. }
  532. // if nothing matches, just return the opening <:
  533. return make_str(chunk_literal("<"));
  534. }
  535. // Parse a link label. Returns 1 if successful.
  536. // Note: unescaped brackets are not allowed in labels.
  537. // The label begins with `[` and ends with the first `]` character
  538. // encountered. Backticks in labels do not start code spans.
  539. static int link_label(subject* subj, chunk *raw_label)
  540. {
  541. int startpos = subj->pos;
  542. int length = 0;
  543. advance(subj); // advance past [
  544. unsigned char c;
  545. while ((c = peek_char(subj)) && c != '[' && c != ']') {
  546. if (c == '\\') {
  547. advance(subj);
  548. length++;
  549. if (ispunct(peek_char(subj))) {
  550. advance(subj);
  551. length++;
  552. }
  553. } else {
  554. advance(subj);
  555. length++;
  556. }
  557. if (length > MAX_LINK_LABEL_LENGTH) {
  558. goto noMatch;
  559. }
  560. }
  561. if (c == ']') { // match found
  562. *raw_label = chunk_dup(&subj->input, startpos + 1, subj->pos - (startpos + 1));
  563. advance(subj); // advance past ]
  564. return 1;
  565. }
  566. noMatch:
  567. subj->pos = startpos; // rewind
  568. return 0;
  569. }
  570. // Return a link, an image, or a literal close bracket.
  571. static cmark_node* handle_close_bracket(subject* subj, cmark_node *parent)
  572. {
  573. int initial_pos;
  574. int starturl, endurl, starttitle, endtitle, endall;
  575. int n;
  576. int sps;
  577. cmark_reference *ref;
  578. bool is_image = false;
  579. chunk urlchunk, titlechunk;
  580. unsigned char *url, *title;
  581. delimiter_stack *opener;
  582. delimiter_stack *tempstack;
  583. cmark_node *link_text;
  584. cmark_node *inl;
  585. chunk raw_label;
  586. int found_label;
  587. advance(subj); // advance past ]
  588. initial_pos = subj->pos;
  589. // look through stack of delimiters for a [ or !
  590. opener = subj->delimiters;
  591. while (opener) {
  592. if (opener->delim_char == '[' || opener->delim_char == '!') {
  593. break;
  594. }
  595. opener = opener->previous;
  596. }
  597. if (opener == NULL) {
  598. return make_str(chunk_literal("]"));
  599. }
  600. // If we got here, we matched a potential link/image text.
  601. is_image = opener->delim_char == '!';
  602. link_text = opener->first_inline->next;
  603. // Now we check to see if it's a link/image.
  604. // First, look for an inline link.
  605. if (peek_char(subj) == '(' &&
  606. ((sps = scan_spacechars(&subj->input, subj->pos + 1)) > -1) &&
  607. ((n = scan_link_url(&subj->input, subj->pos + 1 + sps)) > -1)) {
  608. // try to parse an explicit link:
  609. starturl = subj->pos + 1 + sps; // after (
  610. endurl = starturl + n;
  611. starttitle = endurl + scan_spacechars(&subj->input, endurl);
  612. // ensure there are spaces btw url and title
  613. endtitle = (starttitle == endurl) ? starttitle :
  614. starttitle + scan_link_title(&subj->input, starttitle);
  615. endall = endtitle + scan_spacechars(&subj->input, endtitle);
  616. if (peek_at(subj, endall) == ')') {
  617. subj->pos = endall + 1;
  618. urlchunk = chunk_dup(&subj->input, starturl, endurl - starturl);
  619. titlechunk = chunk_dup(&subj->input, starttitle, endtitle - starttitle);
  620. url = cmark_clean_url(&urlchunk);
  621. title = cmark_clean_title(&titlechunk);
  622. chunk_free(&urlchunk);
  623. chunk_free(&titlechunk);
  624. goto match;
  625. } else {
  626. goto noMatch;
  627. }
  628. }
  629. // Next, look for a following [link label] that matches in refmap.
  630. // skip spaces
  631. subj->pos = subj->pos + scan_spacechars(&subj->input, subj->pos);
  632. raw_label = chunk_literal("");
  633. found_label = link_label(subj, &raw_label);
  634. if (!found_label || raw_label.len == 0) {
  635. chunk_free(&raw_label);
  636. raw_label = chunk_dup(&subj->input, opener->position,
  637. initial_pos - opener->position - 1);
  638. }
  639. if (!found_label) {
  640. // If we have a shortcut reference link, back up
  641. // to before the spacse we skipped.
  642. subj->pos = initial_pos;
  643. }
  644. ref = cmark_reference_lookup(subj->refmap, &raw_label);
  645. chunk_free(&raw_label);
  646. if (ref != NULL) { // found
  647. url = bufdup(ref->url);
  648. title = bufdup(ref->title);
  649. goto match;
  650. } else {
  651. goto noMatch;
  652. }
  653. noMatch:
  654. // If we fall through to here, it means we didn't match a link:
  655. remove_delimiter(subj, opener); // remove this opener from delimiter stack
  656. subj->pos = initial_pos;
  657. return make_str(chunk_literal("]"));
  658. match:
  659. inl = opener->first_inline;
  660. inl->type = is_image ? NODE_IMAGE : NODE_LINK;
  661. chunk_free(&inl->as.literal);
  662. inl->first_child = link_text;
  663. process_emphasis(subj, opener->previous);
  664. inl->as.link.url = url;
  665. inl->as.link.title = title;
  666. inl->next = NULL;
  667. if (link_text) {
  668. cmark_node *tmp;
  669. link_text->prev = NULL;
  670. for (tmp = link_text; tmp->next != NULL; tmp = tmp->next) {
  671. tmp->parent = inl;
  672. }
  673. tmp->parent = inl;
  674. inl->last_child = tmp;
  675. }
  676. parent->last_child = inl;
  677. // process_emphasis will remove this delimiter and all later ones.
  678. // Now, if we have a link, we also want to remove earlier link
  679. // delimiters. (This code can be removed if we decide to allow links
  680. // inside links.)
  681. if (!is_image) {
  682. opener = subj->delimiters;
  683. while (opener != NULL) {
  684. tempstack = opener->previous;
  685. if (opener->delim_char == '[') {
  686. remove_delimiter(subj, opener);
  687. }
  688. opener = tempstack;
  689. }
  690. }
  691. return NULL;
  692. }
  693. // Parse a hard or soft linebreak, returning an inline.
  694. // Assumes the subject has a newline at the current position.
  695. static cmark_node* handle_newline(subject *subj)
  696. {
  697. int nlpos = subj->pos;
  698. // skip over newline
  699. advance(subj);
  700. // skip spaces at beginning of line
  701. while (peek_char(subj) == ' ') {
  702. advance(subj);
  703. }
  704. if (nlpos > 1 &&
  705. peek_at(subj, nlpos - 1) == ' ' &&
  706. peek_at(subj, nlpos - 2) == ' ') {
  707. return make_linebreak();
  708. } else {
  709. return make_softbreak();
  710. }
  711. }
  712. static int subject_find_special_char(subject *subj)
  713. {
  714. // "\n\\`&_*[]<!"
  715. static const int8_t SPECIAL_CHARS[256] = {
  716. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  717. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  718. 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  719. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 1, 1, 1, 0, 1,
  722. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  723. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  724. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 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, 0, 0, 0, 0, 0,
  727. 0, 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. int n = subj->pos + 1;
  733. while (n < subj->input.len) {
  734. if (SPECIAL_CHARS[subj->input.data[n]])
  735. return n;
  736. n++;
  737. }
  738. return subj->input.len;
  739. }
  740. // Parse an inline, advancing subject, and add it as a child of parent.
  741. // Return 0 if no inline can be parsed, 1 otherwise.
  742. static int parse_inline(subject* subj, cmark_node * parent)
  743. {
  744. cmark_node* new_inl = NULL;
  745. chunk contents;
  746. unsigned char c;
  747. int endpos;
  748. c = peek_char(subj);
  749. if (c == 0) {
  750. return 0;
  751. }
  752. switch(c){
  753. case '\n':
  754. new_inl = handle_newline(subj);
  755. break;
  756. case '`':
  757. new_inl = handle_backticks(subj);
  758. break;
  759. case '\\':
  760. new_inl = handle_backslash(subj);
  761. break;
  762. case '&':
  763. new_inl = handle_entity(subj);
  764. break;
  765. case '<':
  766. new_inl = handle_pointy_brace(subj);
  767. break;
  768. case '*':
  769. case '_':
  770. new_inl = handle_strong_emph(subj, c);
  771. break;
  772. case '[':
  773. advance(subj);
  774. new_inl = make_str(chunk_literal("["));
  775. subj->delimiters = push_delimiter(subj, 1, '[', true, false, new_inl);
  776. break;
  777. case ']':
  778. new_inl = handle_close_bracket(subj, parent);
  779. break;
  780. case '!':
  781. advance(subj);
  782. if (peek_char(subj) == '[') {
  783. advance(subj);
  784. new_inl = make_str(chunk_literal("!["));
  785. subj->delimiters = push_delimiter(subj, 1, '!', false, true, new_inl);
  786. } else {
  787. new_inl = make_str(chunk_literal("!"));
  788. }
  789. break;
  790. default:
  791. endpos = subject_find_special_char(subj);
  792. contents = chunk_dup(&subj->input, subj->pos, endpos - subj->pos);
  793. subj->pos = endpos;
  794. // if we're at a newline, strip trailing spaces.
  795. if (peek_char(subj) == '\n') {
  796. chunk_rtrim(&contents);
  797. }
  798. new_inl = make_str(contents);
  799. }
  800. if (new_inl != NULL) {
  801. cmark_node_append_child(parent, new_inl);
  802. }
  803. return 1;
  804. }
  805. // Parse inlines from parent's string_content, adding as children of parent.
  806. extern void cmark_parse_inlines(cmark_node* parent, cmark_reference_map *refmap)
  807. {
  808. subject subj;
  809. subject_from_buf(&subj, &parent->string_content, refmap);
  810. while (!is_eof(&subj) && parse_inline(&subj, parent)) ;
  811. process_emphasis(&subj, NULL);
  812. }
  813. // Parse zero or more space characters, including at most one newline.
  814. static void spnl(subject* subj)
  815. {
  816. bool seen_newline = false;
  817. while (peek_char(subj) == ' ' ||
  818. (!seen_newline &&
  819. (seen_newline = peek_char(subj) == '\n'))) {
  820. advance(subj);
  821. }
  822. }
  823. // Parse reference. Assumes string begins with '[' character.
  824. // Modify refmap if a reference is encountered.
  825. // Return 0 if no reference found, otherwise position of subject
  826. // after reference is parsed.
  827. int cmark_parse_reference_inline(strbuf *input, cmark_reference_map *refmap)
  828. {
  829. subject subj;
  830. chunk lab;
  831. chunk url;
  832. chunk title;
  833. int matchlen = 0;
  834. int beforetitle;
  835. subject_from_buf(&subj, input, NULL);
  836. // parse label:
  837. if (!link_label(&subj, &lab))
  838. return 0;
  839. // colon:
  840. if (peek_char(&subj) == ':') {
  841. advance(&subj);
  842. } else {
  843. return 0;
  844. }
  845. // parse link url:
  846. spnl(&subj);
  847. matchlen = scan_link_url(&subj.input, subj.pos);
  848. if (matchlen) {
  849. url = chunk_dup(&subj.input, subj.pos, matchlen);
  850. subj.pos += matchlen;
  851. } else {
  852. return 0;
  853. }
  854. // parse optional link_title
  855. beforetitle = subj.pos;
  856. spnl(&subj);
  857. matchlen = scan_link_title(&subj.input, subj.pos);
  858. if (matchlen) {
  859. title = chunk_dup(&subj.input, subj.pos, matchlen);
  860. subj.pos += matchlen;
  861. } else {
  862. subj.pos = beforetitle;
  863. title = chunk_literal("");
  864. }
  865. // parse final spaces and newline:
  866. while (peek_char(&subj) == ' ') {
  867. advance(&subj);
  868. }
  869. if (peek_char(&subj) == '\n') {
  870. advance(&subj);
  871. } else if (peek_char(&subj) != 0) {
  872. return 0;
  873. }
  874. // insert reference into refmap
  875. cmark_reference_create(refmap, &lab, &url, &title);
  876. return subj.pos;
  877. }