ol-elpher

Org-link support for Elpher
git clone https://git.jamzattack.xyz/ol-elpher
Log | Files | Refs | LICENSE

ol-elpher.el (3702B)


      1 ;;; ol-elpher.el --- Org link support for elpher     -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2020  Jamie Beardslee
      4 
      5 ;; Author: Jamie Beardslee <jdb@jamzattack.xyz>
      6 ;; Version: 2020.07.28
      7 ;; Homepage: https://git.jamzattack.xyz/ol-elpher
      8 ;; Keywords: link, org, gopher
      9 ;; Package-Requires: ((elpher))
     10 
     11 ;; This program is free software; you can redistribute it and/or modify
     12 ;; it under the terms of the GNU General Public License as published by
     13 ;; the Free Software Foundation, either version 3 of the License, or
     14 ;; (at your option) any later version.
     15 
     16 ;; This program is distributed in the hope that it will be useful,
     17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     19 ;; GNU General Public License for more details.
     20 
     21 ;; You should have received a copy of the GNU General Public License
     22 ;; along with this program.  If not, see <https://www.gnu.org/licenses/>.
     23 
     24 ;;; Commentary:
     25 
     26 ;; When this module is active, `org-store-link' in elpher stores a
     27 ;; link to the current URL.  To enable this module, either add it to
     28 ;; `org-modules' or `require' it manually.
     29 
     30 ;;; Code:
     31 
     32 (unless (require 'ol nil t)
     33   (require 'org))
     34 (require 'url-util)
     35 
     36 (declare-function elpher-copy-page-url "elpher")
     37 (declare-function button-get "button")
     38 (defvar elpher-current-page)
     39 
     40 ;; Autoload `elpher-go', so that elpher isn't loaded immediately after
     41 ;; org-mode.
     42 (autoload 'elpher-go "elpher" nil t)
     43 
     44 (org-link-set-parameters "gopher"
     45 			 :follow #'elpher-go
     46 			 :store #'org-elpher-store-gopher-link
     47 			 :export #'org-elpher-export-gopher)
     48 
     49 (org-link-set-parameters "gemini"
     50 			 :follow #'elpher-go
     51 			 :store #'org-elpher-store-gemini-link
     52 			 :export #'org-elpher-export-gemini)
     53 
     54 
     55 ;;; Storing
     56 
     57 ;; If we use the same function for both gopher and gemini,
     58 ;; `org-store-link' will call `completing-read' with two entries of
     59 ;; the same function.
     60 (defun org-elpher--store-link (page)
     61   "Store a link to an elpher PAGE."
     62   (let* ((address (elpher-page-address page))
     63 	 (type (url-type address))
     64 	 (url (elpher-address-to-url address))
     65 	 (desc (elpher-page-display-string page)))
     66     (org-link-store-props
     67      :type type
     68      :link url
     69      :url url
     70      :description desc)))
     71 
     72 (defun org-elpher-store-gemini-link ()
     73   "Store a gemini link."
     74   (let ((page elpher-current-page))
     75     (when (and (eq major-mode 'elpher-mode)
     76 	       (string= "gemini"
     77 			(url-type (elpher-page-address page))))
     78       (org-elpher--store-link page))))
     79 
     80 (defun org-elpher-store-gopher-link ()
     81   "Store a gopher link."
     82   (let ((page elpher-current-page))
     83     (when (and (eq major-mode 'elpher-mode)
     84 	       (string= "gopher"
     85 			(url-type (elpher-page-address page))))
     86       (org-elpher--store-link page))))
     87 
     88 
     89 ;;; Exporting
     90 
     91 ;; org-mode doesn't provide the link type in LINK, so we need separate
     92 ;; functions for gemini and gopher.
     93 (defun org-elpher--export (link description format prefix)
     94   "Export elpher links from an Org file."
     95   (let* ((link (concat prefix link))
     96 	 (desc (or description link)))
     97     (pcase format
     98       (`html (format "<a target=\"_blank\" href=\"%s\">%s</a>" link desc))
     99       (`latex (format "\\href{%s}{%s}" link desc))
    100       (`texinfo (format "@uref{%s,%s}" link desc))
    101       (`ascii (if description
    102 		  (format "%s (%s)" link description)
    103 		link))
    104       (_ link))))
    105 
    106 (defun org-elpher-export-gemini (link description format)
    107   "Export a gemini link from an Org file."
    108   (org-elpher--export link description format "gemini:"))
    109 
    110 (defun org-elpher-export-gopher (link description format)
    111   "Export a gopher link from an Org file."
    112   (org-elpher--export link description format "gopher:"))
    113 
    114 (provide 'ol-elpher)
    115 ;;; ol-elpher.el ends here