config

Personal configuration.
git clone git://code.dwrz.net/config
Log | Files | Refs

ol-doi.el (2470B)


      1 ;;; ol-doi.el --- DOI links support in Org           -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2021-2024 Free Software Foundation, Inc.
      4 
      5 ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
      6 
      7 ;; This file is part of GNU Emacs.
      8 
      9 ;; GNU Emacs is free software: you can redistribute it and/or modify
     10 ;; it under the terms of the GNU General Public License as published by
     11 ;; the Free Software Foundation, either version 3 of the License, or
     12 ;; (at your option) any later version.
     13 
     14 ;; GNU Emacs is distributed in the hope that it will be useful,
     15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
     16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     17 ;; GNU General Public License for more details.
     18 
     19 ;; You should have received a copy of the GNU General Public License
     20 ;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
     21 
     22 ;;; Commentary:
     23 
     24 ;; This library introduces the "doi" link type in Org, and provides
     25 ;; code for opening and exporting such links.
     26 
     27 ;;; Code:
     28 
     29 (require 'org-macs)
     30 (org-assert-version)
     31 
     32 (require 'ol)
     33 
     34 (defcustom org-link-doi-server-url "https://doi.org/"
     35   "The URL of the DOI server."
     36   :group 'org-link-follow
     37   :version "24.3"
     38   :type 'string
     39   :safe #'stringp)
     40 
     41 (defun org-link-doi-open (path arg)
     42   "Open a \"doi\" type link.
     43 PATH is a the path to search for, as a string.
     44 ARG is passed to `browse-url'."
     45   (browse-url (url-encode-url (concat org-link-doi-server-url path)) arg))
     46 
     47 (defun org-link-doi-export (path desc backend info)
     48   "Export a \"doi\" type link.
     49 PATH is the DOI name.  DESC is the description of the link, or
     50 nil.  BACKEND is a symbol representing the backend used for
     51 export.  INFO is a plist containing the export parameters."
     52   (let ((uri (concat org-link-doi-server-url path)))
     53     (pcase backend
     54       (`html
     55        (format "<a href=\"%s\">%s</a>" uri (or desc uri)))
     56       (`latex
     57        (if desc (format "\\href{%s}{%s}" uri desc)
     58 	 (format "\\url{%s}" uri)))
     59       (`ascii
     60        (if (not desc) (format "<%s>" uri)
     61          (concat (format "[%s]" desc)
     62 		 (and (not (plist-get info :ascii-links-to-notes))
     63 		      (format " (<%s>)" uri)))))
     64       (`texinfo
     65        (if (not desc) (format "@uref{%s}" uri)
     66          (format "@uref{%s, %s}" uri desc)))
     67       (_ uri))))
     68 
     69 (org-link-set-parameters "doi"
     70                          :follow #'org-link-doi-open
     71                          :export #'org-link-doi-export)
     72 
     73 
     74 (provide 'org-link-doi)
     75 (provide 'ol-doi)
     76 ;;; ol-doi.el ends here