xs_html.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* copyright (c) 2022 - 2026 grunfink et al. / MIT license */
  2. #ifndef _XS_HTML_H
  3. #define _XS_HTML_H
  4. typedef struct xs_html xs_html;
  5. xs_str *xs_html_encode(const char *str);
  6. xs_html *xs_html_attr(const char *key, const char *value);
  7. xs_html *xs_html_text(const char *content);
  8. xs_html *xs_html_raw(const char *content);
  9. xs_html *_xs_html_add(xs_html *tag, xs_html *var[]);
  10. #define xs_html_add(tag, ...) _xs_html_add(tag, (xs_html *[]) { __VA_ARGS__, NULL })
  11. xs_html *_xs_html_tag(const char *tag, xs_html *var[]);
  12. #define xs_html_tag(tag, ...) _xs_html_tag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
  13. xs_html *_xs_html_sctag(const char *tag, xs_html *var[]);
  14. #define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
  15. xs_html *_xs_html_container(xs_html *var[]);
  16. #define xs_html_container(...) _xs_html_container((xs_html *[]) { __VA_ARGS__, NULL })
  17. xs_html *_xs_html_xtag(const char *tag, xs_html *var[]);
  18. #define xs_html_xtag(tag, ...) _xs_html_xtag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
  19. void xs_html_render_f(xs_html *h, FILE *f);
  20. xs_str *xs_html_render_s(xs_html *tag, const char *prefix);
  21. #define xs_html_render(tag) xs_html_render_s(tag, NULL)
  22. #ifdef XS_IMPLEMENTATION
  23. typedef enum {
  24. XS_HTML_TAG,
  25. XS_HTML_SCTAG,
  26. XS_HTML_CONTAINER,
  27. XS_HTML_ATTR,
  28. XS_HTML_TEXT
  29. } xs_html_type;
  30. struct xs_html {
  31. xs_html_type type;
  32. xs_str *content;
  33. xs_html *attrs;
  34. xs_html *tags;
  35. xs_html *next;
  36. };
  37. xs_str *xs_html_encode(const char *str)
  38. /* encodes str using HTML entities */
  39. {
  40. xs_str *s = xs_str_new(NULL);
  41. int o = 0;
  42. const char *e = str + strlen(str);
  43. for (;;) {
  44. char *ec = "<>\"'&"; /* characters to escape */
  45. const char *q = e;
  46. int z;
  47. /* find the nearest happening of a char */
  48. while (*ec) {
  49. char *m = memchr(str, *ec++, q - str);
  50. if (m)
  51. q = m;
  52. }
  53. /* copy string to here */
  54. z = q - str;
  55. s = xs_insert_m(s, o, str, z);
  56. o += z;
  57. /* if q points to the end, nothing more to do */
  58. if (q == e)
  59. break;
  60. /* insert the escaped char */
  61. char tmp[8];
  62. z = snprintf(tmp, sizeof(tmp), "&#%d;", *q);
  63. s = xs_insert_m(s, o, tmp, z);
  64. o += z;
  65. str = q + 1;
  66. }
  67. return s;
  68. }
  69. #define XS_HTML_NEW() memset(xs_realloc(NULL, sizeof(xs_html)), '\0', sizeof(xs_html))
  70. xs_html *xs_html_attr(const char *key, const char *value)
  71. /* creates an HTML block with an attribute */
  72. {
  73. xs_html *a = XS_HTML_NEW();
  74. a->type = XS_HTML_ATTR;
  75. if (value) {
  76. xs *ev = xs_html_encode(value);
  77. a->content = xs_fmt("%s=\"%s\"", key, ev);
  78. }
  79. else
  80. a->content = xs_dup(key);
  81. return a;
  82. }
  83. xs_html *xs_html_text(const char *content)
  84. /* creates an HTML block of text, escaping it previously */
  85. {
  86. xs_html *a = XS_HTML_NEW();
  87. a->type = XS_HTML_TEXT;
  88. a->content = xs_is_string(content) ? xs_html_encode(content) : xs_str_new(NULL);
  89. return a;
  90. }
  91. xs_html *xs_html_raw(const char *content)
  92. /* creates an HTML block without escaping (for pre-formatted HTML, comments, etc) */
  93. {
  94. xs_html *a = XS_HTML_NEW();
  95. a->type = XS_HTML_TEXT;
  96. a->content = xs_is_string(content) ? xs_dup(content) : xs_str_new(NULL);
  97. return a;
  98. }
  99. xs_html *_xs_html_add(xs_html *tag, xs_html *var[])
  100. /* add data (attrs, tags or text) to a tag */
  101. {
  102. while (*var) {
  103. xs_html *data = *var++;
  104. if (data->type == XS_HTML_ATTR) {
  105. data->next = tag->attrs;
  106. tag->attrs = data;
  107. }
  108. else {
  109. data->next = tag->tags;
  110. tag->tags = data;
  111. }
  112. }
  113. return tag;
  114. }
  115. static xs_html *_xs_html_tag_t(xs_html_type type, const char *tag, xs_html *var[])
  116. /* creates a tag with a variable list of attributes and subtags */
  117. {
  118. xs_html *a = XS_HTML_NEW();
  119. a->type = type;
  120. /* a tag can be NULL, to be a kind of 'wrapper' */
  121. if (tag)
  122. a->content = xs_dup(tag);
  123. _xs_html_add(a, var);
  124. return a;
  125. }
  126. xs_html *_xs_html_tag(const char *tag, xs_html *var[])
  127. {
  128. return _xs_html_tag_t(XS_HTML_TAG, tag, var);
  129. }
  130. xs_html *_xs_html_sctag(const char *tag, xs_html *var[])
  131. {
  132. return _xs_html_tag_t(XS_HTML_SCTAG, tag, var);
  133. }
  134. xs_html *_xs_html_container(xs_html *var[])
  135. {
  136. return _xs_html_tag_t(XS_HTML_CONTAINER, NULL, var);
  137. }
  138. xs_html *_xs_html_xtag(const char *tag, xs_html *var[])
  139. {
  140. if (xs_is_null(tag))
  141. return _xs_html_container(var);
  142. else
  143. if (xs_endswith(tag, "/")) {
  144. xs *tag2 = xs_crop_i(xs_dup(tag), 0, -1);
  145. return _xs_html_sctag(tag2, var);
  146. }
  147. return _xs_html_tag(tag, var);
  148. }
  149. void xs_html_render_f(xs_html *h, FILE *f)
  150. /* renders the tag and its subtags into a file */
  151. {
  152. if (h == NULL)
  153. return;
  154. /* follow the chain */
  155. xs_html_render_f(h->next, f);
  156. switch (h->type) {
  157. case XS_HTML_TAG:
  158. fprintf(f, "<%s", h->content);
  159. /* attributes */
  160. xs_html_render_f(h->attrs, f);
  161. fprintf(f, ">");
  162. /* sub-tags */
  163. xs_html_render_f(h->tags, f);
  164. fprintf(f, "</%s>", h->content);
  165. break;
  166. case XS_HTML_SCTAG:
  167. fprintf(f, "<%s", h->content);
  168. /* attributes */
  169. xs_html_render_f(h->attrs, f);
  170. fprintf(f, "/>");
  171. break;
  172. case XS_HTML_CONTAINER:
  173. /* sub-tags */
  174. xs_html_render_f(h->tags, f);
  175. break;
  176. case XS_HTML_ATTR:
  177. fprintf(f, " ");
  178. /* fallthrough */
  179. case XS_HTML_TEXT:
  180. fprintf(f, "%s", h->content);
  181. break;
  182. }
  183. xs_free(h->content);
  184. xs_free(h);
  185. }
  186. xs_str *xs_html_render_s(xs_html *tag, const char *prefix)
  187. /* renders to a string */
  188. {
  189. xs_str *s = NULL;
  190. size_t sz;
  191. FILE *f;
  192. if ((f = open_memstream(&s, &sz)) != NULL) {
  193. if (prefix)
  194. fprintf(f, "%s", prefix);
  195. xs_html_render_f(tag, f);
  196. fclose(f);
  197. }
  198. return s;
  199. }
  200. #endif /* XS_IMPLEMENTATION */
  201. #endif /* _XS_HTML_H */