💾 Archived View for mederle.de › b › i › elpher-org.el captured on 2023-04-26 at 13:21:20.

View Raw

More Information

⬅️ Previous capture (2021-12-03)

-=-=-=-=-=-=-

(use-package elpher
  :ensure t
  :config
  (defun elpher-render-org (data &optional _mime-type-string)
    "Render DATA as Org by opening a new buffer and switching to Org mode"
    (let ((buf (generate-new-buffer "elpher-org")))
      (with-current-buffer buf
        (if (not data)
            t
          (progn (switch-to-buffer buf)
                 (setq elpher-org-map (make-sparse-keymap))
                 (define-key elpher-org-map (kbd "b") (lambda () (interactive)
                                                        (kill-current-buffer)
                                                        (switch-to-buffer "*elpher*")
                                                        (elpher-back)))
                 (define-key elpher-org-map (kbd "W") (lambda () (interactive) (save-buffer default-directory t)))
                 (make-local-variable 'org-confirm-babel-evaluate)
                 (setq org-confirm-babel-evaluate t)
                 (org-mode)
                 (insert data)
                 (view-mode t)
                 (use-local-map elpher-org-map)
                 (message "Use W to save to file, b to return to elpher. View-mode keybindings apply."))))))
  (defun elpher-render-gemini (body &optional mime-type-string)
    "Render gemini response BODY with rendering MIME-TYPE-STRING."
    (if (not body)
        t
      (let* ((mime-type-string* (if (or (not mime-type-string)
                                        (string-empty-p mime-type-string))
                                    "text/gemini; charset=utf-8"
                                  mime-type-string))
             (mime-type-split (split-string mime-type-string* ";" t))
             (mime-type (string-trim (car mime-type-split)))
             (parameters (mapcar (lambda (s)
                                   (let ((key-val (split-string s "=")))
                                     (list (downcase (string-trim (car key-val)))
                                           (downcase (string-trim (cadr key-val))))))
                                 (cdr mime-type-split))))
        (when (string-prefix-p "text/" mime-type)
          (setq body (decode-coding-string
                      body
                      (if (assoc "charset" parameters)
                          (intern (cadr (assoc "charset" parameters)))
                        'utf-8)))
          (setq body (replace-regexp-in-string "\r" "" body)))
        (pcase mime-type
          ((or "text/gemini" "")
           (elpher-render-gemini-map body parameters))
          ("text/html"
           (elpher-render-html body))
          ("text/org-mode"
           (elpher-render-org body))
          ((pred (string-prefix-p "text/"))
           (elpher-render-gemini-plain-text body parameters))
          ((pred (string-prefix-p "image/"))
           (elpher-render-image body))
          (_other
           (elpher-render-download body)))))))