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