Backport from xs.

This commit is contained in:
default 2023-11-27 10:19:01 +01:00
parent 5a4de9cc8e
commit 1663adbf56
3 changed files with 33 additions and 14 deletions

2
html.c
View File

@ -2382,7 +2382,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
xs_html_text(content)))); xs_html_text(content))));
} }
*body = xs_html_render_s(rss, xs_dup("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")); *body = xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
*b_size = strlen(*body); *b_size = strlen(*body);
*ctype = "application/rss+xml; charset=utf-8"; *ctype = "application/rss+xml; charset=utf-8";
status = 200; status = 200;

View File

@ -21,8 +21,10 @@ xs_html *_xs_html_tag(char *tag, xs_html *var[]);
xs_html *_xs_html_sctag(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 }) #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); void xs_html_render_f(xs_html *h, FILE *f);
#define xs_html_render(h) xs_html_render_s(h, xs_str_new(NULL)) 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 #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) void xs_html_render_f(xs_html *h, FILE *f)
/* renders the tag and its subtags into s */ /* renders the tag and its subtags into a file */
{ {
xs_html *st; xs_html *st;
switch (h->type) { switch (h->type) {
case XS_HTML_TAG: case XS_HTML_TAG:
case XS_HTML_SCTAG: case XS_HTML_SCTAG:
s = xs_str_cat(s, "<", h->content); fprintf(f, "<%s", h->content);
/* render the attributes */ /* render the attributes */
st = h->f_attr; st = h->f_attr;
while (st) { while (st) {
xs_html *nst = st->next; xs_html *nst = st->next;
s = xs_html_render_s(st, s); xs_html_render_f(st, f);
st = nst; st = nst;
} }
if (h->type == XS_HTML_SCTAG) { if (h->type == XS_HTML_SCTAG) {
/* self-closing tags should not have subtags */ /* self-closing tags should not have subtags */
s = xs_str_cat(s, "/>\n"); fprintf(f, "/>");
} }
else { else {
s = xs_str_cat(s, ">"); fprintf(f, ">");
/* render the subtags */ /* render the subtags */
st = h->f_tag; st = h->f_tag;
while (st) { while (st) {
xs_html *nst = st->next; xs_html *nst = st->next;
s = xs_html_render_s(st, s); xs_html_render_f(st, f);
st = nst; st = nst;
} }
s = xs_str_cat(s, "</", h->content, ">"); fprintf(f, "</%s>", h->content);
} }
break; break;
case XS_HTML_ATTR: case XS_HTML_ATTR:
s = xs_str_cat(s, " ", h->content); fprintf(f, " %s", h->content);
break; break;
case XS_HTML_TEXT: case XS_HTML_TEXT:
s = xs_str_cat(s, h->content); fprintf(f, "%s", h->content);
break; break;
} }
xs_free(h->content); xs_free(h->content);
xs_free(h); 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; return s;
} }

View File

@ -1 +1 @@
/* 0e2c549f2ac6f4840649332097dc8471a3939cef 2023-11-26T16:44:32+01:00 */ /* ba85b6a3e2332fc51d12a5f9dc5ecbd5f5cc1555 2023-11-27T10:00:17+01:00 */