aboutsummaryrefslogtreecommitdiff
path: root/src/inlines.c
blob: 773027ea273a96ea6580aa2b05f07437bac02a78 (plain)
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <stdbool.h>
  5. #include <ctype.h>
  6. #include "cmark.h"
  7. #include "html/houdini.h"
  8. #include "utf8.h"
  9. #include "scanners.h"
  10. #include "inlines.h"
  11. #include "debug.h"
  12. typedef struct OpenerStack {
  13. struct OpenerStack *previous;
  14. node_inl *first_inline;
  15. int delim_count;
  16. unsigned char delim_char;
  17. int position;
  18. } opener_stack;
  19. typedef struct Subject {
  20. chunk input;
  21. int pos;
  22. int label_nestlevel;
  23. reference_map *refmap;
  24. opener_stack *openers;
  25. } subject;
  26. static node_inl *parse_chunk_inlines(chunk *chunk, reference_map *refmap);
  27. static node_inl *parse_inlines_from_subject(subject* subj);
  28. static int parse_inline(subject* subj, node_inl ** last);
  29. static void subject_from_chunk(subject *e, chunk *chunk, reference_map *refmap);
  30. static void subject_from_buf(subject *e, strbuf *buffer, reference_map *refmap);
  31. static int subject_find_special_char(subject *subj);
  32. static unsigned char *bufdup(const unsigned char *buf)
  33. {
  34. unsigned char *new = NULL;
  35. if (buf) {
  36. int len = strlen((char *)buf);
  37. new = calloc(len + 1, sizeof(*new));
  38. if(new != NULL) {
  39. memcpy(new, buf, len + 1);
  40. }
  41. }
  42. return new;
  43. }
  44. static inline node_inl *make_link_(node_inl *label, unsigned char *url, unsigned char *title)
  45. {
  46. node_inl* e = calloc(1, sizeof(*e));
  47. if(e != NULL) {
  48. e->tag = INL_LINK;
  49. e->content.linkable.label = label;
  50. e->content.linkable.url = url;
  51. e->content.linkable.title = title;
  52. e->next = NULL;
  53. }
  54. return e;
  55. }
  56. inline static node_inl* make_ref_link(node_inl* label, reference *ref)
  57. {
  58. return make_link_(label, bufdup(ref->url), bufdup(ref->title));
  59. }
  60. inline static node_inl* make_autolink(node_inl* label, chunk url, int is_email)
  61. {
  62. return make_link_(label, clean_autolink(&url, is_email), NULL);
  63. }
  64. // Create an inline with a linkable string value.
  65. inline static node_inl* make_link(node_inl* label, chunk url, chunk title)
  66. {
  67. return make_link_(label, clean_url(&url), clean_title(&title));
  68. }
  69. inline static node_inl* make_inlines(int t, node_inl* contents)
  70. {
  71. node_inl * e = calloc(1, sizeof(*e));
  72. if(e != NULL) {
  73. e->tag = t;
  74. e->content.inlines = contents;
  75. e->next = NULL;
  76. }
  77. return e;
  78. }
  79. // Create an inline with a literal string value.
  80. inline static node_inl* make_literal(int t, chunk s)
  81. {
  82. node_inl * e = calloc(1, sizeof(*e));
  83. if(e != NULL) {
  84. e->tag = t;
  85. e->content.literal = s;
  86. e->next = NULL;
  87. }
  88. return e;
  89. }
  90. // Create an inline with no value.
  91. inline static node_inl* make_simple(int t)
  92. {
  93. node_inl* e = calloc(1, sizeof(*e));
  94. if(e != NULL) {
  95. e->tag = t;
  96. e->next = NULL;
  97. }
  98. return e;
  99. }
  100. // Macros for creating various kinds of inlines.
  101. #define make_str(s) make_literal(INL_STRING, s)
  102. #define make_code(s) make_literal(INL_CODE, s)
  103. #define make_raw_html(s) make_literal(INL_RAW_HTML, s)
  104. #define make_linebreak() make_simple(INL_LINEBREAK)
  105. #define make_softbreak() make_simple(INL_SOFTBREAK)
  106. #define make_emph(contents) make_inlines(INL_EMPH, contents)
  107. #define make_strong(contents) make_inlines(INL_STRONG, contents)
  108. // Utility function used by free_inlines
  109. void splice_into_list(node_inl* e, node_inl* children) {
  110. node_inl * tmp;
  111. if (children) {
  112. tmp = children;
  113. // Find last child
  114. while (tmp->next) {
  115. tmp = tmp->next;
  116. }
  117. // Splice children into list
  118. tmp->next = e->next;
  119. e->next = children;
  120. }
  121. return ;
  122. }
  123. // Free an inline list. Avoid recursion to prevent stack overflows
  124. // on deeply nested structures.
  125. extern void free_inlines(node_inl* e)
  126. {
  127. node_inl * next;
  128. while (e != NULL) {
  129. switch (e->tag){
  130. case INL_STRING:
  131. case INL_RAW_HTML:
  132. case INL_CODE:
  133. chunk_free(&e->content.literal);
  134. break;
  135. case INL_LINEBREAK:
  136. case INL_SOFTBREAK:
  137. break;
  138. case INL_LINK:
  139. case INL_IMAGE:
  140. free(e->content.linkable.url);
  141. free(e->content.linkable.title);
  142. splice_into_list(e, e->content.linkable.label);
  143. break;
  144. case INL_EMPH:
  145. case INL_STRONG:
  146. splice_into_list(e, e->content.inlines);
  147. break;
  148. default:
  149. log_warn("Unknown inline tag %d", e->tag);
  150. break;
  151. }
  152. next = e->next;
  153. free(e);
  154. e = next;
  155. }
  156. }
  157. // Append inline list b to the end of inline list a.
  158. // Return pointer to head of new list.
  159. inline static node_inl* append_inlines(node_inl* a, node_inl* b)
  160. {
  161. if (a == NULL) { // NULL acts like an empty list
  162. return b;
  163. }
  164. node_inl* cur = a;
  165. while (cur->next) {
  166. cur = cur->next;
  167. }
  168. cur->next = b;
  169. return a;
  170. }
  171. static void subject_from_buf(subject *e, strbuf *buffer, reference_map *refmap)
  172. {
  173. e->input.data = buffer->ptr;
  174. e->input.len = buffer->size;
  175. e->input.alloc = 0;
  176. e->pos = 0;
  177. e->label_nestlevel = 0;
  178. e->refmap = refmap;
  179. e->openers = NULL;
  180. chunk_rtrim(&e->input);
  181. }
  182. static void subject_from_chunk(subject *e, chunk *chunk, reference_map *refmap)
  183. {
  184. e->input.data = chunk->data;
  185. e->input.len = chunk->len;
  186. e->input.alloc = 0;
  187. e->pos = 0;
  188. e->label_nestlevel = 0;
  189. e->refmap = refmap;
  190. e->openers = NULL;
  191. chunk_rtrim(&e->input);
  192. }
  193. inline static int isbacktick(int c)
  194. {
  195. return (c == '`');
  196. }
  197. static inline unsigned char peek_char(subject *subj)
  198. {
  199. return (subj->pos < subj->input.len) ? subj->input.data[subj->pos] : 0;
  200. }
  201. static inline unsigned char peek_at(subject *subj, int pos)
  202. {
  203. return subj->input.data[pos];
  204. }
  205. // Return true if there are more characters in the subject.
  206. inline static int is_eof(subject* subj)
  207. {
  208. return (subj->pos >= subj->input.len);
  209. }
  210. // Advance the subject. Doesn't check for eof.
  211. #define advance(subj) (subj)->pos += 1
  212. // Take characters while a predicate holds, and return a string.
  213. inline static chunk take_while(subject* subj, int (*f)(int))
  214. {
  215. unsigned char c;
  216. int startpos = subj->pos;
  217. int len = 0;
  218. while ((c = peek_char(subj)) && (*f)(c)) {
  219. advance(subj);
  220. len++;
  221. }
  222. return chunk_dup(&subj->input, startpos, len);
  223. }
  224. // Try to process a backtick code span that began with a
  225. // span of ticks of length openticklength length (already
  226. // parsed). Return 0 if you don't find matching closing
  227. // backticks, otherwise return the position in the subject
  228. // after the closing backticks.
  229. static int scan_to_closing_backticks(subject* subj, int openticklength)
  230. {
  231. // read non backticks
  232. unsigned char c;
  233. while ((c = peek_char(subj)) && c != '`') {
  234. advance(subj);
  235. }
  236. if (is_eof(subj)) {
  237. return 0; // did not find closing ticks, return 0
  238. }
  239. int numticks = 0;
  240. while (peek_char(subj) == '`') {
  241. advance(subj);
  242. numticks++;
  243. }
  244. if (numticks != openticklength){
  245. return(scan_to_closing_backticks(subj, openticklength));
  246. }
  247. return (subj->pos);
  248. }
  249. // Parse backtick code section or raw backticks, return an inline.
  250. // Assumes that the subject has a backtick at the current position.
  251. static node_inl* handle_backticks(subject *subj)
  252. {
  253. chunk openticks = take_while(subj, isbacktick);
  254. int startpos = subj->pos;
  255. int endpos = scan_to_closing_backticks(subj, openticks.len);
  256. if (endpos == 0) { // not found
  257. subj->pos = startpos; // rewind
  258. return make_str(openticks);
  259. } else {
  260. strbuf buf = GH_BUF_INIT;
  261. strbuf_set(&buf, subj->input.data + startpos, endpos - startpos - openticks.len);
  262. strbuf_trim(&buf);
  263. strbuf_normalize_whitespace(&buf);
  264. return make_code(chunk_buf_detach(&buf));
  265. }
  266. }
  267. // Scan ***, **, or * and return number scanned, or 0.
  268. // Advances position.
  269. static int scan_delims(subject* subj, unsigned char c, bool * can_open, bool * can_close)
  270. {
  271. int numdelims = 0;
  272. unsigned char char_before, char_after;
  273. char_before = subj->pos == 0 ? '\n' : peek_at(subj, subj->pos - 1);
  274. while (peek_char(subj) == c) {
  275. numdelims++;
  276. advance(subj);
  277. }
  278. char_after = peek_char(subj);
  279. *can_open = numdelims > 0 && !isspace(char_after);
  280. *can_close = numdelims > 0 && !isspace(char_before);
  281. if (c == '_') {
  282. *can_open = *can_open && !isalnum(char_before);
  283. *can_close = *can_close && !isalnum(char_after);
  284. }
  285. return numdelims;
  286. }
  287. static void free_openers(subject* subj, opener_stack* istack)
  288. {
  289. opener_stack * tempstack;
  290. while (subj->openers != istack) {
  291. tempstack = subj->openers;
  292. subj->openers = subj->openers->previous;
  293. free(tempstack);
  294. }
  295. }
  296. static opener_stack * push_opener(subject *subj,
  297. int numdelims,
  298. unsigned char c,
  299. node_inl *inl_text)
  300. {
  301. opener_stack *istack =
  302. (opener_stack*)malloc(sizeof(opener_stack));
  303. if (istack == NULL) {
  304. return NULL;
  305. }
  306. istack->delim_count = numdelims;
  307. istack->delim_char = c;
  308. istack->first_inline = inl_text;
  309. istack->previous = subj->openers;
  310. istack->position = subj->pos;
  311. return istack;
  312. }
  313. // Parse strong/emph or a fallback.
  314. // Assumes the subject has '_' or '*' at the current position.
  315. static node_inl* handle_strong_emph(subject* subj, unsigned char c, node_inl **last)
  316. {
  317. bool can_open, can_close;
  318. int numdelims;
  319. int useDelims;
  320. int openerDelims;
  321. opener_stack * istack;
  322. node_inl * inl;
  323. node_inl * emph;
  324. node_inl * inl_text;
  325. numdelims = scan_delims(subj, c, &can_open, &can_close);
  326. if (can_close)
  327. {
  328. // walk the stack and find a matching opener, if there is one
  329. istack = subj->openers;
  330. while (true)
  331. {
  332. if (istack == NULL)
  333. goto cannotClose;
  334. if (istack->delim_char == c)
  335. break;
  336. istack = istack->previous;
  337. }
  338. // calculate the actual number of delimeters used from this closer
  339. openerDelims = istack->delim_count;
  340. if (numdelims < 3 || openerDelims < 3) {
  341. useDelims = numdelims <= openerDelims ? numdelims : openerDelims;
  342. } else { // (numdelims >= 3 && openerDelims >= 3)
  343. useDelims = numdelims % 2 == 0 ? 2 : 1;
  344. }
  345. if (istack->delim_count == useDelims)
  346. {
  347. // the opener is completely used up - remove the stack entry and reuse the inline element
  348. inl = istack->first_inline;
  349. inl->tag = useDelims == 1 ? INL_EMPH : INL_STRONG;
  350. chunk_free(&inl->content.literal);
  351. inl->content.inlines = inl->next;
  352. inl->next = NULL;
  353. // remove this opener and all later ones from stack:
  354. free_openers(subj, istack->previous);
  355. *last = inl;
  356. }
  357. else
  358. {
  359. // the opener will only partially be used - stack entry remains (truncated) and a new inline is added.
  360. inl = istack->first_inline;
  361. istack->delim_count -= useDelims;
  362. inl->content.literal.len = istack->delim_count;
  363. emph = useDelims == 1 ? make_emph(inl->next) : make_strong(inl->next);
  364. inl->next = emph;
  365. // remove all later openers from stack:
  366. free_openers(subj, istack);
  367. *last = emph;
  368. }
  369. // if the closer was not fully used, move back a char or two and try again.
  370. if (useDelims < numdelims)
  371. {
  372. subj->pos = subj->pos - numdelims + useDelims;
  373. return NULL;
  374. }
  375. return NULL; // make_str(chunk_literal(""));
  376. }
  377. cannotClose:
  378. inl_text = make_str(chunk_dup(&subj->input, subj->pos - numdelims, numdelims));
  379. if (can_open)
  380. {
  381. subj->openers = push_opener(subj,
  382. numdelims,
  383. c,
  384. inl_text);
  385. }
  386. return inl_text;
  387. }
  388. // Parse backslash-escape or just a backslash, returning an inline.
  389. static node_inl* handle_backslash(subject *subj)
  390. {
  391. advance(subj);
  392. unsigned char nextchar = peek_char(subj);
  393. if (ispunct(nextchar)) { // only ascii symbols and newline can be escaped
  394. advance(subj);
  395. return make_str(chunk_dup(&subj->input, subj->pos - 1, 1));
  396. } else if (nextchar == '\n') {
  397. advance(subj);
  398. return make_linebreak();
  399. } else {
  400. return make_str(chunk_literal("\\"));
  401. }
  402. }
  403. // Parse an entity or a regular "&" string.
  404. // Assumes the subject has an '&' character at the current position.
  405. static node_inl* handle_entity(subject* subj)
  406. {
  407. strbuf ent = GH_BUF_INIT;
  408. size_t len;
  409. advance(subj);
  410. len = houdini_unescape_ent(&ent,
  411. subj->input.data + subj->pos,
  412. subj->input.len - subj->pos
  413. );
  414. if (len == 0)
  415. return make_str(chunk_literal("&"));
  416. subj->pos += len;
  417. return make_str(chunk_buf_detach(&ent));
  418. }
  419. // Like make_str, but parses entities.
  420. // Returns an inline sequence consisting of str and entity elements.
  421. static node_inl *make_str_with_entities(chunk *content)
  422. {
  423. strbuf unescaped = GH_BUF_INIT;
  424. if (houdini_unescape_html(&unescaped, content->data, (size_t)content->len)) {
  425. return make_str(chunk_buf_detach(&unescaped));
  426. } else {
  427. return make_str(*content);
  428. }
  429. }
  430. // Clean a URL: remove surrounding whitespace and surrounding <>,
  431. // and remove \ that escape punctuation.
  432. unsigned char *clean_url(chunk *url)
  433. {
  434. strbuf buf = GH_BUF_INIT;
  435. chunk_trim(url);
  436. if (url->len == 0)
  437. return NULL;
  438. if (url->data[0] == '<' && url->data[url->len - 1] == '>') {
  439. houdini_unescape_html_f(&buf, url->data + 1, url->len - 2);
  440. } else {
  441. houdini_unescape_html_f(&buf, url->data, url->len);
  442. }
  443. strbuf_unescape(&buf);
  444. return strbuf_detach(&buf);
  445. }
  446. unsigned char *clean_autolink(chunk *url, int is_email)
  447. {
  448. strbuf buf = GH_BUF_INIT;
  449. chunk_trim(url);
  450. if (url->len == 0)
  451. return NULL;
  452. if (is_email)
  453. strbuf_puts(&buf, "mailto:");
  454. houdini_unescape_html_f(&buf, url->data, url->len);
  455. return strbuf_detach(&buf);
  456. }
  457. // Clean a title: remove surrounding quotes and remove \ that escape punctuation.
  458. unsigned char *clean_title(chunk *title)
  459. {
  460. strbuf buf = GH_BUF_INIT;
  461. unsigned char first, last;
  462. if (title->len == 0)
  463. return NULL;
  464. first = title->data[0];
  465. last = title->data[title->len - 1];
  466. // remove surrounding quotes if any:
  467. if ((first == '\'' && last == '\'') ||
  468. (first == '(' && last == ')') ||
  469. (first == '"' && last == '"')) {
  470. houdini_unescape_html_f(&buf, title->data + 1, title->len - 2);
  471. } else {
  472. houdini_unescape_html_f(&buf, title->data, title->len);
  473. }
  474. strbuf_unescape(&buf);
  475. return strbuf_detach(&buf);
  476. }
  477. // Parse an autolink or HTML tag.
  478. // Assumes the subject has a '<' character at the current position.
  479. static node_inl* handle_pointy_brace(subject* subj)
  480. {
  481. int matchlen = 0;
  482. chunk contents;
  483. advance(subj); // advance past first <
  484. // first try to match a URL autolink
  485. matchlen = scan_autolink_uri(&subj->input, subj->pos);
  486. if (matchlen > 0) {
  487. contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
  488. subj->pos += matchlen;
  489. return make_autolink(
  490. make_str_with_entities(&contents),
  491. contents, 0
  492. );
  493. }
  494. // next try to match an email autolink
  495. matchlen = scan_autolink_email(&subj->input, subj->pos);
  496. if (matchlen > 0) {
  497. contents = chunk_dup(&subj->input, subj->pos, matchlen - 1);
  498. subj->pos += matchlen;
  499. return make_autolink(
  500. make_str_with_entities(&contents),
  501. contents, 1
  502. );
  503. }
  504. // finally, try to match an html tag
  505. matchlen = scan_html_tag(&subj->input, subj->pos);
  506. if (matchlen > 0) {
  507. contents = chunk_dup(&subj->input, subj->pos - 1, matchlen + 1);
  508. subj->pos += matchlen;
  509. return make_raw_html(contents);
  510. }
  511. // if nothing matches, just return the opening <:
  512. return make_str(chunk_literal("<"));
  513. }
  514. // Parse a link label. Returns 1 if successful.
  515. // Unless raw_label is null, it is set to point to the raw contents of the [].
  516. // Assumes the subject has a '[' character at the current position.
  517. // Returns 0 and does not advance if no matching ] is found.
  518. // Note the precedence: code backticks have precedence over label bracket
  519. // markers, which have precedence over *, _, and other inline formatting
  520. // markers. So, 2 below contains a link while 1 does not:
  521. // 1. [a link `with a ](/url)` character
  522. // 2. [a link *with emphasized ](/url) text*
  523. static int link_label(subject* subj, chunk *raw_label)
  524. {
  525. int nestlevel = 0;
  526. node_inl* tmp = NULL;
  527. int startpos = subj->pos;
  528. if (subj->label_nestlevel) {
  529. // if we've already checked to the end of the subject
  530. // for a label, even with a different starting [, we
  531. // know we won't find one here and we can just return.
  532. // Note: nestlevel 1 would be: [foo [bar]
  533. // nestlevel 2 would be: [foo [bar [baz]
  534. subj->label_nestlevel--;
  535. return 0;
  536. }
  537. advance(subj); // advance past [
  538. unsigned char c;
  539. while ((c = peek_char(subj)) && (c != ']' || nestlevel > 0)) {
  540. switch (c) {
  541. case '`':
  542. tmp = handle_backticks(subj);
  543. free_inlines(tmp);
  544. break;
  545. case '<':
  546. tmp = handle_pointy_brace(subj);
  547. free_inlines(tmp);
  548. break;
  549. case '[': // nested []
  550. nestlevel++;
  551. advance(subj);
  552. break;
  553. case ']': // nested []
  554. nestlevel--;
  555. advance(subj);
  556. break;
  557. case '\\':
  558. advance(subj);
  559. if (ispunct(peek_char(subj))) {
  560. advance(subj);
  561. }
  562. break;
  563. default:
  564. advance(subj);
  565. }
  566. }
  567. if (nestlevel == 0 && c == ']') {
  568. *raw_label = chunk_dup(&subj->input, startpos + 1, subj->pos - (startpos + 1));
  569. subj->label_nestlevel = 0;
  570. advance(subj); // advance past ]
  571. return 1;
  572. } else {
  573. if (c == 0) {
  574. subj->label_nestlevel = nestlevel;
  575. }
  576. subj->pos = startpos; // rewind
  577. return 0;
  578. }
  579. }
  580. // Parse a link or the link portion of an image, or return a fallback.
  581. static node_inl* handle_left_bracket(subject* subj)
  582. {
  583. node_inl *lab = NULL;
  584. node_inl *result = NULL;
  585. reference *ref;
  586. int n;
  587. int sps;
  588. int found_label;
  589. int endlabel, startpos, starturl, endurl, starttitle, endtitle, endall;
  590. chunk rawlabel;
  591. chunk url, title;
  592. startpos = subj->pos;
  593. found_label = link_label(subj, &rawlabel);
  594. endlabel = subj->pos;
  595. if (found_label) {
  596. if (peek_char(subj) == '(' &&
  597. ((sps = scan_spacechars(&subj->input, subj->pos + 1)) > -1) &&
  598. ((n = scan_link_url(&subj->input, subj->pos + 1 + sps)) > -1)) {
  599. // try to parse an explicit link:
  600. starturl = subj->pos + 1 + sps; // after (
  601. endurl = starturl + n;
  602. starttitle = endurl + scan_spacechars(&subj->input, endurl);
  603. // ensure there are spaces btw url and title
  604. endtitle = (starttitle == endurl) ? starttitle :
  605. starttitle + scan_link_title(&subj->input, starttitle);
  606. endall = endtitle + scan_spacechars(&subj->input, endtitle);
  607. if (peek_at(subj, endall) == ')') {
  608. subj->pos = endall + 1;
  609. url = chunk_dup(&subj->input, starturl, endurl - starturl);
  610. title = chunk_dup(&subj->input, starttitle, endtitle - starttitle);
  611. lab = parse_chunk_inlines(&rawlabel, NULL);
  612. return make_link(lab, url, title);
  613. } else {
  614. goto noMatch;
  615. }
  616. } else {
  617. chunk rawlabel_tmp;
  618. chunk reflabel;
  619. // Check for reference link.
  620. // First, see if there's another label:
  621. subj->pos = subj->pos + scan_spacechars(&subj->input, endlabel);
  622. reflabel = rawlabel;
  623. // if followed by a nonempty link label, we change reflabel to it:
  624. if (peek_char(subj) == '[' && link_label(subj, &rawlabel_tmp)) {
  625. if (rawlabel_tmp.len > 0)
  626. reflabel = rawlabel_tmp;
  627. } else {
  628. subj->pos = endlabel;
  629. }
  630. // lookup rawlabel in subject->reference_map:
  631. ref = reference_lookup(subj->refmap, &reflabel);
  632. if (ref != NULL) { // found
  633. lab = parse_chunk_inlines(&rawlabel, NULL);
  634. result = make_ref_link(lab, ref);
  635. } else {
  636. goto noMatch;
  637. }
  638. return result;
  639. }
  640. }
  641. noMatch:
  642. // If we fall through to here, it means we didn't match a link:
  643. subj->pos = startpos + 1; // advance past [
  644. return make_str(chunk_literal("["));
  645. }
  646. // Parse a hard or soft linebreak, returning an inline.
  647. // Assumes the subject has a newline at the current position.
  648. static node_inl* handle_newline(subject *subj)
  649. {
  650. int nlpos = subj->pos;
  651. // skip over newline
  652. advance(subj);
  653. // skip spaces at beginning of line
  654. while (peek_char(subj) == ' ') {
  655. advance(subj);
  656. }
  657. if (nlpos > 1 &&
  658. peek_at(subj, nlpos - 1) == ' ' &&
  659. peek_at(subj, nlpos - 2) == ' ') {
  660. return make_linebreak();
  661. } else {
  662. return make_softbreak();
  663. }
  664. }
  665. // Parse inlines til end of subject, returning inlines.
  666. extern node_inl* parse_inlines_from_subject(subject* subj)
  667. {
  668. node_inl* result = NULL;
  669. node_inl** last = &result;
  670. node_inl* first = NULL;
  671. while (!is_eof(subj) && parse_inline(subj, last)) {
  672. if (!first) {
  673. first = *last;
  674. }
  675. }
  676. opener_stack* istack = subj->openers;
  677. opener_stack* temp;
  678. while (istack != NULL) {
  679. temp = istack->previous;
  680. free(istack);
  681. istack = temp;
  682. }
  683. return first;
  684. }
  685. node_inl *parse_chunk_inlines(chunk *chunk, reference_map *refmap)
  686. {
  687. subject subj;
  688. subject_from_chunk(&subj, chunk, refmap);
  689. return parse_inlines_from_subject(&subj);
  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 to last element.
  720. // Adjust tail to point to new last element of list.
  721. // Return 0 if no inline can be parsed, 1 otherwise.
  722. static int parse_inline(subject* subj, node_inl ** last)
  723. {
  724. node_inl* new = NULL;
  725. chunk contents;
  726. unsigned char c;
  727. int endpos;
  728. c = peek_char(subj);
  729. if (c == 0) {
  730. return 0;
  731. }
  732. switch(c){
  733. case '\n':
  734. new = handle_newline(subj);
  735. break;
  736. case '`':
  737. new = handle_backticks(subj);
  738. break;
  739. case '\\':
  740. new = handle_backslash(subj);
  741. break;
  742. case '&':
  743. new = handle_entity(subj);
  744. break;
  745. case '<':
  746. new = handle_pointy_brace(subj);
  747. break;
  748. case '_':
  749. new = handle_strong_emph(subj, '_', last);
  750. break;
  751. case '*':
  752. new = handle_strong_emph(subj, '*', last);
  753. break;
  754. case '[':
  755. new = handle_left_bracket(subj);
  756. break;
  757. case '!':
  758. advance(subj);
  759. if (peek_char(subj) == '[') {
  760. new = handle_left_bracket(subj);
  761. if (new != NULL && new->tag == INL_LINK) {
  762. new->tag = INL_IMAGE;
  763. } else {
  764. new = append_inlines(make_str(chunk_literal("!")), new);
  765. }
  766. } else {
  767. new = make_str(chunk_literal("!"));
  768. }
  769. break;
  770. default:
  771. endpos = subject_find_special_char(subj);
  772. contents = chunk_dup(&subj->input, subj->pos, endpos - subj->pos);
  773. subj->pos = endpos;
  774. // if we're at a newline, strip trailing spaces.
  775. if (peek_char(subj) == '\n') {
  776. chunk_rtrim(&contents);
  777. }
  778. new = make_str(contents);
  779. }
  780. if (*last == NULL) {
  781. *last = new;
  782. } else if (new) {
  783. append_inlines(*last, new);
  784. *last = new;
  785. }
  786. return 1;
  787. }
  788. extern node_inl* parse_inlines(strbuf *input, reference_map *refmap)
  789. {
  790. subject subj;
  791. subject_from_buf(&subj, input, refmap);
  792. return parse_inlines_from_subject(&subj);
  793. }
  794. // Parse zero or more space characters, including at most one newline.
  795. void spnl(subject* subj)
  796. {
  797. bool seen_newline = false;
  798. while (peek_char(subj) == ' ' ||
  799. (!seen_newline &&
  800. (seen_newline = peek_char(subj) == '\n'))) {
  801. advance(subj);
  802. }
  803. }
  804. // Parse reference. Assumes string begins with '[' character.
  805. // Modify refmap if a reference is encountered.
  806. // Return 0 if no reference found, otherwise position of subject
  807. // after reference is parsed.
  808. int parse_reference_inline(strbuf *input, reference_map *refmap)
  809. {
  810. subject subj;
  811. chunk lab;
  812. chunk url;
  813. chunk title;
  814. int matchlen = 0;
  815. int beforetitle;
  816. subject_from_buf(&subj, input, NULL);
  817. // parse label:
  818. if (!link_label(&subj, &lab))
  819. return 0;
  820. // colon:
  821. if (peek_char(&subj) == ':') {
  822. advance(&subj);
  823. } else {
  824. return 0;
  825. }
  826. // parse link url:
  827. spnl(&subj);
  828. matchlen = scan_link_url(&subj.input, subj.pos);
  829. if (matchlen) {
  830. url = chunk_dup(&subj.input, subj.pos, matchlen);
  831. subj.pos += matchlen;
  832. } else {
  833. return 0;
  834. }
  835. // parse optional link_title
  836. beforetitle = subj.pos;
  837. spnl(&subj);
  838. matchlen = scan_link_title(&subj.input, subj.pos);
  839. if (matchlen) {
  840. title = chunk_dup(&subj.input, subj.pos, matchlen);
  841. subj.pos += matchlen;
  842. } else {
  843. subj.pos = beforetitle;
  844. title = chunk_literal("");
  845. }
  846. // parse final spaces and newline:
  847. while (peek_char(&subj) == ' ') {
  848. advance(&subj);
  849. }
  850. if (peek_char(&subj) == '\n') {
  851. advance(&subj);
  852. } else if (peek_char(&subj) != 0) {
  853. return 0;
  854. }
  855. // insert reference into refmap
  856. reference_create(refmap, &lab, &url, &title);
  857. return subj.pos;
  858. }