mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-15 01:55:03 +00:00
New function replace_shortnames().
This commit is contained in:
parent
9419b80809
commit
d1b2fcf426
1 changed files with 34 additions and 63 deletions
97
html.c
97
html.c
|
@ -41,37 +41,24 @@ int login(snac *snac, const xs_dict *headers)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
xs_str *actor_name(xs_dict *actor)
|
xs_str *replace_shortnames(xs_str *s, xs_list *tag)
|
||||||
/* gets the actor name */
|
/* replaces all the :shortnames: with the emojis in tag */
|
||||||
{
|
{
|
||||||
xs_list *p;
|
if (!xs_is_null(tag)) {
|
||||||
char *v;
|
xs *tag_list = NULL;
|
||||||
xs_str *name;
|
if (xs_type(tag) == XSTYPE_DICT) {
|
||||||
|
|
||||||
if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
|
|
||||||
if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
|
|
||||||
v = "anonymous";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
name = encode_html(v);
|
|
||||||
|
|
||||||
/* replace the :shortnames: */
|
|
||||||
if (!xs_is_null(p = xs_dict_get(actor, "tag"))) {
|
|
||||||
xs *tag = NULL;
|
|
||||||
if (xs_type(p) == XSTYPE_DICT) {
|
|
||||||
/* not a list */
|
/* not a list */
|
||||||
tag = xs_list_new();
|
tag_list = xs_list_new();
|
||||||
tag = xs_list_append(tag, p);
|
tag_list = xs_list_append(tag_list, tag);
|
||||||
} else {
|
} else {
|
||||||
/* is a list */
|
/* is a list */
|
||||||
tag = xs_dup(p);
|
tag_list = xs_dup(tag);
|
||||||
}
|
}
|
||||||
|
|
||||||
xs_list *tags = tag;
|
xs_list *p = tag_list;
|
||||||
|
char *v;
|
||||||
|
|
||||||
/* iterate the tags */
|
while (xs_list_iter(&p, &v)) {
|
||||||
while (xs_list_iter(&tags, &v)) {
|
|
||||||
char *t = xs_dict_get(v, "type");
|
char *t = xs_dict_get(v, "type");
|
||||||
|
|
||||||
if (t && strcmp(t, "Emoji") == 0) {
|
if (t && strcmp(t, "Emoji") == 0) {
|
||||||
|
@ -80,16 +67,34 @@ xs_str *actor_name(xs_dict *actor)
|
||||||
|
|
||||||
if (n && i) {
|
if (n && i) {
|
||||||
char *u = xs_dict_get(i, "url");
|
char *u = xs_dict_get(i, "url");
|
||||||
xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" "
|
xs_html *img = xs_html_sctag("img",
|
||||||
"style=\"height: 1em; vertical-align: middle;\"/>", u);
|
xs_html_attr("loading", "lazy"),
|
||||||
|
xs_html_attr("src", u),
|
||||||
|
xs_html_attr("style", "height: 1em; vertical-align: middle;"));
|
||||||
|
|
||||||
name = xs_replace_i(name, n, img);
|
xs *s1 = xs_html_render(img);
|
||||||
|
s = xs_replace_i(s, n, s1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return name;
|
return s;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
xs_str *actor_name(xs_dict *actor)
|
||||||
|
/* gets the actor name */
|
||||||
|
{
|
||||||
|
char *v;
|
||||||
|
|
||||||
|
if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') {
|
||||||
|
if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') {
|
||||||
|
v = "anonymous";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -1413,7 +1418,6 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
|
||||||
char *content = xs_dict_get(msg, "content");
|
char *content = xs_dict_get(msg, "content");
|
||||||
|
|
||||||
xs *c = sanitize(xs_is_null(content) ? "" : content);
|
xs *c = sanitize(xs_is_null(content) ? "" : content);
|
||||||
char *p, *v;
|
|
||||||
|
|
||||||
/* do some tweaks to the content */
|
/* do some tweaks to the content */
|
||||||
c = xs_replace_i(c, "\r", "");
|
c = xs_replace_i(c, "\r", "");
|
||||||
|
@ -1429,40 +1433,7 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local,
|
||||||
}
|
}
|
||||||
|
|
||||||
/* replace the :shortnames: */
|
/* replace the :shortnames: */
|
||||||
if (!xs_is_null(p = xs_dict_get(msg, "tag"))) {
|
c = replace_shortnames(c, xs_dict_get(msg, "tag"));
|
||||||
xs *tag = NULL;
|
|
||||||
if (xs_type(p) == XSTYPE_DICT) {
|
|
||||||
/* not a list */
|
|
||||||
tag = xs_list_new();
|
|
||||||
tag = xs_list_append(tag, p);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
if (xs_type(p) == XSTYPE_LIST)
|
|
||||||
tag = xs_dup(p);
|
|
||||||
else
|
|
||||||
tag = xs_list_new();
|
|
||||||
|
|
||||||
xs_list *tags = tag;
|
|
||||||
|
|
||||||
/* iterate the tags */
|
|
||||||
while (xs_list_iter(&tags, &v)) {
|
|
||||||
char *t = xs_dict_get(v, "type");
|
|
||||||
|
|
||||||
if (t && strcmp(t, "Emoji") == 0) {
|
|
||||||
char *n = xs_dict_get(v, "name");
|
|
||||||
char *i = xs_dict_get(v, "icon");
|
|
||||||
|
|
||||||
if (n && i) {
|
|
||||||
char *u = xs_dict_get(i, "url");
|
|
||||||
xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" "
|
|
||||||
"style=\"height: 2em; vertical-align: middle;\" "
|
|
||||||
"title=\"%s\"/>", u, n);
|
|
||||||
|
|
||||||
c = xs_replace_i(c, n, img);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/* c contains sanitized HTML */
|
/* c contains sanitized HTML */
|
||||||
xs_html_add(snac_content,
|
xs_html_add(snac_content,
|
||||||
|
|
Loading…
Reference in a new issue