aboutsummaryrefslogtreecommitdiff
path: root/src/html/html.c
blob: 6041fdea7dd14c584c4e925486164bdec0ea7fd9 (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 a node_block list to HTML. Returns 0 on success, and sets result.
  28. void blocks_to_html(strbuf *html, node_block *b, bool tight)
  29. {
  30. struct ListData *data;
  31. while(b != NULL) {
  32. switch(b->tag) {
  33. case document:
  34. blocks_to_html(html, b->children, false);
  35. break;
  36. case paragraph:
  37. if (tight) {
  38. inlines_to_html(html, b->inline_content);
  39. } else {
  40. cr(html);
  41. strbuf_puts(html, "<p>");
  42. inlines_to_html(html, b->inline_content);
  43. strbuf_puts(html, "</p>\n");
  44. }
  45. break;
  46. case block_quote:
  47. cr(html);
  48. strbuf_puts(html, "<blockquote>\n");
  49. blocks_to_html(html, b->children, false);
  50. strbuf_puts(html, "</blockquote>\n");
  51. break;
  52. case list_item:
  53. cr(html);
  54. strbuf_puts(html, "<li>");
  55. blocks_to_html(html, b->children, tight);
  56. strbuf_trim(html); /* TODO: rtrim */
  57. strbuf_puts(html, "</li>\n");
  58. break;
  59. case list:
  60. // make sure a list starts at the beginning of the line:
  61. cr(html);
  62. data = &(b->attributes.list_data);
  63. if (data->start > 1) {
  64. strbuf_printf(html, "<%s start=\"%d\">\n",
  65. data->list_type == bullet ? "ul" : "ol",
  66. data->start);
  67. } else {
  68. strbuf_puts(html, data->list_type == bullet ? "<ul>\n" : "<ol>\n");
  69. }
  70. blocks_to_html(html, b->children, data->tight);
  71. strbuf_puts(html, data->list_type == bullet ? "</ul>" : "</ol>");
  72. strbuf_putc(html, '\n');
  73. break;
  74. case atx_header:
  75. case setext_header:
  76. cr(html);
  77. strbuf_printf(html, "<h%d>", b->attributes.header_level);
  78. inlines_to_html(html, b->inline_content);
  79. strbuf_printf(html, "</h%d>\n", b->attributes.header_level);
  80. break;
  81. case indented_code:
  82. case fenced_code:
  83. cr(html);
  84. strbuf_puts(html, "<pre");
  85. if (b->tag == fenced_code) {
  86. strbuf *info = &b->attributes.fenced_code_data.info;
  87. if (strbuf_len(info) > 0) {
  88. int first_tag = strbuf_strchr(info, ' ', 0);
  89. if (first_tag < 0)
  90. first_tag = strbuf_len(info);
  91. strbuf_puts(html, " class=\"");
  92. escape_html(html, info->ptr, first_tag);
  93. strbuf_putc(html, '"');
  94. }
  95. }
  96. strbuf_puts(html, "><code>");
  97. escape_html(html, b->string_content.ptr, b->string_content.size);
  98. strbuf_puts(html, "</code></pre>\n");
  99. break;
  100. case html_block:
  101. strbuf_put(html, b->string_content.ptr, b->string_content.size);
  102. break;
  103. case hrule:
  104. strbuf_puts(html, "<hr />\n");
  105. break;
  106. case reference_def:
  107. break;
  108. default:
  109. assert(false);
  110. }
  111. b = b->next;
  112. }
  113. }
  114. // Convert an inline list to HTML. Returns 0 on success, and sets result.
  115. void inlines_to_html(strbuf *html, node_inl* ils)
  116. {
  117. strbuf scrap = GH_BUF_INIT;
  118. while(ils != NULL) {
  119. switch(ils->tag) {
  120. case INL_STRING:
  121. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  122. break;
  123. case INL_LINEBREAK:
  124. strbuf_puts(html, "<br />\n");
  125. break;
  126. case INL_SOFTBREAK:
  127. strbuf_putc(html, '\n');
  128. break;
  129. case INL_CODE:
  130. strbuf_puts(html, "<code>");
  131. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  132. strbuf_puts(html, "</code>");
  133. break;
  134. case INL_RAW_HTML:
  135. case INL_ENTITY:
  136. strbuf_put(html,
  137. ils->content.literal.data,
  138. ils->content.literal.len);
  139. break;
  140. case INL_LINK:
  141. strbuf_puts(html, "<a href=\"");
  142. if (ils->content.linkable.url)
  143. escape_href(html, ils->content.linkable.url, -1);
  144. if (ils->content.linkable.title) {
  145. strbuf_puts(html, "\" title=\"");
  146. escape_html(html, ils->content.linkable.title, -1);
  147. }
  148. strbuf_puts(html, "\">");
  149. inlines_to_html(html, ils->content.inlines);
  150. strbuf_puts(html, "</a>");
  151. break;
  152. case INL_IMAGE:
  153. strbuf_puts(html, "<img src=\"");
  154. if (ils->content.linkable.url)
  155. escape_href(html, ils->content.linkable.url, -1);
  156. inlines_to_html(&scrap, ils->content.inlines);
  157. strbuf_puts(html, "\" alt=\"");
  158. if (scrap.size)
  159. escape_html(html, scrap.ptr, scrap.size);
  160. strbuf_clear(&scrap);
  161. if (ils->content.linkable.title) {
  162. strbuf_puts(html, "\" title=\"");
  163. escape_html(html, ils->content.linkable.title, -1);
  164. }
  165. strbuf_puts(html, "\"/>");
  166. break;
  167. case INL_STRONG:
  168. strbuf_puts(html, "<strong>");
  169. inlines_to_html(html, ils->content.inlines);
  170. strbuf_puts(html, "</strong>");
  171. break;
  172. case INL_EMPH:
  173. strbuf_puts(html, "<em>");
  174. inlines_to_html(html, ils->content.inlines);
  175. strbuf_puts(html, "</em>");
  176. break;
  177. }
  178. ils = ils->next;
  179. }
  180. }