emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [wip-cite-new] Exporting to pandoc md (and from there to zotero odt)
@ 2021-06-23 19:56 Anders Johansson
  2021-06-23 20:02 ` Bruce D'Arcus
  0 siblings, 1 reply; 6+ messages in thread
From: Anders Johansson @ 2021-06-23 19:56 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1956 bytes --]

Hi all,
I appreciate the work done on the cite functionality!
I use zotero for managing my library and have previously used zotxt
and org-zotxt (along with some additions in my zotxt-extra library)
for being able to cite in org-mode and export correctly to various
formats.
https://gitlab.com/egh/zotxt
https://gitlab.com/egh/zotxt-emacs
https://gitlab.com/andersjohansson/emacs-zotxt-extra

Zotxt has used a custom link format for citations (similar to org-ref)
which links to zotero but optionally also contains a citekey (defined
via the zotero-better-bibtex extension to zotero). I actually wrote my
PhD thesis with this setup, but the connection to Zotero has always
felt a little wobbly and Zotero is also quite slow for my large
library.

Now seems to be a great time to convert to a workflow with the new
cite format. To enable a conversion from org mode to word processors
with Zotero ”active” citations I created two libraries to manage this
via pandoc and the pandoc filter created for the zotero-better-bibtex
extension.
(see: https://retorque.re/zotero-better-bibtex/exporting/pandoc/)

This allows me a workflow like:
1. keep an updated  .bib-file of my library in zotero (with the help
of zotero-better-bibtex)
2. cite in org-mode (for me with completion via helm-bibtex and
https://github.com/bdarcus/bibtex-actions/pull/113)
3. Export to an odt-file with active zotero citations (with oc-pandoc
and ox-md-pandoc-zotero)

I attach them here in case anyone else finds some part of this workflow usable.
https://gitlab.com/andersjohansson/org-cite-pandoc
https://gitlab.com/andersjohansson/ox-md-pandoc-zotero

I couldn’t find any previous definitions for exporting to the pandoc
cite format so I quickly hacked up org-cite-pandoc. I suppose it could
be be widely useful and included in org. Feel free to use it as
suitable (I have FSF copyright assignment for emacs).

Best,
Anders Johansson

[-- Attachment #2: oc-pandoc.el --]
[-- Type: text/x-emacs-lisp, Size: 3363 bytes --]

;;; oc-pandoc.el --- Export org to pandoc markdown with citations  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Anders Johansson

;; Author: Anders Johansson <mejlaandersj@gmail.com>
;; Created: 2021-06-23
;; Modified: 2021-06-23
;; Keywords: org, wp

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'oc)
(require 'ox)
(require 'org-element)

(defun org-cite-pandoc--get-key (ref)
  "Return @key from REF object."
  (concat "@" (org-element-property :key ref)))

(defun org-cite-pandoc--format-citation (citation noauthor info)
  "Format CITATION object according to pandoc format.
NOAUTHOR non-nil for noauthor style.
INFO is the export state, as a property list."
  (org-export-data
   (org-cite-concat "["
                    (when noauthor "-")
                    ;; no support for global prefix or suffix
                    ;; (org-element-property :prefix citation)
                    (org-cite-mapconcat
                     (lambda (ref)
                       (org-cite-concat
                        (org-element-property :prefix ref)
                        (org-cite-pandoc--get-key ref)
                        (when-let ((suf (org-element-property :suffix ref)))
                          (org-cite-concat "," suf))))
                     (org-cite-get-references citation)
                     ";")
                    ;; (org-element-property :suffix citation)
                    "]")
   info))

(defun org-cite-pandoc-export-citation (citation style _ info)
  "Export CITATION object.
STYLE is the expected citation style, as a pair of strings or nil.  INFO is the
export communication channel, as a property list."
  (pcase style
    (`(,(or "text" "t") . ,_)
     ;; This would generate multiple in-text citations like:
     ;; AuthorA (2020), Author B (2021)
     (mapconcat #'org-cite-pandoc--get-key (org-cite-get-references citation) ", "))
    ;; "noauthor" style.
    (`(,(or "noauthor" "na") . ,_)
     (org-cite-pandoc--format-citation citation t info))
    ;; Default ("nil") style.
    (`(,_ . ,_)
     (org-cite-pandoc--format-citation citation nil info))
    ;; This should not happen.
    (_ (error "Invalid style: %S" style))))

