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