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