diff options
| author | 2023-11-20 20:28:20 +0100 | |
|---|---|---|
| committer | 2023-11-20 20:28:20 +0100 | |
| commit | 1ad4a936496868e770b709f33390c5abce0487da (patch) | |
| tree | f30b51eace2de2e68e8dd2bfc3efd3c1b052f9d9 | |
| parent | html_actor_icon() returns an xs_str instead of adding to another one. (diff) | |
| download | snac2-1ad4a936496868e770b709f33390c5abce0487da.tar.gz snac2-1ad4a936496868e770b709f33390c5abce0487da.tar.xz snac2-1ad4a936496868e770b709f33390c5abce0487da.zip | |
html_actor_icon() uses xs_html.
| -rw-r--r-- | html.c | 78 | ||||
| -rw-r--r-- | xs_html.h | 44 | ||||
| -rw-r--r-- | xs_version.h | 2 |
3 files changed, 71 insertions, 53 deletions
| @@ -95,7 +95,7 @@ xs_str *actor_name(xs_dict *actor) | |||
| 95 | xs_str *html_actor_icon(xs_dict *actor, const char *date, | 95 | xs_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\">🔒</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("🔒"))); | ||
| 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\">🤖</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("🤖"))); | ||
| 148 | } | ||
| 136 | 149 | ||
| 137 | if (xs_is_null(date)) { | 150 | if (xs_is_null(date)) { |
| 138 | s = xs_str_cat(s, "\n \n"); | 151 | xs_html_add(actor_icon, |
| 152 | xs_html_raw(" ")); | ||
| 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 | ||
| @@ -12,12 +12,15 @@ xs_html *xs_html_attr(char *key, char *value); | |||
| 12 | xs_html *xs_html_text(char *content); | 12 | xs_html *xs_html_text(char *content); |
| 13 | xs_html *xs_html_raw(char *content); | 13 | xs_html *xs_html_raw(char *content); |
| 14 | 14 | ||
| 15 | xs_html *xs_html_add(xs_html *tag, xs_html *data); | 15 | xs_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 | ||
| 17 | xs_html *_xs_html_tag(char *tag, xs_html *var[]); | 18 | xs_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 | |||
| 19 | xs_html *_xs_html_sctag(char *tag, xs_html *var[]); | 21 | xs_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 | |||
| 21 | xs_str *_xs_html_render(xs_html *h, xs_str *s); | 24 | xs_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 | ||
| 130 | xs_html *xs_html_add(xs_html *tag, xs_html *data) | 133 | xs_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 */ | ||