commit 1663adb
default
·
2023-11-27 09:19:01 +0000 UTC
parent 5a4de9c
Backport from xs.
3 files changed,
+33,
-14
M
html.c
+1,
-1
1@@ -2382,7 +2382,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
2 xs_html_text(content))));
3 }
4
5- *body = xs_html_render_s(rss, xs_dup("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"));
6+ *body = xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
7 *b_size = strlen(*body);
8 *ctype = "application/rss+xml; charset=utf-8";
9 status = 200;
+31,
-12
1@@ -21,8 +21,10 @@ xs_html *_xs_html_tag(char *tag, xs_html *var[]);
2 xs_html *_xs_html_sctag(char *tag, xs_html *var[]);
3 #define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
4
5-xs_str *xs_html_render_s(xs_html *h, xs_str *s);
6-#define xs_html_render(h) xs_html_render_s(h, xs_str_new(NULL))
7+void xs_html_render_f(xs_html *h, FILE *f);
8+xs_str *xs_html_render_s(xs_html *tag, char *prefix);
9+#define xs_html_render(tag) xs_html_render_s(tag, NULL)
10+
11
12 #ifdef XS_IMPLEMENTATION
13
14@@ -187,55 +189,72 @@ xs_html *_xs_html_sctag(char *tag, xs_html *var[])
15 }
16
17
18-xs_str *xs_html_render_s(xs_html *h, xs_str *s)
19-/* renders the tag and its subtags into s */
20+void xs_html_render_f(xs_html *h, FILE *f)
21+/* renders the tag and its subtags into a file */
22 {
23 xs_html *st;
24
25 switch (h->type) {
26 case XS_HTML_TAG:
27 case XS_HTML_SCTAG:
28- s = xs_str_cat(s, "<", h->content);
29+ fprintf(f, "<%s", h->content);
30
31 /* render the attributes */
32 st = h->f_attr;
33 while (st) {
34 xs_html *nst = st->next;
35- s = xs_html_render_s(st, s);
36+ xs_html_render_f(st, f);
37 st = nst;
38 }
39
40 if (h->type == XS_HTML_SCTAG) {
41 /* self-closing tags should not have subtags */
42- s = xs_str_cat(s, "/>\n");
43+ fprintf(f, "/>");
44 }
45 else {
46- s = xs_str_cat(s, ">");
47+ fprintf(f, ">");
48
49 /* render the subtags */
50 st = h->f_tag;
51 while (st) {
52 xs_html *nst = st->next;
53- s = xs_html_render_s(st, s);
54+ xs_html_render_f(st, f);
55 st = nst;
56 }
57
58- s = xs_str_cat(s, "</", h->content, ">");
59+ fprintf(f, "</%s>", h->content);
60 }
61
62 break;
63
64 case XS_HTML_ATTR:
65- s = xs_str_cat(s, " ", h->content);
66+ fprintf(f, " %s", h->content);
67 break;
68
69 case XS_HTML_TEXT:
70- s = xs_str_cat(s, h->content);
71+ fprintf(f, "%s", h->content);
72 break;
73 }
74
75 xs_free(h->content);
76 xs_free(h);
77+}
78+
79+
80+xs_str *xs_html_render_s(xs_html *tag, char *prefix)
81+/* renders to a string */
82+{
83+ xs_str *s = NULL;
84+ size_t sz;
85+ FILE *f;
86+
87+ if ((f = open_memstream(&s, &sz)) != NULL) {
88+ if (prefix)
89+ fprintf(f, "%s", prefix);
90+
91+ xs_html_render_f(tag, f);
92+ fclose(f);
93+ }
94
95 return s;
96 }
+1,
-1
1@@ -1 +1 @@
2-/* 0e2c549f2ac6f4840649332097dc8471a3939cef 2023-11-26T16:44:32+01:00 */
3+/* ba85b6a3e2332fc51d12a5f9dc5ecbd5f5cc1555 2023-11-27T10:00:17+01:00 */