org-bibtex-extras.el 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. ;;; org-bibtex-extras --- extras for working with org-bibtex entries
  2. ;; Copyright (C) 2008-2018 Free Software Foundation, Inc.
  3. ;; Author: Eric Schulte <eric dot schulte at gmx dot com>
  4. ;; Keywords: outlines, hypermedia, bibtex, d3
  5. ;; Homepage: https://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 'ol-bibtex)
  53. (declare-function org-trim "org" (s &optional keep-lead))
  54. (defcustom obe-bibtex-file nil "File holding bibtex entries.")
  55. (defcustom obe-html-link-base nil
  56. "Base of citation links.
  57. For example, to point to your `obe-bibtex-file' use the following.
  58. (setq obe-html-link-base (format \"file:%s\" obe-bibtex-file))
  59. ")
  60. (defvar obe-citations nil)
  61. (defun obe-citations ()
  62. "Return all citations from `obe-bibtex-file'."
  63. (or obe-citations
  64. (save-window-excursion
  65. (find-file (or obe-bibtex-file
  66. (error "`obe-bibtex-file' has not been configured")))
  67. (goto-char (point-min))
  68. (while (re-search-forward " :CUSTOM_ID: \\(.+\\)$" nil t)
  69. (push (org-no-properties (match-string 1))
  70. obe-citations))
  71. obe-citations)))
  72. (defun obe-html-export-citations ()
  73. "Convert all \\cite{...} citations in the current file into HTML links."
  74. (save-excursion
  75. (goto-char (point-min))
  76. (while (re-search-forward "\\\\cite{\\([^\000}]+\\)}" nil t)
  77. (replace-match
  78. (save-match-data
  79. (mapconcat (lambda (c) (format "[[%s#%s][%s]]" obe-html-link-base c c))
  80. (mapcar #'org-trim
  81. (split-string (match-string 1) ",")) ", "))))))
  82. (defun obe-meta-to-json (meta &optional fields)
  83. "Turn a list of META data from citations into a string of json."
  84. (let ((counter 1) nodes links)
  85. (flet ((id (it) (position it nodes :test #'string= :key #'car))
  86. (col (k) (mapcar (lambda (r) (cdr (assoc k r))) meta))
  87. (add (lst)
  88. (dolist (el lst) (push (cons el counter) nodes))
  89. (incf counter)))
  90. ;; build the nodes of the graph
  91. (add (col :title))
  92. (add (remove-if (lambda (author) (string-match "others" author))
  93. (remove-duplicates (apply #'append (col :authors))
  94. :test #'string=)))
  95. (dolist (field fields)
  96. (add (remove-duplicates (col field) :test #'string=)))
  97. ;; build the links in the graph
  98. (dolist (citation meta)
  99. (let ((dest (id (cdr (assq :title citation)))))
  100. (dolist (author (mapcar #'id (cdr (assq :authors citation))))
  101. (when author (push (cons author dest) links)))
  102. (let ((jid (id (cdr (assq :journal citation)))))
  103. (when jid (push (cons jid dest) links)))
  104. (let ((cid (id (cdr (assq :category citation)))))
  105. (when cid (push (cons cid dest) links)))))
  106. ;; build the json string
  107. (format "{\"nodes\":[%s],\"links\":[%s]}"
  108. (mapconcat
  109. (lambda (pair)
  110. (format "{\"name\":%S,\"group\":%d}"
  111. (car pair) (cdr pair)))
  112. nodes ",")
  113. (mapconcat
  114. (lambda (link)
  115. (format "{\"source\":%d,\"target\":%d,\"value\":1}"
  116. (car link) (cdr link)))
  117. (meta-to-links meta nodes) ",")))))
  118. (provide 'org-bibtex-extras)
  119. ;;; org-bibtex-extras ends here