Browse Source

Added support for markdown-like ![alt text](image url).

default 9 months ago
parent
commit
5f6a42453c
2 changed files with 36 additions and 0 deletions
  1. 5 0
      doc/snac.5
  2. 31 0
      format.c

+ 5 - 0
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.

+ 31 - 0
format.c

@@ -91,6 +91,7 @@ static xs_str *format_line(const char *line, xs_list **attach)
             "`[^`]+`"                           "|"
             "~~[^~]+~~"                         "|"
             "\\*\\*?\\*?[^\\*]+\\*?\\*?\\*"     "|"
+            "!\\[[^]]+\\]\\([^\\)]+\\)"         "|"
             "\\[[^]]+\\]\\([^\\)]+\\)"          "|"
             "https?:/" "/[^[:space:]]+"
         ")");
@@ -169,6 +170,36 @@ static xs_str *format_line(const char *line, xs_list **attach)
                 else
                     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);
         }