commit d1b2fcf

default  ·  2023-11-29 12:16:24 +0000 UTC
parent 9419b80
New function replace_shortnames().
1 files changed,  +34, -63
M html.c
M html.c
+34, -63
  1@@ -41,37 +41,24 @@ int login(snac *snac, const xs_dict *headers)
  2 }
  3 
  4 
  5-xs_str *actor_name(xs_dict *actor)
  6-/* gets the actor name */
  7+xs_str *replace_shortnames(xs_str *s, xs_list *tag)
  8+/* replaces all the :shortnames: with the emojis in tag */
  9 {
 10-    xs_list *p;
 11-    char *v;
 12-    xs_str *name;
 13-
 14-    if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
 15-        if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
 16-            v = "anonymous";
 17-        }
 18-    }
 19-
 20-    name = encode_html(v);
 21-
 22-    /* replace the :shortnames: */
 23-    if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
 24-        xs *tag = NULL;
 25-        if (xs_type(p) == XSTYPE_DICT) {
 26+    if (!xs_is_null(tag)) {
 27+        xs *tag_list = NULL;
 28+        if (xs_type(tag) == XSTYPE_DICT) {
 29             /* not a list */
 30-            tag = xs_list_new();
 31-            tag = xs_list_append(tag, p);
 32+            tag_list = xs_list_new();
 33+            tag_list = xs_list_append(tag_list, tag);
 34         } else {
 35             /* is a list */
 36-            tag = xs_dup(p);
 37+            tag_list = xs_dup(tag);
 38         }
 39 
 40-        xs_list *tags = tag;
 41+        xs_list *p = tag_list;
 42+        char *v;
 43 
 44-        /* iterate the tags */
 45-        while (xs_list_iter(&tags, &v)) {
 46+        while (xs_list_iter(&p, &v)) {
 47             char *t = xs_dict_get(v, "type");
 48 
 49             if (t && strcmp(t, "Emoji") == 0) {
 50@@ -80,16 +67,34 @@ xs_str *actor_name(xs_dict *actor)
 51 
 52                 if (n && i) {
 53                     char *u = xs_dict_get(i, "url");
 54-                    xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" "
 55-                        "style=\"height: 1em; vertical-align: middle;\"/>", u);
 56+                    xs_html *img = xs_html_sctag("img",
 57+                        xs_html_attr("loading", "lazy"),
 58+                        xs_html_attr("src", u),
 59+                        xs_html_attr("style", "height: 1em; vertical-align: middle;"));
 60 
 61-                    name = xs_replace_i(name, n, img);
 62+                    xs *s1 = xs_html_render(img);
 63+                    s = xs_replace_i(s, n, s1);
 64                 }
 65             }
 66         }
 67     }
 68 
 69-    return name;
 70+    return s;
 71+}
 72+
 73+
 74+xs_str *actor_name(xs_dict *actor)
 75+/* gets the actor name */
 76+{
 77+    char *v;
 78+
 79+    if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
 80+        if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
 81+            v = "anonymous";
 82+        }
 83+    }
 84+
 85+    return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"));
 86 }
 87 
 88 
 89@@ -1413,7 +1418,6 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
 90         char *content = xs_dict_get(msg, "content");
 91 
 92         xs *c = sanitize(xs_is_null(content) ? "" : content);
 93-        char *p, *v;
 94 
 95         /* do some tweaks to the content */
 96         c = xs_replace_i(c, "\r", "");
 97@@ -1429,40 +1433,7 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
 98         }
 99 
100         /* replace the :shortnames: */
101-        if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
102-            xs *tag = NULL;
103-            if (xs_type(p) == XSTYPE_DICT) {
104-                /* not a list */
105-                tag = xs_list_new();
106-                tag = xs_list_append(tag, p);
107-            }
108-            else
109-            if (xs_type(p) == XSTYPE_LIST)
110-                tag = xs_dup(p);
111-            else
112-                tag = xs_list_new();
113-
114-            xs_list *tags = tag;
115-
116-            /* iterate the tags */
117-            while (xs_list_iter(&tags, &v)) {
118-                char *t = xs_dict_get(v, "type");
119-
120-                if (t && strcmp(t, "Emoji") == 0) {
121-                    char *n = xs_dict_get(v, "name");
122-                    char *i = xs_dict_get(v, "icon");
123-
124-                    if (n && i) {
125-                        char *u = xs_dict_get(i, "url");
126-                        xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" "
127-                                        "style=\"height: 2em; vertical-align: middle;\" "
128-                                        "title=\"%s\"/>", u, n);
129-
130-                        c = xs_replace_i(c, n, img);
131-                    }
132-                }
133-            }
134-        }
135+        c = replace_shortnames(c, xs_dict_get(msg, "tag"));
136 
137         /* c contains sanitized HTML */
138         xs_html_add(snac_content,