1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263
|
/* copyright (c) 2022 - 2025 grunfink et al. / MIT license */
#ifndef _XS_HTML_H
#define _XS_HTML_H
typedef struct xs_html xs_html;
xs_str *xs_html_encode(const char *str);
xs_html *xs_html_attr(const char *key, const char *value);
xs_html *xs_html_text(const char *content);
xs_html *xs_html_raw(const char *content);
xs_html *_xs_html_add(xs_html *tag, xs_html *var[]);
#define xs_html_add(tag, ...) _xs_html_add(tag, (xs_html *[]) { __VA_ARGS__, NULL })
xs_html *_xs_html_tag(const char *tag, xs_html *var[]);
#define xs_html_tag(tag, ...) _xs_html_tag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
xs_html *_xs_html_sctag(const char *tag, xs_html *var[]);
#define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
xs_html *_xs_html_container(xs_html *var[]);
#define xs_html_container(...) _xs_html_container((xs_html *[]) { __VA_ARGS__, NULL })
void xs_html_render_f(xs_html *h, FILE *f);
xs_str *xs_html_render_s(xs_html *tag, const char *prefix);
#define xs_html_render(tag) xs_html_render_s(tag, NULL)
#ifdef XS_IMPLEMENTATION
typedef enum {
XS_HTML_TAG,
XS_HTML_SCTAG,
XS_HTML_CONTAINER,
XS_HTML_ATTR,
XS_HTML_TEXT
} xs_html_type;
struct xs_html {
xs_html_type type;
xs_str *content;
xs_html *attrs;
xs_html *tags;
xs_html *next;
};
xs_str *xs_html_encode(const char *str)
/* encodes str using HTML entities */
{
xs_str *s = xs_str_new(NULL);
int o = 0;
const char *e = str + strlen(str);
for (;;) {
char *ec = "<>\"'&"; /* characters to escape */
const char *q = e;
int z;
/* find the nearest happening of a char */
while (*ec) {
char *m = memchr(str, *ec++, q - str);
if (m)
q = m;
}
/* copy string to here */
z = q - str;
s = xs_insert_m(s, o, str, z);
o += z;
/* if q points to the end, nothing more to do */
if (q == e)
break;
/* insert the escaped char */
char tmp[8];
z = snprintf(tmp, sizeof(tmp), "&#%d;", *q);
s = xs_insert_m(s, o, tmp, z);
o += z;
str = q + 1;
}
return s;
}
#define XS_HTML_NEW() memset(xs_realloc(NULL, sizeof(xs_html)), '\0', sizeof(xs_html))
xs_html *xs_html_attr(const char *key, const char *value)
/* creates an HTML block with an attribute */
{
xs_html *a = XS_HTML_NEW();
a->type = XS_HTML_ATTR;
if (value) {
xs *ev = xs_html_encode(value);
a->content = xs_fmt("%s=\"%s\"", key, ev);
}
else
a->content = xs_dup(key);
return a;
}
xs_html *xs_html_text(const char *content)
/* creates an HTML block of text, escaping it previously */
{
xs_html *a = XS_HTML_NEW();
a->type = XS_HTML_TEXT;
a->content = xs_is_string(content) ? xs_html_encode(content) : xs_str_new(NULL);
return a;
}
xs_html *xs_html_raw(const char *content)
/* creates an HTML block without escaping (for pre-formatted HTML, comments, etc) */
{
xs_html *a = XS_HTML_NEW();
a->type = XS_HTML_TEXT;
a->content = xs_is_string(content) ? xs_dup(content) : xs_str_new(NULL);
return a;
}
xs_html *_xs_html_add(xs_html *tag, xs_html *var[])
/* add data (attrs, tags or text) to a tag */
{
while (*var) {
xs_html *data = *var++;
if (data->type == XS_HTML_ATTR) {
data->next = tag->attrs;
tag->attrs = data;
}
else {
data->next = tag->tags;
tag->tags = data;
}
}
return tag;
}
static xs_html *_xs_html_tag_t(xs_html_type type, const char *tag, xs_html *var[])
/* creates a tag with a variable list of attributes and subtags */
{
xs_html *a = XS_HTML_NEW();
a->type = type;
/* a tag can be NULL, to be a kind of 'wrapper' */
if (tag)
a->content = xs_dup(tag);
_xs_html_add(a, var);
return a;
}
xs_html *_xs_html_tag(const char *tag, xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_TAG, tag, var);
}
xs_html *_xs_html_sctag(const char *tag, xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_SCTAG, tag, var);
}
xs_html *_xs_html_container(xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_CONTAINER, NULL, var);
}
void xs_html_render_f(xs_html *h, FILE *f)
/* renders the tag and its subtags into a file */
{
if (h == NULL)
return;
/* follow the chain */
xs_html_render_f(h->next, f);
switch (h->type) {
case XS_HTML_TAG:
fprintf(f, "<%s", h->content);
/* attributes */
xs_html_render_f(h->attrs, f);
fprintf(f, ">");
/* sub-tags */
xs_html_render_f(h->tags, f);
fprintf(f, "</%s>", h->content);
break;
case XS_HTML_SCTAG:
fprintf(f, "<%s", h->content);
/* attributes */
xs_html_render_f(h->attrs, f);
fprintf(f, "/>");
break;
case XS_HTML_CONTAINER:
/* sub-tags */
xs_html_render_f(h->tags, f);
break;
case XS_HTML_ATTR:
fprintf(f, " ");
/* fallthrough */
case XS_HTML_TEXT:
fprintf(f, "%s", h->content);
break;
}
xs_free(h->content);
xs_free(h);
}
xs_str *xs_html_render_s(xs_html *tag, const 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;
}
#endif /* XS_IMPLEMENTATION */
#endif /* _XS_HTML_H */
|