aboutsummaryrefslogtreecommitdiff
path: root/src/html/html.c
blob: 74f6791e4a9d262344eaf4314e3e7eeb0d96efde (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 BLOCK_DOCUMENT:
  34. blocks_to_html(html, b->children, false);
  35. break;
  36. case BLOCK_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_BQUOTE:
  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 BLOCK_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 BLOCK_LIST:
  60. // make sure a list starts at the beginning of the line:
  61. cr(html);
  62. data = &(b->as.list);
  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 BLOCK_ATX_HEADER:
  75. case BLOCK_SETEXT_HEADER:
  76. cr(html);
  77. strbuf_printf(html, "<h%d>", b->as.header.level);
  78. inlines_to_html(html, b->inline_content);
  79. strbuf_printf(html, "</h%d>\n", b->as.header.level);
  80. break;
  81. case BLOCK_INDENTED_CODE:
  82. case BLOCK_FENCED_CODE:
  83. cr(html);
  84. strbuf_puts(html, "<pre><code");
  85. if (b->tag == BLOCK_FENCED_CODE) {
  86. strbuf *info = &b->as.code.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=\"lang-");
  92. escape_html(html, info->ptr, first_tag);
  93. strbuf_putc(html, '"');
  94. }
  95. }
  96. strbuf_putc(html, '>');
  97. escape_html(html, b->string_content.ptr, b->string_content.size);
  98. strbuf_puts(html, "</code></pre>\n");
  99. break;
  100. case BLOCK_HTML:
  101. strbuf_put(html, b->string_content.ptr, b->string_content.size);
  102. break;
  103. case BLOCK_HRULE:
  104. strbuf_puts(html, "<hr />\n");
  105. break;
  106. case BLOCK_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. strbuf_put(html,
  136. ils->content.literal.data,
  137. ils->content.literal.len);
  138. break;
  139. case INL_LINK:
  140. strbuf_puts(html, "<a href=\"");
  141. if (ils->content.linkable.url)
  142. escape_href(html, ils->content.linkable.url, -1);
  143. if (ils->content.linkable.title) {
  144. strbuf_puts(html, "\" title=\"");
  145. escape_html(html, ils->content.linkable.title, -1);
  146. }
  147. strbuf_puts(html, "\">");
  148. inlines_to_html(html, ils->content.inlines);
  149. strbuf_puts(html, "</a>");
  150. break;
  151. case INL_IMAGE:
  152. strbuf_puts(html, "<img src=\"");
  153. if (ils->content.linkable.url)
  154. escape_href(html, ils->content.linkable.url, -1);
  155. inlines_to_html(&scrap, ils->content.inlines);
  156. strbuf_puts(html, "\" alt=\"");
  157. if (scrap.size)
  158. escape_html(html, scrap.ptr, scrap.size);
  159. strbuf_clear(&scrap);
  160. if (ils->content.linkable.title) {
  161. strbuf_puts(html, "\" title=\"");
  162. escape_html(html, ils->content.linkable.title, -1);
  163. }
  164. strbuf_puts(html, "\"/>");
  165. break;
  166. case INL_STRONG:
  167. strbuf_puts(html, "<strong>");
  168. inlines_to_html(html, ils->content.inlines);
  169. strbuf_puts(html, "</strong>");
  170. break;
  171. case INL_EMPH:
  172. strbuf_puts(html, "<em>");
  173. inlines_to_html(html, ils->content.inlines);
  174. strbuf_puts(html, "</em>");
  175. break;
  176. }
  177. ils = ils->next;
  178. }
  179. }