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