aboutsummaryrefslogtreecommitdiff
path: root/src/html.c
blob: 40b5e94d5f53c820717eb2d65aef42584383cae3 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include "config.h"
  6. #include "cmark.h"
  7. #include "node.h"
  8. #include "buffer.h"
  9. #include "houdini.h"
  10. // Functions to convert cmark_nodes to HTML strings.
  11. static void escape_html(cmark_strbuf *dest, const unsigned char *source, int length)
  12. {
  13. if (length < 0)
  14. length = strlen((char *)source);
  15. houdini_escape_html0(dest, source, (size_t)length, 0);
  16. }
  17. static void escape_href(cmark_strbuf *dest, const unsigned char *source, int length)
  18. {
  19. if (length < 0)
  20. length = strlen((char *)source);
  21. houdini_escape_href(dest, source, (size_t)length);
  22. }
  23. static inline void cr(cmark_strbuf *html)
  24. {
  25. if (html->size && html->ptr[html->size - 1] != '\n')
  26. cmark_strbuf_putc(html, '\n');
  27. }
  28. struct render_state {
  29. cmark_strbuf* html;
  30. cmark_node *plain;
  31. };
  32. static int
  33. S_render_node(cmark_node *node, cmark_event_type ev_type, void *vstate)
  34. {
  35. struct render_state *state = vstate;
  36. cmark_node *parent;
  37. cmark_node *grandparent;
  38. cmark_strbuf *html = state->html;
  39. char start_header[] = "<h0>";
  40. char end_header[] = "</h0>";
  41. bool tight;
  42. bool entering = (ev_type == CMARK_EVENT_ENTER);
  43. if (state->plain == node) { // back at original node
  44. state->plain = NULL;
  45. }
  46. if (state->plain != NULL) {
  47. switch(node->type) {
  48. case CMARK_NODE_TEXT:
  49. case CMARK_NODE_CODE:
  50. case CMARK_NODE_INLINE_HTML:
  51. escape_html(html, node->as.literal.data,
  52. node->as.literal.len);
  53. break;
  54. case CMARK_NODE_LINEBREAK:
  55. case CMARK_NODE_SOFTBREAK:
  56. cmark_strbuf_putc(html, ' ');
  57. break;
  58. default:
  59. break;
  60. }
  61. return 1;
  62. }
  63. switch (node->type) {
  64. case CMARK_NODE_DOCUMENT:
  65. break;
  66. case CMARK_NODE_BLOCK_QUOTE:
  67. if (entering) {
  68. cr(html);
  69. cmark_strbuf_puts(html, "<blockquote>\n");
  70. } else {
  71. cr(html);
  72. cmark_strbuf_puts(html, "</blockquote>\n");
  73. }
  74. break;
  75. case CMARK_NODE_LIST: {
  76. cmark_list_type list_type = node->as.list.list_type;
  77. int start = node->as.list.start;
  78. if (entering) {
  79. cr(html);
  80. if (list_type == CMARK_BULLET_LIST) {
  81. cmark_strbuf_puts(html, "<ul>\n");
  82. }
  83. else if (start == 1) {
  84. cmark_strbuf_puts(html, "<ol>\n");
  85. }
  86. else {
  87. cmark_strbuf_printf(html, "<ol start=\"%d\">\n",
  88. start);
  89. }
  90. } else {
  91. cmark_strbuf_puts(html,
  92. list_type == CMARK_BULLET_LIST ?
  93. "</ul>\n" : "</ol>\n");
  94. }
  95. break;
  96. }
  97. case CMARK_NODE_LIST_ITEM:
  98. if (entering) {
  99. cr(html);
  100. cmark_strbuf_puts(html, "<li>");
  101. } else {
  102. cmark_strbuf_puts(html, "</li>\n");
  103. }
  104. break;
  105. case CMARK_NODE_HEADER:
  106. if (entering) {
  107. cr(html);
  108. start_header[2] = '0' + node->as.header.level;
  109. cmark_strbuf_puts(html, start_header);
  110. } else {
  111. end_header[3] = '0' + node->as.header.level;
  112. cmark_strbuf_puts(html, end_header);
  113. cmark_strbuf_putc(html, '\n');
  114. }
  115. break;
  116. case CMARK_NODE_CODE_BLOCK:
  117. cr(html);
  118. if (!node->as.code.fenced || node->as.code.info.len == 0) {
  119. cmark_strbuf_puts(html, "<pre><code>");
  120. }
  121. else {
  122. int first_tag = 0;
  123. while (first_tag < node->as.code.info.len &&
  124. node->as.code.info.data[first_tag] != ' ') {
  125. first_tag += 1;
  126. }
  127. cmark_strbuf_puts(html, "<pre><code class=\"language-");
  128. escape_html(html, node->as.code.info.data, first_tag);
  129. cmark_strbuf_puts(html, "\">");
  130. }
  131. escape_html(html, node->as.code.literal.data,
  132. node->as.code.literal.len);
  133. cmark_strbuf_puts(html, "</code></pre>\n");
  134. break;
  135. case CMARK_NODE_HTML:
  136. cr(html);
  137. cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len);
  138. break;
  139. case CMARK_NODE_HRULE:
  140. cr(html);
  141. cmark_strbuf_puts(html, "<hr />\n");
  142. break;
  143. case CMARK_NODE_PARAGRAPH:
  144. parent = cmark_node_parent(node);
  145. grandparent = cmark_node_parent(parent);
  146. if (grandparent != NULL &&
  147. grandparent->type == CMARK_NODE_LIST) {
  148. tight = grandparent->as.list.tight;
  149. } else {
  150. tight = false;
  151. }
  152. if (!tight) {
  153. if (entering) {
  154. cr(html);
  155. cmark_strbuf_puts(html, "<p>");
  156. } else {
  157. cmark_strbuf_puts(html, "</p>\n");
  158. }
  159. }
  160. break;
  161. case CMARK_NODE_TEXT:
  162. escape_html(html, node->as.literal.data,
  163. node->as.literal.len);
  164. break;
  165. case CMARK_NODE_LINEBREAK:
  166. cmark_strbuf_puts(html, "<br />\n");
  167. break;
  168. case CMARK_NODE_SOFTBREAK:
  169. cmark_strbuf_putc(html, '\n');
  170. break;
  171. case CMARK_NODE_CODE:
  172. cmark_strbuf_puts(html, "<code>");
  173. escape_html(html, node->as.literal.data, node->as.literal.len);
  174. cmark_strbuf_puts(html, "</code>");
  175. break;
  176. case CMARK_NODE_INLINE_HTML:
  177. cmark_strbuf_put(html, node->as.literal.data, node->as.literal.len);
  178. break;
  179. case CMARK_NODE_STRONG:
  180. if (entering) {
  181. cmark_strbuf_puts(html, "<strong>");
  182. } else {
  183. cmark_strbuf_puts(html, "</strong>");
  184. }
  185. break;
  186. case CMARK_NODE_EMPH:
  187. if (entering) {
  188. cmark_strbuf_puts(html, "<em>");
  189. } else {
  190. cmark_strbuf_puts(html, "</em>");
  191. }
  192. break;
  193. case CMARK_NODE_LINK:
  194. if (entering) {
  195. cmark_strbuf_puts(html, "<a href=\"");
  196. if (node->as.link.url)
  197. escape_href(html, node->as.link.url, -1);
  198. if (node->as.link.title) {
  199. cmark_strbuf_puts(html, "\" title=\"");
  200. escape_html(html, node->as.link.title, -1);
  201. }
  202. cmark_strbuf_puts(html, "\">");
  203. } else {
  204. cmark_strbuf_puts(html, "</a>");
  205. }
  206. break;
  207. case CMARK_NODE_IMAGE:
  208. if (entering) {
  209. cmark_strbuf_puts(html, "<img src=\"");
  210. if (node->as.link.url)
  211. escape_href(html, node->as.link.url, -1);
  212. cmark_strbuf_puts(html, "\" alt=\"");
  213. state->plain = node;
  214. } else {
  215. if (node->as.link.title) {
  216. cmark_strbuf_puts(html, "\" title=\"");
  217. escape_html(html, node->as.link.title, -1);
  218. }
  219. cmark_strbuf_puts(html, "\" />");
  220. }
  221. break;
  222. default:
  223. assert(false);
  224. break;
  225. }
  226. // cmark_strbuf_putc(html, 'x');
  227. return 1;
  228. }
  229. char *cmark_render_html(cmark_node *root)
  230. {
  231. char *result;
  232. cmark_strbuf html = GH_BUF_INIT;
  233. cmark_event_type ev_type;
  234. cmark_node *cur;
  235. struct render_state state = { &html, NULL };
  236. cmark_iter *iter = cmark_iter_new(root);
  237. while ((ev_type = cmark_iter_next(iter)) != CMARK_EVENT_DONE) {
  238. cur = cmark_iter_get_node(iter);
  239. S_render_node(cur, ev_type, &state);
  240. }
  241. result = (char *)cmark_strbuf_detach(&html);
  242. cmark_iter_free(iter);
  243. cmark_strbuf_free(&html);
  244. return result;
  245. }