From 6459f187970db82528d1be0eb0dc2c05f38dc8ae Mon Sep 17 00:00:00 2001 From: Alexander Griffith Date: Mon, 24 Apr 2017 02:44:24 -0400 Subject: Fixed #41, inline images --- lisp/mastodon-media.el | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++ lisp/mastodon-tl.el | 8 +++-- 2 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 lisp/mastodon-media.el (limited to 'lisp') diff --git a/lisp/mastodon-media.el b/lisp/mastodon-media.el new file mode 100644 index 0000000..6ee0138 --- /dev/null +++ b/lisp/mastodon-media.el @@ -0,0 +1,97 @@ +;;; mastodon-tl.el --- HTTP request/response functions for mastodon.el + +;; Copyright (C) 2017 Johnson Denen +;; Author: Johnson Denen +;; Homepage: https://github.com/jdenen/mastodon.el + +;; This file is not part of GNU Emacs. + +;; This file is part of mastodon.el. + +;; mastodon.el is free software: you can redistribute it and/or modify +;; it under the terms of the GNU General Public License as published by +;; the Free Software Foundation, either version 3 of the License, or +;; (at your option) any later version. + +;; mastodon.el is distributed in the hope that it will be useful, +;; but WITHOUT ANY WARRANTY; without even the implied warranty of +;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +;; GNU General Public License for more details. + +;; You should have received a copy of the GNU General Public License +;; along with mastodon.el. If not, see . + +;;; Commentary: + +;; mastodon-media.el provides functions for inlining media. + +;;; Code: + +(defun mastodon-media--image-from-url (url) + "Takes a url and returns an image" + (let ((buffer (url-retrieve-synchronously url))) + (unwind-protect + (let ((data (with-current-buffer buffer + (goto-char (point-min)) + (search-forward "\n\n") + (buffer-substring (point) (point-max))))) + (insert "\n") + (insert-image (create-image data nil t))) + (kill-buffer buffer)))) + +(defun mastodon-media--select-media-line(start) + "Returns the list of line coordinates of a line that + +contains `Media_Links::'" + (when start (goto-char (point-min))) + (search-forward-regexp "Media_Link::" nil nil nil) + (let ((start (progn (move-beginning-of-line '()) (point))) + (end (progn (move-end-of-line '()) (point)))) + (list start end))) + +(defun mastodon-media--select-first-media-line() + (mastodon-media--select-media-line 1)) + +(defun mastodon-media--check-missing(link) + "Checks to make sure that the missing string has + +not been returned." + (let((missing "/files/small/missing.png")) + (not(equal link missing)))) + +(defun mastodon-media--select-next-media-line() + (mastodon-media--select-media-line '())) + +(defun mastodon-media--line-to-link(line) + "Removes the identifier from the media line leaving + +just a url" + (replace-regexp-in-string "Media_Link:: " "" + (buffer-substring + (car line) + (cadr line)))) + +(defun mastodon-media--delete-line(line) + "Deletes the current media line" + (delete-region (car line) (cadr line))) + +(defun mastodon-media--inline-images-aux ( not-first) + "Recursivly goes through all of the `Media_Links:' in the buffer" + (let* ((line (mastodon-media--select-media-line (not not-first))) + (link (mastodon-media--line-to-link line))) + (when (mastodon-media--check-missing link) + (progn (mastodon-media--image-from-url link) + (mastodon-media--delete-line line)))) + (mastodon-inline-images-aux 1)) + +(defun mastodon-media--inline-images() + "A wrapper for the `mastodon-media--inline-images-aux' that catches + +errors thrown by reaching the end of the buffer" + (interactive) + (condition-case nil + (progn (mastodon-media--inline-images-aux '()) t) + (error nil))) + +(provide 'mastodon-media) +;;; mastodon-media.el ends here diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el index 2d323b9..385580e 100644 --- a/lisp/mastodon-tl.el +++ b/lisp/mastodon-tl.el @@ -28,8 +28,9 @@ ;;; Code: -(require 'mastodon-http nil t) -(require 'mastodon-toot nil t) +(require 'mastodon-http) +(require 'mastodon-toot) +(require 'mastodon-media) (defgroup mastodon-tl nil "Timelines in Mastodon." @@ -203,7 +204,8 @@ also render the html" (defun mastodon-tl--timeline (toots) "Display each toot in TOOTS." (mapcar 'mastodon-tl--toot toots) - (replace-regexp "\n\n\n | " "\n | " nil (point-min) (point-max))) + (replace-regexp "\n\n\n | " "\n | " nil (point-min) (point-max)) + (mastodon-media--inline-images)) (defun mastodon-tl--more-json (timeline id) "Return JSON for TIMELINE before ID." -- cgit v1.2.3