diff options
| author | 2022-10-07 18:30:54 +0200 | |
|---|---|---|
| committer | 2022-10-07 18:30:54 +0200 | |
| commit | 9aec7c9fa8fe5396451aae608f8b9c58346f4b4e (patch) | |
| tree | f5a76086ed506e3cf9fa6520b00e147ec4101123 | |
| parent | Version 2.02 RELEASED. (diff) | |
| download | penes-snac2-9aec7c9fa8fe5396451aae608f8b9c58346f4b4e.tar.gz penes-snac2-9aec7c9fa8fe5396451aae608f8b9c58346f4b4e.tar.xz penes-snac2-9aec7c9fa8fe5396451aae608f8b9c58346f4b4e.zip | |
Moved message formatting to format.c.
| -rw-r--r-- | Makefile | 20 | ||||
| -rw-r--r-- | format.c | 116 | ||||
| -rw-r--r-- | html.c | 110 |
3 files changed, 127 insertions, 119 deletions
| @@ -3,7 +3,7 @@ CFLAGS=-g -Wall | |||
| 3 | 3 | ||
| 4 | all: snac | 4 | all: snac |
| 5 | 5 | ||
| 6 | snac: snac.o main.o data.o http.o httpd.o webfinger.o activitypub.o html.o utils.o | 6 | snac: snac.o main.o data.o http.o httpd.o webfinger.o activitypub.o html.o utils.o format.o |
| 7 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -pthread -o $@ | 7 | $(CC) -L/usr/local/lib *.o -lcurl -lcrypto -pthread -o $@ |
| 8 | 8 | ||
| 9 | .c.o: | 9 | .c.o: |
| @@ -25,17 +25,19 @@ install: | |||
| 25 | install -m 644 doc/snac.8 $(PREFIX)/man/man8/snac.8 | 25 | install -m 644 doc/snac.8 $(PREFIX)/man/man8/snac.8 |
| 26 | 26 | ||
| 27 | activitypub.o: activitypub.c xs.h xs_encdec.h xs_json.h xs_curl.h \ | 27 | activitypub.o: activitypub.c xs.h xs_encdec.h xs_json.h xs_curl.h \ |
| 28 | xs_mime.h xs_openssl.h xs_regex.h xs_time.h snac.h | 28 | xs_mime.h xs_openssl.h xs_regex.h xs_time.h snac.h |
| 29 | data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h snac.h | 29 | data.o: data.c xs.h xs_io.h xs_json.h xs_openssl.h xs_glob.h snac.h |
| 30 | format.o: format.c xs.h xs_regex.h snac.h | ||
| 30 | html.o: html.c xs.h xs_io.h xs_encdec.h xs_json.h xs_regex.h xs_set.h \ | 31 | html.o: html.c xs.h xs_io.h xs_encdec.h xs_json.h xs_regex.h xs_set.h \ |
| 31 | xs_openssl.h xs_time.h snac.h | 32 | xs_openssl.h xs_time.h snac.h |
| 32 | http.o: http.c xs.h xs_io.h xs_encdec.h xs_openssl.h xs_curl.h xs_time.h \ | 33 | http.o: http.c xs.h xs_io.h xs_encdec.h xs_openssl.h xs_curl.h xs_time.h \ |
| 33 | snac.h | 34 | snac.h |
| 34 | httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ | 35 | httpd.o: httpd.c xs.h xs_io.h xs_encdec.h xs_json.h xs_socket.h \ |
| 35 | xs_httpd.h snac.h | 36 | xs_httpd.h snac.h |
| 36 | main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h | 37 | main.o: main.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h |
| 37 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h xs_openssl.h \ | 38 | snac.o: snac.c xs.h xs_io.h xs_encdec.h xs_json.h xs_curl.h \ |
| 38 | xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h xs_time.h xs_glob.h \ | 39 | xs_openssl.h xs_socket.h xs_httpd.h xs_mime.h xs_regex.h xs_set.h \ |
| 39 | snac.h | 40 | xs_time.h xs_glob.h snac.h |
| 40 | utils.o: utils.c xs.h xs_io.h xs_encdec.h xs_json.h snac.h | 41 | utils.o: utils.c xs.h xs_io.h xs_encdec.h xs_json.h xs_time.h \ |
| 42 | xs_openssl.h snac.h | ||
| 41 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h | 43 | webfinger.o: webfinger.c xs.h xs_encdec.h xs_json.h xs_curl.h snac.h |
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 | } | ||
| @@ -12,116 +12,6 @@ | |||
| 12 | 12 | ||
| 13 | #include "snac.h" | 13 | #include "snac.h" |
| 14 | 14 | ||
| 15 | d_char *not_really_markdown(char *content, d_char **f_content) | ||
| 16 | /* formats a content using some Markdown rules */ | ||
| 17 | { | ||
| 18 | d_char *s = NULL; | ||
| 19 | int in_pre = 0; | ||
| 20 | int in_blq = 0; | ||
| 21 | xs *list; | ||
| 22 | char *p, *v; | ||
| 23 | xs *wrk = xs_str_new(NULL); | ||
| 24 | |||
| 25 | { | ||
| 26 | /* split by special markup */ | ||
| 27 | xs *sm = xs_regex_split(content, | ||
| 28 | "(`[^`]+`|\\*\\*?[^\\*]+\\*?\\*|https?:/" "/[^[:space:]]+)"); | ||
| 29 | int n = 0; | ||
| 30 | |||
| 31 | p = sm; | ||
| 32 | while (xs_list_iter(&p, &v)) { | ||
| 33 | if ((n & 0x1)) { | ||
| 34 | /* markup */ | ||
| 35 | if (xs_startswith(v, "`")) { | ||
| 36 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | ||
| 37 | xs *s2 = xs_fmt("<code>%s</code>", s1); | ||
| 38 | wrk = xs_str_cat(wrk, s2); | ||
| 39 | } | ||
| 40 | else | ||
| 41 | if (xs_startswith(v, "**")) { | ||
| 42 | xs *s1 = xs_crop(xs_dup(v), 2, -2); | ||
| 43 | xs *s2 = xs_fmt("<b>%s</b>", s1); | ||
| 44 | wrk = xs_str_cat(wrk, s2); | ||
| 45 | } | ||
| 46 | else | ||
| 47 | if (xs_startswith(v, "*")) { | ||
| 48 | xs *s1 = xs_crop(xs_dup(v), 1, -1); | ||
| 49 | xs *s2 = xs_fmt("<i>%s</i>", s1); | ||
| 50 | wrk = xs_str_cat(wrk, s2); | ||
| 51 | } | ||
| 52 | else | ||
| 53 | if (xs_startswith(v, "http")) { | ||
| 54 | xs *s1 = xs_fmt("<a href=\"%s\">%s</a>", v, v); | ||
| 55 | wrk = xs_str_cat(wrk, s1); | ||
| 56 | } | ||
| 57 | else | ||
| 58 | /* what the hell is this */ | ||
| 59 | wrk = xs_str_cat(wrk, v); | ||
| 60 | } | ||
| 61 | else | ||
| 62 | /* surrounded text, copy directly */ | ||
| 63 | wrk = xs_str_cat(wrk, v); | ||
| 64 | |||
| 65 | n++; | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | /* now work by lines */ | ||
| 70 | p = list = xs_split(wrk, "\n"); | ||
| 71 | |||
| 72 | s = xs_str_new(NULL); | ||
| 73 | |||
| 74 | while (xs_list_iter(&p, &v)) { | ||
| 75 | xs *ss = xs_strip(xs_dup(v)); | ||
| 76 | |||
| 77 | if (xs_startswith(ss, "```")) { | ||
| 78 | if (!in_pre) | ||
| 79 | s = xs_str_cat(s, "<pre>"); | ||
| 80 | else | ||
| 81 | s = xs_str_cat(s, "</pre>"); | ||
| 82 | |||
| 83 | in_pre = !in_pre; | ||
| 84 | continue; | ||
| 85 | } | ||
| 86 | |||
| 87 | if (xs_startswith(ss, ">")) { | ||
| 88 | /* delete the > and subsequent spaces */ | ||
| 89 | ss = xs_strip(xs_crop(ss, 1, 0)); | ||
| 90 | |||
| 91 | if (!in_blq) { | ||
| 92 | s = xs_str_cat(s, "<blockquote>"); | ||
| 93 | in_blq = 1; | ||
| 94 | } | ||
| 95 | |||
| 96 | s = xs_str_cat(s, ss); | ||
| 97 | s = xs_str_cat(s, "<br>"); | ||
| 98 | |||
| 99 | continue; | ||
| 100 | } | ||
| 101 | |||
| 102 | if (in_blq) { | ||
| 103 | s = xs_str_cat(s, "</blockquote>"); | ||
| 104 | in_blq = 0; | ||
| 105 | } | ||
| 106 | |||
| 107 | s = xs_str_cat(s, ss); | ||
| 108 | s = xs_str_cat(s, "<br>"); | ||
| 109 | } | ||
| 110 | |||
| 111 | if (in_blq) | ||
| 112 | s = xs_str_cat(s, "</blockquote>"); | ||
| 113 | if (in_pre) | ||
| 114 | s = xs_str_cat(s, "</pre>"); | ||
| 115 | |||
| 116 | /* some beauty fixes */ | ||
| 117 | s = xs_replace_i(s, "</blockquote><br>", "</blockquote>"); | ||
| 118 | |||
| 119 | *f_content = s; | ||
| 120 | |||
| 121 | return *f_content; | ||
| 122 | } | ||
| 123 | |||
| 124 | |||
| 125 | int login(snac *snac, char *headers) | 15 | int login(snac *snac, char *headers) |
| 126 | /* tries a login */ | 16 | /* tries a login */ |
| 127 | { | 17 | { |