ol-doi.el 2.3 KB

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