xs_html.h 5.4 KB

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