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