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