Skip to content

Instantly share code, notes, and snippets.

@andersjohansson
Last active November 12, 2022 16:44
Show Gist options
  • Save andersjohansson/324a01364eb5a5435c65 to your computer and use it in GitHub Desktop.
Save andersjohansson/324a01364eb5a5435c65 to your computer and use it in GitHub Desktop.
Using org-zotxt with working export to odt scannable by odf/rdf scan (http://zotero-odf-scan.github.io/zotero-odf-scan/) and using pandoc cite-syntax. This depends on org-pdcite (https://gitlab.com/egh/org-pdcite/). I use it with extended allowed characters, this branch: https://gitlab.com/andersjohansson/org-pdcite/commits/aj-extensions. BibLaT…
(require 'org-zotxt)
(require 'org-pdcite)
(setq org-zotxt-link-description-style :betterbibtexkey)
(org-add-link-type
"zotero"
(lambda (rest)
(zotxt-select-key (substring rest 15)))
#'aj/org-zotero-link))
(defun aj/org-zotero-link (path desc format)
(require 'zotxt)
(require 'org-zotxt)
(let ((lp (aj/org-zotero-link-parse-desc
(replace-regexp-in-string "\\\\_" "_" (substring-no-properties desc)))))
(unless (null lp)
(cond ((eq format 'odt)
(let ((rtos (split-string
(deferred:$
(zotxt-get-item-deferred
`(:key ,(org-zotxt-extract-link-id-from-path path))
:248bebf1-46ab-4067-9f93-ec3d2960d0cd)
(deferred:nextc it
(lambda (item)
(plist-get item :248bebf1-46ab-4067-9f93-ec3d2960d0cd)))
(deferred:sync! it))
"{\\||\\|}" nil " +")))
(format "{%s | %s | %s %s | %s |%s}"
(or (plist-get lp :prefix) " ")
(concat
(when (plist-get lp :suppress-author) "-")
(replace-regexp-in-string "&" "&" (nth 2 rtos)))
(or (plist-get lp :locator-word) " ")
(or (plist-get lp :locator) " ")
(or (plist-get lp :suffix) " ")
(nth 5 rtos))))
((member format '(latex beamer))
(when aj/zotxt-autobib-export ;; add key for collecting bib-data
(let ((key (org-zotxt-extract-link-id-from-path path)))
(unless (member key aj/zotxt-autobib-collected-keys)
(push key aj/zotxt-autobib-collected-keys))))
(format
(if aj/org-zotxt-nobiblatex
"\\cite[%s][%s]{%s}"
(if (plist-get lp :supress-author)
"\\autocite*[%s][%s]{%s}"
"\\autocite[%s][%s]{%s}"))
(or (plist-get lp :prefix) "")
(let ((lw (plist-get lp :locator-word))
(lo (plist-get lp :locator))
(su (plist-get lp :suffix)))
(if lo
(concat
(when lw
(when (or aj/org-zotxt-nobiblatex
(not (member lw '("p." "pp." "s."))))
; No need
; to print these explicitly, maybe
; more could be added
(concat lw "~"))) ; add no-breaking-space
lo
(when su (concat "; " su))) ; locator+suffix. Possible?
(or su ""))) ; only suffix
(plist-get lp :citekey)))
((eq format 'md)
(format "[%s]" desc)) ;should work reasonably with pandoc-citeproc
((eq format 'html)
(deferred:$
(zotxt-get-item-bibliography-deferred
`(:key , (org-zotxt-extract-link-id-from-path path)))
(deferred:nextc it
(lambda (item)
(plist-get item :citation-html)))
(deferred:sync! it)))
(t nil)))))
(defcustom aj/org-zotxt-nobiblatex nil
"Just use cite instead of autocites when exporting to latex"
:group 'org-zotxt
:safe 'booleanp)
(use-package org-pdcite
:load-path "~/kodat/elisp/org-pdcite"
:commands 'org-pdcite-full-cite-parser)
(defun aj/org-zotero-link-parse-desc (desc)
;; make-sure everything is loaded here
;;(require 'org-pdcite)
(with-temp-buffer
(insert desc)
(goto-char (point-min))
(org-pdcite-full-cite-parser)))
;; Merge adjacent autocite-commands in latex export
(defun aj/org-zotxt-merge-autocites (string backend info)
(if (member backend '(latex beamer))
(with-temp-buffer
(insert string)
(dolist (re '(("\\\\autocite\\(\\(?:\\[[^]]*\\]\\)*{[^}]+}\\)" "\\autocites%s" "")
("\\\\cite\\[\\]\\[\\]{\\([^}]+\\)}" "\\cite{%s}" ",")))
(goto-char 1)
(while (search-forward-regexp (car re) nil t)
(let (struct fc)
(push (match-string 1) struct)
(setq fc (match-beginning 0))
(while (looking-at (concat "[ ,]?" (car re)))
(push (match-string 1) struct)
(goto-char (match-end 0)))
(delete-region fc (match-end 0))
(goto-char fc)
(insert (format (nth 1 re) (mapconcat #'identity struct (nth 2 re)))))))
(buffer-string))
string))
(with-eval-after-load "ox"
(setq org-export-filter-final-output-functions
(cons 'aj/org-zotxt-merge-autocites org-export-filter-final-output-functions)))
;;; Automatic export of bibtexfile with references cited in org-file
(defcustom aj/zotxt-autobib-export t
"Whether to generate a bibtex file from found zotxt-references
in exported org buffer"
:group 'org-zotxt
:safe 'booleanp)
(defcustom aj/zotxt-autobib-collected-keys nil
"Keys for references in org-file currently exported to latex,
which should be exported to bib(la)tex."
:group 'org-zotxt)
(defcustom aj/zotxt-autobib-translator "f895aa0d-f28e-47fe-b247-2ea77c6ed583"
"Translator id to use for fetching bib(la)tex data from Zotero"
:group 'org-zotxt
:safe 'stringp
:type '(choice
(string :tag "Custom")
(const :tag "BibTeX" "9cb70025-a888-4a29-a210-93ec52da40d4")
(const :tag "BibLaTeX" "b6e39b57-8942-4d11-8259-342c46ce395f")
(const :tag "Better-bibtex" "ca65189f-8815-4afe-8c8b-8c7c15f0edca")
(const :tag "Better-biblatex" "f895aa0d-f28e-47fe-b247-2ea77c6ed583")))
(defcustom aj/zotxt-autobib-dispoptions nil
"Translator dispOptions to use for fetching bib(la)tex data from Zotero"
:group 'org-zotxt
:safe 'stringp
:type '(choice
(string :tag "Custom")
(const :tag "journalAbbreviations" "useJournalAbbreviation:true")))
(defcustom aj/zotxt-autobib-filename "orgzotxtauto.bib"
"Filename for automatically generated bib file"
:group 'org-zotxt
:safe 'stringp)
(with-eval-after-load "ox"
(add-to-list 'org-export-filter-final-output-functions
#'aj/org-zotxt-makebibfile)
(add-hook 'org-export-before-processing-hook #'aj/org-zotxt-autobib-reset))
(defun aj/org-zotxt-autobib-reset (backend)
(setq aj/zotxt-autobib-collected-keys nil))
(defun aj/org-zotxt-makebibfile (string backend info)
"Exports a bib-file with bib(la)tex-data collected for the
zotxt-references in the exported org buffer"
(if (and aj/zotxt-autobib-export
(member backend '(latex beamer))
aj/zotxt-autobib-collected-keys)
(let* ((keys (mapconcat #'identity aj/zotxt-autobib-collected-keys ","))
(trans (intern (concat ":" aj/zotxt-autobib-translator)))
(bibstring
(deferred:$
(zotxt-get-item-deferred `(:key ,keys) trans aj/zotxt-autobib-dispoptions)
(deferred:nextc it
(lambda (item)
(plist-get item trans)))
(deferred:sync! it))))
(when bibstring
(with-current-buffer (generate-new-buffer "orgzotxtautobib")
(set-buffer-file-coding-system 'no-conversion)
; some strange encoding errors
; but when reverting file it is
; correct utf8
(insert bibstring)
(write-file aj/zotxt-autobib-filename))
(if aj/org-zotxt-nobiblatex
(replace-regexp-in-string
"\\\\bibliography{[^}]+}"
(format "\\bibliography{%s}" aj/zotxt-autobib-filename) string t t)
(replace-regexp-in-string
"\\\\begin{document}"
(format "\\addbibresource{%s}\n\\begin{document}" aj/zotxt-autobib-filename)
string t t))))
string))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment