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