mirror of
https://codeberg.org/grunfink/snac2.git
synced 2024-11-26 06:53:37 +00:00
More formatting tweaks.
This commit is contained in:
parent
73323a7fc0
commit
bb7bcc674c
1 changed files with 62 additions and 54 deletions
56
format.c
56
format.c
|
@ -34,22 +34,14 @@ struct {
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
d_char *not_really_markdown(char *content)
|
static d_char *format_line(const char *line)
|
||||||
/* formats a content using some Markdown rules */
|
/* formats a line */
|
||||||
{
|
{
|
||||||
d_char *s = NULL;
|
d_char *s = xs_str_new(NULL);
|
||||||
int in_pre = 0;
|
|
||||||
int in_blq = 0;
|
|
||||||
xs *list;
|
|
||||||
char *p, *v;
|
char *p, *v;
|
||||||
xs *wrk = xs_str_new(NULL);
|
|
||||||
|
|
||||||
/* some preparation to avoid writing very kludgy code */
|
/* split by markup */
|
||||||
xs *p_content = xs_replace(content, "```", "@pre@");
|
xs *sm = xs_regex_split(line,
|
||||||
|
|
||||||
{
|
|
||||||
/* split by special markup */
|
|
||||||
xs *sm = xs_regex_split(p_content,
|
|
||||||
"(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
|
"(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)");
|
||||||
int n = 0;
|
int n = 0;
|
||||||
|
|
||||||
|
@ -60,45 +52,55 @@ d_char *not_really_markdown(char *content)
|
||||||
if (xs_startswith(v, "`")) {
|
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);
|
s = xs_str_cat(s, s2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (xs_startswith(v, "**")) {
|
if (xs_startswith(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);
|
s = xs_str_cat(s, s2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (xs_startswith(v, "*")) {
|
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("<i>%s</i>", s1);
|
xs *s2 = xs_fmt("<i>%s</i>", s1);
|
||||||
wrk = xs_str_cat(wrk, s2);
|
s = xs_str_cat(s, s2);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
if (xs_startswith(v, "http")) {
|
if (xs_startswith(v, "http")) {
|
||||||
xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v, v);
|
xs *s1 = xs_fmt("<a href=\"%s\" target=\"_blank\">%s</a>", v, v);
|
||||||
wrk = xs_str_cat(wrk, s1);
|
s = xs_str_cat(s, s1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
wrk = xs_str_cat(wrk, v);
|
s = xs_str_cat(s, v);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
/* surrounded text, copy directly */
|
/* surrounded text, copy directly */
|
||||||
wrk = xs_str_cat(wrk, v);
|
s = xs_str_cat(s, v);
|
||||||
|
|
||||||
n++;
|
n++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return s;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now work by lines */
|
|
||||||
p = list = xs_split(wrk, "\n");
|
|
||||||
|
|
||||||
s = xs_str_new(NULL);
|
d_char *not_really_markdown(char *content)
|
||||||
|
/* formats a content using some Markdown rules */
|
||||||
|
{
|
||||||
|
d_char *s = xs_str_new(NULL);
|
||||||
|
int in_pre = 0;
|
||||||
|
int in_blq = 0;
|
||||||
|
xs *list;
|
||||||
|
char *p, *v;
|
||||||
|
|
||||||
|
/* work by lines */
|
||||||
|
p = list = xs_split(content, "\n");
|
||||||
|
|
||||||
while (xs_list_iter(&p, &v)) {
|
while (xs_list_iter(&p, &v)) {
|
||||||
xs *ss = xs_strip(xs_dup(v));
|
xs *ss = NULL;
|
||||||
|
|
||||||
if (xs_startswith(ss, "@pre@")) {
|
if (strcmp(v, "```") == 0) {
|
||||||
if (!in_pre)
|
if (!in_pre)
|
||||||
s = xs_str_cat(s, "<pre>");
|
s = xs_str_cat(s, "<pre>");
|
||||||
else
|
else
|
||||||
|
@ -108,6 +110,11 @@ d_char *not_really_markdown(char *content)
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (in_pre)
|
||||||
|
ss = xs_dup(v);
|
||||||
|
else
|
||||||
|
ss = xs_strip(format_line(v));
|
||||||
|
|
||||||
if (xs_startswith(ss, ">")) {
|
if (xs_startswith(ss, ">")) {
|
||||||
/* delete the > and subsequent spaces */
|
/* delete the > and subsequent spaces */
|
||||||
ss = xs_strip(xs_crop(ss, 1, 0));
|
ss = xs_strip(xs_crop(ss, 1, 0));
|
||||||
|
@ -138,6 +145,7 @@ d_char *not_really_markdown(char *content)
|
||||||
s = xs_str_cat(s, "</pre>");
|
s = xs_str_cat(s, "</pre>");
|
||||||
|
|
||||||
/* some beauty fixes */
|
/* some beauty fixes */
|
||||||
|
s = xs_replace_i(s, "<br><br><blockquote>", "<br><blockquote>");
|
||||||
s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
|
s = xs_replace_i(s, "</blockquote><br>", "</blockquote>");
|
||||||
s = xs_replace_i(s, "</pre><br>", "</pre>");
|
s = xs_replace_i(s, "</pre><br>", "</pre>");
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue