Added HTML selector rendering.
authorTim Vaughan <tgvaughan@gmail.com>
Fri, 14 Jun 2019 23:36:17 +0000 (01:36 +0200)
committerTim Vaughan <tgvaughan@gmail.com>
Fri, 14 Jun 2019 23:36:17 +0000 (01:36 +0200)
elpher.el

index 992211e..4b08a50 100644 (file)
--- a/elpher.el
+++ b/elpher.el
@@ -1,4 +1,4 @@
-;;; elpher.el --- a friendly gopher client.
+;;; elpher.el --- A friendly gopher client.
 
 ;; Copyright (C) 2019 Tim Vaughan
 
@@ -44,7 +44,7 @@
 ;; page containing information on key bindings and suggested starting
 ;; points for your gopher exploration.
 
-;; Faces, caching options and start page can be configured via
+;; Faces, caching and other options can be configured via
 ;; the Elpher customization group in Applications.
 
 ;;; Code:
@@ -52,6 +52,7 @@
 (provide 'elpher)
 (require 'seq)
 (require 'pp)
+(require 'shr)
 
 ;;; Global constants
 ;;
     (?g elpher-get-image-node "im" elpher-image)
     (?p elpher-get-image-node "im" elpher-image)
     (?I elpher-get-image-node "im" elpher-image)
+    (?d elpher-get-node-download "d" elpher-binary)
     (?h elpher-get-url-node "W" elpher-url)
     (bookmarks elpher-get-bookmarks-node "#" elpher-index)
     (start elpher-get-start-node "#" elpher-index))
@@ -218,6 +220,7 @@ special address types, such as 'start for the start page."
   (elt address 3))
 
 (defun elpher-address-special-p (address)
+  "Return non-nil if ADDRESS is special (e.g. start page, bookmarks page)."
   (not (elpher-address-host address)))
 
 ;; Node
@@ -653,15 +656,42 @@ calls, as is necessary if the match is performed by `string-match'."
 
 ;; URL retrieval
 
+(defun elpher-insert-rendered-html (string)
+  "Use shr to insert rendered view of html STRING into current buffer."
+  (let ((dom (with-temp-buffer
+               (insert string)
+               (libxml-parse-html-region (point-min) (point-max)))))
+    (shr-insert-document dom)))
+
 (defun elpher-get-url-node ()
   "Getter which attempts to open the URL specified by the current node."
   (let* ((address (elpher-node-address elpher-current-node))
          (selector (elpher-address-selector address)))
-    (elpher-visit-parent-node) ; Do first in case of non-local exits.
     (let ((url (elt (split-string selector "URL:") 1)))
-      (if elpher-open-urls-with-eww
-          (browse-web url)
-        (browse-url url)))))
+      (if url
+          (progn
+            (elpher-visit-parent-node) ; Do first in case of non-local exits.
+            (message "Opening URL...")
+            (if elpher-open-urls-with-eww
+                (browse-web url)
+              (browse-url url)))
+        (let ((content (elpher-get-cached-content address)))
+          (if content
+              (progn
+                (elpher-with-clean-buffer
+                 (insert content)
+                 (elpher-restore-pos)))
+            (elpher-with-clean-buffer
+             (insert "LOADING HTML... (use 'u' to cancel)"))
+            (elpher-get-selector address
+                                 (lambda (proc event)
+                                   (unless (string-prefix-p "deleted" event)
+                                     (elpher-with-clean-buffer
+                                      (elpher-insert-rendered-html elpher-selector-string)
+                                      (goto-char (point-min))
+                                      (elpher-cache-content
+                                       (elpher-node-address elpher-current-node)
+                                       (buffer-string))))))))))))
 
 ;; Telnet node connection
 
@@ -684,7 +714,7 @@ calls, as is necessary if the match is performed by `string-match'."
 ;; Bookmarks page node retrieval
 
 (defun elpher-get-bookmarks-node ()
-  "Getter which loads and displays the current bookmark list."
+  "Getter to load and display the current bookmark list."
   (elpher-with-clean-buffer
    (insert "---- Bookmark list ----\n\n")
    (let ((bookmarks (elpher-load-bookmarks)))
@@ -703,7 +733,7 @@ calls, as is necessary if the match is performed by `string-match'."
            "- x: delete selected bookmark\n"
            "- a: rename selected bookmark\n\n"
            "Bookmarks are stored in the file "
-           (locate-user-emacs-file "elpher-bookmarks")) 
+           (locate-user-emacs-file "elpher-bookmarks"))
    (elpher-restore-pos)))
   
 
@@ -822,7 +852,7 @@ host, selector and port."
   (interactive)
   (if elpher-current-node
       (if (elpher-address-special-p (elpher-node-address elpher-current-node))
-          (error "This page was not generated by a server.")
+          (error "This page was not generated by a server")
         (elpher-visit-node elpher-current-node
                            #'elpher-get-node-raw))
     (message "No current site.")))