From cb8502f9ee8c3c393ba44613519bd5490ecf9775 Mon Sep 17 00:00:00 2001 From: Johnson Denen Date: Fri, 14 Apr 2017 09:27:42 -0400 Subject: Retrieve home timeline with `mastodon' function --- lisp/mastodon-auth.el | 1 - lisp/mastodon-http.el | 1 - lisp/mastodon-tl.el | 94 +++++++++++++++++++++++++++++++++++++++++++++++ lisp/mastodon.el | 11 +++++- test/mastodon-tl-tests.el | 16 ++++++++ 5 files changed, 119 insertions(+), 4 deletions(-) create mode 100644 lisp/mastodon-tl.el create mode 100644 test/mastodon-tl-tests.el diff --git a/lisp/mastodon-auth.el b/lisp/mastodon-auth.el index ef16ab9..69404ac 100644 --- a/lisp/mastodon-auth.el +++ b/lisp/mastodon-auth.el @@ -28,7 +28,6 @@ ;;; Code: (require 'plstore) -(require 'mastodon) (require 'mastodon-http) (defgroup mastodon-auth nil diff --git a/lisp/mastodon-http.el b/lisp/mastodon-http.el index b6cc879..35af12c 100644 --- a/lisp/mastodon-http.el +++ b/lisp/mastodon-http.el @@ -27,7 +27,6 @@ ;;; Code: -(require 'mastodon) (require 'json) (defgroup mastodon-http nil diff --git a/lisp/mastodon-tl.el b/lisp/mastodon-tl.el new file mode 100644 index 0000000..e782631 --- /dev/null +++ b/lisp/mastodon-tl.el @@ -0,0 +1,94 @@ +;;; 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-tl.el provides timeline functions. + +;;; Code: + +(require 'mastodon-http) + +(defgroup mastodon-tl nil + "Timelines in Mastodon." + :prefix "mastodon-tl-" + :group 'mastodon) + +(defface mastodon-tl-toot-text-face + '((t (:foreground "LightGray"))) + "Toot content face.") + +(defface mastodon-tl-toot-display-name-face + '((t (:foreground "cyan"))) + "Mastodon user handle face.") + +(defun mastodon-tl--from-toot (key toot) + "Return value for KEY in TOOT." + (cdr (assoc key toot))) + +(defun mastodon-tl--render-display-name (name) + (insert + (propertize (concat name " ") + 'face 'mastodon-tl-toot-display-name-face))) + +(defun mastodon-tl--render-header (handle name id url) + (insert "=== ") + (mastodon-tl--render-display-name name) + (insert (concat "@" handle "\n"))) + +(defun mastodon-tl--remove-html (toot) + (let* ((t1 (replace-regexp-in-string "<\/p>" "\n\n" toot)) + (t2 (replace-regexp-in-string "<\/?span>" "" t1)) + (t3 (replace-regexp-in-string "" "" t2))) + t3)) + +(defun mastodon-tl--render-content (toot) + (let* ((content (mastodon-tl--remove-html toot))) + (insert + (propertize content + 'face 'mastodon-tl-toot-text-face)) + (insert "\n"))) + +(defun mastodon-tl--render-toot (toot) + (let ((id (number-to-string (mastodon-tl--from-toot 'id toot))) + (handle (mastodon-tl--from-toot 'acct (mastodon-tl--from-toot 'account toot))) + (name (mastodon-tl--from-toot 'display_name (mastodon-tl--from-toot 'account toot))) + (text (mastodon-tl--from-toot 'content toot)) + (url (mastodon-tl--from-toot 'url toot))) + (mastodon-tl--render-header handle name id url) + (mastodon-tl--render-content text))) + +(defun mastodon-tl--render-timeline (buffer json) + (with-output-to-temp-buffer buffer + (switch-to-buffer buffer) + (mapcar 'mastodon-tl--render-toot json)) + (html2text)) + +(defun mastodon-tl--get (timeline) + (let* ((url (mastodon--api-for (concat "timelines/" timeline))) + (tl-buff (concat "*mastodon-" timeline "*")) + (tl-json (mastodon-http--get-json url))) + (mastodon-tl--render-timeline tl-buff tl-json))) + +(provide 'mastodon-tl) +;;; mastodon-tl.el ends here diff --git a/lisp/mastodon.el b/lisp/mastodon.el index 07f9f6c..eb5718c 100644 --- a/lisp/mastodon.el +++ b/lisp/mastodon.el @@ -30,6 +30,8 @@ ;;; Code: +(require 'mastodon-auth) + (defgroup mastodon nil "Interface with Mastodon." :prefix "mastodon-" @@ -55,12 +57,18 @@ (message (concat "mastodon.el v" (buffer-string))))) +;;;###autoload +(defun mastodon () + (interactive) + (require 'mastodon-tl) + (mastodon-tl--get "home")) + ;;;###autoload (defun mastodon-toot () "Update a Mastodon instance with new toot. Content is captured in a new buffer." (interactive) + (require 'mastodon-toot) (progn - (require 'mastodon-toot) (switch-to-buffer-other-window (get-buffer-create "*new toot*")) (mastodon-toot-mode t))) @@ -69,7 +77,6 @@ "Registers mastodon.el with the Mastodon instance." (interactive) (progn - (require 'mastodon-auth) (mastodon--store-client-id-and-secret))) (provide 'mastodon) diff --git a/test/mastodon-tl-tests.el b/test/mastodon-tl-tests.el new file mode 100644 index 0000000..5571e70 --- /dev/null +++ b/test/mastodon-tl-tests.el @@ -0,0 +1,16 @@ +(require 'el-mock) +(load-file "../lisp/mastodon-tl.el") + +(ert-deftest mastodon-tl:from-toot () + "Should return the value for KEY in a list." + (should (string= (mastodon-tl--from-toot "foo" '(("foo" . "bar"))) "bar"))) + +(ert-deftest mastodon-tl:remove-html:remove-p-and-span () + "Should remove

and tags that are not parsed by `html2text'." + (let ((input "

foobar

")) + (should (string= (mastodon-tl--remove-html input) "foobar\n")))) + +(ert-deftest mastodon-tl:remove-html:remove-hcard-span () + "Should remove tags with a class of 'h-card'." + (let ((input "foobar")) + (should (string= (mastodon-tl--remove-html input) "foobar")))) -- cgit v1.2.3