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; int in_blq = 0;
xs *list; xs *list;
char *p, *v; char *p, *v;
xs *wrk = xs_dup(content); xs *wrk = xs_str_new(NULL);
/* 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);
}
}
{ {
/* double asterisks */ /* split by special markup */
xs *ml = xs_regex_match(wrk, "\\*\\*[^\\*]+\\*\\*"); xs *sm = xs_regex_split(content,
p = ml; "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^ ]*)");
int n = 0;
p = sm;
while (xs_list_iter(&p, &v)) { while (xs_list_iter(&p, &v)) {
xs *s1 = xs_crop(xs_dup(v), 2, -2); if ((n & 0x1)) {
xs *s2 = xs_fmt("<b>%s</b>", s1); /* markup */
if (xs_startswith(v, "`")) {
wrk = xs_replace_i(wrk, v, s2); xs *s1 = xs_crop(xs_dup(v), 1, -1);
} xs *s2 = xs_fmt("<code>%s</code>", s1);
} wrk = xs_str_cat(wrk, s2);
}
{ else
/* single asterisks */ if (xs_startswith(v, "**")) {
xs *ml = xs_regex_match(wrk, "\\*[^\\*]+\\*"); xs *s1 = xs_crop(xs_dup(v), 2, -2);
p = ml; xs *s2 = xs_fmt("<b>%s</b>", s1);
wrk = xs_str_cat(wrk, s2);
while (xs_list_iter(&p, &v)) { }
xs *s1 = xs_crop(xs_dup(v), 1, -1); else
xs *s2 = xs_fmt("<i>%s</i>", s1); if (xs_startswith(v, "*")) {
xs *s1 = xs_crop(xs_dup(v), 1, -1);
wrk = xs_replace_i(wrk, v, s2); xs *s2 = xs_fmt("<i>%s</i>", s1);
} wrk = xs_str_cat(wrk, s2);
} }
else
#if 0 if (xs_startswith(v, "http")) {
{ xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v);
/* urls */ wrk = xs_str_cat(wrk, s1);
xs *done = xs_list_new(); }
xs *ml = xs_regex_matchall(wrk, "https?:/" "/[^ ]+"); else
p = ml; /* what the hell is this */
wrk = xs_str_cat(wrk, v);
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);
} }
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"); p = list = xs_split(wrk, "\n");
s = xs_str_new(NULL); s = xs_str_new(NULL);