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