dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

ol-doi.el (2439B)


      1 ;;; ol-doi.el --- DOI links support in Org           -*- lexical-binding: t; -*-
      2 
      3 ;; Copyright (C) 2021-2023 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   (browse-url (url-encode-url (concat org-link-doi-server-url path)) arg))
     45 
     46 (defun org-link-doi-export (path desc backend info)
     47   "Export a \"doi\" type link.
     48 PATH is the DOI name.  DESC is the description of the link, or
     49 nil.  BACKEND is a symbol representing the backend used for
     50 export.  INFO is a plist containing the export parameters."
     51   (let ((uri (concat org-link-doi-server-url path)))
     52     (pcase backend
     53       (`html
     54        (format "<a href=\"%s\">%s</a>" uri (or desc uri)))
     55       (`latex
     56        (if desc (format "\\href{%s}{%s}" uri desc)
     57 	 (format "\\url{%s}" uri)))
     58       (`ascii
     59        (if (not desc) (format "<%s>" uri)
     60          (concat (format "[%s]" desc)
     61 		 (and (not (plist-get info :ascii-links-to-notes))
     62 		      (format " (<%s>)" uri)))))
     63       (`texinfo
     64        (if (not desc) (format "@uref{%s}" uri)
     65          (format "@uref{%s, %s}" uri desc)))
     66       (_ uri))))
     67 
     68 (org-link-set-parameters "doi"
     69                          :follow #'org-link-doi-open
     70                          :export #'org-link-doi-export)
     71 
     72 
     73 (provide 'org-link-doi)
     74 (provide 'ol-doi)
     75 ;;; ol-doi.el ends here