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