org-bibtex-extras.el 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. ;;; org-bibtex-extras --- extras for working with org-bibtex entries
  2. ;; Copyright (C) 2008-2014 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte <eric dot schulte at gmx dot com>
  4. ;; Keywords: outlines, hypermedia, bibtex, d3
  5. ;; Homepage: http://orgmode.org
  6. ;; Version: 0.01
  7. ;; This file is not yet part of GNU Emacs.
  8. ;; This program is free software; you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation; either version 3, or (at your option)
  11. ;; any later version.
  12. ;; This program is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Warning: This should certainly be considered EXPERIMENTAL and still
  20. ;; in development, feedback is welcome, but don't expect it
  21. ;; to work.
  22. ;; This file add some extra functionality to your bibtex entries which
  23. ;; are stored as Org-mode headlines using org-bibtex.el. Most
  24. ;; features expect that you keep all of your reading notes in a single
  25. ;; file, set the `obe-bibtex-file' variable to the path to this file.
  26. ;;
  27. ;; - d3 view :: d3 is a Javascript library which supports interactive
  28. ;; display of graphs. To view your citations as a d3
  29. ;; graph, execute the following which will create a .json
  30. ;; export of your references file, then grab a copy of
  31. ;; d3, edit examples/force/force.js to replace
  32. ;;
  33. ;; var source`"miserables.json";
  34. ;;
  35. ;; with
  36. ;;
  37. ;; var source`"your-references.json";
  38. ;;
  39. ;; then view examples/force/force.html in your browser.
  40. ;;
  41. ;; - HTML export :: Customize the `obe-html-link-base' variable so
  42. ;; that it points to an html export of your
  43. ;; references, then add the following to your html
  44. ;; export hook, and citations will be resolved during
  45. ;; html export.
  46. ;;
  47. ;; (add-hook 'org-export-first-hook
  48. ;; (lambda ()
  49. ;; (when (equal org-export-current-backend 'html)
  50. ;; (obe-html-export-citations))))
  51. ;;; Code:
  52. (require 'org-bibtex)
  53. (defcustom obe-bibtex-file nil "File holding bibtex entries.")
  54. (defcustom obe-html-link-base nil
  55. "Base of citation links.
  56. For example, to point to your `obe-bibtex-file' use the following.
  57. (setq obe-html-link-base (format \"file:%s\" obe-bibtex-file))
  58. ")
  59. (defvar obe-citations nil)
  60. (defun obe-citations ()
  61. "Return all citations from `obe-bibtex-file'."
  62. (or obe-citations
  63. (save-window-excursion
  64. (find-file obe-bibtex-file)
  65. (goto-char (point-min))
  66. (while (re-search-forward " :CUSTOM_ID: \\(.+\\)$" nil t)
  67. (push (org-no-properties (match-string 1))
  68. obe-citations))
  69. obe-citations)))
  70. (defun obe-goto-citation (&optional citation)
  71. "Visit a citation given its ID."
  72. (interactive)
  73. (let ((citation (or citation
  74. (org-icompleting-read "Citation: "
  75. (obe-citations)))))
  76. (find-file obe-bibtex-file)
  77. (goto-char (point-min))
  78. (when (re-search-forward (format " :CUSTOM_ID: %s" citation) nil t)
  79. (outline-previous-visible-heading 1)
  80. t)))
  81. (defun obe-html-export-citations ()
  82. "Convert all \\cite{...} citations in the current file into HTML links."
  83. (save-excursion
  84. (goto-char (point-min))
  85. (while (re-search-forward "\\\\cite{\\([^\000}]+\\)}" nil t)
  86. (replace-match
  87. (save-match-data
  88. (mapconcat (lambda (c) (format "[[%s#%s][%s]]" obe-html-link-base c c))
  89. (mapcar #'org-babel-trim
  90. (split-string (match-string 1) ",")) ", "))))))
  91. (defun obe-get-meta-data (citation)
  92. "Collect meta-data for CITATION."
  93. (save-excursion
  94. (when (obe-goto-citation citation)
  95. (let ((pt (point)))
  96. `((:authors . ,(split-string (org-entry-get pt "AUTHOR") " and " t))
  97. (:title . ,(org-no-properties (org-get-heading 1 1)))
  98. (:journal . ,(org-entry-get pt "JOURNAL")))))))
  99. (defun obe-meta-to-json (meta &optional fields)
  100. "Turn a list of META data from citations into a string of json."
  101. (let ((counter 1) nodes links)
  102. (flet ((id (it) (position it nodes :test #'string= :key #'car))
  103. (col (k) (mapcar (lambda (r) (cdr (assoc k r))) meta))
  104. (add (lst)
  105. (dolist (el lst) (push (cons el counter) nodes))
  106. (incf counter)))
  107. ;; build the nodes of the graph
  108. (add (col :title))
  109. (add (remove-if (lambda (author) (string-match "others" author))
  110. (remove-duplicates (apply #'append (col :authors))
  111. :test #'string=)))
  112. (dolist (field fields)
  113. (add (remove-duplicates (col field) :test #'string=)))
  114. ;; build the links in the graph
  115. (dolist (citation meta)
  116. (let ((dest (id (cdr (assoc :title citation)))))
  117. (dolist (author (mapcar #'id (cdr (assoc :authors citation))))
  118. (when author (push (cons author dest) links)))
  119. (let ((jid (id (cdr (assoc :journal citation)))))
  120. (when jid (push (cons jid dest) links)))
  121. (let ((cid (id (cdr (assoc :category citation)))))
  122. (when cid (push (cons cid dest) links)))))
  123. ;; build the json string
  124. (format "{\"nodes\":[%s],\"links\":[%s]}"
  125. (mapconcat
  126. (lambda (pair)
  127. (format "{\"name\":%S,\"group\":%d}"
  128. (car pair) (cdr pair)))
  129. nodes ",")
  130. (mapconcat
  131. (lambda (link)
  132. (format "{\"source\":%d,\"target\":%d,\"value\":1}"
  133. (car link) (cdr link)))
  134. (meta-to-links meta nodes) ",")))))
  135. (provide 'org-bibtex-extras)
  136. ;;; org-bibtex-extras ends here