summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--html.c2
-rw-r--r--xs_html.h43
-rw-r--r--xs_version.h2
3 files changed, 33 insertions, 14 deletions
diff --git a/html.c b/html.c
index 2c40d70..1bbd153 100644
--- a/html.c
+++ b/html.c
@@ -2382,7 +2382,7 @@ int html_get_handler(const xs_dict *req, const char *q_path,
2382 xs_html_text(content)))); 2382 xs_html_text(content))));
2383 } 2383 }
2384 2384
2385 *body = xs_html_render_s(rss, xs_dup("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")); 2385 *body = xs_html_render_s(rss, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
2386 *b_size = strlen(*body); 2386 *b_size = strlen(*body);
2387 *ctype = "application/rss+xml; charset=utf-8"; 2387 *ctype = "application/rss+xml; charset=utf-8";
2388 status = 200; 2388 status = 200;
diff --git a/xs_html.h b/xs_html.h
index e9e251e..16ee5ec 100644
--- a/xs_html.h
+++ b/xs_html.h
@@ -21,8 +21,10 @@ xs_html *_xs_html_tag(char *tag, xs_html *var[]);
21xs_html *_xs_html_sctag(char *tag, xs_html *var[]); 21xs_html *_xs_html_sctag(char *tag, xs_html *var[]);
22#define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL }) 22#define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
23 23
24xs_str *xs_html_render_s(xs_html *h, xs_str *s); 24void xs_html_render_f(xs_html *h, FILE *f);
25#define xs_html_render(h) xs_html_render_s(h, xs_str_new(NULL)) 25xs_str *xs_html_render_s(xs_html *tag, char *prefix);
26#define xs_html_render(tag) xs_html_render_s(tag, NULL)
27
26 28
27#ifdef XS_IMPLEMENTATION 29#ifdef XS_IMPLEMENTATION
28 30
@@ -187,55 +189,72 @@ xs_html *_xs_html_sctag(char *tag, xs_html *var[])
187} 189}
188 190
189 191
190xs_str *xs_html_render_s(xs_html *h, xs_str *s) 192void xs_html_render_f(xs_html *h, FILE *f)
191/* renders the tag and its subtags into s */ 193/* renders the tag and its subtags into a file */
192{ 194{
193 xs_html *st; 195 xs_html *st;
194 196
195 switch (h->type) { 197 switch (h->type) {
196 case XS_HTML_TAG: 198 case XS_HTML_TAG:
197 case XS_HTML_SCTAG: 199 case XS_HTML_SCTAG:
198 s = xs_str_cat(s, "<", h->content); 200 fprintf(f, "<%s", h->content);
199 201
200 /* render the attributes */ 202 /* render the attributes */
201 st = h->f_attr; 203 st = h->f_attr;
202 while (st) { 204 while (st) {
203 xs_html *nst = st->next; 205 xs_html *nst = st->next;
204 s = xs_html_render_s(st, s); 206 xs_html_render_f(st, f);
205 st = nst; 207 st = nst;
206 } 208 }
207 209
208 if (h->type == XS_HTML_SCTAG) { 210 if (h->type == XS_HTML_SCTAG) {
209 /* self-closing tags should not have subtags */ 211 /* self-closing tags should not have subtags */
210 s = xs_str_cat(s, "/>\n"); 212 fprintf(f, "/>");
211 } 213 }
212 else { 214 else {
213 s = xs_str_cat(s, ">"); 215 fprintf(f, ">");
214 216
215 /* render the subtags */ 217 /* render the subtags */
216 st = h->f_tag; 218 st = h->f_tag;
217 while (st) { 219 while (st) {
218 xs_html *nst = st->next; 220 xs_html *nst = st->next;
219 s = xs_html_render_s(st, s); 221 xs_html_render_f(st, f);
220 st = nst; 222 st = nst;
221 } 223 }
222 224
223 s = xs_str_cat(s, "</", h->content, ">"); 225 fprintf(f, "</%s>", h->content);
224 } 226 }
225 227
226 break; 228 break;
227 229
228 case XS_HTML_ATTR: 230 case XS_HTML_ATTR:
229 s = xs_str_cat(s, " ", h->content); 231 fprintf(f, " %s", h->content);
230 break; 232 break;
231 233
232 case XS_HTML_TEXT: 234 case XS_HTML_TEXT:
233 s = xs_str_cat(s, h->content); 235 fprintf(f, "%s", h->content);
234 break; 236 break;
235 } 237 }
236 238
237 xs_free(h->content); 239 xs_free(h->content);
238 xs_free(h); 240 xs_free(h);
241}
242
243
244xs_str *xs_html_render_s(xs_html *tag, char *prefix)
245/* renders to a string */
246{
247 xs_str *s = NULL;
248 size_t sz;
249 FILE *f;
250
251 if ((f = open_memstream(&s, &sz)) != NULL) {
252 if (prefix)
253 fprintf(f, "%s", prefix);
254
255 xs_html_render_f(tag, f);
256 fclose(f);
257 }
239 258
240 return s; 259 return s;
241} 260}
diff --git a/xs_version.h b/xs_version.h
index b9ac423..02fd9b7 100644
--- a/xs_version.h
+++ b/xs_version.h
@@ -1 +1 @@
/* 0e2c549f2ac6f4840649332097dc8471a3939cef 2023-11-26T16:44:32+01:00 */ /* ba85b6a3e2332fc51d12a5f9dc5ecbd5f5cc1555 2023-11-27T10:00:17+01:00 */