123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137 |
- (require 'org-bibtex)
- (declare-function org-trim "org" (s &optional keep-lead))
- (defcustom obe-bibtex-file nil "File holding bibtex entries.")
- (defcustom obe-html-link-base nil
- "Base of citation links.
- For example, to point to your `obe-bibtex-file' use the following.
- (setq obe-html-link-base (format \"file:%s\" obe-bibtex-file))
- ")
- (defvar obe-citations nil)
- (defun obe-citations ()
- "Return all citations from `obe-bibtex-file'."
- (or obe-citations
- (save-window-excursion
- (find-file (or obe-bibtex-file
- (error "`obe-bibtex-file' has not been configured")))
- (goto-char (point-min))
- (while (re-search-forward " :CUSTOM_ID: \\(.+\\)$" nil t)
- (push (org-no-properties (match-string 1))
- obe-citations))
- obe-citations)))
- (defun obe-html-export-citations ()
- "Convert all \\cite{...} citations in the current file into HTML links."
- (save-excursion
- (goto-char (point-min))
- (while (re-search-forward "\\\\cite{\\([^\000}]+\\)}" nil t)
- (replace-match
- (save-match-data
- (mapconcat (lambda (c) (format "[[%s#%s][%s]]" obe-html-link-base c c))
- (mapcar #'org-trim
- (split-string (match-string 1) ",")) ", "))))))
- (defun obe-meta-to-json (meta &optional fields)
- "Turn a list of META data from citations into a string of json."
- (let ((counter 1) nodes links)
- (flet ((id (it) (position it nodes :test #'string= :key #'car))
- (col (k) (mapcar (lambda (r) (cdr (assoc k r))) meta))
- (add (lst)
- (dolist (el lst) (push (cons el counter) nodes))
- (incf counter)))
-
- (add (col :title))
- (add (remove-if (lambda (author) (string-match "others" author))
- (remove-duplicates (apply #'append (col :authors))
- :test #'string=)))
- (dolist (field fields)
- (add (remove-duplicates (col field) :test #'string=)))
-
- (dolist (citation meta)
- (let ((dest (id (cdr (assq :title citation)))))
- (dolist (author (mapcar #'id (cdr (assq :authors citation))))
- (when author (push (cons author dest) links)))
- (let ((jid (id (cdr (assq :journal citation)))))
- (when jid (push (cons jid dest) links)))
- (let ((cid (id (cdr (assq :category citation)))))
- (when cid (push (cons cid dest) links)))))
-
- (format "{\"nodes\":[%s],\"links\":[%s]}"
- (mapconcat
- (lambda (pair)
- (format "{\"name\":%S,\"group\":%d}"
- (car pair) (cdr pair)))
- nodes ",")
- (mapconcat
- (lambda (link)
- (format "{\"source\":%d,\"target\":%d,\"value\":1}"
- (car link) (cdr link)))
- (meta-to-links meta nodes) ",")))))
- (provide 'org-bibtex-extras)
|