diff options
author | default <nobody@localhost> | 2023-11-27 10:19:01 +0100 |
---|---|---|
committer | default <nobody@localhost> | 2023-11-27 10:19:01 +0100 |
commit | 1663adbf565f57236617ab227431dd95132d6c9c (patch) | |
tree | aeb8272653d49342d45b0bd9c53ab667ec4cf21e /xs_html.h | |
parent | 5a4de9cc8e80ed5a55cd2ec59dfc9b5eadf86700 (diff) |
Backport from xs.
Diffstat (limited to 'xs_html.h')
-rw-r--r-- | xs_html.h | 43 |
1 files changed, 31 insertions, 12 deletions
@@ -21,8 +21,10 @@ xs_html *_xs_html_tag(char *tag, xs_html *var[]); xs_html *_xs_html_sctag(char *tag, xs_html *var[]); #define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL }) -xs_str *xs_html_render_s(xs_html *h, xs_str *s); -#define xs_html_render(h) xs_html_render_s(h, xs_str_new(NULL)) +void xs_html_render_f(xs_html *h, FILE *f); +xs_str *xs_html_render_s(xs_html *tag, char *prefix); +#define xs_html_render(tag) xs_html_render_s(tag, NULL) + #ifdef XS_IMPLEMENTATION @@ -187,55 +189,72 @@ xs_html *_xs_html_sctag(char *tag, xs_html *var[]) } -xs_str *xs_html_render_s(xs_html *h, xs_str *s) -/* renders the tag and its subtags into s */ +void xs_html_render_f(xs_html *h, FILE *f) +/* renders the tag and its subtags into a file */ { xs_html *st; switch (h->type) { case XS_HTML_TAG: case XS_HTML_SCTAG: - s = xs_str_cat(s, "<", h->content); + fprintf(f, "<%s", h->content); /* render the attributes */ st = h->f_attr; while (st) { xs_html *nst = st->next; - s = xs_html_render_s(st, s); + xs_html_render_f(st, f); st = nst; } if (h->type == XS_HTML_SCTAG) { /* self-closing tags should not have subtags */ - s = xs_str_cat(s, "/>\n"); + fprintf(f, "/>"); } else { - s = xs_str_cat(s, ">"); + fprintf(f, ">"); /* render the subtags */ st = h->f_tag; while (st) { xs_html *nst = st->next; - s = xs_html_render_s(st, s); + xs_html_render_f(st, f); st = nst; } - s = xs_str_cat(s, "</", h->content, ">"); + fprintf(f, "</%s>", h->content); } break; case XS_HTML_ATTR: - s = xs_str_cat(s, " ", h->content); + fprintf(f, " %s", h->content); break; case XS_HTML_TEXT: - s = xs_str_cat(s, h->content); + fprintf(f, "%s", h->content); break; } xs_free(h->content); xs_free(h); +} + + +xs_str *xs_html_render_s(xs_html *tag, char *prefix) +/* renders to a string */ +{ + xs_str *s = NULL; + size_t sz; + FILE *f; + + if ((f = open_memstream(&s, &sz)) != NULL) { + if (prefix) + fprintf(f, "%s", prefix); + + xs_html_render_f(tag, f); + fclose(f); + } return s; } |