forked from mirrors/treebird
Regex stuff
FossilOrigin-Name: 6ed1d24098367d00abf2401fc05e9f4c8c101d90a4a7fb5d8f611a8a029539c0
This commit is contained in:
parent
3451a48882
commit
5d34ac70aa
3 changed files with 38 additions and 15 deletions
46
src/reply.c
46
src/reply.c
|
@ -16,6 +16,7 @@
|
|||
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include <regex.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include "reply.h"
|
||||
|
@ -44,30 +45,57 @@ char* construct_post_box(char* reply_id,
|
|||
return reply_html;
|
||||
}
|
||||
|
||||
char* reply_status(char* id,
|
||||
struct mstdnt_status* statuses_before,
|
||||
size_t before_len,
|
||||
struct mstdnt_status* status)
|
||||
{
|
||||
char* stat_reply;
|
||||
#define REGEX_REPLY "<a class=\"u-url mention\".*href=\"https:\\/\\/(.*)\\/.*\".*>@<span>(.*)<\\/span>"
|
||||
//#define REGEX_REPLY "<a class=\"u-url mention\".*href=\"https:\\/\\/(.*)\\/.*\".*>@<span>(.*)<\\/span>"
|
||||
|
||||
char* reply_status(char* id, struct mstdnt_status* status)
|
||||
{
|
||||
char* content = status->content;
|
||||
char* stat_reply;
|
||||
// Regex
|
||||
regex_t regex;
|
||||
regmatch_t pmatch[2];
|
||||
regoff_t off_url, len_url, off_name, len_name;
|
||||
|
||||
// Replies
|
||||
size_t replies_size, replies_size_orig;
|
||||
char* replies = malloc(replies_size = strlen(status->account.acct)+2);
|
||||
|
||||
// Load first reply
|
||||
replies[0] = '@';
|
||||
strcpy(replies+1, status->account.acct);
|
||||
replies[replies_size-1] = ' ';
|
||||
|
||||
for (int i = before_len-1; i >= 1; --i)
|
||||
// Compile regex
|
||||
if (regcomp(®ex, REGEX_REPLY, REG_EXTENDED))
|
||||
{
|
||||
fputs("Invalid regex!", stderr);
|
||||
free(replies);
|
||||
}
|
||||
|
||||
int j = 0;
|
||||
|
||||
for (int i = 0;; ++i)
|
||||
{
|
||||
if (regexec(®ex, content + j, 2, pmatch, 0))
|
||||
break;
|
||||
|
||||
off_url = pmatch[0].rm_so + j;
|
||||
len_url = pmatch[0].rm_eo - pmatch[0].rm_so;
|
||||
|
||||
off_name = pmatch[1].rm_so + j;
|
||||
len_name = pmatch[1].rm_eo - pmatch[1].rm_so;
|
||||
|
||||
replies_size_orig = replies_size;
|
||||
replies_size += strlen(statuses_before[i].account.acct)+2;
|
||||
replies_size += len_url+2;
|
||||
|
||||
// Realloc string
|
||||
replies = realloc(replies, replies_size+1);
|
||||
|
||||
j += off_url + len_url + off_name + len_name;
|
||||
|
||||
replies[replies_size_orig] = '@';
|
||||
strcpy(replies + replies_size_orig + 1, statuses_before[i].account.acct);
|
||||
memcpy(replies + replies_size_orig + 1, content + off_url, len_url);
|
||||
replies[replies_size-1] = ' ';
|
||||
|
||||
}
|
||||
|
|
|
@ -24,10 +24,7 @@ char* construct_post_box(char* reply_id,
|
|||
char* default_content,
|
||||
int* size);
|
||||
|
||||
char* reply_status(char* id,
|
||||
struct mstdnt_status* statuses_before,
|
||||
size_t before_len,
|
||||
struct mstdnt_status* status);
|
||||
char* reply_status(char* id, struct mstdnt_status* status);
|
||||
|
||||
|
||||
#endif // REPLY_H
|
||||
|
|
|
@ -164,8 +164,6 @@ void content_status(mastodont_t* api, char** data, size_t data_size, int is_repl
|
|||
if (is_reply)
|
||||
{
|
||||
stat_reply = reply_status(data[0],
|
||||
statuses_before,
|
||||
stat_before_len,
|
||||
&status);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue