1
0
Fork 0

- [gemini] added the option to view gemini page's source.

This commit is contained in:
cage 2020-07-15 12:40:30 +02:00
parent 848c543b1f
commit fb0fd48865
6 changed files with 104 additions and 18 deletions

View File

@ -301,6 +301,8 @@
(define-key "b" #'gemini-history-back *gemini-message-keymap*)
(define-key "U" #'gemini-view-source *gemini-message-keymap*)
;; tags keymap
(define-key "up" #'tag-go-up *tags-keymap*)

View File

@ -19,7 +19,8 @@
(defstruct gemini-metadata
(links)
(history))
(history)
(source-file))
(defun add-url-to-history (window url)
(let* ((metadata (message-window:metadata window))
@ -50,7 +51,7 @@
gemini-client:+gemini-default-port+)))
(handler-case
(progn
(multiple-value-bind (status x meta body gemini-text gemini-links)
(multiple-value-bind (status x meta response)
(gemini-client:request host
path
:query query
@ -90,17 +91,19 @@
((gemini-client:response-sensitive-input-p status)
(error 'conditions:not-implemented-error
:text "Sensitive input not implemented"))
(gemini-text
((gemini-client:gemini-file-response-p response)
(setf (message-window:source-text *message-window*)
gemini-text)
(gemini-client:rendered-file response))
(setf (gemini-metadata-links (message-window:metadata *message-window*))
gemini-links)
(gemini-client:links response))
(setf (gemini-metadata-source-file (message-window:metadata *message-window*))
(gemini-client:source response))
(setf (keybindings *message-window*)
keybindings:*gemini-message-keymap*)
(draw *message-window*))
(t
(fs:with-anaphoric-temp-file (stream)
(write-sequence body stream)
(write-sequence response stream)
(force-output stream)
(os-utils:xdg-open fs:temp-file))))))
(gemini-client:gemini-tofu-error (e)
@ -137,3 +140,11 @@
history)
(ui:info-message (format nil (_ "Going back to: ~a") last))
(request last)))
(defun view-source (window)
(when-let* ((metadata (message-window:metadata window))
(source (gemini-metadata-source-file metadata))
(last (misc:safe-last-elt (gemini-metadata-history metadata))))
(setf (message-window:source-text window) source)
(draw window)
(ui:info-message (format nil (_ "Viewing source of ~a") last))))

View File

@ -180,7 +180,58 @@
:h3-prefix (swconf:gemini-h3-prefix)
:bullet-prefix (swconf:gemini-bullet-prefix))))
(defun parse-response (stream host port path &key (theme *gemini-page-theme*))
(defclass gemini-file-response ()
((status-code
:initform nil
:initarg :status-code
:accessor status-code)
(status-code-message
:initform ""
:initarg :status-code-message
:accessor status-code-message)
(meta
:initform nil
:initarg :meta
:accessor meta)
(parsed-file
:initform nil
:initarg :parsed-file
:accessor parsed-file)
(rendered-file
:initform nil
:initarg :rendered-file
:accessor rendered-file)
(source-url
:initform nil
:initarg :source-url
:accessor source-url)
(source
:initform nil
:initarg :source
:accessor source)
(links
:initform nil
:initarg :links
:accessor links)))
(defun gemini-file-response-p (object)
(typep object 'gemini-file-response))
(defun make-gemini-file-response (status-code status-code-message
meta parsed-file
rendered-file source-url
source links)
(make-instance 'gemini-file-response
:status-code status-code
:status-code-message status-code-message
:meta meta
:parsed-file parsed-file
:rendered-file rendered-file
:source-url source-url
:source source
:links links))
(defun parse-response (stream host port path query &key (theme *gemini-page-theme*))
(let* ((header-raw (misc:list->array (loop for c = (read-byte stream)
while (/= c 10)
collect c)
@ -198,18 +249,23 @@
((header-success-p parsed-header)
(let ((body (read-all stream)))
(if (mime-gemini-p meta)
(let ((parsed (parse-gemini-file (babel:octets-to-string body
:errorp nil))))
(let* ((file-string (babel:octets-to-string body :errorp nil))
(parsed (parse-gemini-file file-string))
(url (make-gemini-uri host path query)))
(values status-code
(description +20+)
meta
parsed
(format nil
"-> ~a://~a:~a~a~2%~a"
+gemini-scheme+
host port path
(sexp->text parsed theme))
(sexp->links parsed host port path)))
(make-gemini-file-response status-code
(description +20+)
meta
parsed
(format nil
"-> ~a~2%~a"
url
(sexp->text parsed theme))
url
file-string
(sexp->links parsed host port path))))
(results +20+ body))))
((or (header-input-request-p parsed-header)
(header-redirect-p parsed-header))
@ -252,7 +308,7 @@
(write-sequence (babel:string-to-octets request) ssl-stream)
(force-output ssl-stream)
(multiple-value-bind (status description meta body gemini-text gemini-links)
(parse-response ssl-stream host port path)
(parse-response ssl-stream host port path query)
(values status description meta body gemini-text
gemini-links)))))))
(when socket

View File

@ -85,4 +85,14 @@
:response-redirect-p
:absolute-url-p
:init-default-gemini-theme
:gemini-file-response
:status-code
:status-code-message
:meta
:parsed-file
:rendered-file
:source-url
:source
:links
:gemini-file-response-p
:request))

View File

@ -1901,10 +1901,12 @@
:gemini-metadata-p
:make-gemini-metadata
:gemini-metadata-links
:gemini-metadata-source-file
:gemini-metadata-p
:copy-gemini-metadata
:add-url-to-history
:history-back
:view-source
:request))
(defpackage :main-window
@ -2060,7 +2062,8 @@
:reset-timeline-pagination
:poll-vote
:open-gemini-address
:gemini-history-back))
:gemini-history-back
:gemini-view-source))
(defpackage :modules
(:use

View File

@ -1355,3 +1355,7 @@ This command will remove those limits so that we can just jump to the last messa
(defun gemini-history-back ()
"Reopen a previous visited gemini address"
(push-event (make-instance 'gemini-back-event)))
(defun gemini-view-source ()
"Shows the source of current gemini page"
(gemini-viewer:view-source specials:*message-window*))