snac2/xs_html.h

264 lines
5.4 KiB
C
Raw Normal View History

2024-01-04 11:22:03 +03:00
/* copyright (c) 2022 - 2024 grunfink et al. / MIT license */
#ifndef _XS_HTML_H
#define _XS_HTML_H
typedef struct xs_html xs_html;
2024-05-21 15:12:15 +03:00
xs_str *xs_html_encode(const char *str);
2024-05-21 15:12:15 +03:00
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);
2023-11-20 22:28:20 +03:00
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 })
2024-05-21 15:12:15 +03:00
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 })
2023-11-20 22:28:20 +03:00
2024-05-21 15:12:15 +03:00
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 })
2023-11-20 22:28:20 +03:00
2023-12-03 19:26:50 +03:00
xs_html *_xs_html_container(xs_html *var[]);
#define xs_html_container(...) _xs_html_container((xs_html *[]) { __VA_ARGS__, NULL })
2023-11-27 12:19:01 +03:00
void xs_html_render_f(xs_html *h, FILE *f);
2024-05-21 15:12:15 +03:00
xs_str *xs_html_render_s(xs_html *tag, const char *prefix);
2023-11-27 12:19:01 +03:00
#define xs_html_render(tag) xs_html_render_s(tag, NULL)
#ifdef XS_IMPLEMENTATION
typedef enum {
XS_HTML_TAG,
XS_HTML_SCTAG,
2023-12-03 19:26:50 +03:00
XS_HTML_CONTAINER,
XS_HTML_ATTR,
XS_HTML_TEXT
} xs_html_type;
struct xs_html {
xs_html_type type;
xs_str *content;
2023-12-08 08:13:08 +03:00
xs_html *attrs;
xs_html *tags;
xs_html *next;
};
2024-05-21 15:12:15 +03:00
xs_str *xs_html_encode(const char *str)
/* encodes str using HTML entities */
{
xs_str *s = xs_str_new(NULL);
int o = 0;
2024-05-21 15:12:15 +03:00
const char *e = str + strlen(str);
for (;;) {
char *ec = "<>\"'&"; /* characters to escape */
2024-05-21 15:12:15 +03:00
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];
2023-12-03 19:26:50 +03:00
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))
2024-05-21 15:12:15 +03:00
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;
}
2024-05-21 15:12:15 +03:00
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_html_encode(content);
return a;
}
2024-05-21 15:12:15 +03:00
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_dup(content);
return a;
}
2023-11-20 22:28:20 +03:00
xs_html *_xs_html_add(xs_html *tag, xs_html *var[])
/* add data (attrs, tags or text) to a tag */
{
2023-11-20 22:28:20 +03:00
while (*var) {
xs_html *data = *var++;
2023-11-20 22:28:20 +03:00
if (data->type == XS_HTML_ATTR) {
2023-12-08 08:13:08 +03:00
data->next = tag->attrs;
tag->attrs = data;
2023-11-20 22:28:20 +03:00
}
else {
2023-12-08 08:13:08 +03:00
data->next = tag->tags;
tag->tags = data;
2023-11-20 22:28:20 +03:00
}
}
return tag;
}
2024-05-21 15:12:15 +03:00
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();
2023-12-03 13:43:40 +03:00
a->type = type;
/* a tag can be NULL, to be a kind of 'wrapper' */
if (tag)
a->content = xs_dup(tag);
2023-11-20 22:28:20 +03:00
_xs_html_add(a, var);
return a;
}
2024-05-21 15:12:15 +03:00
xs_html *_xs_html_tag(const char *tag, xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_TAG, tag, var);
}
2024-05-21 15:12:15 +03:00
xs_html *_xs_html_sctag(const char *tag, xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_SCTAG, tag, var);
}
2023-12-03 19:26:50 +03:00
xs_html *_xs_html_container(xs_html *var[])
{
return _xs_html_tag_t(XS_HTML_CONTAINER, NULL, var);
}
2023-11-27 12:19:01 +03:00
void xs_html_render_f(xs_html *h, FILE *f)
/* renders the tag and its subtags into a file */
{
2023-12-04 01:47:25 +03:00
if (h == NULL)
return;
2023-12-08 08:13:08 +03:00
/* follow the chain */
xs_html_render_f(h->next, f);
switch (h->type) {
case XS_HTML_TAG:
2023-12-03 19:26:50 +03:00
fprintf(f, "<%s", h->content);
2023-12-04 01:47:25 +03:00
/* attributes */
2023-12-08 08:13:08 +03:00
xs_html_render_f(h->attrs, f);
2023-12-04 01:47:25 +03:00
fprintf(f, ">");
2023-12-04 01:47:25 +03:00
/* sub-tags */
2023-12-08 08:13:08 +03:00
xs_html_render_f(h->tags, f);
2023-12-04 01:47:25 +03:00
fprintf(f, "</%s>", h->content);
break;
case XS_HTML_SCTAG:
fprintf(f, "<%s", h->content);
2023-12-03 19:26:50 +03:00
2023-12-04 01:47:25 +03:00
/* attributes */
2023-12-08 08:13:08 +03:00
xs_html_render_f(h->attrs, f);
2023-12-04 01:47:25 +03:00
fprintf(f, "/>");
2023-12-03 19:26:50 +03:00
break;
case XS_HTML_CONTAINER:
2023-12-04 01:47:25 +03:00
/* sub-tags */
2023-12-08 08:13:08 +03:00
xs_html_render_f(h->tags, f);
break;
case XS_HTML_ATTR:
2023-12-04 01:47:25 +03:00
fprintf(f, " ");
/* fallthrough */
case XS_HTML_TEXT:
2023-11-27 12:19:01 +03:00
fprintf(f, "%s", h->content);
break;
}
xs_free(h->content);
xs_free(h);
2023-11-27 12:19:01 +03:00
}
2024-05-21 15:12:15 +03:00
xs_str *xs_html_render_s(xs_html *tag, const char *prefix)
2023-11-27 12:19:01 +03:00
/* 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 */