commonmark.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <stdint.h>
  5. #include <assert.h>
  6. #include "config.h"
  7. #include "cmark.h"
  8. #include "node.h"
  9. #include "buffer.h"
  10. #include "utf8.h"
  11. #include "scanners.h"
  12. #include "render.h"
  13. #define OUT(s, wrap, escaping) renderer->out(renderer, s, wrap, escaping)
  14. #define LIT(s) renderer->out(renderer, s, false, LITERAL)
  15. #define CR() renderer->cr(renderer)
  16. #define BLANKLINE() renderer->blankline(renderer)
  17. #define ENCODED_SIZE 20
  18. #define LISTMARKER_SIZE 20
  19. // Functions to convert cmark_nodes to commonmark strings.
  20. static CMARK_INLINE void outc(cmark_renderer *renderer, cmark_escaping escape,
  21. int32_t c, unsigned char nextc) {
  22. bool needs_escaping = false;
  23. bool follows_digit =
  24. renderer->buffer->size > 0 &&
  25. cmark_isdigit(renderer->buffer->ptr[renderer->buffer->size - 1]);
  26. char encoded[ENCODED_SIZE];
  27. needs_escaping =
  28. c < 0x80 && escape != LITERAL &&
  29. ((escape == NORMAL &&
  30. (c < 0x20 ||
  31. c == '*' || c == '_' || c == '[' || c == ']' || c == '#' || c == '<' ||
  32. c == '>' || c == '\\' || c == '`' || c == '!' ||
  33. (c == '&' && cmark_isalpha(nextc)) || (c == '!' && nextc == '[') ||
  34. (renderer->begin_content && (c == '-' || c == '+' || c == '=') &&
  35. // begin_content doesn't get set to false til we've passed digits
  36. // at the beginning of line, so...
  37. !follows_digit) ||
  38. (renderer->begin_content && (c == '.' || c == ')') && follows_digit &&
  39. (nextc == 0 || cmark_isspace(nextc))))) ||
  40. (escape == URL &&
  41. (c == '`' || c == '<' || c == '>' || cmark_isspace(c) || c == '\\' ||
  42. c == ')' || c == '(')) ||
  43. (escape == TITLE &&
  44. (c == '`' || c == '<' || c == '>' || c == '"' || c == '\\')));
  45. if (needs_escaping) {
  46. if (escape == URL && cmark_isspace(c)) {
  47. // use percent encoding for spaces
  48. snprintf(encoded, ENCODED_SIZE, "%%%2X", c);
  49. cmark_strbuf_puts(renderer->buffer, encoded);
  50. renderer->column += 3;
  51. } else if (cmark_ispunct(c)) {
  52. cmark_render_ascii(renderer, "\\");
  53. cmark_render_code_point(renderer, c);
  54. } else { // render as entity
  55. snprintf(encoded, ENCODED_SIZE, "&#%d;", c);
  56. cmark_strbuf_puts(renderer->buffer, encoded);
  57. renderer->column += strlen(encoded);
  58. }
  59. } else {
  60. cmark_render_code_point(renderer, c);
  61. }
  62. }
  63. static int longest_backtick_sequence(const char *code) {
  64. int longest = 0;
  65. int current = 0;
  66. size_t i = 0;
  67. size_t code_len = strlen(code);
  68. while (i <= code_len) {
  69. if (code[i] == '`') {
  70. current++;
  71. } else {
  72. if (current > longest) {
  73. longest = current;
  74. }
  75. current = 0;
  76. }
  77. i++;
  78. }
  79. return longest;
  80. }
  81. static int shortest_unused_backtick_sequence(const char *code) {
  82. // note: if the shortest sequence is >= 32, this returns 32
  83. // so as not to overflow the bit array.
  84. uint32_t used = 1;
  85. int current = 0;
  86. size_t i = 0;
  87. size_t code_len = strlen(code);
  88. while (i <= code_len) {
  89. if (code[i] == '`') {
  90. current++;
  91. } else {
  92. if (current > 0 && current < 32) {
  93. used |= (1U << current);
  94. }
  95. current = 0;
  96. }
  97. i++;
  98. }
  99. // return number of first bit that is 0:
  100. i = 0;
  101. while (i < 32 && used & 1) {
  102. used = used >> 1;
  103. i++;
  104. }
  105. return (int)i;
  106. }
  107. static bool is_autolink(cmark_node *node) {
  108. cmark_chunk *title;
  109. cmark_chunk *url;
  110. cmark_node *link_text;
  111. char *realurl;
  112. int realurllen;
  113. if (node->type != CMARK_NODE_LINK) {
  114. return false;
  115. }
  116. url = &node->as.link.url;
  117. if (url->len == 0 || scan_scheme(url, 0) == 0) {
  118. return false;
  119. }
  120. title = &node->as.link.title;
  121. // if it has a title, we can't treat it as an autolink:
  122. if (title->len > 0) {
  123. return false;
  124. }
  125. link_text = node->first_child;
  126. if (link_text == NULL) {
  127. return false;
  128. }
  129. cmark_consolidate_text_nodes(link_text);
  130. realurl = (char *)url->data;
  131. realurllen = url->len;
  132. if (strncmp(realurl, "mailto:", 7) == 0) {
  133. realurl += 7;
  134. realurllen -= 7;
  135. }
  136. return (realurllen == link_text->as.literal.len &&
  137. strncmp(realurl, (char *)link_text->as.literal.data,
  138. link_text->as.literal.len) == 0);
  139. }
  140. // if node is a block node, returns node.
  141. // otherwise returns first block-level node that is an ancestor of node.
  142. // if there is no block-level ancestor, returns NULL.
  143. static cmark_node *get_containing_block(cmark_node *node) {
  144. while (node) {
  145. if (node->type >= CMARK_NODE_FIRST_BLOCK &&
  146. node->type <= CMARK_NODE_LAST_BLOCK) {
  147. return node;
  148. } else {
  149. node = node->parent;
  150. }
  151. }
  152. return NULL;
  153. }
  154. static int S_render_node(cmark_renderer *renderer, cmark_node *node,
  155. cmark_event_type ev_type, int options) {
  156. cmark_node *tmp;
  157. int list_number;
  158. cmark_delim_type list_delim;
  159. int numticks;
  160. bool extra_spaces;
  161. int i;
  162. bool entering = (ev_type == CMARK_EVENT_ENTER);
  163. const char *info, *code, *title;
  164. char fencechar[2] = {'\0', '\0'};
  165. size_t info_len, code_len;
  166. char listmarker[LISTMARKER_SIZE];
  167. char *emph_delim;
  168. bool first_in_list_item;
  169. bufsize_t marker_width;
  170. bool allow_wrap = renderer->width > 0 && !(CMARK_OPT_NOBREAKS & options) &&
  171. !(CMARK_OPT_HARDBREAKS & options);
  172. // Don't adjust tight list status til we've started the list.
  173. // Otherwise we loose the blank line between a paragraph and
  174. // a following list.
  175. if (!(node->type == CMARK_NODE_ITEM && node->prev == NULL && entering)) {
  176. tmp = get_containing_block(node);
  177. renderer->in_tight_list_item =
  178. tmp && // tmp might be NULL if there is no containing block
  179. ((tmp->type == CMARK_NODE_ITEM &&
  180. cmark_node_get_list_tight(tmp->parent)) ||
  181. (tmp && tmp->parent && tmp->parent->type == CMARK_NODE_ITEM &&
  182. cmark_node_get_list_tight(tmp->parent->parent)));
  183. }
  184. switch (node->type) {
  185. case CMARK_NODE_DOCUMENT:
  186. break;
  187. case CMARK_NODE_BLOCK_QUOTE:
  188. if (entering) {
  189. LIT("> ");
  190. renderer->begin_content = true;
  191. cmark_strbuf_puts(renderer->prefix, "> ");
  192. } else {
  193. cmark_strbuf_truncate(renderer->prefix, renderer->prefix->size - 2);
  194. BLANKLINE();
  195. }
  196. break;
  197. case CMARK_NODE_LIST:
  198. if (!entering && node->next && (node->next->type == CMARK_NODE_CODE_BLOCK ||
  199. node->next->type == CMARK_NODE_LIST)) {
  200. // this ensures that a following indented code block or list will be
  201. // inteprereted correctly.
  202. CR();
  203. LIT("<!-- end list -->");
  204. BLANKLINE();
  205. }
  206. break;
  207. case CMARK_NODE_ITEM:
  208. if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
  209. marker_width = 4;
  210. } else {
  211. list_number = cmark_node_get_list_start(node->parent);
  212. list_delim = cmark_node_get_list_delim(node->parent);
  213. tmp = node;
  214. while (tmp->prev) {
  215. tmp = tmp->prev;
  216. list_number += 1;
  217. }
  218. // we ensure a width of at least 4 so
  219. // we get nice transition from single digits
  220. // to double
  221. snprintf(listmarker, LISTMARKER_SIZE, "%d%s%s", list_number,
  222. list_delim == CMARK_PAREN_DELIM ? ")" : ".",
  223. list_number < 10 ? " " : " ");
  224. marker_width = strlen(listmarker);
  225. }
  226. if (entering) {
  227. if (cmark_node_get_list_type(node->parent) == CMARK_BULLET_LIST) {
  228. LIT(" - ");
  229. renderer->begin_content = true;
  230. } else {
  231. LIT(listmarker);
  232. renderer->begin_content = true;
  233. }
  234. for (i = marker_width; i--;) {
  235. cmark_strbuf_putc(renderer->prefix, ' ');
  236. }
  237. } else {
  238. cmark_strbuf_truncate(renderer->prefix,
  239. renderer->prefix->size - marker_width);
  240. CR();
  241. }
  242. break;
  243. case CMARK_NODE_HEADING:
  244. if (entering) {
  245. for (i = cmark_node_get_heading_level(node); i > 0; i--) {
  246. LIT("#");
  247. }
  248. LIT(" ");
  249. renderer->begin_content = true;
  250. renderer->no_linebreaks = true;
  251. } else {
  252. renderer->no_linebreaks = false;
  253. BLANKLINE();
  254. }
  255. break;
  256. case CMARK_NODE_CODE_BLOCK:
  257. first_in_list_item = node->prev == NULL && node->parent &&
  258. node->parent->type == CMARK_NODE_ITEM;
  259. if (!first_in_list_item) {
  260. BLANKLINE();
  261. }
  262. info = cmark_node_get_fence_info(node);
  263. info_len = strlen(info);
  264. fencechar[0] = strchr(info, '`') == NULL ? '`' : '~';
  265. code = cmark_node_get_literal(node);
  266. code_len = strlen(code);
  267. // use indented form if no info, and code doesn't
  268. // begin or end with a blank line, and code isn't
  269. // first thing in a list item
  270. if (info_len == 0 && (code_len > 2 && !cmark_isspace(code[0]) &&
  271. !(cmark_isspace(code[code_len - 1]) &&
  272. cmark_isspace(code[code_len - 2]))) &&
  273. !first_in_list_item) {
  274. LIT(" ");
  275. cmark_strbuf_puts(renderer->prefix, " ");
  276. OUT(cmark_node_get_literal(node), false, LITERAL);
  277. cmark_strbuf_truncate(renderer->prefix, renderer->prefix->size - 4);
  278. } else {
  279. numticks = longest_backtick_sequence(code) + 1;
  280. if (numticks < 3) {
  281. numticks = 3;
  282. }
  283. for (i = 0; i < numticks; i++) {
  284. LIT(fencechar);
  285. }
  286. LIT(" ");
  287. OUT(info, false, LITERAL);
  288. CR();
  289. OUT(cmark_node_get_literal(node), false, LITERAL);
  290. CR();
  291. for (i = 0; i < numticks; i++) {
  292. LIT(fencechar);
  293. }
  294. }
  295. BLANKLINE();
  296. break;
  297. case CMARK_NODE_HTML_BLOCK:
  298. BLANKLINE();
  299. OUT(cmark_node_get_literal(node), false, LITERAL);
  300. BLANKLINE();
  301. break;
  302. case CMARK_NODE_CUSTOM_BLOCK:
  303. BLANKLINE();
  304. OUT(entering ? cmark_node_get_on_enter(node) : cmark_node_get_on_exit(node),
  305. false, LITERAL);
  306. BLANKLINE();
  307. break;
  308. case CMARK_NODE_THEMATIC_BREAK:
  309. BLANKLINE();
  310. LIT("-----");
  311. BLANKLINE();
  312. break;
  313. case CMARK_NODE_PARAGRAPH:
  314. if (!entering) {
  315. BLANKLINE();
  316. }
  317. break;
  318. case CMARK_NODE_TEXT:
  319. OUT(cmark_node_get_literal(node), allow_wrap, NORMAL);
  320. break;
  321. case CMARK_NODE_LINEBREAK:
  322. if (!(CMARK_OPT_HARDBREAKS & options)) {
  323. LIT(" ");
  324. }
  325. CR();
  326. break;
  327. case CMARK_NODE_SOFTBREAK:
  328. if (CMARK_OPT_HARDBREAKS & options) {
  329. LIT(" ");
  330. CR();
  331. } else if (!renderer->no_linebreaks && renderer->width == 0 &&
  332. !(CMARK_OPT_HARDBREAKS & options) &&
  333. !(CMARK_OPT_NOBREAKS & options)) {
  334. CR();
  335. } else {
  336. OUT(" ", allow_wrap, LITERAL);
  337. }
  338. break;
  339. case CMARK_NODE_CODE:
  340. code = cmark_node_get_literal(node);
  341. code_len = strlen(code);
  342. numticks = shortest_unused_backtick_sequence(code);
  343. extra_spaces = code_len == 0 ||
  344. code[0] == '`' || code[code_len - 1] == '`' ||
  345. code[0] == ' ' || code[code_len - 1] == ' ';
  346. for (i = 0; i < numticks; i++) {
  347. LIT("`");
  348. }
  349. if (extra_spaces) {
  350. LIT(" ");
  351. }
  352. OUT(cmark_node_get_literal(node), allow_wrap, LITERAL);
  353. if (extra_spaces) {
  354. LIT(" ");
  355. }
  356. for (i = 0; i < numticks; i++) {
  357. LIT("`");
  358. }
  359. break;
  360. case CMARK_NODE_HTML_INLINE:
  361. OUT(cmark_node_get_literal(node), false, LITERAL);
  362. break;
  363. case CMARK_NODE_CUSTOM_INLINE:
  364. OUT(entering ? cmark_node_get_on_enter(node) : cmark_node_get_on_exit(node),
  365. false, LITERAL);
  366. break;
  367. case CMARK_NODE_STRONG:
  368. if (entering) {
  369. LIT("**");
  370. } else {
  371. LIT("**");
  372. }
  373. break;
  374. case CMARK_NODE_EMPH:
  375. // If we have EMPH(EMPH(x)), we need to use *_x_*
  376. // because **x** is STRONG(x):
  377. if (node->parent && node->parent->type == CMARK_NODE_EMPH &&
  378. node->next == NULL && node->prev == NULL) {
  379. emph_delim = "_";
  380. } else {
  381. emph_delim = "*";
  382. }
  383. if (entering) {
  384. LIT(emph_delim);
  385. } else {
  386. LIT(emph_delim);
  387. }
  388. break;
  389. case CMARK_NODE_LINK:
  390. if (is_autolink(node)) {
  391. if (entering) {
  392. LIT("<");
  393. if (strncmp(cmark_node_get_url(node), "mailto:", 7) == 0) {
  394. LIT((const char *)cmark_node_get_url(node) + 7);
  395. } else {
  396. LIT((const char *)cmark_node_get_url(node));
  397. }
  398. LIT(">");
  399. // return signal to skip contents of node...
  400. return 0;
  401. }
  402. } else {
  403. if (entering) {
  404. LIT("[");
  405. } else {
  406. LIT("](");
  407. OUT(cmark_node_get_url(node), false, URL);
  408. title = cmark_node_get_title(node);
  409. if (strlen(title) > 0) {
  410. LIT(" \"");
  411. OUT(title, false, TITLE);
  412. LIT("\"");
  413. }
  414. LIT(")");
  415. }
  416. }
  417. break;
  418. case CMARK_NODE_IMAGE:
  419. if (entering) {
  420. LIT("![");
  421. } else {
  422. LIT("](");
  423. OUT(cmark_node_get_url(node), false, URL);
  424. title = cmark_node_get_title(node);
  425. if (strlen(title) > 0) {
  426. OUT(" \"", allow_wrap, LITERAL);
  427. OUT(title, false, TITLE);
  428. LIT("\"");
  429. }
  430. LIT(")");
  431. }
  432. break;
  433. default:
  434. assert(false);
  435. break;
  436. }
  437. return 1;
  438. }
  439. char *cmark_render_commonmark(cmark_node *root, int options, int width) {
  440. if (options & CMARK_OPT_HARDBREAKS) {
  441. // disable breaking on width, since it has
  442. // a different meaning with OPT_HARDBREAKS
  443. width = 0;
  444. }
  445. return cmark_render(root, options, width, outc, S_render_node);
  446. }