(defun org-cite-pandoc-export-bibliography (_k _f _s _p backend _i)
  "Generate bibliography.
Just outputs a #refs section when BACKEND is markdown."
  (when (org-export-derived-backend-p backend 'md)
    "::: {#refs}\n:::"))

\f
;;; Register processor
(org-cite-register-processor 'pandoc
  :export-citation #'org-cite-pandoc-export-citation
  :export-bibliography #'org-cite-pandoc-export-bibliography)

(provide 'org-cite-pandoc)
(provide 'oc-pandoc)
;;; oc-pandoc.el ends here

;; Local Variables:
;; nameless-current-name: "org-cite-pandoc"
;; End:

[-- Attachment #3: ox-md-pandoc-zotero.el --]
[-- Type: text/x-emacs-lisp, Size: 2747 bytes --]

;;; ox-md-pandoc-zotero.el --- Export org citations to md and then to zotero formats via pandoc  -*- lexical-binding: t; -*-

;; Copyright (C) 2021  Anders Johansson

;; Author: Anders Johansson <mejlaandersj@gmail.com>
;; Created: 2021-06-23
;; Modified: 2021-06-23
;; Keywords: org, wp

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;

;;; Code:

(require 'ox-md)

(defgroup ox-md-pandoc-zotero nil
  "Customization for org export to zotero via pandoc md."
  :group 'org-export)

(defcustom ox-md-pandoc-path-to-bbt-filter ""
  "Path to the betterbibtex pandoc filter for converting md to zotero odt/word.

Latest file should be here:
https://raw.githubusercontent.com/retorquere/zotero-better-bibtex/master/site/content/exporting/zotero.lua"
  :type 'file)

(defcustom ox-md-pandoc-format "odt"
  "Final export format. odt or docx."
  :type '(choice
          (const "odt")
          (const "docx")))

(defcustom ox-md-pandoc-style "apa"
  "File to set in zotero output file. A valid csl style."
  :type 'string)

(org-export-define-derived-backend 'md-pandoc-zotero 'md
  :menu-entry
  '(?p "MD→pandoc zotero export"
       ((?z "As zotero odt" ox-md-pandoc-zotero-odt))))

(defun ox-md-pandoc-zotero-odt (async subtreep visible-only body-only)
  "Export to md and then to odt with pandoc and zotero-better-bibtex filter."
  (interactive)
  (let ((outfile (org-export-output-file-name ".md" subtreep)))
    (org-export-to-file 'md outfile async subtreep visible-only body-only nil
                        #'ox-md-pandoc-process-zotero-wpfile)))

(defun ox-md-pandoc-process-zotero-wpfile (file)
  "Process markdown FILE to zotero odt or docx."
  (org-open-file
   (org-compile-file
    file
    (list
     (format
      "pandoc -s -o %%b.%s --lua-filter=%s --metadata=zotero_author_in_text=true --metadata=zotero_csl_style=%s %%f"
      ox-md-pandoc-format
      ox-md-pandoc-path-to-bbt-filter
      ox-md-pandoc-style))
    ox-md-pandoc-format
    "See *ox-md-pandoc-zotero output* for details"
    (get-buffer-create "*ox-md-pandoc-zotero output*"))))


(provide 'ox-md-pandoc-zotero)
;;; ox-md-pandoc-zotero.el ends here

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2022-03-10 19:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-06-23 19:56 [wip-cite-new] Exporting to pandoc md (and from there to zotero odt) Anders Johansson
2021-06-23 20:02 ` Bruce D'Arcus
2021-06-29 18:29   ` Matt Price
2021-07-02 15:49     ` Matt Price
2021-07-02 22:10       ` Anders Johansson
2022-03-10 19:50         ` Anders Johansson

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).