emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[nongnu] elpa/xkcd a565d632fd 41/82: Support gif and jpg image extension


From: ELPA Syncer
Subject: [nongnu] elpa/xkcd a565d632fd 41/82: Support gif and jpg image extensions.
Date: Mon, 12 Dec 2022 21:59:52 -0500 (EST)

branch: elpa/xkcd
commit a565d632fd4b3f3787d200de79d521bff51f16fc
Author: Vibhav Pant <vibhavp@gmail.com>
Commit: Vibhav Pant <vibhavp@gmail.com>

    Support gif and jpg image extensions.
---
 README.md |  5 +----
 xkcd.el   | 45 ++++++++++++++++++++++++++++++++-------------
 2 files changed, 33 insertions(+), 17 deletions(-)

diff --git a/README.md b/README.md
index 506433b19d..59c3b3167c 100644
--- a/README.md
+++ b/README.md
@@ -24,7 +24,7 @@ Clone this repository to a desired location, and add the 
following snippet to yo
 
 Files are cached by default to ~/.emacs.d/xkcd/. This can be changed by 
changing `xkcd-cache-dir` in the group "xkcd". (customize-group xkcd)
 
-`xkcd-get-latest` loads up the latest xkcd.
+`xkcd` loads up the latest xkcd.
 # Current keybindings:
 | Keybinding | Use                            |  Function      |
 |:----------:|:------------------------------:|:--------------:|
@@ -33,8 +33,5 @@ Files are cached by default to ~/.emacs.d/xkcd/. This can be 
changed by changing
 | `<right>`  | Load next xkcd                 | (xkcd-next)    |
 | `<left>`   | Load previous xkcd             | (xkcd-prev)    |
 
-#Bugs
-Some comics using a different image extension do not to load.
-
 #TODO
 Add support for custom faces.
diff --git a/xkcd.el b/xkcd.el
index 94963bf61c..08acf65c99 100644
--- a/xkcd.el
+++ b/xkcd.el
@@ -81,15 +81,27 @@ be located in xkcd-cache-dir"
       (kill-buffer (current-buffer)))
     json-string))
 
+(defun xkcd-get-image-type (url)
+  (let ((substr (substring url (- (length url) 3))))
+   (cond
+    ((string= substr "png")
+     'png)
+    ((string= substr "jpg")
+     'jpg)
+    (t 'gif))))
+
 (defun xkcd-download (url num)
   "Download the image linked by URL. If the file arleady exists, do nothing"
   ;;check if the cache directory exists
   (if (not (file-exists-p xkcd-cache-dir))
       (make-directory xkcd-cache-dir))
-  (let ((name (concat xkcd-cache-dir (number-to-string num) ".png")))
+  (let ((name (concat xkcd-cache-dir (number-to-string num) "." (substring
+                                                            url
+                                                            (- (length url) 
3)))))
     (if (file-exists-p name)
-       nil
-      (url-copy-file url name))))
+       name
+      (url-copy-file url name))
+    name))
 
 (defun xkcd-cache-json (num json-string)
   "Save xkcd NUM's JSON-STRING to cache directory, and write xkcd-latest to a 
file"
@@ -110,6 +122,19 @@ be located in xkcd-cache-dir"
        (save-buffer)
        (kill-buffer (current-buffer))))))
 
+(defun xkcd-insert-image (file num)
+  "Insert image FILENAME in buffer with the title-text, and animate if 
necessary"
+  (let ((image (create-image (concat xkcd-cache-dir
+                                    (number-to-string num)
+                                    "."
+                                    (substring file (- (length file) 3)))
+                            (xkcd-get-image-type file)))
+       (start (point)))
+    (insert-image image)
+    (if (image-multi-frame-p image)
+       (image-animate image 0 t))
+    (add-text-properties start (point) '(help-echo xkcd-alt))))
+
 ;;;###autoload
 (defun xkcd-get (num)
   "Get the xkcd number NUM"
@@ -129,24 +154,18 @@ be located in xkcd-cache-dir"
                                      "/info.0.json") num)))
        (img nil)
        (num nil)
-       (title nil))
+       (title nil)
+       (file nil))
     (setq num (cdr (assoc 'num (json-read-from-string out))))
     (setq img (cdr (assoc 'img (json-read-from-string out))))
     
     ;; FIXME: This looks pretty ugly.
     (message "Getting comic...")
-    (xkcd-download img num)
+    (setq file (xkcd-download img num))
     (setq title (format "%d: %s" (cdr (assoc 'num (json-read-from-string out)))
                        (cdr (assoc 'safe_title (json-read-from-string out)))))
     (insert (concat title "\n"))
-    (let ((start (point)))
-      (insert-image (create-image
-                     (concat xkcd-cache-dir
-                             (number-to-string
-                              (cdr
-                               (assoc 'num (json-read-from-string out)))) 
".png") 'png))
-      (add-text-properties start (point) '(help-echo xkcd-alt))
-      )
+    (xkcd-insert-image file num)
     (if (eq xkcd-cur 0)
        (setq xkcd-cur (cdr (assoc 'num (json-read-from-string out)))))
     (xkcd-cache-json num out)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]