aboutsummaryrefslogtreecommitdiff
path: root/src/inlines.c
blob: 8235f596de2a17f13cfb35b05589ad29dea1c011 (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 "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_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() make_simple(CMARK_NODE_EMPH)
  21. #define make_strong() make_simple(CMARK_NODE_STRONG)
  22. typedef struct delimiter {
  23. struct delimiter *previous;
  24. struct delimiter *next;
  25. cmark_node *inl_text;
  26. unsigned char delim_char;
  27. int position;
  28. bool can_open;
  29. bool can_close;
  30. bool active;
  31. } delimiter;
  32. typedef struct {
  33. cmark_chunk input;
  34. int pos;
  35. cmark_reference_map *refmap;
  36. delimiter *last_delim;
  37. } subject;
  38. static delimiter*
  39. S_insert_emph(subject *subj, delimiter *opener, delimiter *closer);
  40. static int parse_inline(subject* subj, cmark_node * parent);
  41. static void subject_from_buf(subject *e, cmark_strbuf *buffer,
  42. cmark_reference_map *refmap);
  43. static int subject_find_special_char(subject *subj);
  44. static unsigned char *cmark_clean_autolink(cmark_chunk *url, int is_email)
  45. {
  46. cmark_strbuf buf = GH_BUF_INIT;
  47. cmark_chunk_trim(url);
  48. if (url->len == 0)
  49. return NULL;
  50. if (is_email)
  51. cmark_strbuf_puts(&buf, "mailto:");
  52. houdini_unescape_html_f(&buf, url->data, url->len);
  53. return cmark_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. // Create an inline with a literal string value.
  74. static inline cmark_node* make_literal(cmark_node_type t, cmark_chunk s)
  75. {
  76. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  77. if(e != NULL) {
  78. e->type = t;
  79. e->as.literal = s;
  80. e->next = NULL;
  81. e->prev = NULL;
  82. e->parent = NULL;
  83. e->first_child = NULL;
  84. e->last_child = NULL;
  85. // These fields aren't used for inlines:
  86. e->start_line = 0;
  87. e->start_column = 0;
  88. e->end_line = 0;
  89. }
  90. return e;
  91. }
  92. // Create an inline with no value.
  93. static inline cmark_node* make_simple(cmark_node_type t)
  94. {
  95. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  96. if(e != NULL) {
  97. e->type = t;
  98. e->next = NULL;
  99. e->prev = NULL;
  100. e->parent = NULL;
  101. e->first_child = NULL;
  102. e->last_child = NULL;
  103. // These fields aren't used for inlines:
  104. e->start_line = 0;
  105. e->start_column = 0;
  106. e->end_line = 0;
  107. }
  108. return e;
  109. }
  110. static unsigned char *bufdup(const unsigned char *buf)
  111. {
  112. unsigned char *new_buf = NULL;
  113. if (buf) {
  114. int len = strlen((char *)buf);
  115. new_buf = (unsigned char *)calloc(len + 1, sizeof(*new_buf));
  116. if(new_buf != NULL) {
  117. memcpy(new_buf, buf, len + 1);
  118. }
  119. }
  120. return new_buf;
  121. }
  122. static void subject_from_buf(subject *e, cmark_strbuf *buffer,
  123. cmark_reference_map *refmap)
  124. {
  125. e->input.data = buffer->ptr;
  126. e->input.len = buffer->size;
  127. e->input.alloc = 0;
  128. e->pos = 0;
  129. e->refmap = refmap;
  130. e->last_delim = NULL;
  131. cmark_chunk_rtrim(&e->input);
  132. }
  133. static inline int isbacktick(int c)
  134. {
  135. return (c == '`');
  136. }
  137. static inline unsigned char peek_char(subject *subj)
  138. {
  139. return (subj->pos < subj->input.len) ? subj->input.data[subj->pos] : 0;
  140. }
  141. static inline unsigned char peek_at(subject *subj, int pos)
  142. {
  143. return subj->input.data[pos];
  144. }
  145. // Return true if there are more characters in the subject.
  146. static inline int is_eof(subject* subj)
  147. {
  148. return (subj->pos >= subj->input.len);
  149. }
  150. // Advance the subject. Doesn't check for eof.
  151. #define advance(subj) (subj)->pos += 1
  152. // Take characters while a predicate holds, and return a string.
  153. static inline cmark_chunk take_while(subject* subj, int (*f)(int))
  154. {
  155. unsigned char c;
  156. int startpos = subj->pos;
  157. int len = 0;
  158. while ((c = peek_char(subj)) && (*f)(c)) {
  159. advance(subj);
  160. len++;
  161. }
  162. return cmark_chunk_dup(&subj->input, startpos, len);
  163. }
  164. // Try to process a backtick code span that began with a
  165. // span of ticks of length openticklength length (already
  166. // parsed). Return 0 if you don't find matching closing
  167. // backticks, otherwise return the position in the subject
  168. // after the closing backticks.
  169. static int scan_to_closing_backticks(subject* subj, int openticklength)
  170. {
  171. // read non backticks
  172. unsigned char c;
  173. while ((c = peek_char(subj)) && c != '`') {
  174. advance(subj);
  175. }
  176. if (is_eof(subj)) {
  177. return 0; // did not find closing ticks, return 0
  178. }
  179. int numticks = 0;
  180. while (peek_char(subj) == '`') {
  181. advance(subj);
  182. numticks++;
  183. }
  184. if (numticks != openticklength){
  185. return(scan_to_closing_backticks(subj, openticklength));
  186. }
  187. return (subj->pos);
  188. }
  189. // Parse backtick code section or raw backticks, return an inline.
  190. // Assumes that the subject has a backtick at the current position.
  191. static cmark_node* handle_backticks(subject *subj)
  192. {
  193. cmark_chunk openticks = take_while(subj, isbacktick);
  194. int startpos = subj->pos;
  195. int endpos = scan_to_closing_backticks(subj, openticks.len);
  196. if (endpos == 0) { // not found
  197. subj->pos = startpos; // rewind
  198. return make_str(openticks);
  199. } else {
  200. cmark_strbuf buf = GH_BUF_INIT;
  201. cmark_strbuf_set(&buf, subj->input.data + startpos, endpos - startpos - openticks.len);
  202. cmark_strbuf_trim(&buf);
  203. cmark_strbuf_normalize_whitespace(&buf);
  204. return make_code(cmark_chunk_buf_detach(&buf));
  205. }
  206. }
  207. // Scan ***, **, or * and return number scanned, or 0.
  208. // Advances position.
  209. static int
  210. scan_delims(subject* subj, unsigned char c, bool * can_open, bool * can_close)
  211. {
  212. int numdelims = 0;
  213. int before_char_pos;
  214. int32_t after_char = 0;
  215. int32_t before_char = 0;
  216. int len;
  217. if (subj->pos == 0) {
  218. before_char = 10;
  219. } else {
  220. before_char_pos = subj->pos - 1;
  221. // walk back to the beginning of the UTF_8 sequence:
  222. while (peek_at(subj, before_char_pos) >> 6 == 2 &&
  223. before_char_pos > 0) {
  224. before_char_pos -= 1;
  225. }
  226. len = utf8proc_iterate(subj->input.data + before_char_pos,
  227. subj->pos - before_char_pos, &before_char);
  228. if (len == -1) {
  229. before_char = 10;
  230. }
  231. }
  232. while (peek_char(subj) == c) {
  233. numdelims++;
  234. advance(subj);
  235. }
  236. len = utf8proc_iterate(subj->input.data + subj->pos,
  237. subj->input.len - subj->pos, &after_char);
  238. if (len == -1) {
  239. after_char = 10;
  240. }
  241. *can_open = numdelims > 0 && !utf8proc_is_space(after_char) &&
  242. !(utf8proc_is_punctuation(after_char) &&
  243. !utf8proc_is_space(before_char) &&
  244. !utf8proc_is_punctuation(before_char));
  245. *can_close = numdelims > 0 && !utf8proc_is_space(before_char) &&
  246. !(utf8proc_is_punctuation(before_char) &&
  247. !utf8proc_is_space(after_char) &&
  248. !utf8proc_is_punctuation(after_char));
  249. if (c == '_') {
  250. *can_open = *can_open &&
  251. !(before_char < 128 && isalnum((char)before_char));
  252. *can_close = *can_close &&
  253. !(before_char < 128 && isalnum((char)after_char));
  254. }
  255. return numdelims;
  256. }
  257. /*
  258. static void print_delimiters(subject *subj)
  259. {
  260. delimiter *delim;
  261. delim = subj->last_delim;
  262. while (delim != NULL) {
  263. printf("Item at %p: %d %d %d next(%p) prev(%p)\n",
  264. delim, delim->delim_char,
  265. delim->can_open, delim->can_close,
  266. delim->next, delim->previous);
  267. delim = delim->previous;
  268. }
  269. }
  270. */
  271. static void remove_delimiter(subject *subj, delimiter *delim)
  272. {
  273. if (delim == NULL) return;
  274. if (delim->next == NULL) {
  275. // end of list:
  276. assert(delim == subj->last_delim);
  277. subj->last_delim = delim->previous;
  278. } else {
  279. delim->next->previous = delim->previous;
  280. }
  281. if (delim->previous != NULL) {
  282. delim->previous->next = delim->next;
  283. }
  284. free(delim);
  285. }
  286. static void push_delimiter(subject *subj, unsigned char c, bool can_open,
  287. bool can_close, cmark_node *inl_text)
  288. {
  289. delimiter *delim =
  290. (delimiter*)malloc(sizeof(delimiter));
  291. if (delim == NULL) {
  292. return;
  293. }
  294. delim->delim_char = c;
  295. delim->can_open = can_open;
  296. delim->can_close = can_close;
  297. delim->inl_text = inl_text;
  298. delim->previous = subj->last_delim;
  299. delim->next = NULL;
  300. if (delim->previous != NULL) {
  301. delim->previous->next = delim;
  302. }
  303. delim->position = subj->pos;
  304. delim->active = true;
  305. subj->last_delim = delim;
  306. }
  307. // Parse strong/emph or a fallback.
  308. // Assumes the subject has '_' or '*' at the current position.
  309. static cmark_node* handle_strong_emph(subject* subj, unsigned char c)
  310. {
  311. int numdelims;
  312. cmark_node * inl_text;
  313. bool can_open, can_close;
  314. numdelims = scan_delims(subj, c, &can_open, &can_close);
  315. inl_text = make_str(cmark_chunk_dup(&subj->input, subj->pos - numdelims, numdelims));
  316. if (can_open || can_close) {
  317. push_delimiter(subj, c, can_open, can_close, inl_text);
  318. }
  319. return inl_text;
  320. }
  321. static void process_emphasis(subject *subj, delimiter *start_delim)
  322. {
  323. delimiter *closer = subj->last_delim;
  324. delimiter *opener;
  325. // move back to first relevant delim.
  326. while (closer != NULL && closer->previous != start_delim) {
  327. closer = closer->previous;
  328. }
  329. // now move forward, looking for closers, and handling each
  330. while (closer != NULL) {
  331. if (closer->can_close &&
  332. (closer->delim_char == '*' || closer->delim_char == '_')) {
  333. // Now look backwards for first matching opener:
  334. opener = closer->previous;
  335. while (opener != NULL && opener != start_delim) {
  336. if (opener->delim_char == closer->delim_char &&
  337. opener->can_open) {
  338. break;
  339. }
  340. opener = opener->previous;
  341. }
  342. if (opener != NULL && opener != start_delim) {
  343. closer = S_insert_emph(subj, opener, closer);
  344. } else {
  345. closer = closer->next;
  346. }
  347. } else {
  348. closer = closer->next;
  349. }
  350. }
  351. // free all delimiters in list until start_delim:
  352. while (subj->last_delim != start_delim) {
  353. remove_delimiter(subj, subj->last_delim);
  354. }
  355. }
  356. static delimiter*
  357. S_insert_emph(subject *subj, delimiter *opener, delimiter *closer)
  358. {
  359. delimiter *delim, *tmp_delim;
  360. int use_delims;
  361. cmark_node *opener_inl = opener->inl_text;
  362. cmark_node *closer_inl = closer->inl_text;
  363. int opener_num_chars = opener_inl->as.literal.len;
  364. int closer_num_chars = closer_inl->as.literal.len;
  365. cmark_node *tmp, *emph, *first_child, *last_child;
  366. // calculate the actual number of characters used from this closer
  367. if (closer_num_chars < 3 || opener_num_chars < 3) {
  368. use_delims = closer_num_chars <= opener_num_chars ?
  369. closer_num_chars : opener_num_chars;
  370. } else { // closer and opener both have >= 3 characters
  371. use_delims = closer_num_chars % 2 == 0 ? 2 : 1;
  372. }
  373. // remove used characters from associated inlines.
  374. opener_num_chars -= use_delims;
  375. closer_num_chars -= use_delims;
  376. opener_inl->as.literal.len = opener_num_chars;
  377. closer_inl->as.literal.len = closer_num_chars;
  378. // free delimiters between opener and closer
  379. delim = closer->previous;
  380. while (delim != NULL && delim != opener) {
  381. tmp_delim = delim->previous;
  382. remove_delimiter(subj, delim);
  383. delim = tmp_delim;
  384. }
  385. first_child = opener_inl->next;
  386. last_child = closer_inl->prev;
  387. // if opener has 0 characters, remove it and its associated inline
  388. if (opener_num_chars == 0) {
  389. // replace empty opener inline with emph
  390. cmark_chunk_free(&(opener_inl->as.literal));
  391. emph = opener_inl;
  392. emph->type = use_delims == 1 ? NODE_EMPH : NODE_STRONG;
  393. // remove opener from list
  394. remove_delimiter(subj, opener);
  395. }
  396. else {
  397. // create new emph or strong, and splice it in to our inlines
  398. // between the opener and closer
  399. emph = use_delims == 1 ? make_emph() : make_strong();
  400. emph->parent = opener_inl->parent;
  401. emph->prev = opener_inl;
  402. opener_inl->next = emph;
  403. }
  404. // push children below emph
  405. emph->next = closer_inl;
  406. closer_inl->prev = emph;
  407. emph->first_child = first_child;
  408. emph->last_child = last_child;
  409. // fix children pointers
  410. first_child->prev = NULL;
  411. last_child->next = NULL;
  412. for (tmp = first_child; tmp != NULL; tmp = tmp->next) {
  413. tmp->parent = emph;
  414. }
  415. // if closer has 0 characters, remove it and its associated inline
  416. if (closer_num_chars == 0) {
  417. // remove empty closer inline
  418. cmark_node_free(closer_inl);
  419. // remove closer from list
  420. tmp_delim = closer->next;
  421. remove_delimiter(subj, closer);
  422. closer = tmp_delim;
  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(cmark_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(cmark_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. cmark_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(cmark_chunk_literal("&"));
  454. subj->pos += len;
  455. return make_str(cmark_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(cmark_chunk *content)
  460. {
  461. cmark_strbuf unescaped = GH_BUF_INIT;
  462. if (houdini_unescape_html(&unescaped, content->data, (size_t)content->len)) {
  463. return make_str(cmark_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(cmark_chunk *url)
  471. {
  472. cmark_strbuf buf = GH_BUF_INIT;
  473. cmark_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. cmark_strbuf_unescape(&buf);
  482. return cmark_strbuf_detach(&buf);
  483. }
  484. unsigned char *cmark_clean_title(cmark_chunk *title)
  485. {
  486. cmark_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. cmark_strbuf_unescape(&buf);
  501. return cmark_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. cmark_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 = cmark_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 = cmark_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 = cmark_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(cmark_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, cmark_chunk *raw_label)
  545. {
  546. int startpos = subj->pos;
  547. int length = 0;
  548. unsigned char c;
  549. // advance past [
  550. if (peek_char(subj) == '[') {
  551. advance(subj);
  552. } else {
  553. return 0;
  554. }
  555. while ((c = peek_char(subj)) && c != '[' && c != ']') {
  556. if (c == '\\') {
  557. advance(subj);
  558. length++;
  559. if (ispunct(peek_char(subj))) {
  560. advance(subj);
  561. length++;
  562. }
  563. } else {
  564. advance(subj);
  565. length++;
  566. }
  567. if (length > MAX_LINK_LABEL_LENGTH) {
  568. goto noMatch;
  569. }
  570. }
  571. if (c == ']') { // match found
  572. *raw_label = cmark_chunk_dup(&subj->input, startpos + 1, subj->pos - (startpos + 1));
  573. advance(subj); // advance past ]
  574. return 1;
  575. }
  576. noMatch:
  577. subj->pos = startpos; // rewind
  578. return 0;
  579. }
  580. // Return a link, an image, or a literal close bracket.
  581. static cmark_node* handle_close_bracket(subject* subj, cmark_node *parent)
  582. {
  583. int initial_pos;
  584. int starturl, endurl, starttitle, endtitle, endall;
  585. int n;
  586. int sps;
  587. cmark_reference *ref;
  588. bool is_image = false;
  589. cmark_chunk urlcmark_chunk, titlecmark_chunk;
  590. unsigned char *url, *title;
  591. delimiter *opener;
  592. cmark_node *link_text;
  593. cmark_node *inl;
  594. cmark_chunk raw_label;
  595. int found_label;
  596. advance(subj); // advance past ]
  597. initial_pos = subj->pos;
  598. // look through list of delimiters for a [ or !
  599. opener = subj->last_delim;
  600. while (opener) {
  601. if (opener->delim_char == '[' || opener->delim_char == '!') {
  602. break;
  603. }
  604. opener = opener->previous;
  605. }
  606. if (opener == NULL) {
  607. return make_str(cmark_chunk_literal("]"));
  608. }
  609. if (!opener->active) {
  610. // take delimiter off stack
  611. remove_delimiter(subj, opener);
  612. return make_str(cmark_chunk_literal("]"));
  613. }
  614. // If we got here, we matched a potential link/image text.
  615. is_image = opener->delim_char == '!';
  616. link_text = opener->inl_text->next;
  617. // Now we check to see if it's a link/image.
  618. // First, look for an inline link.
  619. if (peek_char(subj) == '(' &&
  620. ((sps = scan_spacechars(&subj->input, subj->pos + 1)) > -1) &&
  621. ((n = scan_link_url(&subj->input, subj->pos + 1 + sps)) > -1)) {
  622. // try to parse an explicit link:
  623. starturl = subj->pos + 1 + sps; // after (
  624. endurl = starturl + n;
  625. starttitle = endurl + scan_spacechars(&subj->input, endurl);
  626. // ensure there are spaces btw url and title
  627. endtitle = (starttitle == endurl) ? starttitle :
  628. starttitle + scan_link_title(&subj->input, starttitle);
  629. endall = endtitle + scan_spacechars(&subj->input, endtitle);
  630. if (peek_at(subj, endall) == ')') {
  631. subj->pos = endall + 1;
  632. urlcmark_chunk = cmark_chunk_dup(&subj->input, starturl, endurl - starturl);
  633. titlecmark_chunk = cmark_chunk_dup(&subj->input, starttitle, endtitle - starttitle);
  634. url = cmark_clean_url(&urlcmark_chunk);
  635. title = cmark_clean_title(&titlecmark_chunk);
  636. cmark_chunk_free(&urlcmark_chunk);
  637. cmark_chunk_free(&titlecmark_chunk);
  638. goto match;
  639. } else {
  640. goto noMatch;
  641. }
  642. }
  643. // Next, look for a following [link label] that matches in refmap.
  644. // skip spaces
  645. subj->pos = subj->pos + scan_spacechars(&subj->input, subj->pos);
  646. raw_label = cmark_chunk_literal("");
  647. found_label = link_label(subj, &raw_label);
  648. if (!found_label || raw_label.len == 0) {
  649. cmark_chunk_free(&raw_label);
  650. raw_label = cmark_chunk_dup(&subj->input, opener->position,
  651. initial_pos - opener->position - 1);
  652. }
  653. if (!found_label) {
  654. // If we have a shortcut reference link, back up
  655. // to before the spacse we skipped.
  656. subj->pos = initial_pos;
  657. }
  658. ref = cmark_reference_lookup(subj->refmap, &raw_label);
  659. cmark_chunk_free(&raw_label);
  660. if (ref != NULL) { // found
  661. url = bufdup(ref->url);
  662. title = bufdup(ref->title);
  663. goto match;
  664. } else {
  665. goto noMatch;
  666. }
  667. noMatch:
  668. // If we fall through to here, it means we didn't match a link:
  669. remove_delimiter(subj, opener); // remove this opener from delimiter list
  670. subj->pos = initial_pos;
  671. return make_str(cmark_chunk_literal("]"));
  672. match:
  673. inl = opener->inl_text;
  674. inl->type = is_image ? NODE_IMAGE : NODE_LINK;
  675. cmark_chunk_free(&inl->as.literal);
  676. inl->first_child = link_text;
  677. process_emphasis(subj, opener->previous);
  678. inl->as.link.url = url;
  679. inl->as.link.title = title;
  680. inl->next = NULL;
  681. if (link_text) {
  682. cmark_node *tmp;
  683. link_text->prev = NULL;
  684. for (tmp = link_text; tmp->next != NULL; tmp = tmp->next) {
  685. tmp->parent = inl;
  686. }
  687. tmp->parent = inl;
  688. inl->last_child = tmp;
  689. }
  690. parent->last_child = inl;
  691. // process_emphasis will remove this delimiter and all later ones.
  692. // Now, if we have a link, we also want to deactivate earlier link
  693. // delimiters. (This code can be removed if we decide to allow links
  694. // inside links.)
  695. if (!is_image) {
  696. opener = subj->last_delim;
  697. while (opener != NULL) {
  698. if (opener->delim_char == '[') {
  699. if (!opener->active) {
  700. break;
  701. } else {
  702. opener->active = false;
  703. }
  704. }
  705. opener = opener->previous;
  706. }
  707. }
  708. return NULL;
  709. }
  710. // Parse a hard or soft linebreak, returning an inline.
  711. // Assumes the subject has a newline at the current position.
  712. static cmark_node* handle_newline(subject *subj)
  713. {
  714. int nlpos = subj->pos;
  715. // skip over newline
  716. advance(subj);
  717. // skip spaces at beginning of line
  718. while (peek_char(subj) == ' ') {
  719. advance(subj);
  720. }
  721. if (nlpos > 1 &&
  722. peek_at(subj, nlpos - 1) == ' ' &&
  723. peek_at(subj, nlpos - 2) == ' ') {
  724. return make_linebreak();
  725. } else {
  726. return make_softbreak();
  727. }
  728. }
  729. static int subject_find_special_char(subject *subj)
  730. {
  731. // "\n\\`&_*[]<!"
  732. static const int8_t SPECIAL_CHARS[256] = {
  733. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  734. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  735. 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  736. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
  737. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  738. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0, 1,
  739. 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  740. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  741. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  742. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  743. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  744. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  745. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  746. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  747. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  748. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  749. int n = subj->pos + 1;
  750. while (n < subj->input.len) {
  751. if (SPECIAL_CHARS[subj->input.data[n]])
  752. return n;
  753. n++;
  754. }
  755. return subj->input.len;
  756. }
  757. // Parse an inline, advancing subject, and add it as a child of parent.
  758. // Return 0 if no inline can be parsed, 1 otherwise.
  759. static int parse_inline(subject* subj, cmark_node * parent)
  760. {
  761. cmark_node* new_inl = NULL;
  762. cmark_chunk contents;
  763. unsigned char c;
  764. int endpos;
  765. c = peek_char(subj);
  766. if (c == 0) {
  767. return 0;
  768. }
  769. switch(c){
  770. case '\n':
  771. new_inl = handle_newline(subj);
  772. break;
  773. case '`':
  774. new_inl = handle_backticks(subj);
  775. break;
  776. case '\\':
  777. new_inl = handle_backslash(subj);
  778. break;
  779. case '&':
  780. new_inl = handle_entity(subj);
  781. break;
  782. case '<':
  783. new_inl = handle_pointy_brace(subj);
  784. break;
  785. case '*':
  786. case '_':
  787. new_inl = handle_strong_emph(subj, c);
  788. break;
  789. case '[':
  790. advance(subj);
  791. new_inl = make_str(cmark_chunk_literal("["));
  792. push_delimiter(subj, '[', true, false, new_inl);
  793. break;
  794. case ']':
  795. new_inl = handle_close_bracket(subj, parent);
  796. break;
  797. case '!':
  798. advance(subj);
  799. if (peek_char(subj) == '[') {
  800. advance(subj);
  801. new_inl = make_str(cmark_chunk_literal("!["));
  802. push_delimiter(subj, '!', false, true, new_inl);
  803. } else {
  804. new_inl = make_str(cmark_chunk_literal("!"));
  805. }
  806. break;
  807. default:
  808. endpos = subject_find_special_char(subj);
  809. contents = cmark_chunk_dup(&subj->input, subj->pos, endpos - subj->pos);
  810. subj->pos = endpos;
  811. // if we're at a newline, strip trailing spaces.
  812. if (peek_char(subj) == '\n') {
  813. cmark_chunk_rtrim(&contents);
  814. }
  815. new_inl = make_str(contents);
  816. }
  817. if (new_inl != NULL) {
  818. cmark_node_append_child(parent, new_inl);
  819. }
  820. return 1;
  821. }
  822. // Parse inlines from parent's string_content, adding as children of parent.
  823. extern void cmark_parse_inlines(cmark_node* parent, cmark_reference_map *refmap)
  824. {
  825. subject subj;
  826. subject_from_buf(&subj, &parent->string_content, refmap);
  827. while (!is_eof(&subj) && parse_inline(&subj, parent)) ;
  828. process_emphasis(&subj, NULL);
  829. }
  830. // Parse zero or more space characters, including at most one newline.
  831. static void spnl(subject* subj)
  832. {
  833. bool seen_newline = false;
  834. while (peek_char(subj) == ' ' ||
  835. (!seen_newline &&
  836. (seen_newline = peek_char(subj) == '\n'))) {
  837. advance(subj);
  838. }
  839. }
  840. // Parse reference. Assumes string begins with '[' character.
  841. // Modify refmap if a reference is encountered.
  842. // Return 0 if no reference found, otherwise position of subject
  843. // after reference is parsed.
  844. int cmark_parse_reference_inline(cmark_strbuf *input, cmark_reference_map *refmap)
  845. {
  846. subject subj;
  847. cmark_chunk lab;
  848. cmark_chunk url;
  849. cmark_chunk title;
  850. int matchlen = 0;
  851. int beforetitle;
  852. subject_from_buf(&subj, input, NULL);
  853. // parse label:
  854. if (!link_label(&subj, &lab))
  855. return 0;
  856. // colon:
  857. if (peek_char(&subj) == ':') {
  858. advance(&subj);
  859. } else {
  860. return 0;
  861. }
  862. // parse link url:
  863. spnl(&subj);
  864. matchlen = scan_link_url(&subj.input, subj.pos);
  865. if (matchlen) {
  866. url = cmark_chunk_dup(&subj.input, subj.pos, matchlen);
  867. subj.pos += matchlen;
  868. } else {
  869. return 0;
  870. }
  871. // parse optional link_title
  872. beforetitle = subj.pos;
  873. spnl(&subj);
  874. matchlen = scan_link_title(&subj.input, subj.pos);
  875. if (matchlen) {
  876. title = cmark_chunk_dup(&subj.input, subj.pos, matchlen);
  877. subj.pos += matchlen;
  878. } else {
  879. subj.pos = beforetitle;
  880. title = cmark_chunk_literal("");
  881. }
  882. // parse final spaces and newline:
  883. while (peek_char(&subj) == ' ') {
  884. advance(&subj);
  885. }
  886. if (peek_char(&subj) == '\n') {
  887. advance(&subj);
  888. } else if (peek_char(&subj) != 0) {
  889. return 0;
  890. }
  891. // insert reference into refmap
  892. cmark_reference_create(refmap, &lab, &url, &title);
  893. return subj.pos;
  894. }