xs_html.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. void xs_html_render_f(xs_html *h, FILE *f);
  16. xs_str *xs_html_render_s(xs_html *tag, char *prefix);
  17. #define xs_html_render(tag) xs_html_render_s(tag, NULL)
  18. #ifdef XS_IMPLEMENTATION
  19. typedef enum {
  20. XS_HTML_TAG,
  21. XS_HTML_SCTAG,
  22. XS_HTML_ATTR,
  23. XS_HTML_TEXT
  24. } xs_html_type;
  25. struct xs_html {
  26. xs_html_type type;
  27. xs_str *content;
  28. xs_html *f_attr;
  29. xs_html *l_attr;
  30. xs_html *f_tag;
  31. xs_html *l_tag;
  32. xs_html *next;
  33. };
  34. xs_str *xs_html_encode(char *str)
  35. /* encodes str using HTML entities */
  36. {
  37. xs_str *s = xs_str_new(NULL);
  38. int o = 0;
  39. char *e = str + strlen(str);
  40. for (;;) {
  41. char *ec = "<>\"'&"; /* characters to escape */
  42. char *q = e;
  43. int z;
  44. /* find the nearest happening of a char */
  45. while (*ec) {
  46. char *m = memchr(str, *ec++, q - str);
  47. if (m)
  48. q = m;
  49. }
  50. /* copy string to here */
  51. z = q - str;
  52. s = xs_insert_m(s, o, str, z);
  53. o += z;
  54. /* if q points to the end, nothing more to do */
  55. if (q == e)
  56. break;
  57. /* insert the escaped char */
  58. char tmp[8];
  59. snprintf(tmp, sizeof(tmp), "&#%d;", *q);
  60. z = strlen(tmp);
  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(char *key, 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(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(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. xs_html **first;
  103. xs_html **last;
  104. if (data->type == XS_HTML_ATTR) {
  105. first = &tag->f_attr;
  106. last = &tag->l_attr;
  107. }
  108. else {
  109. first = &tag->f_tag;
  110. last = &tag->l_tag;
  111. }
  112. if (*first == NULL)
  113. *first = data;
  114. if (*last != NULL)
  115. (*last)->next = data;
  116. *last = data;
  117. }
  118. return tag;
  119. }
  120. static xs_html *_xs_html_tag_t(xs_html_type type, char *tag, xs_html *var[])
  121. /* creates a tag with a variable list of attributes and subtags */
  122. {
  123. xs_html *a = XS_HTML_NEW();
  124. a->type = type;
  125. a->content = xs_dup(tag);
  126. _xs_html_add(a, var);
  127. return a;
  128. }
  129. xs_html *_xs_html_tag(char *tag, xs_html *var[])
  130. {
  131. return _xs_html_tag_t(XS_HTML_TAG, tag, var);
  132. }
  133. xs_html *_xs_html_sctag(char *tag, xs_html *var[])
  134. {
  135. return _xs_html_tag_t(XS_HTML_SCTAG, tag, var);
  136. }
  137. void xs_html_render_f(xs_html *h, FILE *f)
  138. /* renders the tag and its subtags into a file */
  139. {
  140. xs_html *st;
  141. switch (h->type) {
  142. case XS_HTML_TAG:
  143. case XS_HTML_SCTAG:
  144. fprintf(f, "<%s", h->content);
  145. /* render the attributes */
  146. st = h->f_attr;
  147. while (st) {
  148. xs_html *nst = st->next;
  149. xs_html_render_f(st, f);
  150. st = nst;
  151. }
  152. if (h->type == XS_HTML_SCTAG) {
  153. /* self-closing tags should not have subtags */
  154. fprintf(f, "/>");
  155. }
  156. else {
  157. fprintf(f, ">");
  158. /* render the subtags */
  159. st = h->f_tag;
  160. while (st) {
  161. xs_html *nst = st->next;
  162. xs_html_render_f(st, f);
  163. st = nst;
  164. }
  165. fprintf(f, "</%s>", h->content);
  166. }
  167. break;
  168. case XS_HTML_ATTR:
  169. fprintf(f, " %s", h->content);
  170. break;
  171. case XS_HTML_TEXT:
  172. fprintf(f, "%s", h->content);
  173. break;
  174. }
  175. xs_free(h->content);
  176. xs_free(h);
  177. }
  178. xs_str *xs_html_render_s(xs_html *tag, char *prefix)
  179. /* renders to a string */
  180. {
  181. xs_str *s = NULL;
  182. size_t sz;
  183. FILE *f;
  184. if ((f = open_memstream(&s, &sz)) != NULL) {
  185. if (prefix)
  186. fprintf(f, "%s", prefix);
  187. xs_html_render_f(tag, f);
  188. fclose(f);
  189. }
  190. return s;
  191. }
  192. #endif /* XS_IMPLEMENTATION */
  193. #endif /* _XS_HTML_H */