From 1808df8f753fc1d8146004fe9e480d2d4ee04991 Mon Sep 17 00:00:00 2001 From: blankie Date: Thu, 23 Nov 2023 11:32:21 +1100 Subject: [PATCH] Add custom emoji support for plain text --- client.cpp | 2 +- routes/css.cpp | 8 +++++--- routes/user.cpp | 8 ++++---- servehelper.cpp | 16 ++++++++++++---- servehelper.h | 1 + 5 files changed, 23 insertions(+), 12 deletions(-) diff --git a/client.cpp b/client.cpp index 963679a..926956c 100644 --- a/client.cpp +++ b/client.cpp @@ -90,7 +90,7 @@ CURL* MastodonClient::_get_easy() { try { 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_USERAGENT, "Coyote (https://gitlab.com/blankX/coyote; blankie@nixnetmail.com)"); setopt(curl, CURLOPT_SHARE, this->_share); diff --git a/routes/css.cpp b/routes/css.cpp index e8b32c4..b230831 100644 --- a/routes/css.cpp +++ b/routes/css.cpp @@ -44,6 +44,8 @@ a:hover { .custom_emoji { height: 1em; width: 1em; + /* https://stackoverflow.com/a/489394 */ + vertical-align: middle; } /* ERROR PAGE */ @@ -66,14 +68,14 @@ a:hover { object-fit: cover; } -.user_page-user_pfp { +.user_page-main_header { display: flex; } -.user_page-user_pfp img { +.user_page-profile { width: 7.5em; height: 7.5em; } -.user_page-user_pfp span { +.user_page-main_header span { margin-top: auto; margin-bottom: auto; margin-left: 0.5em; diff --git a/routes/user.cpp b/routes/user.cpp index f3a62a6..5fa0786 100644 --- a/routes/user.cpp +++ b/routes/user.cpp @@ -66,12 +66,12 @@ static inline Element user_header(const httplib::Request& req, const Account& ac Element("a", {{"href", 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("img", {{"alt", "User profile picture"}, {"src", account.avatar}}, {}), + Element("img", {{"class", "user_page-profile"}, {"alt", "User profile picture"}, {"src", account.avatar}}, {}), }), 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("b", {"Joined: "}), std::string(created_at_str), Element("br"), @@ -100,7 +100,7 @@ static inline Element user_link_field(const httplib::Request& req, const Account using namespace std::string_literals; 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)}), }); if (field.verified_at >= 0) { diff --git a/servehelper.cpp b/servehelper.cpp index e7da793..d3e039d 100644 --- a/servehelper.cpp +++ b/servehelper.cpp @@ -9,6 +9,7 @@ #include "servehelper.h" #include "lxb_wrapper.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& 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); @@ -168,6 +169,9 @@ blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std return blankie::html::HTMLString(document.serialize()); } +blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::vector& 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& 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(lxb_dom_node_text_content(child, &text_content_len)); std::vector 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_destroy(child); - child = node; + + lxb_dom_node_insert_after(child, nodes[0]); + lxb_dom_node_destroy(child); + 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; diff --git a/servehelper.h b/servehelper.h index 579e17a..8bb8fd5 100644 --- a/servehelper.h +++ b/servehelper.h @@ -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); blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::string& domain_name, const std::vector& emojis, const blankie::html::HTMLString& str); +blankie::html::HTMLString preprocess_html(const httplib::Request& req, const std::vector& emojis, const std::string& str);