From 49bd378f9af4cce5bb299252e2fe503ac8ad8434 Mon Sep 17 00:00:00 2001 From: Vasilij Schneidermann Date: Sat, 9 Sep 2017 21:13:39 +0200 Subject: Force shr-tag-img override, avoid infinite loop Closes #3 --- nov.el | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/nov.el b/nov.el index 90fd67d..d5dab0f 100644 --- a/nov.el +++ b/nov.el @@ -40,6 +40,7 @@ ;;; Code: +(require 'cl-lib) (require 'dash) (require 'esxml) (require 'esxml-query) @@ -360,13 +361,19 @@ This function honors `shr-max-image-proportion' if possible." (nth 1 edges))))))) (insert-image (create-image path nil nil :ascent 100)))) +(defvar nov-original-shr-tag-img-function + (symbol-function 'shr-tag-img)) + (defun nov-render-img (dom) "Custom rendering function for DOM. Uses `shr-tag-img' for external paths and `nov-insert-image' for internal ones." (let ((url (cdr (assq 'src (cadr dom))))) (if (nov-external-url-p url) - (funcall 'shr-tag-img dom) + ;; HACK: avoid hanging in an infinite loop when using + ;; `cl-letf' to override `shr-tag-img' with a function that + ;; might call `shr-tag-img' again + (funcall nov-original-shr-tag-img-function dom) (setq url (expand-file-name url)) (nov-insert-image url)))) @@ -421,7 +428,10 @@ the HTML is rendered with `shr-render-region'." (shr-map nov-mode-map) (shr-external-rendering-functions nov-rendering-functions) (shr-use-fonts nov-variable-pitch)) - (shr-render-region (point-min) (point-max)))) + ;; HACK: `shr-external-rendering-functions' doesn't cover + ;; every usage of `shr-tag-img' + (cl-letf (((symbol-function 'shr-tag-img) 'nov-render-img)) + (shr-render-region (point-min) (point-max))))) (goto-char (point-min)))) (defun nov-find-document (predicate) -- cgit v1.2.3