ol-doi.el 2.3 KB

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