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