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

78
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 */ /* 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)) {
if ((n & 0x1)) {
/* markup */
if (xs_startswith(v, "`")) {
xs *s1 = xs_crop(xs_dup(v), 1, -1); xs *s1 = xs_crop(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<code>%s</code>", s1); xs *s2 = xs_fmt("<code>%s</code>", s1);
wrk = xs_str_cat(wrk, s2);
wrk = xs_replace_i(wrk, v, s2);
} }
} else
if (xs_startswith(v, "**")) {
{
/* double asterisks */
xs *ml = xs_regex_match(wrk, "\\*\\*[^\\*]+\\*\\*");
p = ml;
while (xs_list_iter(&p, &v)) {
xs *s1 = xs_crop(xs_dup(v), 2, -2); xs *s1 = xs_crop(xs_dup(v), 2, -2);
xs *s2 = xs_fmt("<b>%s</b>", s1); xs *s2 = xs_fmt("<b>%s</b>", s1);
wrk = xs_str_cat(wrk, s2);
wrk = xs_replace_i(wrk, v, s2);
} }
} else
if (xs_startswith(v, "*")) {
{
/* 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 *s1 = xs_crop(xs_dup(v), 1, -1);
xs *s2 = xs_fmt("<i>%s</i>", s1); 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);
wrk = xs_replace_i(wrk, v, s2); n++;
} }
} }
#if 0 /* now work by lines */
{
/* 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);
}
}
}
#endif
/* now work on lines */
p = list = xs_split(wrk, "\n"); p = list = xs_split(wrk, "\n");
s = xs_str_new(NULL); s = xs_str_new(NULL);