summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--doc/snac.55
-rw-r--r--format.c31
2 files changed, 36 insertions, 0 deletions
diff --git a/doc/snac.5 b/doc/snac.5
index 1b07155..fc991b2 100644
--- a/doc/snac.5
+++ b/doc/snac.5
@@ -43,6 +43,11 @@ int main(int argc, char *argv[])
Standalone URLs are converted to links. Also, from version 2.54,
markdown-style links in the form of [link label](url) are also
supported.
+.It attached images
+Standalone URLs for which the final extension is recognized as an
+image (.jpg, .gif, .png, etc), are converted to ActivityPub image
+attachments. Also, from version 2.57, markdown-style image links
+in the form of ![alt text](image url) are also supported.
.It line separators
Horizonal rules can be inserted by typing three minus symbols
alone in a line.
diff --git a/format.c b/format.c
index 170d28a..573b702 100644
--- a/format.c
+++ b/format.c
@@ -91,6 +91,7 @@ static xs_str *format_line(const char *line, xs_list **attach)
"`[^`]+`" "|"
"~~[^~]+~~" "|"
"\\*\\*?\\*?[^\\*]+\\*?\\*?\\*" "|"
+ "!\\[[^]]+\\]\\([^\\)]+\\)" "|"
"\\[[^]]+\\]\\([^\\)]+\\)" "|"
"https?:/" "/[^[:space:]]+"
")");
@@ -170,6 +171,36 @@ static xs_str *format_line(const char *line, xs_list **attach)
s = xs_str_cat(s, v);
}
else
+ if (*v == '!') {
+ /* markdown-like images ![alt text](url to image) */
+ xs *w = xs_strip_chars_i(xs_replace(v, "#", "#"), "![)");
+ xs *l = xs_split_n(w, "](", 1);
+
+ if (xs_list_len(l) == 2) {
+ const char *alt_text = xs_list_get(l, 0);
+ const char *img_url = xs_list_get(l, 1);
+ const char *mime = xs_mime_by_ext(img_url);
+
+ if (attach != NULL && xs_startswith(mime, "image/")) {
+ xs *d = xs_dict_new();
+
+ d = xs_dict_append(d, "mediaType", mime);
+ d = xs_dict_append(d, "url", img_url);
+ d = xs_dict_append(d, "name", alt_text);
+ d = xs_dict_append(d, "type", "Image");
+
+ *attach = xs_list_append(*attach, d);
+ }
+ else {
+ xs *link = xs_fmt("<a href=\"%s\">%s</a>", img_url, alt_text);
+
+ s = xs_str_cat(s, link);
+ }
+ }
+ else
+ s = xs_str_cat(s, v);
+ }
+ else
s = xs_str_cat(s, v);
}
else