Add custom emoji support for plain text

This commit is contained in:
blankie 2023-11-23 11:32:21 +11:00
parent a090aeabcd
commit 1808df8f75
Signed by: blankie
GPG Key ID: CC15FC822C7F61F5
5 changed files with 23 additions and 12 deletions

View File

@ -90,7 +90,7 @@ CURL* MastodonClient::_get_easy() {
try { try {
setopt(curl, CURLOPT_FAILONERROR, 1L); setopt(curl, CURLOPT_FAILONERROR, 1L);
setopt(curl, CURLOPT_TIMEOUT_MS, 5000L); setopt(curl, CURLOPT_TIMEOUT_MS, 10000L);
setopt(curl, CURLOPT_PROTOCOLS_STR, "https"); setopt(curl, CURLOPT_PROTOCOLS_STR, "https");
setopt(curl, CURLOPT_USERAGENT, "Coyote (https://gitlab.com/blankX/coyote; blankie@nixnetmail.com)"); setopt(curl, CURLOPT_USERAGENT, "Coyote (https://gitlab.com/blankX/coyote; blankie@nixnetmail.com)");
setopt(curl, CURLOPT_SHARE, this->_share); setopt(curl, CURLOPT_SHARE, this->_share);

View File

@ -44,6 +44,8 @@ a:hover {
.custom_emoji { .custom_emoji {
height: 1em; height: 1em;
width: 1em; width: 1em;
/* https://stackoverflow.com/a/489394 */
vertical-align: middle;
} }
/* ERROR PAGE */ /* ERROR PAGE */
@ -66,14 +68,14 @@ a:hover {
object-fit: cover; object-fit: cover;
} }
.user_page-user_pfp { .user_page-main_header {
display: flex; display: flex;
} }
.user_page-user_pfp img { .user_page-profile {
width: 7.5em; width: 7.5em;
height: 7.5em; height: 7.5em;
} }
.user_page-user_pfp span { .user_page-main_header span {
margin-top: auto; margin-top: auto;
margin-bottom: auto; margin-bottom: auto;
margin-left: 0.5em; margin-left: 0.5em;

View File

@ -66,12 +66,12 @@ static inline Element user_header(const httplib::Request& req, const Account& ac
Element("a", {{"href", account.header}}, { Element("a", {{"href", account.header}}, {
Element("img", {{"class", "user_page-header"}, {"alt", "User header"}, {"src", account.header}}, {}), Element("img", {{"class", "user_page-header"}, {"alt", "User header"}, {"src", account.header}}, {}),
}), }),
Element("div", {{"class", "user_page-user_pfp"}}, { Element("div", {{"class", "user_page-main_header"}}, {
Element("a", {{"href", account.avatar}}, { Element("a", {{"href", account.avatar}}, {
Element("img", {{"alt", "User profile picture"}, {"src", account.avatar}}, {}), Element("img", {{"class", "user_page-profile"}, {"alt", "User profile picture"}, {"src", account.avatar}}, {}),
}), }),
Element("span", { Element("span", {
Element("b", {account.display_name}), " (", account.username, "@", account.domain_name, ")", Element("b", {preprocess_html(req, account.emojis, account.display_name)}), " (", account.username, "@", account.domain_name, ")",
Element("br"), Element("br"),
Element("br"), Element("b", {"Joined: "}), std::string(created_at_str), Element("br"), Element("b", {"Joined: "}), std::string(created_at_str),
Element("br"), Element("br"),
@ -100,7 +100,7 @@ static inline Element user_link_field(const httplib::Request& req, const Account
using namespace std::string_literals; using namespace std::string_literals;
Element tr("tr", { Element tr("tr", {
Element("th", {field.name}), Element("th", {preprocess_html(req, account.emojis, field.name)}),
Element("td", {preprocess_html(req, account.domain_name, account.emojis, field.value_html)}), Element("td", {preprocess_html(req, account.domain_name, account.emojis, field.value_html)}),
}); });
if (field.verified_at >= 0) { if (field.verified_at >= 0) {

View File

@ -9,6 +9,7 @@
#include "servehelper.h" #include "servehelper.h"
#include "lxb_wrapper.h" #include "lxb_wrapper.h"
#include "routes/routes.h" #include "routes/routes.h"
#include "blankie/escape.h"
static inline void preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector<Emoji>& emojis, lxb_dom_element_t* element); static inline void preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector<Emoji>& emojis, lxb_dom_element_t* element);
static inline void preprocess_link(const httplib::Request& req, const std::string& domain_name, lxb_dom_element_t* element); static inline void preprocess_link(const httplib::Request& req, const std::string& domain_name, lxb_dom_element_t* element);
@ -168,6 +169,9 @@ blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std
return blankie::html::HTMLString(document.serialize()); return blankie::html::HTMLString(document.serialize());
} }
blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::vector<Emoji>& emojis, const std::string& str) {
return preprocess_html(req, "", emojis, blankie::html::HTMLString(blankie::html::escape(str)));
}
static inline void preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector<Emoji>& emojis, lxb_dom_element_t* element) { static inline void preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector<Emoji>& emojis, lxb_dom_element_t* element) {
@ -258,10 +262,14 @@ static inline lxb_dom_node_t* emojify(lxb_dom_node_t* child, const std::vector<E
const char* text_content = reinterpret_cast<const char*>(lxb_dom_node_text_content(child, &text_content_len)); const char* text_content = reinterpret_cast<const char*>(lxb_dom_node_text_content(child, &text_content_len));
std::vector<lxb_dom_node_t*> nodes = emojify(child->owner_document, std::string(text_content, text_content_len), emojis); std::vector<lxb_dom_node_t*> nodes = emojify(child->owner_document, std::string(text_content, text_content_len), emojis);
for (lxb_dom_node_t* node : nodes) {
lxb_dom_node_insert_after(child, node); lxb_dom_node_insert_after(child, nodes[0]);
lxb_dom_node_destroy(child); lxb_dom_node_destroy(child);
child = node; child = nodes[0];
for (size_t i = 1; i < nodes.size(); i++) {
lxb_dom_node_insert_after(child, nodes[i]);
child = nodes[i];
} }
return child; return child;

View File

@ -20,3 +20,4 @@ std::string proxy_mastodon_url(const httplib::Request& req, const std::string& u
bool should_send_304(const httplib::Request& req, uint64_t hash); bool should_send_304(const httplib::Request& req, uint64_t hash);
blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector<Emoji>& emojis, const blankie::html::HTMLString& str); blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector<Emoji>& emojis, const blankie::html::HTMLString& str);
blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::vector<Emoji>& emojis, const std::string& str);