summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--html.c78
-rw-r--r--xs_html.h44
-rw-r--r--xs_version.h2
3 files changed, 71 insertions, 53 deletions
diff --git a/html.c b/html.c
index 5195ba7..43973e8 100644
--- a/html.c
+++ b/html.c
@@ -95,7 +95,7 @@ xs_str *actor_name(xs_dict *actor)
95xs_str *html_actor_icon(xs_dict *actor, const char *date, 95xs_str *html_actor_icon(xs_dict *actor, const char *date,
96 const char *udate, const char *url, int priv) 96 const char *udate, const char *url, int priv)
97{ 97{
98 xs_str *s = xs_str_new(NULL); 98 xs_html *actor_icon = xs_html_tag("p", NULL);
99 99
100 xs *avatar = NULL; 100 xs *avatar = NULL;
101 char *v; 101 char *v;
@@ -111,31 +111,45 @@ xs_str *html_actor_icon(xs_dict *actor, const char *date,
111 if (avatar == NULL) 111 if (avatar == NULL)
112 avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64()); 112 avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
113 113
114 { 114 xs_html_add(actor_icon,
115 xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" loading=\"lazy\" " 115 xs_html_sctag("img",
116 "src=\"%s\" alt=\"\"/>\n", avatar); 116 xs_html_attr("loading", "lazy"),
117 s = xs_str_cat(s, s1); 117 xs_html_attr("class", "snac-avatar"),
118 } 118 xs_html_attr("src", avatar),
119 xs_html_attr("alt", "")),
120 xs_html_tag("a",
121 xs_html_attr("href", xs_dict_get(actor, "id")),
122 xs_html_attr("class", "p-author h-card snac-author"),
123 xs_html_raw(name))); /* name is already html-escaped */
119 124
120 {
121 xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
122 xs_dict_get(actor, "id"), name);
123 s = xs_str_cat(s, s1);
124 }
125 125
126 if (!xs_is_null(url)) { 126 if (!xs_is_null(url)) {
127 xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", url); 127 xs_html_add(actor_icon,
128 s = xs_str_cat(s, s1); 128 xs_html_text(" "),
129 xs_html_tag("a",
130 xs_html_attr("href", (char *)url),
131 xs_html_text("»")));
129 } 132 }
130 133
131 if (priv) 134 if (priv) {
132 s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>"); 135 xs_html_add(actor_icon,
136 xs_html_text(" "),
137 xs_html_tag("span",
138 xs_html_attr("title", "private"),
139 xs_html_raw("&#128274;")));
140 }
133 141
134 if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) 142 if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
135 s = xs_str_cat(s, " <span title=\"bot\">&#129302;</span>"); 143 xs_html_add(actor_icon,
144 xs_html_text(" "),
145 xs_html_tag("span",
146 xs_html_attr("title", "bot"),
147 xs_html_raw("&#129302;")));
148 }
136 149
137 if (xs_is_null(date)) { 150 if (xs_is_null(date)) {
138 s = xs_str_cat(s, "\n&nbsp;\n"); 151 xs_html_add(actor_icon,
152 xs_html_raw("&nbsp;"));
139 } 153 }
140 else { 154 else {
141 xs *date_label = xs_crop_i(xs_dup(date), 0, 10); 155 xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
@@ -149,18 +163,16 @@ xs_str *html_actor_icon(xs_dict *actor, const char *date,
149 date_title = xs_str_cat(date_title, " / ", udate); 163 date_title = xs_str_cat(date_title, " / ", udate);
150 } 164 }
151 165
152 xs *edt = encode_html(date_title); 166 xs_html_add(actor_icon,
153 xs *edl = encode_html(date_label); 167 xs_html_text(" "),
154 xs *s1 = xs_fmt( 168 xs_html_tag("time",
155 "\n<time class=\"dt-published snac-pubdate\" title=\"%s\">%s</time>\n", 169 xs_html_attr("class", "dt-published snac-pubdate"),
156 edt, edl); 170 xs_html_attr("title", date_title),
157 171 xs_html_text(date_label)));
158 s = xs_str_cat(s, s1);
159 } 172 }
160 173
161 { 174 {
162 char *username, *id; 175 char *username, *id;
163 xs *s1;
164 176
165 if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') { 177 if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
166 /* This should never be reached */ 178 /* This should never be reached */
@@ -176,15 +188,15 @@ xs_str *html_actor_icon(xs_dict *actor, const char *date,
176 xs *domain = xs_split(id, "/"); 188 xs *domain = xs_split(id, "/");
177 xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2)); 189 xs *user = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
178 190
179 xs *u1 = encode_html(user); 191 xs_html_add(actor_icon,
180 s1 = xs_fmt( 192 xs_html_sctag("br", NULL),
181 "<br><a href=\"%s\" class=\"p-author-tag h-card snac-author-tag\">%s</a>", 193 xs_html_tag("a",
182 xs_dict_get(actor, "id"), u1); 194 xs_html_attr("href", xs_dict_get(actor, "id")),
183 195 xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
184 s = xs_str_cat(s, s1); 196 xs_html_text(user)));
185 } 197 }
186 198
187 return s; 199 return xs_html_render(actor_icon);
188} 200}
189 201
190 202
diff --git a/xs_html.h b/xs_html.h
index 744df5b..d34a7e0 100644
--- a/xs_html.h
+++ b/xs_html.h
@@ -12,12 +12,15 @@ xs_html *xs_html_attr(char *key, char *value);
12xs_html *xs_html_text(char *content); 12xs_html *xs_html_text(char *content);
13xs_html *xs_html_raw(char *content); 13xs_html *xs_html_raw(char *content);
14 14
15xs_html *xs_html_add(xs_html *tag, xs_html *data); 15xs_html *_xs_html_add(xs_html *tag, xs_html *var[]);
16#define xs_html_add(tag, ...) _xs_html_add(tag, (xs_html *[]) { __VA_ARGS__, NULL })
16 17
17xs_html *_xs_html_tag(char *tag, xs_html *var[]); 18xs_html *_xs_html_tag(char *tag, xs_html *var[]);
18#define xs_html_tag(tag, ...) _xs_html_tag(tag, (xs_html *[]) { __VA_ARGS__, NULL }) 19#define xs_html_tag(tag, ...) _xs_html_tag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
20
19xs_html *_xs_html_sctag(char *tag, xs_html *var[]); 21xs_html *_xs_html_sctag(char *tag, xs_html *var[]);
20#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
21xs_str *_xs_html_render(xs_html *h, xs_str *s); 24xs_str *_xs_html_render(xs_html *h, xs_str *s);
22#define xs_html_render(h) _xs_html_render(h, xs_str_new(NULL)) 25#define xs_html_render(h) _xs_html_render(h, xs_str_new(NULL))
23 26
@@ -127,28 +130,32 @@ xs_html *xs_html_raw(char *content)
127} 130}
128 131
129 132
130xs_html *xs_html_add(xs_html *tag, xs_html *data) 133xs_html *_xs_html_add(xs_html *tag, xs_html *var[])
131/* add data (attrs, tags or text) to a tag */ 134/* add data (attrs, tags or text) to a tag */
132{ 135{
133 xs_html **first; 136 while (*var) {
134 xs_html **last; 137 xs_html *data = *var++;
135 138
136 if (data->type == XS_HTML_ATTR) { 139 xs_html **first;
137 first = &tag->f_attr; 140 xs_html **last;
138 last = &tag->l_attr;
139 }
140 else {
141 first = &tag->f_tag;
142 last = &tag->l_tag;
143 }
144 141
145 if (*first == NULL) 142 if (data->type == XS_HTML_ATTR) {
146 *first = data; 143 first = &tag->f_attr;
144 last = &tag->l_attr;
145 }
146 else {
147 first = &tag->f_tag;
148 last = &tag->l_tag;
149 }
147 150
148 if (*last != NULL) 151 if (*first == NULL)
149 (*last)->next = data; 152 *first = data;
150 153
151 *last = data; 154 if (*last != NULL)
155 (*last)->next = data;
156
157 *last = data;
158 }
152 159
153 return tag; 160 return tag;
154} 161}
@@ -162,8 +169,7 @@ static xs_html *_xs_html_tag_t(xs_html_type type, char *tag, xs_html *var[])
162 a->type = type; 169 a->type = type;
163 a->content = xs_dup(tag); 170 a->content = xs_dup(tag);
164 171
165 while (*var) 172 _xs_html_add(a, var);
166 xs_html_add(a, *var++);
167 173
168 return a; 174 return a;
169} 175}
diff --git a/xs_version.h b/xs_version.h
index b9b734b..fd4dd8f 100644
--- a/xs_version.h
+++ b/xs_version.h
@@ -1 +1 @@
/* 63beb583926bb5dfec89e1d694172cc887614460 2023-11-19T19:51:05+01:00 */ /* d9404322f5bad91811bc0ad13d63360b586919cc 2023-11-20T20:13:34+01:00 */