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