commit 1ad4a93

default  ·  2023-11-20 19:28:20 +0000 UTC
parent 4fe2ccd
html_actor_icon() uses xs_html.
3 files changed,  +71, -53
M html.c
M html.c
+45, -33
  1@@ -95,7 +95,7 @@ xs_str *actor_name(xs_dict *actor)
  2 xs_str *html_actor_icon(xs_dict *actor, const char *date,
  3                         const char *udate, const char *url, int priv)
  4 {
  5-    xs_str *s = xs_str_new(NULL);
  6+    xs_html *actor_icon = xs_html_tag("p", NULL);
  7 
  8     xs *avatar = NULL;
  9     char *v;
 10@@ -111,31 +111,45 @@ xs_str *html_actor_icon(xs_dict *actor, const char *date,
 11     if (avatar == NULL)
 12         avatar = xs_fmt("data:image/png;base64, %s", default_avatar_base64());
 13 
 14-    {
 15-        xs *s1 = xs_fmt("<p><img class=\"snac-avatar\" loading=\"lazy\" "
 16-                        "src=\"%s\" alt=\"\"/>\n", avatar);
 17-        s = xs_str_cat(s, s1);
 18-    }
 19+    xs_html_add(actor_icon,
 20+        xs_html_sctag("img",
 21+            xs_html_attr("loading", "lazy"),
 22+            xs_html_attr("class",   "snac-avatar"),
 23+            xs_html_attr("src",     avatar),
 24+            xs_html_attr("alt",     "")),
 25+        xs_html_tag("a",
 26+            xs_html_attr("href",    xs_dict_get(actor, "id")),
 27+            xs_html_attr("class",   "p-author h-card snac-author"),
 28+            xs_html_raw(name))); /* name is already html-escaped */
 29 
 30-    {
 31-        xs *s1 = xs_fmt("<a href=\"%s\" class=\"p-author h-card snac-author\">%s</a>",
 32-            xs_dict_get(actor, "id"), name);
 33-        s = xs_str_cat(s, s1);
 34-    }
 35 
 36     if (!xs_is_null(url)) {
 37-        xs *s1 = xs_fmt(" <a href=\"%s\">»</a>", url);
 38-        s = xs_str_cat(s, s1);
 39+        xs_html_add(actor_icon,
 40+            xs_html_text(" "),
 41+            xs_html_tag("a",
 42+                xs_html_attr("href", (char *)url),
 43+                xs_html_text("»")));
 44     }
 45 
 46-    if (priv)
 47-        s = xs_str_cat(s, " <span title=\"private\">&#128274;</span>");
 48+    if (priv) {
 49+        xs_html_add(actor_icon,
 50+            xs_html_text(" "),
 51+            xs_html_tag("span",
 52+                xs_html_attr("title", "private"),
 53+                xs_html_raw("&#128274;")));
 54+    }
 55 
 56-    if (strcmp(xs_dict_get(actor, "type"), "Service") == 0)
 57-        s = xs_str_cat(s, " <span title=\"bot\">&#129302;</span>");
 58+    if (strcmp(xs_dict_get(actor, "type"), "Service") == 0) {
 59+        xs_html_add(actor_icon,
 60+            xs_html_text(" "),
 61+            xs_html_tag("span",
 62+                xs_html_attr("title", "bot"),
 63+                xs_html_raw("&#129302;")));
 64+    }
 65 
 66     if (xs_is_null(date)) {
 67-        s = xs_str_cat(s, "\n&nbsp;\n");
 68+        xs_html_add(actor_icon,
 69+            xs_html_raw("&nbsp;"));
 70     }
 71     else {
 72         xs *date_label = xs_crop_i(xs_dup(date), 0, 10);
 73@@ -149,18 +163,16 @@ xs_str *html_actor_icon(xs_dict *actor, const char *date,
 74             date_title = xs_str_cat(date_title, " / ", udate);
 75         }
 76 
 77-        xs *edt = encode_html(date_title);
 78-        xs *edl = encode_html(date_label);
 79-        xs *s1 = xs_fmt(
 80-            "\n<time class=\"dt-published snac-pubdate\" title=\"%s\">%s</time>\n",
 81-                edt, edl);
 82-
 83-        s = xs_str_cat(s, s1);
 84+        xs_html_add(actor_icon,
 85+            xs_html_text(" "),
 86+            xs_html_tag("time",
 87+                xs_html_attr("class", "dt-published snac-pubdate"),
 88+                xs_html_attr("title", date_title),
 89+                xs_html_text(date_label)));
 90     }
 91 
 92     {
 93         char *username, *id;
 94-        xs *s1;
 95 
 96         if (xs_is_null(username = xs_dict_get(actor, "preferredUsername")) || *username == '\0') {
 97             /* This should never be reached */
 98@@ -176,15 +188,15 @@ xs_str *html_actor_icon(xs_dict *actor, const char *date,
 99         xs *domain = xs_split(id, "/");
100         xs *user   = xs_fmt("@%s@%s", username, xs_list_get(domain, 2));
101 
102-        xs *u1 = encode_html(user);
103-        s1 = xs_fmt(
104-            "<br><a href=\"%s\" class=\"p-author-tag h-card snac-author-tag\">%s</a>",
105-                xs_dict_get(actor, "id"), u1);
106-
107-        s = xs_str_cat(s, s1);
108+        xs_html_add(actor_icon,
109+            xs_html_sctag("br", NULL),
110+            xs_html_tag("a",
111+                xs_html_attr("href",  xs_dict_get(actor, "id")),
112+                xs_html_attr("class", "p-author-tag h-card snac-author-tag"),
113+                xs_html_text(user)));
114     }
115 
116-    return s;
117+    return xs_html_render(actor_icon);
118 }
119 
120 
+25, -19
 1@@ -12,12 +12,15 @@ xs_html *xs_html_attr(char *key, char *value);
 2 xs_html *xs_html_text(char *content);
 3 xs_html *xs_html_raw(char *content);
 4 
 5-xs_html *xs_html_add(xs_html *tag, xs_html *data);
 6+xs_html *_xs_html_add(xs_html *tag, xs_html *var[]);
 7+#define xs_html_add(tag, ...) _xs_html_add(tag, (xs_html *[]) { __VA_ARGS__, NULL })
 8 
 9 xs_html *_xs_html_tag(char *tag, xs_html *var[]);
10 #define xs_html_tag(tag, ...) _xs_html_tag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
11+
12 xs_html *_xs_html_sctag(char *tag, xs_html *var[]);
13 #define xs_html_sctag(tag, ...) _xs_html_sctag(tag, (xs_html *[]) { __VA_ARGS__, NULL })
14+
15 xs_str *_xs_html_render(xs_html *h, xs_str *s);
16 #define xs_html_render(h) _xs_html_render(h, xs_str_new(NULL))
17 
18@@ -127,28 +130,32 @@ xs_html *xs_html_raw(char *content)
19 }
20 
21 
22-xs_html *xs_html_add(xs_html *tag, xs_html *data)
23+xs_html *_xs_html_add(xs_html *tag, xs_html *var[])
24 /* add data (attrs, tags or text) to a tag */
25 {
26-    xs_html **first;
27-    xs_html **last;
28+    while (*var) {
29+        xs_html *data = *var++;
30 
31-    if (data->type == XS_HTML_ATTR) {
32-        first = &tag->f_attr;
33-        last  = &tag->l_attr;
34-    }
35-    else {
36-        first = &tag->f_tag;
37-        last  = &tag->l_tag;
38-    }
39+        xs_html **first;
40+        xs_html **last;
41 
42-    if (*first == NULL)
43-        *first = data;
44+        if (data->type == XS_HTML_ATTR) {
45+            first = &tag->f_attr;
46+            last  = &tag->l_attr;
47+        }
48+        else {
49+            first = &tag->f_tag;
50+            last  = &tag->l_tag;
51+        }
52 
53-    if (*last != NULL)
54-        (*last)->next = data;
55+        if (*first == NULL)
56+            *first = data;
57 
58-    *last = data;
59+        if (*last != NULL)
60+            (*last)->next = data;
61+
62+        *last = data;
63+    }
64 
65     return tag;
66 }
67@@ -162,8 +169,7 @@ static xs_html *_xs_html_tag_t(xs_html_type type, char *tag, xs_html *var[])
68     a->type    = type;
69     a->content = xs_dup(tag);
70 
71-    while (*var)
72-        xs_html_add(a, *var++);
73+    _xs_html_add(a, var);
74 
75     return a;
76 }
+1, -1
1@@ -1 +1 @@
2-/* 63beb583926bb5dfec89e1d694172cc887614460 2023-11-19T19:51:05+01:00 */
3+/* d9404322f5bad91811bc0ad13d63360b586919cc 2023-11-20T20:13:34+01:00 */