Klimi's new dotfiles with stow.
25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

123 satır
4.9 KiB

4 yıl önce
  1. ;;; biblio-doi.el --- Retrieve BibTeX entries by DOI -*- lexical-binding: t -*-
  2. ;; Copyright (C) 2016 Clément Pit-Claudel
  3. ;; Author: Clément Pit-Claudel <clement.pitclaudel@live.com>
  4. ;; URL: http://github.com/cpitclaudel/biblio.el
  5. ;; This program 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. ;;
  10. ;; This program is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;;
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;
  19. ;; Retrieve and insert BibTeX records by DOI using `doi-insert-bibtex'.
  20. ;; Information is retrieved from DOI issuing sites of each DOI using the
  21. ;; “application/x-bibtex” and “text/bibliography” request types, falling back to
  22. ;; CrossCite if unavailable.
  23. ;;
  24. ;; This package integrates with `biblio-selection-mode', and is part of the more
  25. ;; general `biblio' package (which see for more documentation).
  26. (require 'biblio-core)
  27. ;;; Code:
  28. (defun biblio-doi--dx-url (doi)
  29. "Create a doi.org url for DOI."
  30. (format "http://doi.org/%s" doi))
  31. (defun biblio-doi--crosscite-url (doi)
  32. "Create a crosscite URL to use as a fallback for DOI.
  33. Not all content providers provide BibTeX formatted entries, so
  34. instead of failing reroute the request through crosscite, which
  35. requests a generic format and crates the BibTeX on its own."
  36. (format "http://crosscite.org/citeproc/format?doi=%s&style=bibtex&lang=en-US" doi))
  37. (defconst biblio-doi--dx-mime-accept
  38. ;; “Accept:” header; Zenodo recognize x-bibtex but not text/bibliography
  39. "text/bibliography;style=bibtex, application/x-bibtex")
  40. (defun biblio-doi--set-mime-accept ()
  41. "Set `url-mime-accept-string' before contacting the DOI server."
  42. ;; Ugly: let-binding or buffer-locally setting `url-mime-accept-string' does
  43. ;; not work, because `url-http-create-request' can be called from a
  44. ;; sentinel, or from an entirely new buffer (after a redirection).
  45. (setq url-mime-accept-string biblio-doi--dx-mime-accept))
  46. (defun biblio-doi--restore-mime-accept ()
  47. "Restore `url-mime-accept-string'."
  48. (kill-local-variable 'url-mime-accept-string)
  49. (setq-default url-mime-accept-string nil))
  50. (defun biblio-doi--insert (bibtex buffer)
  51. "Insert formatted BIBTEX into BUFFER."
  52. (with-current-buffer buffer
  53. (insert bibtex "\n\n")))
  54. (defun biblio-doi--generic-url-callback-1 (errors forward-to)
  55. "Helper function for `biblio-doi--generic-url-callback'.
  56. ERRORS, FORWARD-TO: see there."
  57. (funcall forward-to
  58. (unless errors
  59. (biblio-format-bibtex (biblio-response-as-utf-8)))))
  60. (defun biblio-doi--generic-url-callback (cleanup-fn forward-to)
  61. "Make an URL-ready callback.
  62. Call CLEANUP-FN in any case, and FORWARD-TO with BibTeX source
  63. or nil depending on whether an error occured. If error 406
  64. occurs, forward nil; otherwise, signal the error. This is
  65. essentially a thin wrapper around `biblio-generic-url-callback'."
  66. (biblio-generic-url-callback
  67. (lambda (&optional errors)
  68. "Handle response from BibTeX server."
  69. (biblio-doi--generic-url-callback-1 errors forward-to))
  70. cleanup-fn '(http . 406)))
  71. (defun biblio-doi--crosscite-callback (forward-to)
  72. "Generate a handler for response of CrossCite server.
  73. FORWARD-TO is the callback to call with the results of the search."
  74. (biblio-doi--generic-url-callback #'ignore forward-to))
  75. (defun biblio-doi--forward-bibtex-crosscite (doi forward-to)
  76. "Forward BibTeX entry for DOI from CrossCite to FORWARD-TO."
  77. (biblio-url-retrieve (biblio-doi--crosscite-url doi) (biblio-doi--crosscite-callback forward-to)))
  78. (defun biblio-doi--dx-callback (forward-to)
  79. "Generate a handler for response of DX server.
  80. FORWARD-TO is the callback to call with the results of the search."
  81. (biblio-doi--generic-url-callback #'biblio-doi--restore-mime-accept forward-to))
  82. (defun biblio-doi--forward-bibtex-dx (doi forward-to)
  83. "Forward BibTeX entry for DOI from doi.org to FORWARD-TO."
  84. (biblio-doi--set-mime-accept)
  85. (biblio-url-retrieve (biblio-doi--dx-url doi) (biblio-doi--dx-callback forward-to)))
  86. (defun biblio-doi-forward-bibtex (doi forward-to)
  87. "Pass BibTeX entry for DOI to FORWARD-TO."
  88. (biblio-doi--forward-bibtex-dx
  89. doi (lambda (result)
  90. (if result (funcall forward-to result)
  91. (biblio-doi--forward-bibtex-crosscite doi forward-to)))))
  92. ;;;###autoload
  93. (defun doi-insert-bibtex (doi)
  94. "Insert BibTeX entry matching DOI."
  95. (interactive "MDOI: ")
  96. (let ((target-buffer (current-buffer)))
  97. (biblio-doi-forward-bibtex
  98. (biblio-cleanup-doi doi)
  99. (lambda (result) (biblio-doi--insert result target-buffer)))))
  100. (provide 'biblio-doi)
  101. ;;; biblio-doi.el ends here