Greentexts

FossilOrigin-Name: 21819591a6175572cfcacf2c40260e46010dae8209d39bc34870cfb56cca82cb
This commit is contained in:
me@ow.nekobit.net 2022-04-20 14:14:31 +00:00
parent 3861ac32be
commit 716809aa0a
7 changed files with 46 additions and 17 deletions

View file

@ -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
{

5
dist/treebird20.css vendored
View file

@ -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
{

5
dist/treebird40.css vendored
View file

@ -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
{

View file

@ -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 "((?:^|<br/?>|\\s)&gt;.*?)(?:<br/?>|$)"
#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(&gt_string, "<span class=\"greentext\">%s</span>", 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);

View file

@ -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;
}

View file

@ -20,9 +20,11 @@
#define TREE_STRING_H
#include <stddef.h>
#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

View file

@ -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? <><", ">", "&gt;");
char* res3 = strrepl(">implying\nhuh? <><", ">", "&gt;", STRREPL_ALL);
assert(strcmp(res3, "&gt;implying\nhuh? <&gt;<") == 0);
char* res4 = strrepl(">lol >hi", ">", ">>>");
char* res4 = strrepl(">lol >hi", ">", ">>>", STRREPL_ALL);
assert(strcmp(res4, ">>>lol >>>hi") == 0);
free(res1);
free(res2);