diff options
| author | 2023-11-29 13:16:24 +0100 | |
|---|---|---|
| committer | 2023-11-29 13:16:24 +0100 | |
| commit | d1b2fcf4263c9bb8911aafe7a7f1d3b6c936b752 (patch) | |
| tree | fc92d199bb277e75d219eef7b49cf8b0b8926fe1 /html.c | |
| parent | Got rid of dead code. (diff) | |
| download | snac2-d1b2fcf4263c9bb8911aafe7a7f1d3b6c936b752.tar.gz snac2-d1b2fcf4263c9bb8911aafe7a7f1d3b6c936b752.tar.xz snac2-d1b2fcf4263c9bb8911aafe7a7f1d3b6c936b752.zip | |
New function replace_shortnames().
Diffstat (limited to 'html.c')
| -rw-r--r-- | html.c | 97 |
1 files changed, 34 insertions, 63 deletions
| @@ -41,37 +41,24 @@ int login(snac *snac, const xs_dict *headers) | |||
| 41 | } | 41 | } |
| 42 | 42 | ||
| 43 | 43 | ||
| 44 | xs_str *actor_name(xs_dict *actor) | 44 | xs_str *replace_shortnames(xs_str *s, xs_list *tag) |
| 45 | /* gets the actor name */ | 45 | /* replaces all the :shortnames: with the emojis in tag */ |
| 46 | { | 46 | { |
| 47 | xs_list *p; | 47 | if (!xs_is_null(tag)) { |
| 48 | char *v; | 48 | xs *tag_list = NULL; |
| 49 | xs_str *name; | 49 | if (xs_type(tag) == XSTYPE_DICT) { |
| 50 | |||
| 51 | if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') { | ||
| 52 | if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') { | ||
| 53 | v = "anonymous"; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | |||
| 57 | name = encode_html(v); | ||
| 58 | |||
| 59 | /* replace the :shortnames: */ | ||
| 60 | if (!xs_is_null(p = xs_dict_get(actor, "tag"))) { | ||
| 61 | xs *tag = NULL; | ||
| 62 | if (xs_type(p) == XSTYPE_DICT) { | ||
| 63 | /* not a list */ | 50 | /* not a list */ |
| 64 | tag = xs_list_new(); | 51 | tag_list = xs_list_new(); |
| 65 | tag = xs_list_append(tag, p); | 52 | tag_list = xs_list_append(tag_list, tag); |
| 66 | } else { | 53 | } else { |
| 67 | /* is a list */ | 54 | /* is a list */ |
| 68 | tag = xs_dup(p); | 55 | tag_list = xs_dup(tag); |
| 69 | } | 56 | } |
| 70 | 57 | ||
| 71 | xs_list *tags = tag; | 58 | xs_list *p = tag_list; |
| 59 | char *v; | ||
| 72 | 60 | ||
| 73 | /* iterate the tags */ | 61 | while (xs_list_iter(&p, &v)) { |
| 74 | while (xs_list_iter(&tags, &v)) { | ||
| 75 | char *t = xs_dict_get(v, "type"); | 62 | char *t = xs_dict_get(v, "type"); |
| 76 | 63 | ||
| 77 | if (t && strcmp(t, "Emoji") == 0) { | 64 | if (t && strcmp(t, "Emoji") == 0) { |
| @@ -80,16 +67,34 @@ xs_str *actor_name(xs_dict *actor) | |||
| 80 | 67 | ||
| 81 | if (n && i) { | 68 | if (n && i) { |
| 82 | char *u = xs_dict_get(i, "url"); | 69 | char *u = xs_dict_get(i, "url"); |
| 83 | xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" " | 70 | xs_html *img = xs_html_sctag("img", |
| 84 | "style=\"height: 1em; vertical-align: middle;\"/>", u); | 71 | xs_html_attr("loading", "lazy"), |
| 72 | xs_html_attr("src", u), | ||
| 73 | xs_html_attr("style", "height: 1em; vertical-align: middle;")); | ||
| 85 | 74 | ||
| 86 | name = xs_replace_i(name, n, img); | 75 | xs *s1 = xs_html_render(img); |
| 76 | s = xs_replace_i(s, n, s1); | ||
| 87 | } | 77 | } |
| 88 | } | 78 | } |
| 89 | } | 79 | } |
| 90 | } | 80 | } |
| 91 | 81 | ||
| 92 | return name; | 82 | return s; |
| 83 | } | ||
| 84 | |||
| 85 | |||
| 86 | xs_str *actor_name(xs_dict *actor) | ||
| 87 | /* gets the actor name */ | ||
| 88 | { | ||
| 89 | char *v; | ||
| 90 | |||
| 91 | if (xs_is_null((v = xs_dict_get(actor, "name"))) || *v == '\0') { | ||
| 92 | if (xs_is_null(v = xs_dict_get(actor, "preferredUsername")) || *v == '\0') { | ||
| 93 | v = "anonymous"; | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | return replace_shortnames(xs_html_encode(v), xs_dict_get(actor, "tag")); | ||
| 93 | } | 98 | } |
| 94 | 99 | ||
| 95 | 100 | ||
| @@ -1413,7 +1418,6 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local, | |||
| 1413 | char *content = xs_dict_get(msg, "content"); | 1418 | char *content = xs_dict_get(msg, "content"); |
| 1414 | 1419 | ||
| 1415 | xs *c = sanitize(xs_is_null(content) ? "" : content); | 1420 | xs *c = sanitize(xs_is_null(content) ? "" : content); |
| 1416 | char *p, *v; | ||
| 1417 | 1421 | ||
| 1418 | /* do some tweaks to the content */ | 1422 | /* do some tweaks to the content */ |
| 1419 | c = xs_replace_i(c, "\r", ""); | 1423 | c = xs_replace_i(c, "\r", ""); |
| @@ -1429,40 +1433,7 @@ xs_html *html_entry(snac *user, xs_dict *msg, int local, | |||
| 1429 | } | 1433 | } |
| 1430 | 1434 | ||
| 1431 | /* replace the :shortnames: */ | 1435 | /* replace the :shortnames: */ |
| 1432 | if (!xs_is_null(p = xs_dict_get(msg, "tag"))) { | 1436 | c = replace_shortnames(c, xs_dict_get(msg, "tag")); |
| 1433 | xs *tag = NULL; | ||
| 1434 | if (xs_type(p) == XSTYPE_DICT) { | ||
| 1435 | /* not a list */ | ||
| 1436 | tag = xs_list_new(); | ||
| 1437 | tag = xs_list_append(tag, p); | ||
| 1438 | } | ||
| 1439 | else | ||
| 1440 | if (xs_type(p) == XSTYPE_LIST) | ||
| 1441 | tag = xs_dup(p); | ||
| 1442 | else | ||
| 1443 | tag = xs_list_new(); | ||
| 1444 | |||
| 1445 | xs_list *tags = tag; | ||
| 1446 | |||
| 1447 | /* iterate the tags */ | ||
| 1448 | while (xs_list_iter(&tags, &v)) { | ||
| 1449 | char *t = xs_dict_get(v, "type"); | ||
| 1450 | |||
| 1451 | if (t && strcmp(t, "Emoji") == 0) { | ||
| 1452 | char *n = xs_dict_get(v, "name"); | ||
| 1453 | char *i = xs_dict_get(v, "icon"); | ||
| 1454 | |||
| 1455 | if (n && i) { | ||
| 1456 | char *u = xs_dict_get(i, "url"); | ||
| 1457 | xs *img = xs_fmt("<img loading=\"lazy\" src=\"%s\" " | ||
| 1458 | "style=\"height: 2em; vertical-align: middle;\" " | ||
| 1459 | "title=\"%s\"/>", u, n); | ||
| 1460 | |||
| 1461 | c = xs_replace_i(c, n, img); | ||
| 1462 | } | ||
| 1463 | } | ||
| 1464 | } | ||
| 1465 | } | ||
| 1466 | 1437 | ||
| 1467 | /* c contains sanitized HTML */ | 1438 | /* c contains sanitized HTML */ |
| 1468 | xs_html_add(snac_content, | 1439 | xs_html_add(snac_content, |