aboutsummaryrefslogtreecommitdiff
path: root/src/html/html.c
blob: 6261b9c0b72170fe419d684630195343746923d3 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "cmark.h"
  7. #include "debug.h"
  8. #include "html/houdini.h"
  9. typedef struct RenderStack {
  10. struct RenderStack *previous;
  11. char* literal;
  12. union {
  13. node_inl *inl;
  14. node_block *block;
  15. } next_sibling;
  16. bool tight;
  17. } render_stack;
  18. static void free_render_stack(render_stack * rstack)
  19. {
  20. render_stack * tempstack;
  21. while (rstack) {
  22. tempstack = rstack;
  23. rstack = rstack->previous;
  24. free(tempstack);
  25. }
  26. }
  27. static render_stack* push_inline(render_stack* rstack,
  28. node_inl* inl,
  29. char* literal)
  30. {
  31. render_stack* newstack;
  32. newstack = (render_stack*)malloc(sizeof(render_stack));
  33. if (newstack == NULL) {
  34. return NULL;
  35. }
  36. newstack->previous = rstack;
  37. newstack->next_sibling.inl = inl;
  38. newstack->literal = literal;
  39. return newstack;
  40. }
  41. static render_stack* push_block(render_stack* rstack,
  42. node_block* block,
  43. char* literal,
  44. bool tight)
  45. {
  46. render_stack* newstack;
  47. newstack = (render_stack*)malloc(sizeof(render_stack));
  48. if (newstack == NULL) {
  49. return NULL;
  50. }
  51. newstack->previous = rstack;
  52. newstack->next_sibling.block = block;
  53. newstack->literal = literal;
  54. newstack->tight = tight;
  55. return newstack;
  56. }
  57. static render_stack* pop_render_stack(render_stack* rstack)
  58. {
  59. render_stack* top = rstack;
  60. if (rstack == NULL) {
  61. return NULL;
  62. }
  63. rstack = rstack->previous;
  64. top->previous = NULL;
  65. free_render_stack(top);
  66. return rstack;
  67. }
  68. // Functions to convert node_block and inline lists to HTML strings.
  69. static void escape_html(strbuf *dest, const unsigned char *source, int length)
  70. {
  71. if (length < 0)
  72. length = strlen((char *)source);
  73. houdini_escape_html0(dest, source, (size_t)length, 0);
  74. }
  75. static void escape_href(strbuf *dest, const unsigned char *source, int length)
  76. {
  77. if (length < 0)
  78. length = strlen((char *)source);
  79. houdini_escape_href(dest, source, (size_t)length);
  80. }
  81. static inline void cr(strbuf *html)
  82. {
  83. if (html->size && html->ptr[html->size - 1] != '\n')
  84. strbuf_putc(html, '\n');
  85. }
  86. // Convert an inline list to HTML. Returns 0 on success, and sets result.
  87. static void inlines_to_html(strbuf *html, node_inl* ils)
  88. {
  89. strbuf scrap = GH_BUF_INIT;
  90. node_inl* children;
  91. render_stack* rstack = NULL;
  92. while(ils != NULL) {
  93. children = NULL;
  94. switch(ils->tag) {
  95. case INL_STRING:
  96. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  97. break;
  98. case INL_LINEBREAK:
  99. strbuf_puts(html, "<br />\n");
  100. break;
  101. case INL_SOFTBREAK:
  102. strbuf_putc(html, '\n');
  103. break;
  104. case INL_CODE:
  105. strbuf_puts(html, "<code>");
  106. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  107. strbuf_puts(html, "</code>");
  108. break;
  109. case INL_RAW_HTML:
  110. strbuf_put(html,
  111. ils->content.literal.data,
  112. ils->content.literal.len);
  113. break;
  114. case INL_LINK:
  115. strbuf_puts(html, "<a href=\"");
  116. if (ils->content.linkable.url)
  117. escape_href(html, ils->content.linkable.url, -1);
  118. if (ils->content.linkable.title) {
  119. strbuf_puts(html, "\" title=\"");
  120. escape_html(html, ils->content.linkable.title, -1);
  121. }
  122. strbuf_puts(html, "\">");
  123. children = ils->content.inlines;
  124. rstack = push_inline(rstack, ils->next, "</a>");
  125. break;
  126. case INL_IMAGE:
  127. strbuf_puts(html, "<img src=\"");
  128. if (ils->content.linkable.url)
  129. escape_href(html, ils->content.linkable.url, -1);
  130. inlines_to_html(&scrap, ils->content.inlines);
  131. strbuf_puts(html, "\" alt=\"");
  132. if (scrap.size)
  133. escape_html(html, scrap.ptr, scrap.size);
  134. strbuf_clear(&scrap);
  135. if (ils->content.linkable.title) {
  136. strbuf_puts(html, "\" title=\"");
  137. escape_html(html, ils->content.linkable.title, -1);
  138. }
  139. strbuf_puts(html, "\"/>");
  140. break;
  141. case INL_STRONG:
  142. strbuf_puts(html, "<strong>");
  143. children = ils->content.inlines;
  144. rstack = push_inline(rstack, ils->next, "</strong>");
  145. break;
  146. case INL_EMPH:
  147. strbuf_puts(html, "<em>");
  148. children = ils->content.inlines;
  149. rstack = push_inline(rstack, ils->next, "</em>");
  150. break;
  151. }
  152. if (children) {
  153. ils = children;
  154. } else {
  155. ils = ils->next;
  156. }
  157. while (ils == NULL && rstack != NULL) {
  158. strbuf_puts(html, rstack->literal);
  159. ils = rstack->next_sibling.inl;
  160. rstack = pop_render_stack(rstack);
  161. }
  162. }
  163. strbuf_free(&scrap);
  164. free_render_stack(rstack);
  165. }
  166. // Convert a node_block list to HTML. Returns 0 on success, and sets result.
  167. static void blocks_to_html(strbuf *html, node_block *b, bool tight)
  168. {
  169. struct ListData *data;
  170. while(b != NULL) {
  171. switch(b->tag) {
  172. case BLOCK_DOCUMENT:
  173. blocks_to_html(html, b->children, false);
  174. break;
  175. case BLOCK_PARAGRAPH:
  176. if (tight) {
  177. inlines_to_html(html, b->inline_content);
  178. } else {
  179. cr(html);
  180. strbuf_puts(html, "<p>");
  181. inlines_to_html(html, b->inline_content);
  182. strbuf_puts(html, "</p>\n");
  183. }
  184. break;
  185. case BLOCK_BQUOTE:
  186. cr(html);
  187. strbuf_puts(html, "<blockquote>\n");
  188. blocks_to_html(html, b->children, false);
  189. strbuf_puts(html, "</blockquote>\n");
  190. break;
  191. case BLOCK_LIST_ITEM:
  192. cr(html);
  193. strbuf_puts(html, "<li>");
  194. blocks_to_html(html, b->children, tight);
  195. strbuf_trim(html); /* TODO: rtrim */
  196. strbuf_puts(html, "</li>\n");
  197. break;
  198. case BLOCK_LIST:
  199. // make sure a list starts at the beginning of the line:
  200. cr(html);
  201. data = &(b->as.list);
  202. if (data->start > 1) {
  203. strbuf_printf(html, "<%s start=\"%d\">\n",
  204. data->list_type == bullet ? "ul" : "ol",
  205. data->start);
  206. } else {
  207. strbuf_puts(html, data->list_type == bullet ? "<ul>\n" : "<ol>\n");
  208. }
  209. blocks_to_html(html, b->children, data->tight);
  210. strbuf_puts(html, data->list_type == bullet ? "</ul>" : "</ol>");
  211. strbuf_putc(html, '\n');
  212. break;
  213. case BLOCK_ATX_HEADER:
  214. case BLOCK_SETEXT_HEADER:
  215. cr(html);
  216. strbuf_printf(html, "<h%d>", b->as.header.level);
  217. inlines_to_html(html, b->inline_content);
  218. strbuf_printf(html, "</h%d>\n", b->as.header.level);
  219. break;
  220. case BLOCK_INDENTED_CODE:
  221. case BLOCK_FENCED_CODE:
  222. cr(html);
  223. strbuf_puts(html, "<pre><code");
  224. if (b->tag == BLOCK_FENCED_CODE) {
  225. strbuf *info = &b->as.code.info;
  226. if (strbuf_len(info) > 0) {
  227. int first_tag = strbuf_strchr(info, ' ', 0);
  228. if (first_tag < 0)
  229. first_tag = strbuf_len(info);
  230. strbuf_puts(html, " class=\"language-");
  231. escape_html(html, info->ptr, first_tag);
  232. strbuf_putc(html, '"');
  233. }
  234. }
  235. strbuf_putc(html, '>');
  236. escape_html(html, b->string_content.ptr, b->string_content.size);
  237. strbuf_puts(html, "</code></pre>\n");
  238. break;
  239. case BLOCK_HTML:
  240. strbuf_put(html, b->string_content.ptr, b->string_content.size);
  241. break;
  242. case BLOCK_HRULE:
  243. strbuf_puts(html, "<hr />\n");
  244. break;
  245. case BLOCK_REFERENCE_DEF:
  246. break;
  247. default:
  248. assert(false);
  249. }
  250. b = b->next;
  251. }
  252. }
  253. void cmark_render_html(strbuf *html, node_block *root)
  254. {
  255. blocks_to_html(html, root, false);
  256. }