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