diff options
| author | 2022-10-07 18:30:54 +0200 | |
|---|---|---|
| committer | 2022-10-07 18:30:54 +0200 | |
| commit | 9aec7c9fa8fe5396451aae608f8b9c58346f4b4e (patch) | |
| tree | f5a76086ed506e3cf9fa6520b00e147ec4101123 /format.c | |
| parent | Version 2.02 RELEASED. (diff) | |
| download | snac2-9aec7c9fa8fe5396451aae608f8b9c58346f4b4e.tar.gz snac2-9aec7c9fa8fe5396451aae608f8b9c58346f4b4e.tar.xz snac2-9aec7c9fa8fe5396451aae608f8b9c58346f4b4e.zip | |
Moved message formatting to format.c.
Diffstat (limited to 'format.c')
| -rw-r--r-- | format.c | 116 |
1 files changed, 116 insertions, 0 deletions
diff --git a/format.c b/format.c new file mode 100644 index 0000000..b5d4cf7 --- /dev/null +++ b/format.c | |||
| @@ -0,0 +1,116 @@ | |||
| 1 | /* snac - A simple, minimalistic ActivityPub instance */ | ||
| 2 | /* copyright (c) 2022 grunfink - MIT license */ | ||
| 3 | |||
| 4 | #include "xs.h" | ||
| 5 | #include "xs_regex.h" | ||
| 6 | |||
| 7 | #include "snac.h" | ||
| 8 | |||
| 9 | d_char *not_really_markdown(char *content, d_char **f_content) | ||
| 10 | /* formats a content using some Markdown rules */ | ||
| 11 | { | ||
| 12 | d_char *s = NULL; | ||
| 13 | int in_pre = 0; | ||
| 14 | int in_blq = 0; | ||
| 15 | xs *list; | ||
| 16 | char *p, *v; | ||
| 17 | xs *wrk = xs_str_new(NULL); | ||
| 18 | |||
| 19 | { | ||
| 20 | /* split by special markup */ | ||
| 21 | xs *sm = xs_regex_split(content, | ||
| 22 | "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)"); | ||
| 23 | int n = 0; | ||
| 24 | |||
| 25 | p = sm; | ||
| 26 | while (xs_list_iter(&p, &v)) { | ||
| 27 | if ((n & 0x1)) { | ||
| 28 | /* markup */ | ||
| 29 | if (xs_startswith(v, "`")) { | ||
| 30 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | ||
| 31 | xs *s2 = xs_fmt("<code>%s</code>", s1); | ||
| 32 | wrk = xs_str_cat(wrk, s2); | ||
| 33 | } | ||
| 34 | else | ||
| 35 | if (xs_startswith(v, "**")) { | ||
| 36 | xs *s1 = xs_crop(xs_dup(v), 2, -2); | ||
| 37 | xs *s2 = xs_fmt("<b>%s</b>", s1); | ||
| 38 | wrk = xs_str_cat(wrk, s2); | ||
| 39 | } | ||
| 40 | else | ||
| 41 | if (xs_startswith(v, "*")) { | ||
| 42 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | ||
| 43 | xs *s2 = xs_fmt("<i>%s</i>", s1); | ||
| 44 | wrk = xs_str_cat(wrk, s2); | ||
| 45 | } | ||
| 46 | else | ||
| 47 | if (xs_startswith(v, "http")) { | ||
| 48 | xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v); | ||
| 49 | wrk = xs_str_cat(wrk, s1); | ||
| 50 | } | ||
| 51 | else | ||
| 52 | /* what the hell is this */ | ||
| 53 | wrk = xs_str_cat(wrk, v); | ||
| 54 | } | ||
| 55 | else | ||
| 56 | /* surrounded text, copy directly */ | ||
| 57 | wrk = xs_str_cat(wrk, v); | ||
| 58 | |||
| 59 | n++; | ||
| 60 | } | ||
| 61 | } | ||
| 62 | |||
| 63 | /* now work by lines */ | ||
| 64 | p = list = xs_split(wrk, "\n"); | ||
| 65 | |||
| 66 | s = xs_str_new(NULL); | ||
| 67 | |||
| 68 | while (xs_list_iter(&p, &v)) { | ||
| 69 | xs *ss = xs_strip(xs_dup(v)); | ||
| 70 | |||
| 71 | if (xs_startswith(ss, "```")) { | ||
| 72 | if (!in_pre) | ||
| 73 | s = xs_str_cat(s, "<pre>"); | ||
| 74 | else | ||
| 75 | s = xs_str_cat(s, "</pre>"); | ||
| 76 | |||
| 77 | in_pre = !in_pre; | ||
| 78 | continue; | ||
| 79 | } | ||
| 80 | |||
| 81 | if (xs_startswith(ss, ">")) { | ||
| 82 | /* delete the > and subsequent spaces */ | ||
| 83 | ss = xs_strip(xs_crop(ss, 1, 0)); | ||
| 84 | |||
| 85 | if (!in_blq) { | ||
| 86 | s = xs_str_cat(s, "<blockquote>"); | ||
| 87 | in_blq = 1; | ||
| 88 | } | ||
| 89 | |||
| 90 | s = xs_str_cat(s, ss); | ||
| 91 | s = xs_str_cat(s, "<br>"); | ||
| 92 | |||
| 93 | continue; | ||
| 94 | } | ||
| 95 | |||
| 96 | if (in_blq) { | ||
| 97 | s = xs_str_cat(s, "</blockquote>"); | ||
| 98 | in_blq = 0; | ||
| 99 | } | ||
| 100 | |||
| 101 | s = xs_str_cat(s, ss); | ||
| 102 | s = xs_str_cat(s, "<br>"); | ||
| 103 | } | ||
| 104 | |||
| 105 | if (in_blq) | ||
| 106 | s = xs_str_cat(s, "</blockquote>"); | ||
| 107 | if (in_pre) | ||
| 108 | s = xs_str_cat(s, "</pre>"); | ||
| 109 | |||
| 110 | /* some beauty fixes */ | ||
| 111 | s = xs_replace_i(s, "</blockquote><br>", "</blockquote>"); | ||
| 112 | |||
| 113 | *f_content = s; | ||
| 114 | |||
| 115 | return *f_content; | ||
| 116 | } | ||