From 716809aa0a4b035e2aa749c3c459a636d0d05890 Mon Sep 17 00:00:00 2001 From: "me@ow.nekobit.net" Date: Wed, 20 Apr 2022 14:14:31 +0000 Subject: [PATCH] Greentexts FossilOrigin-Name: 21819591a6175572cfcacf2c40260e46010dae8209d39bc34870cfb56cca82cb --- dist/treebird20-dark.css | 5 +++++ dist/treebird20.css | 5 +++++ dist/treebird40.css | 5 +++++ src/status.c | 31 +++++++++++++++++++++---------- src/string.c | 5 +++-- src/string.h | 4 +++- test/unit/string_test.c | 8 ++++---- 7 files changed, 46 insertions(+), 17 deletions(-) diff --git a/dist/treebird20-dark.css b/dist/treebird20-dark.css index dbb67c4..d72c1b8 100644 --- a/dist/treebird20-dark.css +++ b/dist/treebird20-dark.css @@ -92,6 +92,11 @@ a, a:visited, a:hover, a:active border: 1px solid #aa0000; } +.greentext +{ + color: #00cc00; +} + /* Cleans up most of the tables */ table.ui-table td { diff --git a/dist/treebird20.css b/dist/treebird20.css index 95a7b4f..3aa0bd4 100644 --- a/dist/treebird20.css +++ b/dist/treebird20.css @@ -88,6 +88,11 @@ a, a:visited, a:hover, a:active padding-right: 2px; } +.greentext +{ + color: #008000; +} + /* Cleans up most of the tables */ table.ui-table td { diff --git a/dist/treebird40.css b/dist/treebird40.css index 8ae06a6..f2f1ce6 100644 --- a/dist/treebird40.css +++ b/dist/treebird40.css @@ -88,6 +88,11 @@ a, a:visited, a:hover, a:active padding-right: 2px; } +.greentext +{ + color: #008000; +} + /* Cleans up most of the tables */ table.ui-table td { diff --git a/src/status.c b/src/status.c index 04c1307..f45609f 100644 --- a/src/status.c +++ b/src/status.c @@ -161,8 +161,8 @@ char* construct_in_reply_to(mastodont_t* api, struct mstdnt_status* status, size return irt_html; } -#define REGEX_GREENTEXT "((?:^|\\s)>.*)$" -#define REGEX_GREENTEXT_LEN 3 +#define REGEX_GREENTEXT "((?:^|
|\\s)>.*?)(?:
|$)" +#define REGEX_GREENTEXT_LEN 6 char* reformat_status(char* content) { @@ -172,9 +172,14 @@ char* reformat_status(char* content) int gt_off; int gt_len; char* res = content; + + // Malloc'd strings + char* reg_string; + char* gt_string; + char* oldres = NULL; - pcre* re = pcre_compile(REGEX_GREENTEXT, 0, &error, &erroffset, NULL); int re_results[REGEX_GREENTEXT_LEN]; + pcre* re = pcre_compile(REGEX_GREENTEXT, 0, &error, &erroffset, NULL); if (re == NULL) { fprintf(stderr, "Couldn't parse regex at offset %d: %s\n", erroffset, error); @@ -184,7 +189,7 @@ char* reformat_status(char* content) for (int ind = 0;;) { - rc = pcre_exec(re, NULL, content, strlen(content), ind, 0, re_results, REGEX_GREENTEXT_LEN); + rc = pcre_exec(re, NULL, res, strlen(res), ind, 0, re_results, REGEX_GREENTEXT_LEN); if (rc < 0) break; @@ -192,13 +197,19 @@ char* reformat_status(char* content) gt_off = re_results[2]; gt_len = re_results[3] - gt_off; - res[gt_off + gt_len] = '\0'; + oldres = res; + + // Malloc find/repl strings + reg_string = malloc(gt_len + 1); + strncpy(reg_string, res + gt_off, gt_len); + reg_string[gt_len] = '\0'; + easprintf(>_string, "%s", reg_string); - if (res != content) - oldres = res; - res = strrepl(res, content + gt_off, "greentext"); - if (oldres) free(oldres); - res[gt_off + gt_len] = 'a'; + res = strrepl(res, reg_string, gt_string, 0); + if (oldres != content) free(oldres); + ind = re_results[2] + strlen(gt_string); + free(reg_string); + free(gt_string); } pcre_free(re); diff --git a/src/string.c b/src/string.c index 4f21fad..fc0dd39 100644 --- a/src/string.c +++ b/src/string.c @@ -53,7 +53,7 @@ char* strnstr(const char* haystack, const char* needle, size_t s) return NULL; } -char* strrepl(char* source, char* find, char* repl) +char* strrepl(char* source, char* find, char* repl, int flags) { const size_t find_len = strlen(find); const size_t repl_len = strlen(repl); @@ -93,8 +93,9 @@ char* strrepl(char* source, char* find, char* repl) result[str_size] = '\0'; // If is_last is true, this value doesn't matter last = curr + find_len; - } while (curr && !is_last); + } while (flags == STRREPL_ALL && curr && !is_last); // Return source string if no replacements return result ? result : source; } + diff --git a/src/string.h b/src/string.h index 87993f3..9baf287 100644 --- a/src/string.h +++ b/src/string.h @@ -20,9 +20,11 @@ #define TREE_STRING_H #include +#define STRREPL_ALL 1 + int streql(char* cmp1, char* cmp2); int strneql(char* cmp1, char* cmp2, size_t cmp1_n); char* strnstr(const char* haystack, const char* needle, size_t s); -char* strrepl(char* source, char* find, char* replace); +char* strrepl(char* source, char* find, char* replace, int flags); #endif // TREE_STRING_H diff --git a/test/unit/string_test.c b/test/unit/string_test.c index c76ea9f..9981849 100644 --- a/test/unit/string_test.c +++ b/test/unit/string_test.c @@ -3,13 +3,13 @@ int string_replace_test(void) { - char* res1 = strrepl("hello world", "wo", "swi"); + char* res1 = strrepl("hello world", "wo", "swi", STRREPL_ALL); assert(strcmp(res1, "hello swirld") == 0); - char* res2 = strrepl("hello world itswo the world wo", "wo", "swi"); + char* res2 = strrepl("hello world itswo the world wo", "wo", "swi", STRREPL_ALL); assert(strcmp(res2, "hello swirld itsswi the swirld swi") == 0); - char* res3 = strrepl(">implying\nhuh? <><", ">", ">"); + char* res3 = strrepl(">implying\nhuh? <><", ">", ">", STRREPL_ALL); assert(strcmp(res3, ">implying\nhuh? <><") == 0); - char* res4 = strrepl(">lol >hi", ">", ">>>"); + char* res4 = strrepl(">lol >hi", ">", ">>>", STRREPL_ALL); assert(strcmp(res4, ">>>lol >>>hi") == 0); free(res1); free(res2);