diff options
Diffstat (limited to 'format.c')
| -rw-r--r-- | format.c | 79 |
1 files changed, 73 insertions, 6 deletions
| @@ -5,6 +5,8 @@ | |||
| 5 | #include "xs_regex.h" | 5 | #include "xs_regex.h" |
| 6 | #include "xs_mime.h" | 6 | #include "xs_mime.h" |
| 7 | #include "xs_html.h" | 7 | #include "xs_html.h" |
| 8 | #include "xs_json.h" | ||
| 9 | #include "xs_time.h" | ||
| 8 | 10 | ||
| 9 | #include "snac.h" | 11 | #include "snac.h" |
| 10 | 12 | ||
| @@ -36,6 +38,46 @@ const char *smileys[] = { | |||
| 36 | }; | 38 | }; |
| 37 | 39 | ||
| 38 | 40 | ||
| 41 | xs_dict *emojis(void) | ||
| 42 | /* returns a dict with the emojis */ | ||
| 43 | { | ||
| 44 | xs *fn = xs_fmt("%s/emojis.json", srv_basedir); | ||
| 45 | FILE *f; | ||
| 46 | |||
| 47 | if (mtime(fn) == 0) { | ||
| 48 | /* file does not exist; create it with the defaults */ | ||
| 49 | xs *d = xs_dict_new(); | ||
| 50 | const char **emo = smileys; | ||
| 51 | |||
| 52 | while (*emo) { | ||
| 53 | d = xs_dict_append(d, emo[0], emo[1]); | ||
| 54 | emo += 2; | ||
| 55 | } | ||
| 56 | |||
| 57 | if ((f = fopen(fn, "w")) != NULL) { | ||
| 58 | xs_json_dump(d, 4, f); | ||
| 59 | fclose(f); | ||
| 60 | } | ||
| 61 | else | ||
| 62 | srv_log(xs_fmt("Error creating '%s'", fn)); | ||
| 63 | } | ||
| 64 | |||
| 65 | xs_dict *d = NULL; | ||
| 66 | |||
| 67 | if ((f = fopen(fn, "r")) != NULL) { | ||
| 68 | d = xs_json_load(f); | ||
| 69 | fclose(f); | ||
| 70 | |||
| 71 | if (d == NULL) | ||
| 72 | srv_log(xs_fmt("JSON parse error in '%s'", fn)); | ||
| 73 | } | ||
| 74 | else | ||
| 75 | srv_log(xs_fmt("Error opening '%s'", fn)); | ||
| 76 | |||
| 77 | return d; | ||
| 78 | } | ||
| 79 | |||
| 80 | |||
| 39 | static xs_str *format_line(const char *line, xs_list **attach) | 81 | static xs_str *format_line(const char *line, xs_list **attach) |
| 40 | /* formats a line */ | 82 | /* formats a line */ |
| 41 | { | 83 | { |
| @@ -106,7 +148,7 @@ static xs_str *format_line(const char *line, xs_list **attach) | |||
| 106 | } | 148 | } |
| 107 | 149 | ||
| 108 | 150 | ||
| 109 | xs_str *not_really_markdown(const char *content, xs_list **attach) | 151 | xs_str *not_really_markdown(const char *content, xs_list **attach, xs_list **tag) |
| 110 | /* formats a content using some Markdown rules */ | 152 | /* formats a content using some Markdown rules */ |
| 111 | { | 153 | { |
| 112 | xs_str *s = xs_str_new(NULL); | 154 | xs_str *s = xs_str_new(NULL); |
| @@ -190,11 +232,36 @@ xs_str *not_really_markdown(const char *content, xs_list **attach) | |||
| 190 | 232 | ||
| 191 | { | 233 | { |
| 192 | /* traditional emoticons */ | 234 | /* traditional emoticons */ |
| 193 | const char **emo = smileys; | 235 | xs *d = emojis(); |
| 194 | 236 | int c = 0; | |
| 195 | while (*emo) { | 237 | char *k, *v; |
| 196 | s = xs_replace_i(s, emo[0], emo[1]); | 238 | |
| 197 | emo += 2; | 239 | while (xs_dict_next(d, &k, &v, &c)) { |
| 240 | const char *t = NULL; | ||
| 241 | |||
| 242 | /* is it an URL to an image? */ | ||
| 243 | if (xs_startswith(v, "https:/" "/") && xs_startswith((t = xs_mime_by_ext(v)), "image/")) { | ||
| 244 | if (tag) { | ||
| 245 | /* add the emoji to the tag list */ | ||
| 246 | xs *e = xs_dict_new(); | ||
| 247 | xs *i = xs_dict_new(); | ||
| 248 | xs *u = xs_str_utctime(0, ISO_DATE_SPEC); | ||
| 249 | |||
| 250 | e = xs_dict_append(e, "id", v); | ||
| 251 | e = xs_dict_append(e, "type", "Emoji"); | ||
| 252 | e = xs_dict_append(e, "name", k); | ||
| 253 | e = xs_dict_append(e, "updated", u); | ||
| 254 | |||
| 255 | i = xs_dict_append(i, "type", "Image"); | ||
| 256 | i = xs_dict_append(i, "mediaType", t); | ||
| 257 | i = xs_dict_append(i, "url", v); | ||
| 258 | e = xs_dict_append(e, "icon", i); | ||
| 259 | |||
| 260 | *tag = xs_list_append(*tag, e); | ||
| 261 | } | ||
| 262 | } | ||
| 263 | else | ||
| 264 | s = xs_replace_i(s, k, v); | ||
| 198 | } | 265 | } |
| 199 | } | 266 | } |
| 200 | 267 | ||