Rewrite not_really_markdown() with xs_regex_split().

This commit is contained in:
default 2022-09-27 17:41:56 +02:00
parent bd2a07691d
commit 5784ddecb4

94
html.c
View File

@ -16,69 +16,53 @@ d_char *not_really_markdown(char *content, d_char **f_content)
int in_blq = 0;
xs *list;
char *p, *v;
xs *wrk = xs_dup(content);
/* global changes */
{
/* backticks */
xs *ml = xs_regex_match(wrk, "`[^`]+`");
p = ml;
while (xs_list_iter(&p, &v)) {
xs *s1 = xs_crop(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<code>%s</code>", s1);
wrk = xs_replace_i(wrk, v, s2);
}
}
xs *wrk = xs_str_new(NULL);
{
/* double asterisks */
xs *ml = xs_regex_match(wrk, "\\*\\*[^\\*]+\\*\\*");
p = ml;
/* split by special markup */
xs *sm = xs_regex_split(content,
"(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
int n = 0;
p = sm;
while (xs_list_iter(&p, &v)) {
xs *s1 = xs_crop(xs_dup(v), 2, -2);
xs *s2 = xs_fmt("<b>%s</b>", s1);
wrk = xs_replace_i(wrk, v, s2);
}
}
{
/* single asterisks */
xs *ml = xs_regex_match(wrk, "\\*[^\\*]+\\*");
p = ml;
while (xs_list_iter(&p, &v)) {
xs *s1 = xs_crop(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<i>%s</i>", s1);
wrk = xs_replace_i(wrk, v, s2);
}
}
#if 0
{
/* urls */
xs *done = xs_list_new();
xs *ml = xs_regex_matchall(wrk, "https?:/" "/[^ ]+");
p = ml;
while (xs_list_iter(&p, &v)) {
if (xs_list_in(done, v) == -1) {
xs *s2 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
wrk = xs_replace_i(wrk, v, s2);
/* keep track of already done replaces */
done = xs_list_append(done, v);
if ((n & 0x1)) {
/* markup */
if (xs_startswith(v, "`")) {
xs *s1 = xs_crop(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<code>%s</code>", s1);
wrk = xs_str_cat(wrk, s2);
}
else
if (xs_startswith(v, "**")) {
xs *s1 = xs_crop(xs_dup(v), 2, -2);
xs *s2 = xs_fmt("<b>%s</b>", s1);
wrk = xs_str_cat(wrk, s2);
}
else
if (xs_startswith(v, "*")) {
xs *s1 = xs_crop(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<i>%s</i>", s1);
wrk = xs_str_cat(wrk, s2);
}
else
if (xs_startswith(v, "http")) {
xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
wrk = xs_str_cat(wrk, s1);
}
else
/* what the hell is this */
wrk = xs_str_cat(wrk, v);
}
else
/* surrounded text, copy directly */
wrk = xs_str_cat(wrk, v);
n++;
}
}
#endif
/* now work on lines */
/* now work by lines */
p = list = xs_split(wrk, "\n");
s = xs_str_new(NULL);