diff options
Diffstat (limited to 'xs_html.h')
| -rw-r--r-- | xs_html.h | 37 |
1 files changed, 26 insertions, 11 deletions
| @@ -21,6 +21,9 @@ 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_html *_xs_html_container(xs_html *var[]); | ||
| 25 | #define xs_html_container(...) _xs_html_container((xs_html *[]) { __VA_ARGS__, NULL }) | ||
| 26 | |||
| 24 | void xs_html_render_f(xs_html *h, FILE *f); | 27 | void xs_html_render_f(xs_html *h, FILE *f); |
| 25 | xs_str *xs_html_render_s(xs_html *tag, char *prefix); | 28 | xs_str *xs_html_render_s(xs_html *tag, char *prefix); |
| 26 | #define xs_html_render(tag) xs_html_render_s(tag, NULL) | 29 | #define xs_html_render(tag) xs_html_render_s(tag, NULL) |
| @@ -31,6 +34,7 @@ xs_str *xs_html_render_s(xs_html *tag, char *prefix); | |||
| 31 | typedef enum { | 34 | typedef enum { |
| 32 | XS_HTML_TAG, | 35 | XS_HTML_TAG, |
| 33 | XS_HTML_SCTAG, | 36 | XS_HTML_SCTAG, |
| 37 | XS_HTML_CONTAINER, | ||
| 34 | XS_HTML_ATTR, | 38 | XS_HTML_ATTR, |
| 35 | XS_HTML_TEXT | 39 | XS_HTML_TEXT |
| 36 | } xs_html_type; | 40 | } xs_html_type; |
| @@ -75,9 +79,7 @@ xs_str *xs_html_encode(char *str) | |||
| 75 | 79 | ||
| 76 | /* insert the escaped char */ | 80 | /* insert the escaped char */ |
| 77 | char tmp[8]; | 81 | char tmp[8]; |
| 78 | snprintf(tmp, sizeof(tmp), "&#%d;", *q); | 82 | z = snprintf(tmp, sizeof(tmp), "&#%d;", *q); |
| 79 | |||
| 80 | z = strlen(tmp); | ||
| 81 | s = xs_insert_m(s, o, tmp, z); | 83 | s = xs_insert_m(s, o, tmp, z); |
| 82 | o += z; | 84 | o += z; |
| 83 | 85 | ||
| @@ -192,6 +194,12 @@ xs_html *_xs_html_sctag(char *tag, xs_html *var[]) | |||
| 192 | } | 194 | } |
| 193 | 195 | ||
| 194 | 196 | ||
| 197 | xs_html *_xs_html_container(xs_html *var[]) | ||
| 198 | { | ||
| 199 | return _xs_html_tag_t(XS_HTML_CONTAINER, NULL, var); | ||
| 200 | } | ||
| 201 | |||
| 202 | |||
| 195 | void xs_html_render_f(xs_html *h, FILE *f) | 203 | void xs_html_render_f(xs_html *h, FILE *f) |
| 196 | /* renders the tag and its subtags into a file */ | 204 | /* renders the tag and its subtags into a file */ |
| 197 | { | 205 | { |
| @@ -200,8 +208,7 @@ void xs_html_render_f(xs_html *h, FILE *f) | |||
| 200 | switch (h->type) { | 208 | switch (h->type) { |
| 201 | case XS_HTML_TAG: | 209 | case XS_HTML_TAG: |
| 202 | case XS_HTML_SCTAG: | 210 | case XS_HTML_SCTAG: |
| 203 | if (h->content) | 211 | fprintf(f, "<%s", h->content); |
| 204 | fprintf(f, "<%s", h->content); | ||
| 205 | 212 | ||
| 206 | /* render the attributes */ | 213 | /* render the attributes */ |
| 207 | st = h->f_attr; | 214 | st = h->f_attr; |
| @@ -213,12 +220,10 @@ void xs_html_render_f(xs_html *h, FILE *f) | |||
| 213 | 220 | ||
| 214 | if (h->type == XS_HTML_SCTAG) { | 221 | if (h->type == XS_HTML_SCTAG) { |
| 215 | /* self-closing tags should not have subtags */ | 222 | /* self-closing tags should not have subtags */ |
| 216 | if (h->content) | 223 | fprintf(f, "/>"); |
| 217 | fprintf(f, "/>"); | ||
| 218 | } | 224 | } |
| 219 | else { | 225 | else { |
| 220 | if (h->content) | 226 | fprintf(f, ">"); |
| 221 | fprintf(f, ">"); | ||
| 222 | 227 | ||
| 223 | /* render the subtags */ | 228 | /* render the subtags */ |
| 224 | st = h->f_tag; | 229 | st = h->f_tag; |
| @@ -228,8 +233,18 @@ void xs_html_render_f(xs_html *h, FILE *f) | |||
| 228 | st = nst; | 233 | st = nst; |
| 229 | } | 234 | } |
| 230 | 235 | ||
| 231 | if (h->content) | 236 | fprintf(f, "</%s>", h->content); |
| 232 | fprintf(f, "</%s>", h->content); | 237 | } |
| 238 | |||
| 239 | break; | ||
| 240 | |||
| 241 | case XS_HTML_CONTAINER: | ||
| 242 | /* render the subtags and nothing more */ | ||
| 243 | st = h->f_tag; | ||
| 244 | while (st) { | ||
| 245 | xs_html *nst = st->next; | ||
| 246 | xs_html_render_f(st, f); | ||
| 247 | st = nst; | ||
| 233 | } | 248 | } |
| 234 | 249 | ||
| 235 | break; | 250 | break; |