aboutsummaryrefslogtreecommitdiff
path: root/src/html/html.c
blob: 6f3bc760dc26deee2647d299e291861310162432 (plain)
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <stdbool.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include "stmd.h"
  7. #include "debug.h"
  8. #include "html/houdini.h"
  9. // Functions to convert node_block and inline lists to HTML strings.
  10. static void escape_html(strbuf *dest, const unsigned char *source, int length)
  11. {
  12. if (length < 0)
  13. length = strlen((char *)source);
  14. houdini_escape_html0(dest, source, (size_t)length, 0);
  15. }
  16. static void escape_href(strbuf *dest, const unsigned char *source, int length)
  17. {
  18. if (length < 0)
  19. length = strlen((char *)source);
  20. houdini_escape_href(dest, source, (size_t)length);
  21. }
  22. static inline void cr(strbuf *html)
  23. {
  24. if (html->size && html->ptr[html->size - 1] != '\n')
  25. strbuf_putc(html, '\n');
  26. }
  27. // Convert an inline list to HTML. Returns 0 on success, and sets result.
  28. static void inlines_to_html(strbuf *html, node_inl* ils)
  29. {
  30. strbuf scrap = GH_BUF_INIT;
  31. while(ils != NULL) {
  32. switch(ils->tag) {
  33. case INL_STRING:
  34. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  35. break;
  36. case INL_LINEBREAK:
  37. strbuf_puts(html, "<br />\n");
  38. break;
  39. case INL_SOFTBREAK:
  40. strbuf_putc(html, '\n');
  41. break;
  42. case INL_CODE:
  43. strbuf_puts(html, "<code>");
  44. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  45. strbuf_puts(html, "</code>");
  46. break;
  47. case INL_RAW_HTML:
  48. strbuf_put(html,
  49. ils->content.literal.data,
  50. ils->content.literal.len);
  51. break;
  52. case INL_LINK:
  53. strbuf_puts(html, "<a href=\"");
  54. if (ils->content.linkable.url)
  55. escape_href(html, ils->content.linkable.url, -1);
  56. if (ils->content.linkable.title) {
  57. strbuf_puts(html, "\" title=\"");
  58. escape_html(html, ils->content.linkable.title, -1);
  59. }
  60. strbuf_puts(html, "\">");
  61. inlines_to_html(html, ils->content.inlines);
  62. strbuf_puts(html, "</a>");
  63. break;
  64. case INL_IMAGE:
  65. strbuf_puts(html, "<img src=\"");
  66. if (ils->content.linkable.url)
  67. escape_href(html, ils->content.linkable.url, -1);
  68. inlines_to_html(&scrap, ils->content.inlines);
  69. strbuf_puts(html, "\" alt=\"");
  70. if (scrap.size)
  71. escape_html(html, scrap.ptr, scrap.size);
  72. strbuf_clear(&scrap);
  73. if (ils->content.linkable.title) {
  74. strbuf_puts(html, "\" title=\"");
  75. escape_html(html, ils->content.linkable.title, -1);
  76. }
  77. strbuf_puts(html, "\"/>");
  78. break;
  79. case INL_STRONG:
  80. strbuf_puts(html, "<strong>");
  81. inlines_to_html(html, ils->content.inlines);
  82. strbuf_puts(html, "</strong>");
  83. break;
  84. case INL_EMPH:
  85. strbuf_puts(html, "<em>");
  86. inlines_to_html(html, ils->content.inlines);
  87. strbuf_puts(html, "</em>");
  88. break;
  89. }
  90. ils = ils->next;
  91. }
  92. }
  93. // Convert a node_block list to HTML. Returns 0 on success, and sets result.
  94. static void blocks_to_html(strbuf *html, node_block *b, bool tight)
  95. {
  96. struct ListData *data;
  97. while(b != NULL) {
  98. switch(b->tag) {
  99. case BLOCK_DOCUMENT:
  100. blocks_to_html(html, b->children, false);
  101. break;
  102. case BLOCK_PARAGRAPH:
  103. if (tight) {
  104. inlines_to_html(html, b->inline_content);
  105. } else {
  106. cr(html);
  107. strbuf_puts(html, "<p>");
  108. inlines_to_html(html, b->inline_content);
  109. strbuf_puts(html, "</p>\n");
  110. }
  111. break;
  112. case BLOCK_BQUOTE:
  113. cr(html);
  114. strbuf_puts(html, "<blockquote>\n");
  115. blocks_to_html(html, b->children, false);
  116. strbuf_puts(html, "</blockquote>\n");
  117. break;
  118. case BLOCK_LIST_ITEM:
  119. cr(html);
  120. strbuf_puts(html, "<li>");
  121. blocks_to_html(html, b->children, tight);
  122. strbuf_trim(html); /* TODO: rtrim */
  123. strbuf_puts(html, "</li>\n");
  124. break;
  125. case BLOCK_LIST:
  126. // make sure a list starts at the beginning of the line:
  127. cr(html);
  128. data = &(b->as.list);
  129. if (data->start > 1) {
  130. strbuf_printf(html, "<%s start=\"%d\">\n",
  131. data->list_type == bullet ? "ul" : "ol",
  132. data->start);
  133. } else {
  134. strbuf_puts(html, data->list_type == bullet ? "<ul>\n" : "<ol>\n");
  135. }
  136. blocks_to_html(html, b->children, data->tight);
  137. strbuf_puts(html, data->list_type == bullet ? "</ul>" : "</ol>");
  138. strbuf_putc(html, '\n');
  139. break;
  140. case BLOCK_ATX_HEADER:
  141. case BLOCK_SETEXT_HEADER:
  142. cr(html);
  143. strbuf_printf(html, "<h%d>", b->as.header.level);
  144. inlines_to_html(html, b->inline_content);
  145. strbuf_printf(html, "</h%d>\n", b->as.header.level);
  146. break;
  147. case BLOCK_INDENTED_CODE:
  148. case BLOCK_FENCED_CODE:
  149. cr(html);
  150. strbuf_puts(html, "<pre><code");
  151. if (b->tag == BLOCK_FENCED_CODE) {
  152. strbuf *info = &b->as.code.info;
  153. if (strbuf_len(info) > 0) {
  154. int first_tag = strbuf_strchr(info, ' ', 0);
  155. if (first_tag < 0)
  156. first_tag = strbuf_len(info);
  157. strbuf_puts(html, " class=\"language-");
  158. escape_html(html, info->ptr, first_tag);
  159. strbuf_putc(html, '"');
  160. }
  161. }
  162. strbuf_putc(html, '>');
  163. escape_html(html, b->string_content.ptr, b->string_content.size);
  164. strbuf_puts(html, "</code></pre>\n");
  165. break;
  166. case BLOCK_HTML:
  167. strbuf_put(html, b->string_content.ptr, b->string_content.size);
  168. break;
  169. case BLOCK_HRULE:
  170. strbuf_puts(html, "<hr />\n");
  171. break;
  172. case BLOCK_REFERENCE_DEF:
  173. break;
  174. default:
  175. assert(false);
  176. }
  177. b = b->next;
  178. }
  179. }
  180. void stmd_render_html(strbuf *html, node_block *root)
  181. {
  182. blocks_to_html(html, root, false);
  183. }