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