diff options
| author | 2023-11-27 10:19:01 +0100 | |
|---|---|---|
| committer | 2023-11-27 10:19:01 +0100 | |
| commit | 1663adbf565f57236617ab227431dd95132d6c9c (patch) | |
| tree | aeb8272653d49342d45b0bd9c53ab667ec4cf21e /xs_html.h | |
| parent | Minor html tweak. (diff) | |
| download | snac2-1663adbf565f57236617ab227431dd95132d6c9c.tar.gz snac2-1663adbf565f57236617ab227431dd95132d6c9c.tar.xz snac2-1663adbf565f57236617ab227431dd95132d6c9c.zip | |
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[]); | |||
| 21 | xs_html *_xs_html_sctag(char *tag, xs_html *var[]); | 21 | xs_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 | ||
| 24 | xs_str *xs_html_render_s(xs_html *h, xs_str *s); | 24 | void xs_html_render_f(xs_html *h, FILE *f); |
| 25 | #define xs_html_render(h) xs_html_render_s(h, xs_str_new(NULL)) | 25 | xs_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 | ||
| 190 | xs_str *xs_html_render_s(xs_html *h, xs_str *s) | 192 | void 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 | |||
| 244 | xs_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 | } |