xs_html.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /* copyright (c) 2022 - 2023 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(char *str);
  6. xs_html *xs_html_attr(char *key, char *value);
  7. xs_html *xs_html_text(char *content);
  8. xs_html *xs_html_raw(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(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(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, 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 *f_attr;
  32. xs_html *l_attr;
  33. xs_html *f_tag;
  34. xs_html *l_tag;
  35. xs_html *next;
  36. };
  37. xs_str *xs_html_encode(char *str)
  38. /* encodes str using HTML entities */
  39. {
  40. xs_str *s = xs_str_new(NULL);
  41. int o = 0;
  42. char *e = str + strlen(str);
  43. for (;;) {
  44. char *ec = "<>\"'&"; /* characters to escape */
  45. 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(char *key, 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(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_html_encode(content);
  89. return a;
  90. }
  91. xs_html *xs_html_raw(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_dup(content);
  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. xs_html **first;
  105. xs_html **last;
  106. if (data->type == XS_HTML_ATTR) {
  107. first = &tag->f_attr;
  108. last = &tag->l_attr;
  109. }
  110. else {
  111. first = &tag->f_tag;
  112. last = &tag->l_tag;
  113. }
  114. if (*first == NULL)
  115. *first = data;
  116. if (*last != NULL)
  117. (*last)->next = data;
  118. *last = data;
  119. }
  120. return tag;
  121. }
  122. static xs_html *_xs_html_tag_t(xs_html_type type, char *tag, xs_html *var[])
  123. /* creates a tag with a variable list of attributes and subtags */
  124. {
  125. xs_html *a = XS_HTML_NEW();
  126. a->type = type;
  127. /* a tag can be NULL, to be a kind of 'wrapper' */
  128. if (tag)
  129. a->content = xs_dup(tag);
  130. _xs_html_add(a, var);
  131. return a;
  132. }
  133. xs_html *_xs_html_tag(char *tag, xs_html *var[])
  134. {
  135. return _xs_html_tag_t(XS_HTML_TAG, tag, var);
  136. }
  137. xs_html *_xs_html_sctag(char *tag, xs_html *var[])
  138. {
  139. return _xs_html_tag_t(XS_HTML_SCTAG, tag, var);
  140. }
  141. xs_html *_xs_html_container(xs_html *var[])
  142. {
  143. return _xs_html_tag_t(XS_HTML_CONTAINER, NULL, var);
  144. }
  145. void xs_html_render_f(xs_html *h, FILE *f)
  146. /* renders the tag and its subtags into a file */
  147. {
  148. if (h == NULL)
  149. return;
  150. switch (h->type) {
  151. case XS_HTML_TAG:
  152. fprintf(f, "<%s", h->content);
  153. /* attributes */
  154. xs_html_render_f(h->f_attr, f);
  155. fprintf(f, ">");
  156. /* sub-tags */
  157. xs_html_render_f(h->f_tag, f);
  158. fprintf(f, "</%s>", h->content);
  159. break;
  160. case XS_HTML_SCTAG:
  161. fprintf(f, "<%s", h->content);
  162. /* attributes */
  163. xs_html_render_f(h->f_attr, f);
  164. fprintf(f, "/>");
  165. break;
  166. case XS_HTML_CONTAINER:
  167. /* sub-tags */
  168. xs_html_render_f(h->f_tag, f);
  169. break;
  170. case XS_HTML_ATTR:
  171. fprintf(f, " ");
  172. /* fallthrough */
  173. case XS_HTML_TEXT:
  174. fprintf(f, "%s", h->content);
  175. break;
  176. }
  177. /* follow the chain */
  178. xs_html_render_f(h->next, f);
  179. xs_free(h->content);
  180. xs_free(h);
  181. }
  182. xs_str *xs_html_render_s(xs_html *tag, char *prefix)
  183. /* renders to a string */
  184. {
  185. xs_str *s = NULL;
  186. size_t sz;
  187. FILE *f;
  188. if ((f = open_memstream(&s, &sz)) != NULL) {
  189. if (prefix)
  190. fprintf(f, "%s", prefix);
  191. xs_html_render_f(tag, f);
  192. fclose(f);
  193. }
  194. return s;
  195. }
  196. #endif /* XS_IMPLEMENTATION */
  197. #endif /* _XS_HTML_H */