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