From e10053155dc8b412837170a4fa3d2d3e2492da28 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Wed, 20 Apr 2022 15:07:46 +0000 Subject: [PATCH] Emojis FossilOrigin-Name: d0746c463cfc117130572a2d835585703541e21174e78433f42f586b18138110 --- dist/treebird20.css | 1 - src/emoji.c | 53 +++++++++++++++++++++++++++++++++++++++++++++ src/emoji.h | 25 +++++++++++++++++++++ src/status.c | 19 ++++++++++++++-- src/status.h | 3 ++- 5 files changed, 97 insertions(+), 4 deletions(-) create mode 100644 src/emoji.c create mode 100644 src/emoji.h diff --git a/dist/treebird20.css b/dist/treebird20.css index 3aa0bd4..8cb9f5a 100644 --- a/dist/treebird20.css +++ b/dist/treebird20.css @@ -62,7 +62,6 @@ ul border-radius: 5px; } - .hidden { display: none; diff --git a/src/emoji.c b/src/emoji.c new file mode 100644 index 0000000..67c79f0 --- /dev/null +++ b/src/emoji.c @@ -0,0 +1,53 @@ +/* + * Treebird - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#include +#include +#include "string.h" +#include "emoji.h" +#include "easprintf.h" + +char* emojify(char* content, struct mstdnt_emoji* emos, size_t emos_len) +{ + size_t sc_len; + char* oldres; + char* res = content; + char* emoji_url_str; + char* coloned; + for (size_t i = 0; i < emos_len; ++i) + { + oldres = res; + + // Add colons around string + sc_len = strlen(emos[i].shortcode); + coloned = malloc(sc_len+3); + coloned[0] = ':'; + strncpy(coloned + 1, emos[i].shortcode, sc_len); + coloned[sc_len-1] = ':'; + coloned[sc_len] = '\0'; + + easprintf(&emoji_url_str, "", emos[i].url); + + res = strrepl(res, coloned, emoji_url_str, STRREPL_ALL); + if (oldres != content) free(oldres); + // Cleanup + free(emoji_url_str); + free(coloned); + } + return res; +} diff --git a/src/emoji.h b/src/emoji.h new file mode 100644 index 0000000..4f33334 --- /dev/null +++ b/src/emoji.h @@ -0,0 +1,25 @@ +/* + * Treebird - Lightweight frontend for Pleroma + * Copyright (C) 2022 Nekobit + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +#ifndef EMOJI_H +#define EMOJI_H +#include + +char* emojify(char* content, struct mstdnt_emoji* emos, size_t emos_len); + +#endif // EMOJI_H diff --git a/src/status.c b/src/status.c index f45609f..06e3e32 100644 --- a/src/status.c +++ b/src/status.c @@ -33,6 +33,7 @@ #include "../config.h" #include "type_string.h" #include "string.h" +#include "emoji.h" // Pages #include "../static/status.chtml" @@ -164,7 +165,21 @@ char* construct_in_reply_to(mastodont_t* api, struct mstdnt_status* status, size #define REGEX_GREENTEXT "((?:^|
|\\s)>.*?)(?:
|$)" #define REGEX_GREENTEXT_LEN 6 -char* reformat_status(char* content) +char* reformat_status(char* content, struct mstdnt_emoji* emos, size_t emos_len) +{ + char* gt_res; + char* emo_res; + gt_res = greentextify(content); + if (emos) + { + emo_res = emojify(gt_res, emos, emos_len); + free(gt_res); + return emo_res; + } + return gt_res; +} + +char* greentextify(char* content) { const char* error; int erroffset; @@ -247,7 +262,7 @@ char* construct_status(mastodont_t* api, notif_reblog.type = MSTDNT_NOTIFICATION_REBLOG; notif = ¬if_reblog; } - char* parse_content = reformat_status(status->content); + char* parse_content = reformat_status(status->content, status->emojis, status->emojis_len); if (status->replies_count) easprintf(&reply_count, NUM_STR, status->replies_count); diff --git a/src/status.h b/src/status.h index 0e13bad..33b7513 100644 --- a/src/status.h +++ b/src/status.h @@ -36,7 +36,8 @@ void content_status_create(struct session* ssn, mastodont_t* api, char** data); char* construct_post_box(char* reply_id, char* default_content, int* size); -char* reformat_status(char* content); +char* reformat_status(char* content, struct mstdnt_emoji* emos, size_t emos_len); +char* greentextify(char* content); char* construct_status(mastodont_t* api, struct mstdnt_status* status, int* size, struct mstdnt_notification* notif, uint8_t flags); char* construct_statuses(mastodont_t* api, struct mstdnt_status* statuses, size_t size, size_t* ret_size);