ol-doi.el 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 'org-macs)
  20. (org-assert-version)
  21. (require 'ol)
  22. (defcustom org-link-doi-server-url "https://doi.org/"
  23. "The URL of the DOI server."
  24. :group 'org-link-follow
  25. :version "24.3"
  26. :type 'string
  27. :safe #'stringp)
  28. (defun org-link-doi-open (path arg)
  29. "Open a \"doi\" type link.
  30. PATH is a the path to search for, as a string."
  31. (browse-url (url-encode-url (concat org-link-doi-server-url path)) arg))
  32. (defun org-link-doi-export (path desc backend info)
  33. "Export a \"doi\" type link.
  34. PATH is the DOI name. DESC is the description of the link, or
  35. nil. BACKEND is a symbol representing the backend used for
  36. export. INFO is a plist containing the export parameters."
  37. (let ((uri (concat org-link-doi-server-url path)))
  38. (pcase backend
  39. (`html
  40. (format "<a href=\"%s\">%s</a>" uri (or desc uri)))
  41. (`latex
  42. (if desc (format "\\href{%s}{%s}" uri desc)
  43. (format "\\url{%s}" uri)))
  44. (`ascii
  45. (if (not desc) (format "<%s>" uri)
  46. (concat (format "[%s]" desc)
  47. (and (not (plist-get info :ascii-links-to-notes))
  48. (format " (<%s>)" uri)))))
  49. (`texinfo
  50. (if (not desc) (format "@uref{%s}" uri)
  51. (format "@uref{%s, %s}" uri desc)))
  52. (_ uri))))
  53. (org-link-set-parameters "doi"
  54. :follow #'org-link-doi-open
  55. :export #'org-link-doi-export)
  56. (provide 'org-link-doi)
  57. (provide 'ol-doi)
  58. ;;; ol-doi.el ends here