aboutsummaryrefslogtreecommitdiff
path: root/src/inlines.c
blob: 57f4b2848666de3f91eb28021c4fadbbd0a54fab (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_STRING, s)
  16. #define make_code(s) make_literal(CMARK_NODE_INLINE_CODE, s)
  17. #define make_raw_html(s) make_literal(CMARK_NODE_INLINE_HTML, s)
  18. #define make_linebreak() make_simple(CMARK_NODE_LINEBREAK)
  19. #define make_softbreak() make_simple(CMARK_NODE_SOFTBREAK)
  20. #define make_emph(contents) make_inlines(CMARK_NODE_EMPH, contents)
  21. #define make_strong(contents) make_inlines(CMARK_NODE_STRONG, contents)
  22. typedef struct DelimiterStack {
  23. struct DelimiterStack *previous;
  24. struct DelimiterStack *next;
  25. cmark_node *first_inline;
  26. int delim_count;
  27. unsigned char delim_char;
  28. int position;
  29. bool can_open;
  30. bool can_close;
  31. } delimiter_stack;
  32. typedef struct Subject {
  33. chunk input;
  34. int pos;
  35. reference_map *refmap;
  36. delimiter_stack *delimiters;
  37. } subject;
  38. static cmark_node *parse_inlines_from_subject(subject* subj);
  39. static int parse_inline(subject* subj, cmark_node ** last);
  40. static void subject_from_buf(subject *e, strbuf *buffer, reference_map *refmap);
  41. static int subject_find_special_char(subject *subj);
  42. static unsigned char *cmark_clean_autolink(chunk *url, int is_email)
  43. {
  44. strbuf buf = GH_BUF_INIT;
  45. chunk_trim(url);
  46. if (url->len == 0)
  47. return NULL;
  48. if (is_email)
  49. strbuf_puts(&buf, "mailto:");
  50. houdini_unescape_html_f(&buf, url->data, url->len);
  51. return strbuf_detach(&buf);
  52. }
  53. static inline cmark_node *make_link(cmark_node *label, unsigned char *url, unsigned char *title)
  54. {
  55. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  56. if(e != NULL) {
  57. e->type = CMARK_NODE_LINK;
  58. e->first_child = label;
  59. e->as.link.url = url;
  60. e->as.link.title = title;
  61. e->next = NULL;
  62. }
  63. return e;
  64. }
  65. static inline cmark_node* make_autolink(cmark_node* label, cmark_chunk url, int is_email)
  66. {
  67. return make_link(label, cmark_clean_autolink(&url, is_email), NULL);
  68. }
  69. static inline cmark_node* make_inlines(cmark_node_type t, cmark_node* contents)
  70. {
  71. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  72. if(e != NULL) {
  73. e->type = t;
  74. e->first_child = contents;
  75. e->next = NULL;
  76. }
  77. return e;
  78. }
  79. // Create an inline with a literal string value.
  80. static inline cmark_node* make_literal(cmark_node_type t, cmark_chunk s)
  81. {
  82. cmark_node * e = (cmark_node *)calloc(1, sizeof(*e));
  83. if(e != NULL) {
  84. e->type = t;
  85. e->as.literal = s;
  86. e->next = NULL;
  87. }
  88. return e;
  89. }
  90. // Create an inline with no value.
  91. static inline cmark_node* make_simple(cmark_node_type t)
  92. {
  93. cmark_node* e = (cmark_node *)calloc(1, sizeof(*e));
  94. if(e != NULL) {
  95. e->type = t;
  96. e->next = NULL;
  97. }
  98. return e;
  99. }
  100. static unsigned char *bufdup(const unsigned char *buf)
  101. {
  102. unsigned char *new_buf = NULL;
  103. if (buf) {
  104. int len = strlen((char *)buf);
  105. new_buf = (unsigned char *)calloc(len + 1, sizeof(*new_buf));
  106. if(new_buf != NULL) {
  107. memcpy(new_buf, buf, len + 1);
  108. }
  109. }
  110. return new_buf;
  111. }
  112. static void subject_from_buf(subject *e, strbuf *buffer, reference_map *refmap)
  113. {
  114. e->input.data = buffer->ptr;
  115. e->input.len = buffer->size;
  116. e->input.alloc = 0;
  117. e->pos = 0;
  118. e->refmap = refmap;
  119. e->delimiters = NULL;
  120. chunk_rtrim(&e->input);
  121. }
  122. static inline int isbacktick(int c)
  123. {
  124. return (c == '`');
  125. }
  126. static inline unsigned char peek_char(subject *subj)
  127. {
  128. return (subj->pos < subj->input.len) ? subj->input.data[subj->pos] : 0;
  129. }
  130. static inline unsigned char peek_at(subject *subj, int pos)
  131. {
  132. return subj->input.data[pos];
  133. }
  134. // Return true if there are more characters in the subject.
  135. static inline int is_eof(subject* subj)
  136. {
  137. return (subj->pos >= subj->input.len);
  138. }
  139. // Advance the subject. Doesn't check for eof.
  140. #define advance(subj) (subj)->pos += 1
  141. // Take characters while a predicate holds, and return a string.
  142. static inline chunk take_while(subject* subj, int (*f)(int))
  143. {
  144. unsigned char c;
  145. int startpos = subj->pos;
  146. int len = 0;
  147. while ((c = peek_char(subj)) && (*f)(c)) {
  148. advance(subj);
  149. len++;
  150. }
  151. return chunk_dup(&subj->input, startpos, len);
  152. }
  153. // Append inline list b to the end of inline list a.
  154. // Return pointer to head of new list.
  155. static inline cmark_node* cmark_append_inlines(cmark_node* a, cmark_node* b)
  156. {
  157. if (a == NULL) { // NULL acts like an empty list
  158. return b;
  159. }
  160. cmark_node* cur = a;
  161. while (cur->next != NULL) {
  162. cur = cur->next;
  163. }
  164. cur->next = b;
  165. b->prev = cur;
  166. return a;
  167. }
  168. // Try to process a backtick code span that began with a
  169. // span of ticks of length openticklength length (already
  170. // parsed). Return 0 if you don't find matching closing
  171. // backticks, otherwise return the position in the subject
  172. // after the closing backticks.
  173. static int scan_to_closing_backticks(subject* subj, int openticklength)
  174. {
  175. // read non backticks
  176. unsigned char c;
  177. while ((c = peek_char(subj)) && c != '`') {
  178. advance(subj);
  179. }
  180. if (is_eof(subj)) {
  181. return 0; // did not find closing ticks, return 0
  182. }
  183. int numticks = 0;
  184. while (peek_char(subj) == '`') {
  185. advance(subj);
  186. numticks++;
  187. }
  188. if (numticks != openticklength){
  189. return(scan_to_closing_backticks(subj, openticklength));
  190. }
  191. return (subj->pos);
  192. }
  193. // Parse backtick code section or raw backticks, return an inline.
  194. // Assumes that the subject has a backtick at the current position.
  195. static cmark_node* handle_backticks(subject *subj)
  196. {
  197. chunk openticks = take_while(subj, isbacktick);
  198. int startpos = subj->pos;
  199. int endpos = scan_to_closing_backticks(subj, openticks.len);
  200. if (endpos == 0) { // not found
  201. subj->pos = startpos; // rewind
  202. return make_str(openticks);
  203. } else {
  204. strbuf buf = GH_BUF_INIT;
  205. strbuf_set(&buf, subj->input.data + startpos, endpos - startpos - openticks.len);
  206. strbuf_trim(&buf);
  207. strbuf_normalize_whitespace(&buf);
  208. return make_code(chunk_buf_detach(&buf));
  209. }
  210. }
  211. // Scan ***, **, or * and return number scanned, or 0.
  212. // Advances position.
  213. static int scan_delims(subject* subj, unsigned char c, bool * can_open, bool * can_close)
  214. {
  215. int numdelims = 0;
  216. unsigned char char_before, char_after;
  217. char_before = subj->pos == 0 ? '\n' : peek_at(subj, subj->pos - 1);
  218. while (peek_char(subj) == c) {
  219. numdelims++;
  220. advance(subj);
  221. }
  222. char_after = peek_char(subj);
  223. *can_open = numdelims > 0 && !isspace(char_after);
  224. *can_close = numdelims > 0 && !isspace(char_before);
  225. if (c == '_') {
  226. *can_open = *can_open && !isalnum(char_before);
  227. *can_close = *can_close && !isalnum(char_after);
  228. }
  229. return numdelims;
  230. }
  231. /*
  232. static void print_delimiters(subject *subj)
  233. {
  234. delimiter_stack *tempstack;
  235. tempstack = subj->delimiters;
  236. while (tempstack != NULL) {
  237. printf("Item at %p: %d %d %d %d next(%p) prev(%p)\n",
  238. tempstack, tempstack->delim_count, tempstack->delim_char,
  239. tempstack->can_open, tempstack->can_close,
  240. tempstack->next, tempstack->previous);
  241. tempstack = tempstack->previous;
  242. }
  243. }
  244. */
  245. static void remove_delimiter(subject *subj, delimiter_stack *stack)
  246. {
  247. if (stack->previous != NULL) {
  248. stack->previous->next = stack->next;
  249. }
  250. if (stack->next == NULL) {
  251. // top of stack
  252. subj->delimiters = stack->previous;
  253. } else {
  254. stack->next->previous = stack->previous;
  255. }
  256. free(stack);
  257. }
  258. static delimiter_stack * push_delimiter(subject *subj,
  259. int numdelims,
  260. unsigned char c,
  261. bool can_open,
  262. bool can_close,
  263. cmark_node *inl_text)
  264. {
  265. delimiter_stack *istack =
  266. (delimiter_stack*)malloc(sizeof(delimiter_stack));
  267. if (istack == NULL) {
  268. return NULL;
  269. }
  270. istack->delim_count = numdelims;
  271. istack->delim_char = c;
  272. istack->can_open = can_open;
  273. istack->can_close = can_close;
  274. istack->first_inline = inl_text;
  275. istack->previous = subj->delimiters;
  276. istack->next = NULL;
  277. if (istack->previous != NULL) {
  278. istack->previous->next = istack;
  279. }
  280. istack->position = subj->pos;
  281. return istack;
  282. }
  283. // Parse strong/emph or a fallback.
  284. // Assumes the subject has '_' or '*' at the current position.
  285. static cmark_node* handle_strong_emph(subject* subj, unsigned char c, cmark_node **last)
  286. {
  287. int numdelims;
  288. cmark_node * inl_text;
  289. bool can_open, can_close;
  290. numdelims = scan_delims(subj, c, &can_open, &can_close);
  291. inl_text = make_str(chunk_dup(&subj->input, subj->pos - numdelims, numdelims));
  292. if (can_open || can_close) {
  293. subj->delimiters = push_delimiter(subj, numdelims, c, can_open, can_close,
  294. inl_text);
  295. }
  296. return inl_text;
  297. }
  298. static void process_emphasis(subject *subj, delimiter_stack *stack_bottom)
  299. {
  300. delimiter_stack *closer = subj->delimiters;
  301. delimiter_stack *opener, *tempstack, *nextstack;
  302. int use_delims;
  303. cmark_node *inl, *tmp, *emph;
  304. // move back to first relevant delim.
  305. while (closer != NULL && closer->previous != stack_bottom) {
  306. closer = closer->previous;
  307. }
  308. // now move forward, looking for closers, and handling each
  309. while (closer != NULL) {
  310. if (closer->can_close &&
  311. (closer->delim_char == '*' || closer->delim_char == '_')) {
  312. // Now look backwards for first matching opener:
  313. opener = closer->previous;
  314. while (opener != NULL && opener != stack_bottom) {
  315. if (opener->delim_char == closer->delim_char &&
  316. opener->can_open) {
  317. break;
  318. }
  319. opener = opener->previous;
  320. }
  321. if (opener != NULL && opener != stack_bottom) {
  322. // calculate the actual number of delimeters used from this closer
  323. if (closer->delim_count < 3 || opener->delim_count < 3) {
  324. use_delims = closer->delim_count <= opener->delim_count ?
  325. closer->delim_count : opener->delim_count;
  326. } else { // closer and opener both have >= 3 delims
  327. use_delims = closer->delim_count % 2 == 0 ? 2 : 1;
  328. }
  329. inl = opener->first_inline;
  330. // remove used delimiters from stack elements and associated inlines.
  331. opener->delim_count -= use_delims;
  332. closer->delim_count -= use_delims;
  333. inl->as.literal.len = opener->delim_count;
  334. closer->first_inline->as.literal.len = closer->delim_count;
  335. // free delimiters between opener and closer
  336. tempstack = closer->previous;
  337. while (tempstack != NULL && tempstack != opener) {
  338. nextstack = tempstack->previous;
  339. remove_delimiter(subj, tempstack);
  340. tempstack = nextstack;
  341. }
  342. // create new emph or strong, and splice it in to our inlines
  343. // between the opener and closer
  344. emph = use_delims == 1 ? make_emph(inl->next) : make_strong(inl->next);
  345. emph->next = closer->first_inline;
  346. emph->prev = inl;
  347. inl->next = emph;
  348. // if opener has 0 delims, remove it and its associated inline
  349. if (opener->delim_count == 0) {
  350. // replace empty opener inline with emph
  351. chunk_free(&(inl->as.literal));
  352. inl->type = emph->type;
  353. inl->next = emph->next;
  354. inl->first_child = emph->first_child;
  355. free(emph);
  356. emph = inl;
  357. // remove opener from stack
  358. remove_delimiter(subj, opener);
  359. }
  360. // fix tree structure
  361. tmp = emph->first_child;
  362. while (tmp->next != NULL && tmp->next != closer->first_inline) {
  363. tmp->parent = emph;
  364. tmp = tmp->next;
  365. }
  366. tmp->parent = emph;
  367. if (tmp->next) {
  368. tmp->next->prev = emph;
  369. }
  370. tmp->next = NULL;
  371. emph->last_child = tmp;
  372. // if closer has 0 delims, remove it and its associated inline
  373. if (closer->delim_count == 0) {
  374. // remove empty closer inline
  375. tmp = closer->first_inline;
  376. emph->next = tmp->next;
  377. if (tmp->next) {
  378. tmp->next->prev = emph;
  379. }
  380. tmp->next = NULL;
  381. cmark_free_nodes(tmp);
  382. // remove closer from stack
  383. tempstack = closer->next;
  384. remove_delimiter(subj, closer);
  385. closer = tempstack;
  386. }
  387. } else {
  388. closer = closer->next;
  389. }
  390. } else {
  391. closer = closer->next;
  392. }
  393. }
  394. // free all delimiters in stack down to stack_bottom:
  395. while (subj->delimiters != stack_bottom) {
  396. remove_delimiter(subj, subj->delimiters);
  397. }
  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 *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 *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. advance(subj); // advance past [
  522. unsigned char c;
  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 **last)
  550. {
  551. int initial_pos;
  552. int starturl, endurl, starttitle, endtitle, endall;
  553. int n;
  554. int sps;
  555. reference *ref;
  556. bool is_image = false;
  557. chunk urlchunk, titlechunk;
  558. unsigned char *url, *title;
  559. delimiter_stack *opener;
  560. delimiter_stack *tempstack;
  561. cmark_node *link_text;
  562. cmark_node *inl;
  563. chunk raw_label;
  564. advance(subj); // advance past ]
  565. initial_pos = subj->pos;
  566. // look through stack of delimiters for a [ or !
  567. opener = subj->delimiters;
  568. while (opener) {
  569. if (opener->delim_char == '[' || opener->delim_char == '!') {
  570. break;
  571. }
  572. opener = opener->previous;
  573. }
  574. if (opener == NULL) {
  575. return make_str(chunk_literal("]"));
  576. }
  577. // If we got here, we matched a potential link/image text.
  578. is_image = opener->delim_char == '!';
  579. link_text = opener->first_inline->next;
  580. // Now we check to see if it's a link/image.
  581. // First, look for an inline link.
  582. if (peek_char(subj) == '(' &&
  583. ((sps = scan_spacechars(&subj->input, subj->pos + 1)) > -1) &&
  584. ((n = scan_link_url(&subj->input, subj->pos + 1 + sps)) > -1)) {
  585. // try to parse an explicit link:
  586. starturl = subj->pos + 1 + sps; // after (
  587. endurl = starturl + n;
  588. starttitle = endurl + scan_spacechars(&subj->input, endurl);
  589. // ensure there are spaces btw url and title
  590. endtitle = (starttitle == endurl) ? starttitle :
  591. starttitle + scan_link_title(&subj->input, starttitle);
  592. endall = endtitle + scan_spacechars(&subj->input, endtitle);
  593. if (peek_at(subj, endall) == ')') {
  594. subj->pos = endall + 1;
  595. urlchunk = chunk_dup(&subj->input, starturl, endurl - starturl);
  596. titlechunk = chunk_dup(&subj->input, starttitle, endtitle - starttitle);
  597. url = clean_url(&urlchunk);
  598. title = clean_title(&titlechunk);
  599. chunk_free(&urlchunk);
  600. chunk_free(&titlechunk);
  601. goto match;
  602. } else {
  603. goto noMatch;
  604. }
  605. }
  606. // Next, look for a following [link label] that matches in refmap.
  607. // skip spaces
  608. subj->pos = subj->pos + scan_spacechars(&subj->input, subj->pos);
  609. raw_label = chunk_literal("");
  610. if (!link_label(subj, &raw_label) || raw_label.len == 0) {
  611. chunk_free(&raw_label);
  612. raw_label = chunk_dup(&subj->input, opener->position,
  613. initial_pos - opener->position - 1);
  614. }
  615. ref = reference_lookup(subj->refmap, &raw_label);
  616. chunk_free(&raw_label);
  617. if (ref != NULL) { // found
  618. url = bufdup(ref->url);
  619. title = bufdup(ref->title);
  620. goto match;
  621. } else {
  622. goto noMatch;
  623. }
  624. noMatch:
  625. // If we fall through to here, it means we didn't match a link:
  626. remove_delimiter(subj, opener); // remove this opener from delimiter stack
  627. subj->pos = initial_pos;
  628. return make_str(chunk_literal("]"));
  629. match:
  630. inl = opener->first_inline;
  631. inl->type = is_image ? NODE_IMAGE : NODE_LINK;
  632. chunk_free(&inl->as.literal);
  633. inl->first_child = link_text;
  634. process_emphasis(subj, opener->previous);
  635. inl->as.link.url = url;
  636. inl->as.link.title = title;
  637. inl->next = NULL;
  638. if (link_text) {
  639. cmark_node *tmp;
  640. link_text->prev = NULL;
  641. for (tmp = link_text; tmp->next != NULL; tmp = tmp->next) {
  642. tmp->parent = inl;
  643. }
  644. tmp->parent = inl;
  645. inl->last_child = tmp;
  646. }
  647. *last = inl;
  648. // process_emphasis will remove this delimiter and all later ones.
  649. // Now, if we have a link, we also want to remove earlier link
  650. // delimiters. (This code can be removed if we decide to allow links
  651. // inside links.)
  652. if (!is_image) {
  653. opener = subj->delimiters;
  654. while (opener != NULL) {
  655. tempstack = opener->previous;
  656. if (opener->delim_char == '[') {
  657. remove_delimiter(subj, opener);
  658. }
  659. opener = tempstack;
  660. }
  661. }
  662. return NULL;
  663. }
  664. // Parse a hard or soft linebreak, returning an inline.
  665. // Assumes the subject has a newline at the current position.
  666. static cmark_node* handle_newline(subject *subj)
  667. {
  668. int nlpos = subj->pos;
  669. // skip over newline
  670. advance(subj);
  671. // skip spaces at beginning of line
  672. while (peek_char(subj) == ' ') {
  673. advance(subj);
  674. }
  675. if (nlpos > 1 &&
  676. peek_at(subj, nlpos - 1) == ' ' &&
  677. peek_at(subj, nlpos - 2) == ' ') {
  678. return make_linebreak();
  679. } else {
  680. return make_softbreak();
  681. }
  682. }
  683. // Parse inlines til end of subject, returning inlines.
  684. extern cmark_node* parse_inlines_from_subject(subject* subj)
  685. {
  686. cmark_node* result = NULL;
  687. cmark_node** last = &result;
  688. cmark_node* first = NULL;
  689. while (!is_eof(subj) && parse_inline(subj, last)) {
  690. if (!first) {
  691. first = *last;
  692. }
  693. }
  694. process_emphasis(subj, NULL);
  695. return first;
  696. }
  697. static int subject_find_special_char(subject *subj)
  698. {
  699. // "\n\\`&_*[]<!"
  700. static const int8_t SPECIAL_CHARS[256] = {
  701. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  702. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  703. 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0,
  704. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 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, 1, 1, 1, 0, 1,
  707. 1, 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. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  716. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
  717. int n = subj->pos + 1;
  718. while (n < subj->input.len) {
  719. if (SPECIAL_CHARS[subj->input.data[n]])
  720. return n;
  721. n++;
  722. }
  723. return subj->input.len;
  724. }
  725. // Parse an inline, advancing subject, and add it to last element.
  726. // Adjust tail to point to new last element of list.
  727. // Return 0 if no inline can be parsed, 1 otherwise.
  728. static int parse_inline(subject* subj, cmark_node ** last)
  729. {
  730. cmark_node* new_inl = NULL;
  731. chunk contents;
  732. unsigned char c;
  733. int endpos;
  734. c = peek_char(subj);
  735. if (c == 0) {
  736. return 0;
  737. }
  738. switch(c){
  739. case '\n':
  740. new_inl = handle_newline(subj);
  741. break;
  742. case '`':
  743. new_inl = handle_backticks(subj);
  744. break;
  745. case '\\':
  746. new_inl = handle_backslash(subj);
  747. break;
  748. case '&':
  749. new_inl = handle_entity(subj);
  750. break;
  751. case '<':
  752. new_inl = handle_pointy_brace(subj);
  753. break;
  754. case '*':
  755. case '_':
  756. new_inl = handle_strong_emph(subj, c, last);
  757. break;
  758. case '[':
  759. advance(subj);
  760. new_inl = make_str(chunk_literal("["));
  761. subj->delimiters = push_delimiter(subj, 1, '[', true, false, new_inl);
  762. break;
  763. case ']':
  764. new_inl = handle_close_bracket(subj, last);
  765. break;
  766. case '!':
  767. advance(subj);
  768. if (peek_char(subj) == '[') {
  769. advance(subj);
  770. new_inl = make_str(chunk_literal("!["));
  771. subj->delimiters = push_delimiter(subj, 1, '!', false, true, new_inl);
  772. } else {
  773. new_inl = make_str(chunk_literal("!"));
  774. }
  775. break;
  776. default:
  777. endpos = subject_find_special_char(subj);
  778. contents = chunk_dup(&subj->input, subj->pos, endpos - subj->pos);
  779. subj->pos = endpos;
  780. // if we're at a newline, strip trailing spaces.
  781. if (peek_char(subj) == '\n') {
  782. chunk_rtrim(&contents);
  783. }
  784. new_inl = make_str(contents);
  785. }
  786. if (*last == NULL) {
  787. *last = new_inl;
  788. } else if (new_inl) {
  789. cmark_append_inlines(*last, new_inl);
  790. *last = new_inl;
  791. }
  792. return 1;
  793. }
  794. extern cmark_node* parse_inlines(strbuf *input, reference_map *refmap)
  795. {
  796. subject subj;
  797. subject_from_buf(&subj, input, refmap);
  798. return parse_inlines_from_subject(&subj);
  799. }
  800. // Parse zero or more space characters, including at most one newline.
  801. static void spnl(subject* subj)
  802. {
  803. bool seen_newline = false;
  804. while (peek_char(subj) == ' ' ||
  805. (!seen_newline &&
  806. (seen_newline = peek_char(subj) == '\n'))) {
  807. advance(subj);
  808. }
  809. }
  810. // Parse reference. Assumes string begins with '[' character.
  811. // Modify refmap if a reference is encountered.
  812. // Return 0 if no reference found, otherwise position of subject
  813. // after reference is parsed.
  814. int parse_reference_inline(strbuf *input, reference_map *refmap)
  815. {
  816. subject subj;
  817. chunk lab;
  818. chunk url;
  819. chunk title;
  820. int matchlen = 0;
  821. int beforetitle;
  822. subject_from_buf(&subj, input, NULL);
  823. // parse label:
  824. if (!link_label(&subj, &lab))
  825. return 0;
  826. // colon:
  827. if (peek_char(&subj) == ':') {
  828. advance(&subj);
  829. } else {
  830. return 0;
  831. }
  832. // parse link url:
  833. spnl(&subj);
  834. matchlen = scan_link_url(&subj.input, subj.pos);
  835. if (matchlen) {
  836. url = chunk_dup(&subj.input, subj.pos, matchlen);
  837. subj.pos += matchlen;
  838. } else {
  839. return 0;
  840. }
  841. // parse optional link_title
  842. beforetitle = subj.pos;
  843. spnl(&subj);
  844. matchlen = scan_link_title(&subj.input, subj.pos);
  845. if (matchlen) {
  846. title = chunk_dup(&subj.input, subj.pos, matchlen);
  847. subj.pos += matchlen;
  848. } else {
  849. subj.pos = beforetitle;
  850. title = chunk_literal("");
  851. }
  852. // parse final spaces and newline:
  853. while (peek_char(&subj) == ' ') {
  854. advance(&subj);
  855. }
  856. if (peek_char(&subj) == '\n') {
  857. advance(&subj);
  858. } else if (peek_char(&subj) != 0) {
  859. return 0;
  860. }
  861. // insert reference into refmap
  862. reference_create(refmap, &lab, &url, &title);
  863. return subj.pos;
  864. }