ox-bibtex.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. ;;; ox-bibtex.el --- Export bibtex fragments
  2. ;; Copyright (C) 2009-2014 Taru Karttunen
  3. ;; Author: Taru Karttunen <taruti@taruti.net>
  4. ;; Nicolas Goaziou <n dot goaziou at gmail dot com>
  5. ;; This file is not currently part of GNU Emacs.
  6. ;; This program is free software; you can redistribute it and/or
  7. ;; modify it under the terms of the GNU General Public License as
  8. ;; published by the Free Software Foundation; either version 2, or (at
  9. ;; your option) any later version.
  10. ;; This program is distributed in the hope that it will be useful, but
  11. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. ;; General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with this program ; see the file COPYING. If not, write to
  16. ;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
  17. ;; Boston, MA 02111-1307, USA.
  18. ;;; Commentary:
  19. ;;
  20. ;; This is an utility to handle BibTeX export to LaTeX, html and ascii
  21. ;; exports. For HTML and ascii it uses the bibtex2html software from:
  22. ;;
  23. ;; http://www.lri.fr/~filliatr/bibtex2html/
  24. ;;
  25. ;; For ascii it uses the pandoc software from:
  26. ;;
  27. ;; http://johnmacfarlane.net/pandoc/
  28. ;;
  29. ;; It also introduces "cite" syntax for Org links.
  30. ;;
  31. ;; The usage is as follows:
  32. ;;
  33. ;; #+BIBLIOGRAPHY: bibfilename stylename optional-options
  34. ;;
  35. ;; e.g. given foo.bib and using style plain:
  36. ;;
  37. ;; #+BIBLIOGRAPHY: foo plain option:-d
  38. ;;
  39. ;; "stylename" can also be "nil", in which case no style will be used.
  40. ;;
  41. ;; Full filepaths are also possible:
  42. ;;
  43. ;; #+BIBLIOGRAPHY: /home/user/Literature/foo.bib plain option:-d
  44. ;;
  45. ;; Optional options are of the form:
  46. ;;
  47. ;; option:-foobar pass '-foobar' to bibtex2html
  48. ;;
  49. ;; e.g.,
  50. ;;
  51. ;; option:-d sort by date
  52. ;; option:-a sort as BibTeX (usually by author) *default*
  53. ;; option:-u unsorted i.e. same order as in .bib file
  54. ;; option:-r reverse the sort
  55. ;;
  56. ;; See the bibtex2html man page for more. Multiple options can be
  57. ;; combined like:
  58. ;;
  59. ;; option:-d option:-r
  60. ;;
  61. ;; Limiting to only the entries cited in the document:
  62. ;;
  63. ;; limit:t
  64. ;;
  65. ;; For LaTeX export this simply inserts the lines
  66. ;;
  67. ;; \bibliographystyle{plain}
  68. ;; \bibliography{foo}
  69. ;;
  70. ;; into the TeX file when exporting.
  71. ;;
  72. ;; For HTML export it:
  73. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  74. ;; bibliography,
  75. ;; 2) creates a foo.html and foo_bib.html,
  76. ;; 3) includes the contents of foo.html in the exported HTML file.
  77. ;;
  78. ;; For ascii export it:
  79. ;; 1) converts all \cite{foo} and [[cite:foo]] to links to the
  80. ;; bibliography,
  81. ;; 2) creates a foo.txt and foo_bib.html,
  82. ;; 3) includes the contents of foo.txt in the exported ascii file.
  83. ;;
  84. ;; For LaTeX export it:
  85. ;; 1) converts all [[cite:foo]] to \cite{foo}.
  86. ;; Initialization
  87. (require 'cl-lib)
  88. ;;; Internal Functions
  89. (defun org-bibtex-get-file (keyword)
  90. "Return bibliography file as a string.
  91. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no file is found,
  92. return nil instead."
  93. (let ((value (org-element-property :value keyword)))
  94. (and value
  95. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  96. (match-string 1 value))))
  97. (defun org-bibtex-get-style (keyword)
  98. "Return bibliography style as a string.
  99. KEYWORD is a \"BIBLIOGRAPHY\" keyword. If no style is found,
  100. return nil instead."
  101. (let ((value (org-element-property :value keyword)))
  102. (and value
  103. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  104. (match-string 2 value))))
  105. (defun org-bibtex-get-arguments (keyword)
  106. "Return \"bibtex2html\" arguments specified by the user.
  107. KEYWORD is a \"BIBLIOGRAPHY\" keyword. Return value is a plist
  108. containing `:options' and `:limit' properties. The former
  109. contains a list of strings to be passed as options to
  110. \"bibtex2html\" process. The latter contains a boolean."
  111. (let ((value (org-element-property :value keyword)))
  112. (and value
  113. (string-match "\\(\\S-+\\)[ \t]+\\(\\S-+\\)\\(.*\\)" value)
  114. (let (options limit)
  115. (dolist (arg (split-string (match-string 3 value))
  116. ;; Return value.
  117. (list :options (nreverse options) :limit limit))
  118. (let* ((s (split-string arg ":"))
  119. (key (car s))
  120. (value (nth 1 s)))
  121. (cond ((equal "limit" key)
  122. (setq limit (not (equal "nil" value))))
  123. ((equal "option" key) (push value options)))))))))
  124. (defun org-bibtex-citation-p (object)
  125. "Non-nil when OBJECT is a citation."
  126. (cl-case (org-element-type object)
  127. (link (equal (org-element-property :type object) "cite"))
  128. (latex-fragment
  129. (string-match "\\`\\\\cite{" (org-element-property :value object)))))
  130. (defun org-bibtex-get-citation-key (citation)
  131. "Return key for a given citation, as a string.
  132. CITATION is a `latex-fragment' or `link' type object satisfying
  133. to `org-bibtex-citation-p' predicate."
  134. (if (eq (org-element-type citation) 'link)
  135. (org-element-property :path citation)
  136. (let ((value (org-element-property :value citation)))
  137. (and (string-match "\\`\\\\cite{" value)
  138. (substring value (match-end 0) -1)))))
  139. ;;; Follow cite: links
  140. (defvar org-bibtex-file nil
  141. "Org file of BibTeX entries.")
  142. (defun org-bibtex-goto-citation (&optional citation)
  143. "Visit a citation given its ID."
  144. (interactive)
  145. (let ((citation (or citation (completing-read "Citation: " (obe-citations)))))
  146. (find-file (or org-bibtex-file
  147. (error "`org-bibtex-file' has not been configured")))
  148. (let ((position (org-find-property "CUSTOM_ID" citation)))
  149. (and position (progn (goto-char position) t)))))
  150. (let ((jump-fn (car (cl-remove-if-not #'fboundp '(ebib org-bibtex-goto-citation)))))
  151. (org-add-link-type "cite" jump-fn))
  152. ;;; Filters
  153. (defun org-bibtex-process-bib-files (tree backend info)
  154. "Send each bibliography in parse tree to \"bibtex2html\" process.
  155. Return new parse tree."
  156. (when (org-export-derived-backend-p backend 'ascii 'html)
  157. ;; Initialize dynamically scoped variables. The first one
  158. ;; contain an alist between keyword objects and their HTML
  159. ;; translation. The second one will contain an alist between
  160. ;; citation keys and names in the output (according to style).
  161. (setq org-bibtex-html-entries-alist nil
  162. org-bibtex-html-keywords-alist nil)
  163. (org-element-map tree 'keyword
  164. (lambda (keyword)
  165. (when (equal (org-element-property :key keyword) "BIBLIOGRAPHY")
  166. (let ((arguments (org-bibtex-get-arguments keyword))
  167. (file (org-bibtex-get-file keyword))
  168. temp-file
  169. out-file)
  170. ;; Test if filename is given with .bib-extension and strip
  171. ;; it off. Filenames with another extensions will be
  172. ;; untouched and will finally rise an error in bibtex2html.
  173. (setq file (if (equal (file-name-extension file) "bib")
  174. (file-name-sans-extension file) file))
  175. ;; Outpufiles of bibtex2html will be put into current working directory
  176. ;; so define a variable for this.
  177. (setq out-file (file-name-sans-extension
  178. (file-name-nondirectory file)))
  179. ;; limit is set: collect citations throughout the document
  180. ;; in TEMP-FILE and pass it to "bibtex2html" as "-citefile"
  181. ;; argument.
  182. (when (plist-get arguments :limit)
  183. (let ((citations
  184. (org-element-map tree '(latex-fragment link)
  185. (lambda (object)
  186. (and (org-bibtex-citation-p object)
  187. (org-bibtex-get-citation-key object))))))
  188. (with-temp-file (setq temp-file (make-temp-file "ox-bibtex"))
  189. (insert (mapconcat 'identity citations "\n")))
  190. (setq arguments
  191. (plist-put arguments
  192. :options
  193. (append (plist-get arguments :options)
  194. (list "-citefile" temp-file))))))
  195. ;; Call "bibtex2html" on specified file.
  196. (unless (eq 0 (apply
  197. 'call-process
  198. (append '("bibtex2html" nil nil nil)
  199. '("-a" "-nodoc" "-noheader" "-nofooter")
  200. (let ((style
  201. (org-not-nil
  202. (org-bibtex-get-style keyword))))
  203. (and style (list "--style" style)))
  204. (plist-get arguments :options)
  205. (list (concat file ".bib")))))
  206. (error "Executing bibtex2html failed"))
  207. (and temp-file (delete-file temp-file))
  208. ;; Open produced HTML file, and collect Bibtex key names
  209. (with-temp-buffer
  210. (insert-file-contents (concat out-file ".html"))
  211. ;; Update `org-bibtex-html-entries-alist'.
  212. (goto-char (point-min))
  213. (while (re-search-forward
  214. "a name=\"\\([-_a-zA-Z0-9:]+\\)\">\\([^<]+\\)" nil t)
  215. (push (cons (match-string 1) (match-string 2))
  216. org-bibtex-html-entries-alist)))
  217. ;; Open produced HTML file, wrap references within a block and
  218. ;; return it.
  219. (with-temp-buffer
  220. (cond
  221. ((org-export-derived-backend-p backend 'html)
  222. (insert (format "<div id=\"bibliography\">\n<h2>%s</h2>\n"
  223. (org-export-translate "References" :html info)))
  224. (insert-file-contents (concat out-file ".html"))
  225. (goto-char (point-max))
  226. (insert "\n</div>"))
  227. ((org-export-derived-backend-p backend 'ascii)
  228. ;; convert HTML references to text w/pandoc
  229. (unless (eq 0 (call-process "pandoc" nil nil nil
  230. (concat out-file ".html")
  231. "-o"
  232. (concat out-file ".txt")))
  233. (error "Executing pandoc failed"))
  234. (insert
  235. (format
  236. "%s\n==========\n\n"
  237. (org-export-translate
  238. "References"
  239. (intern (format ":%s" (plist-get info :ascii-charset)))
  240. info)))
  241. (insert-file-contents (concat out-file ".txt"))
  242. (goto-char (point-min))
  243. (while (re-search-forward
  244. "\\[ \\[bib\\][^ ]+ \\(\\]\\||[\n\r]\\)" nil t)
  245. (replace-match ""))
  246. (goto-char (point-min))
  247. (while (re-search-forward "\\( \\]\\| \\]\\| |\\)" nil t)
  248. (replace-match ""))
  249. (goto-char (point-min))
  250. (while (re-search-forward "[\n\r]\\([\n\r][\n\r]\\)" nil t)
  251. (replace-match "\\1"))))
  252. ;; Update `org-bibtex-html-keywords-alist'.
  253. (push (cons keyword (buffer-string))
  254. org-bibtex-html-keywords-alist)))))))
  255. ;; Return parse tree unchanged.
  256. tree)
  257. (defun org-bibtex-merge-contiguous-citations (tree backend info)
  258. "Merge all contiguous citation in parse tree.
  259. As a side effect, this filter will also turn all \"cite\" links
  260. into \"\\cite{...}\" LaTeX fragments and will extract options.
  261. Cite options are placed into square brackets at the beginning of
  262. the \"\\cite\" command for the LaTeX backend, and are removed for
  263. the HTML and ASCII backends."
  264. (when (org-export-derived-backend-p backend 'html 'latex 'ascii)
  265. (org-element-map tree '(link latex-fragment)
  266. (lambda (object)
  267. (when (org-bibtex-citation-p object)
  268. (let ((new-citation (list 'latex-fragment
  269. (list :value ""
  270. :post-blank (org-element-property
  271. :post-blank object))))
  272. option)
  273. ;; Insert NEW-CITATION right before OBJECT.
  274. (org-element-insert-before new-citation object)
  275. ;; Remove all subsequent contiguous citations from parse
  276. ;; tree, keeping only their citation key.
  277. (let ((keys (list (org-bibtex-get-citation-key object)))
  278. next)
  279. (while (and (setq next (org-export-get-next-element object info))
  280. (or (and (stringp next)
  281. (not (string-match-p "\\S-" next)))
  282. (org-bibtex-citation-p next)))
  283. (unless (stringp next)
  284. (push (org-bibtex-get-citation-key next) keys))
  285. (org-element-extract-element object)
  286. (setq object next))
  287. ;; Find any options in keys, e.g., "(Chapter 2)key" has
  288. ;; the option "Chapter 2".
  289. (setq keys
  290. (mapcar
  291. (lambda (k)
  292. (if (string-match "^(\\([^)]\+\\))\\(.*\\)" k)
  293. (progn
  294. (when (org-export-derived-backend-p backend 'latex)
  295. (setq option (format "[%s]" (match-string 1 k))))
  296. (match-string 2 k))
  297. k))
  298. keys))
  299. (org-element-extract-element object)
  300. ;; Eventually merge all keys within NEW-CITATION. Also
  301. ;; ensure NEW-CITATION has the same :post-blank property
  302. ;; as the last citation removed.
  303. (org-element-put-property
  304. new-citation
  305. :post-blank (org-element-property :post-blank object))
  306. (org-element-put-property
  307. new-citation
  308. :value (format "\\cite%s{%s}"
  309. (or option "")
  310. (mapconcat 'identity (nreverse keys) ",")))))))))
  311. tree)
  312. (eval-after-load 'ox
  313. '(progn (add-to-list 'org-export-filter-parse-tree-functions
  314. 'org-bibtex-process-bib-files)
  315. (add-to-list 'org-export-filter-parse-tree-functions
  316. 'org-bibtex-merge-contiguous-citations)))
  317. ;;; LaTeX Part
  318. (defadvice org-latex-keyword (around bibtex-keyword)
  319. "Translate \"BIBLIOGRAPHY\" keywords into LaTeX syntax.
  320. Fallback to `latex' back-end for other keywords."
  321. (let ((keyword (ad-get-arg 0)))
  322. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  323. ad-do-it
  324. (let ((file (org-bibtex-get-file keyword))
  325. (style (org-not-nil (org-bibtex-get-style keyword))))
  326. (setq ad-return-value
  327. (when file
  328. (concat (and style (format "\\bibliographystyle{%s}\n" style))
  329. (format "\\bibliography{%s}" file))))))))
  330. (ad-activate 'org-latex-keyword)
  331. ;;; HTML Part
  332. (defvar org-bibtex-html-entries-alist nil) ; Dynamically scoped.
  333. (defvar org-bibtex-html-keywords-alist nil) ; Dynamically scoped.
  334. ;;;; Advices
  335. (defadvice org-html-keyword (around bibtex-keyword)
  336. "Translate \"BIBLIOGRAPHY\" keywords into HTML syntax.
  337. Fallback to `html' back-end for other keywords."
  338. (let ((keyword (ad-get-arg 0)))
  339. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  340. ad-do-it
  341. (setq ad-return-value
  342. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  343. (defadvice org-html-latex-fragment (around bibtex-citation)
  344. "Translate \"\\cite\" LaTeX fragments into HTML syntax.
  345. Fallback to `html' back-end for other keywords."
  346. (let ((fragment (ad-get-arg 0)))
  347. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  348. (setq ad-return-value
  349. (format "[%s]"
  350. (mapconcat
  351. (lambda (key)
  352. (format "<a href=\"#%s\">%s</a>"
  353. key
  354. (or (cdr (assoc key org-bibtex-html-entries-alist))
  355. key)))
  356. (org-split-string
  357. (org-bibtex-get-citation-key fragment) ",") ","))))))
  358. (ad-activate 'org-html-keyword)
  359. (ad-activate 'org-html-latex-fragment)
  360. ;;; Ascii Part
  361. (defadvice org-ascii-keyword (around bibtex-keyword)
  362. "Translate \"BIBLIOGRAPHY\" keywords into ascii syntax.
  363. Fallback to `ascii' back-end for other keywords."
  364. (let ((keyword (ad-get-arg 0)))
  365. (if (not (equal (org-element-property :key keyword) "BIBLIOGRAPHY"))
  366. ad-do-it
  367. (setq ad-return-value
  368. (cdr (assq keyword org-bibtex-html-keywords-alist))))))
  369. (defadvice org-ascii-latex-fragment (around bibtex-citation)
  370. "Translate \"\\cite\" LaTeX fragments into ascii syntax.
  371. Fallback to `ascii' back-end for other keywords."
  372. (let ((fragment (ad-get-arg 0)))
  373. (if (not (org-bibtex-citation-p fragment)) ad-do-it
  374. (setq ad-return-value
  375. (format "[%s]"
  376. (mapconcat
  377. (lambda (key)
  378. (or (cdr (assoc key org-bibtex-html-entries-alist))
  379. key))
  380. (org-split-string
  381. (org-bibtex-get-citation-key fragment) ",") ","))))))
  382. (ad-activate 'org-ascii-keyword)
  383. (ad-activate 'org-ascii-latex-fragment)
  384. (provide 'ox-bibtex)
  385. ;;; ox-bibtex.el ends here