aboutsummaryrefslogtreecommitdiff
path: root/src/html/html.c
blob: 41b8fdacf18200e835f5ad89f36488b3ee62c360 (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 block and inline lists to HTML strings.
  10. static void escape_html(gh_buf *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(gh_buf *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(gh_buf *html)
  23. {
  24. if (html->size && html->ptr[html->size - 1] != '\n')
  25. gh_buf_putc(html, '\n');
  26. }
  27. // Convert a block list to HTML. Returns 0 on success, and sets result.
  28. void blocks_to_html(gh_buf *html, 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. gh_buf_puts(html, "<p>");
  42. inlines_to_html(html, b->inline_content);
  43. gh_buf_puts(html, "</p>\n");
  44. }
  45. break;
  46. case block_quote:
  47. cr(html);
  48. gh_buf_puts(html, "<blockquote>\n");
  49. blocks_to_html(html, b->children, false);
  50. gh_buf_puts(html, "</blockquote>\n");
  51. break;
  52. case list_item:
  53. cr(html);
  54. gh_buf_puts(html, "<li>");
  55. blocks_to_html(html, b->children, tight);
  56. gh_buf_trim(html); /* TODO: rtrim */
  57. gh_buf_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. gh_buf_printf(html, "<%s start=\"%d\">\n",
  65. data->list_type == bullet ? "ul" : "ol",
  66. data->start);
  67. } else {
  68. gh_buf_puts(html, data->list_type == bullet ? "<ul>\n" : "<ol>\n");
  69. }
  70. blocks_to_html(html, b->children, data->tight);
  71. gh_buf_puts(html, data->list_type == bullet ? "</ul>" : "</ol>");
  72. gh_buf_putc(html, '\n');
  73. break;
  74. case atx_header:
  75. case setext_header:
  76. cr(html);
  77. gh_buf_printf(html, "<h%d>", b->attributes.header_level);
  78. inlines_to_html(html, b->inline_content);
  79. gh_buf_printf(html, "</h%d>\n", b->attributes.header_level);
  80. break;
  81. case indented_code:
  82. case fenced_code:
  83. cr(html);
  84. gh_buf_puts(html, "<pre");
  85. if (b->tag == fenced_code) {
  86. gh_buf *info = &b->attributes.fenced_code_data.info;
  87. if (gh_buf_len(info) > 0) {
  88. int first_tag = gh_buf_strchr(info, ' ', 0);
  89. if (first_tag < 0)
  90. first_tag = gh_buf_len(info);
  91. gh_buf_puts(html, " class=\"");
  92. escape_html(html, info->ptr, first_tag);
  93. gh_buf_putc(html, '"');
  94. }
  95. }
  96. gh_buf_puts(html, "><code>");
  97. escape_html(html, b->string_content.ptr, b->string_content.size);
  98. gh_buf_puts(html, "</code></pre>\n");
  99. break;
  100. case html_block:
  101. gh_buf_put(html, b->string_content.ptr, b->string_content.size);
  102. break;
  103. case hrule:
  104. gh_buf_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(gh_buf *html, inl* ils)
  116. {
  117. gh_buf 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. gh_buf_puts(html, "<br />\n");
  125. break;
  126. case INL_SOFTBREAK:
  127. gh_buf_putc(html, '\n');
  128. break;
  129. case INL_CODE:
  130. gh_buf_puts(html, "<code>");
  131. escape_html(html, ils->content.literal.data, ils->content.literal.len);
  132. gh_buf_puts(html, "</code>");
  133. break;
  134. case INL_RAW_HTML:
  135. case INL_ENTITY:
  136. gh_buf_put(html,
  137. ils->content.literal.data,
  138. ils->content.literal.len);
  139. break;
  140. case INL_LINK:
  141. gh_buf_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. gh_buf_puts(html, "\" title=\"");
  146. escape_html(html, ils->content.linkable.title, -1);
  147. }
  148. gh_buf_puts(html, "\">");
  149. inlines_to_html(html, ils->content.inlines);
  150. gh_buf_puts(html, "</a>");
  151. break;
  152. case INL_IMAGE:
  153. gh_buf_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. gh_buf_puts(html, "\" alt=\"");
  158. if (scrap.size)
  159. escape_html(html, scrap.ptr, scrap.size);
  160. gh_buf_clear(&scrap);
  161. if (ils->content.linkable.title) {
  162. gh_buf_puts(html, "\" title=\"");
  163. escape_html(html, ils->content.linkable.title, -1);
  164. }
  165. gh_buf_puts(html, "\"/>");
  166. break;
  167. case INL_STRONG:
  168. gh_buf_puts(html, "<strong>");
  169. inlines_to_html(html, ils->content.inlines);
  170. gh_buf_puts(html, "</strong>");
  171. break;
  172. case INL_EMPH:
  173. gh_buf_puts(html, "<em>");
  174. inlines_to_html(html, ils->content.inlines);
  175. gh_buf_puts(html, "</em>");
  176. break;
  177. }
  178. ils = ils->next;
  179. }
  180. }