oc-csl.el 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  1. ;;; oc-csl.el --- csl citation processor for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021-2022 Free Software Foundation, Inc.
  3. ;; Author: Nicolas Goaziou <mail@nicolasgoaziou.fr>
  4. ;; This file is part of GNU Emacs.
  5. ;; GNU Emacs is free software: you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; GNU Emacs is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
  15. ;;; Commentary:
  16. ;; This library registers the `csl' citation processor, which provides
  17. ;; the "export" capability for citations.
  18. ;; The processor relies on the external Citeproc Emacs library, which must be
  19. ;; available prior to loading this library.
  20. ;; By default, citations are rendered in Chicago author-date CSL style. You can
  21. ;; use another style file by specifying it in `org-cite-export-processors' or
  22. ;; from within the document by adding the file name to "cite_export" keyword
  23. ;;
  24. ;; #+cite_export: csl /path/to/style-file.csl
  25. ;; #+cite_export: csl "/path/to/style-file.csl"
  26. ;;
  27. ;; With the variable `org-cite-csl-styles-dir' set appropriately, the
  28. ;; above can even be shortened to
  29. ;;
  30. ;; #+cite_export: csl style-file.csl
  31. ;;
  32. ;; Styles can be downloaded, for instance, from the Zotero Style Repository
  33. ;; (<https://www.zotero.org/styles>). Dependent styles (which are not "unique"
  34. ;; in the Zotero Style Repository terminology) are not supported.
  35. ;; The processor uses the "en-US" CSL locale file shipped with Org for rendering
  36. ;; localized dates and terms in the references, independently of the language
  37. ;; settings of the Org document. Additional CSL locales can be made available
  38. ;; by setting `org-cite-csl-locales-dir' to a directory containing the locale
  39. ;; files in question (see <https://github.com/citation-style-language/locales>
  40. ;; for such files).
  41. ;; Bibliography is defined with the "bibliography" keyword. It supports files
  42. ;; with ".bib", ".bibtex", and ".json" extensions. References are exported using
  43. ;; the "print_bibliography" keyword.
  44. ;; The library supports the following citation styles:
  45. ;;
  46. ;; - author (a), including bare (b), caps (c), bare-caps (bc), full (f),
  47. ;; caps-full (cf), and bare-caps-full (bcf) variants,
  48. ;; - noauthor (na), including bare (b), caps (c) and bare-caps (bc) variants,
  49. ;; - nocite (n),
  50. ;; - year (y), including a bare (b) variant,
  51. ;; - text (t). including caps (c), full (f), and caps-full (cf) variants,
  52. ;; - default style, including bare (b), caps (c) and bare-caps (bc) variants.
  53. ;;
  54. ;; Using "*" as a key in a nocite citation includes all available items in
  55. ;; the printed bibliography.
  56. ;; CSL styles recognize "locator" in citation references' suffix. For example,
  57. ;; in the citation
  58. ;;
  59. ;; [cite:see @Tarski-1965 chapter 1, for an example]
  60. ;;
  61. ;; "chapter 1" is the locator. The whole citation is rendered as
  62. ;;
  63. ;; (see Tarski 1965, chap. 1 for an example)
  64. ;;
  65. ;; in the default CSL style.
  66. ;;
  67. ;; The locator starts with a locator term, among "bk.", "bks.", "book", "chap.",
  68. ;; "chaps.", "chapter", "col.", "cols.", "column", "figure", "fig.", "figs.",
  69. ;; "folio", "fol.", "fols.", "number", "no.", "nos.", "line", "l.", "ll.",
  70. ;; "note", "n.", "nn.", "opus", "op.", "opp.", "page", "p.", "pp.", "paragraph",
  71. ;; "para.", "paras.", "¶", "¶¶", "§", "§§", "part", "pt.", "pts.", "section",
  72. ;; "sec.", "secs.", "sub verbo", "s.v.", "s.vv.", "verse", "v.", "vv.",
  73. ;; "volume", "vol.", and "vols.". It ends with the last comma or digit in the
  74. ;; suffix, whichever comes last, or runs till the end of the suffix.
  75. ;;
  76. ;; The part of the suffix before the locator is appended to reference's prefix.
  77. ;; If no locator term is used, but a number is present, then "page" is assumed.
  78. ;; Filtered sub-bibliographies can be printed by passing filtering
  79. ;; options to the "print_bibliography" keywords. E.g.,
  80. ;;
  81. ;; #+print_bibliography: :type book keyword: emacs
  82. ;;
  83. ;; If you need to use a key multiple times, you can separate its
  84. ;; values with commas, but without any space in-between:
  85. ;;
  86. ;; #+print_bibliography: :keyword abc,xyz :type article
  87. ;; This library was heavily inspired by and borrows from András Simonyi's
  88. ;; Citeproc Org (<https://github.com/andras-simonyi/citeproc-org>) library.
  89. ;; Many thanks to him!
  90. ;;; Code:
  91. (require 'cl-lib)
  92. (require 'map)
  93. (require 'bibtex)
  94. (require 'json)
  95. (require 'oc)
  96. (require 'citeproc nil t)
  97. (declare-function citeproc-style-cite-note "ext:citeproc")
  98. (declare-function citeproc-proc-style "ext:citeproc")
  99. (declare-function citeproc-bt-entry-to-csl "ext:citeproc")
  100. (declare-function citeproc-locale-getter-from-dir "ext:citeproc")
  101. (declare-function citeproc-create "ext:citeproc")
  102. (declare-function citeproc-citation-create "ext:citeproc")
  103. (declare-function citeproc-append-citations "ext:citeproc")
  104. (declare-function citeproc-add-uncited "ext:citeproc")
  105. (declare-function citeproc-render-citations "ext:citeproc")
  106. (declare-function citeproc-render-bib "ext:citeproc")
  107. (declare-function citeproc-hash-itemgetter-from-any "ext:citeproc")
  108. (declare-function org-element-interpret-data "org-element" (data))
  109. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated))
  110. (declare-function org-element-property "org-element" (property element))
  111. (declare-function org-element-put-property "org-element" (element property value))
  112. (declare-function org-export-data "org-export" (data info))
  113. (declare-function org-export-derived-backend-p "org-export" (backend &rest backends))
  114. (declare-function org-export-get-footnote-number "org-export" (footnote info &optional data body-first))
  115. ;;; Customization
  116. ;;;; Location of CSL directories
  117. (defcustom org-cite-csl-locales-dir nil
  118. "Directory of CSL locale files.
  119. If nil then only the fallback en-US locale will be available."
  120. :group 'org-cite
  121. :package-version '(Org . "9.5")
  122. :type '(choice
  123. (directory :tag "Locales directory")
  124. (const :tag "Use en-US locale only" nil))
  125. ;; It's not obvious to me that arbitrary locations are safe.
  126. ;;; :safe #'string-or-null-p
  127. )
  128. (defcustom org-cite-csl-styles-dir nil
  129. "Directory of CSL style files.
  130. Relative style file names are expanded according to document's
  131. default directory. If it fails and the variable is non-nil, Org
  132. looks for style files in this directory, too."
  133. :group 'org-cite
  134. :package-version '(Org . "9.5")
  135. :type '(choice
  136. (directory :tag "Styles directory")
  137. (const :tag "No central directory for style files" nil))
  138. ;; It's not obvious to me that arbitrary locations are safe.
  139. ;;; :safe #'string-or-null-p
  140. )
  141. ;;;; Citelinks
  142. (defcustom org-cite-csl-link-cites t
  143. "When non-nil, link cites to references."
  144. :group 'org-cite
  145. :package-version '(Org . "9.5")
  146. :type 'boolean
  147. :safe #'booleanp)
  148. (defcustom org-cite-csl-no-citelinks-backends '(ascii)
  149. "List of export back-ends for which cite linking is disabled.
  150. Cite linking for export back-ends derived from any of the back-ends listed here,
  151. is also disabled."
  152. :group 'org-cite
  153. :package-version '(Org . "9.5")
  154. :type '(repeat symbol))
  155. ;;;; Output-specific variables
  156. (defcustom org-cite-csl-html-hanging-indent "1.5em"
  157. "Size of hanging-indent for HTML output in valid CSS units."
  158. :group 'org-cite
  159. :package-version '(Org . "9.5")
  160. :type 'string
  161. :safe #'stringp)
  162. (defcustom org-cite-csl-html-label-width-per-char "0.6em"
  163. "Character width in CSS units for calculating entry label widths.
  164. Used only when `second-field-align' is activated by the used CSL style."
  165. :group 'org-cite
  166. :package-version '(Org . "9.5")
  167. :type 'string
  168. :safe #'stringp)
  169. (defcustom org-cite-csl-latex-hanging-indent "1.5em"
  170. "Size of hanging-indent for LaTeX output in valid LaTeX units."
  171. :group 'org-cite
  172. :package-version '(Org . "9.5")
  173. :type 'string
  174. :safe #'stringp)
  175. ;;; Internal variables
  176. (defconst org-cite-csl--etc-dir
  177. (let ((oc-root (file-name-directory (locate-library "oc"))))
  178. (cond
  179. ;; First check whether it looks like we're running from the main
  180. ;; Org repository.
  181. ((let ((csl-org (expand-file-name "../etc/csl/" oc-root)))
  182. (and (file-directory-p csl-org) csl-org)))
  183. ;; Next look for the directory alongside oc.el because package.el
  184. ;; and straight will put all of org-mode/lisp/ in org-mode/.
  185. ((let ((csl-pkg (expand-file-name "etc/csl/" oc-root)))
  186. (and (file-directory-p csl-pkg) csl-pkg)))
  187. ;; Finally fall back the location used by shared system installs
  188. ;; and when running directly from Emacs repository.
  189. (t
  190. (expand-file-name "org/csl/" data-directory))))
  191. "Directory containing CSL-related data files.")
  192. (defconst org-cite-csl--fallback-locales-dir org-cite-csl--etc-dir
  193. "Fallback CSL locale files directory.")
  194. (defconst org-cite-csl--fallback-style-file
  195. (expand-file-name "chicago-author-date.csl"
  196. org-cite-csl--etc-dir)
  197. "Default CSL style file, or nil.
  198. If nil then the Chicago author-date style is used as a fallback.")
  199. (defconst org-cite-csl--label-alist
  200. '(("bk." . "book")
  201. ("bks." . "book")
  202. ("book" . "book")
  203. ("chap." . "chapter")
  204. ("chaps." . "chapter")
  205. ("chapter" . "chapter")
  206. ("col." . "column")
  207. ("cols." . "column")
  208. ("column" . "column")
  209. ("figure" . "figure")
  210. ("fig." . "figure")
  211. ("figs." . "figure")
  212. ("folio" . "folio")
  213. ("fol." . "folio")
  214. ("fols." . "folio")
  215. ("number" . "number")
  216. ("no." . "number")
  217. ("nos." . "number")
  218. ("line" . "line")
  219. ("l." . "line")
  220. ("ll." . "line")
  221. ("note" . "note")
  222. ("n." . "note")
  223. ("nn." . "note")
  224. ("opus" . "opus")
  225. ("op." . "opus")
  226. ("opp." . "opus")
  227. ("page" . "page")
  228. ("p" . "page")
  229. ("p." . "page")
  230. ("pp." . "page")
  231. ("paragraph" . "paragraph")
  232. ("para." . "paragraph")
  233. ("paras." . "paragraph")
  234. ("¶" . "paragraph")
  235. ("¶¶" . "paragraph")
  236. ("part" . "part")
  237. ("pt." . "part")
  238. ("pts." . "part")
  239. ("§" . "section")
  240. ("§§" . "section")
  241. ("section" . "section")
  242. ("sec." . "section")
  243. ("secs." . "section")
  244. ("sub verbo" . "sub verbo")
  245. ("s.v." . "sub verbo")
  246. ("s.vv." . "sub verbo")
  247. ("verse" . "verse")
  248. ("v." . "verse")
  249. ("vv." . "verse")
  250. ("volume" . "volume")
  251. ("vol." . "volume")
  252. ("vols." . "volume"))
  253. "Alist mapping locator names to locators.")
  254. (defconst org-cite-csl--label-regexp
  255. ;; Prior to Emacs-27.1 argument of `regexp' form must be a string literal.
  256. ;; It is the reason why `rx' is avoided here.
  257. (rx-to-string
  258. `(seq (or line-start space)
  259. (regexp ,(regexp-opt (mapcar #'car org-cite-csl--label-alist) t))
  260. (0+ digit)
  261. (or word-end line-end space " "))
  262. t)
  263. "Regexp matching a label in a citation reference suffix.
  264. Label is in match group 1.")
  265. ;;; Internal functions
  266. (defun org-cite-csl--barf-without-citeproc ()
  267. "Raise an error if Citeproc library is not loaded."
  268. (unless (featurep 'citeproc)
  269. (error "Citeproc library is not loaded")))
  270. (defun org-cite-csl--note-style-p (info)
  271. "Non-nil when bibliography style implies wrapping citations in footnotes.
  272. INFO is the export state, as a property list."
  273. (citeproc-style-cite-note
  274. (citeproc-proc-style
  275. (org-cite-csl--processor info))))
  276. (defun org-cite-csl--nocite-p (citation info)
  277. "Non-nil when CITATION object's style is nocite.
  278. INFO is the export state, as a property list."
  279. (member (car (org-cite-citation-style citation info))
  280. '("nocite" "n")))
  281. (defun org-cite-csl--create-structure-params (citation info)
  282. "Return citeproc structure creation params for CITATION object.
  283. STYLE is the citation style, as a string or nil. INFO is the export state, as
  284. a property list."
  285. (let ((style (org-cite-citation-style citation info)))
  286. (pcase style
  287. ;; "author" style.
  288. (`(,(or "author" "a") . ,variant)
  289. (pcase variant
  290. ((or "bare" "b") '(:mode author-only :suppress-affixes t))
  291. ((or "caps" "c") '(:mode author-only :capitalize-first t))
  292. ((or "full" "f") '(:mode author-only :ignore-et-al t))
  293. ((or "bare-caps" "bc") '(:mode author-only :suppress-affixes t :capitalize-first t))
  294. ((or "bare-full" "bf") '(:mode author-only :suppress-affixes t :ignore-et-al t))
  295. ((or "caps-full" "cf") '(:mode author-only :capitalize-first t :ignore-et-al t))
  296. ((or "bare-caps-full" "bcf") '(:mode author-only :suppress-affixes t :capitalize-first t :ignore-et-al t))
  297. (_ '(:mode author-only))))
  298. ;; "noauthor" style.
  299. (`(,(or "noauthor" "na") . ,variant)
  300. (pcase variant
  301. ((or "bare" "b") '(:mode suppress-author :suppress-affixes t))
  302. ((or "caps" "c") '(:mode suppress-author :capitalize-first t))
  303. ((or "bare-caps" "bc")
  304. '(:mode suppress-author :suppress-affixes t :capitalize-first t))
  305. (_ '(:mode suppress-author))))
  306. ;; "year" style.
  307. (`(,(or "year" "y") . ,variant)
  308. (pcase variant
  309. ((or "bare" "b") '(:mode year-only :suppress-affixes t))
  310. (_ '(:mode year-only))))
  311. ;; "text" style.
  312. (`(,(or "text" "t") . ,variant)
  313. (pcase variant
  314. ((or "caps" "c") '(:mode textual :capitalize-first t))
  315. ((or "full" "f") '(:mode textual :ignore-et-al t))
  316. ((or "caps-full" "cf") '(:mode textual :ignore-et-al t :capitalize-first t))
  317. (_ '(:mode textual))))
  318. ;; Default "nil" style.
  319. (`(,_ . ,variant)
  320. (pcase variant
  321. ((or "caps" "c") '(:capitalize-first t))
  322. ((or "bare" "b") '(:suppress-affixes t))
  323. ((or "bare-caps" "bc") '(:suppress-affixes t :capitalize-first t))
  324. (_ nil)))
  325. ;; This should not happen.
  326. (_ (error "Invalid style: %S" style)))))
  327. (defun org-cite-csl--no-citelinks-p (info)
  328. "Non-nil when export BACKEND should not create cite-reference links."
  329. (or (not org-cite-csl-link-cites)
  330. (and org-cite-csl-no-citelinks-backends
  331. (apply #'org-export-derived-backend-p
  332. (plist-get info :back-end)
  333. org-cite-csl-no-citelinks-backends))
  334. ;; No references are being exported anyway.
  335. (not (org-element-map (plist-get info :parse-tree) 'keyword
  336. (lambda (k)
  337. (equal "PRINT_BIBLIOGRAPHY" (org-element-property :key k)))
  338. info t))))
  339. (defun org-cite-csl--output-format (info)
  340. "Return expected Citeproc's output format.
  341. INFO is the export state, as a property list. The return value is a symbol
  342. corresponding to one of the output formats supported by Citeproc: `html',
  343. `latex', or `org'."
  344. (let ((backend (plist-get info :back-end)))
  345. (cond
  346. ((org-export-derived-backend-p backend 'html) 'html)
  347. ((org-export-derived-backend-p backend 'latex) 'latex)
  348. (t 'org))))
  349. (defun org-cite-csl--style-file (info)
  350. "Return style file associated to current export process.
  351. INFO is the export state, as a property list.
  352. When file name is relative, look for it in buffer's default
  353. directory, failing that in `org-cite-csl-styles-dir' if non-nil.
  354. Raise an error if no style file can be found."
  355. (pcase (org-cite-bibliography-style info)
  356. ('nil org-cite-csl--fallback-style-file)
  357. ((and (pred file-name-absolute-p) file) file)
  358. ((and (pred file-exists-p) file) (expand-file-name file))
  359. ((and (guard org-cite-csl-styles-dir)
  360. (pred (lambda (f)
  361. (file-exists-p
  362. (expand-file-name f org-cite-csl-styles-dir))))
  363. file)
  364. (expand-file-name file org-cite-csl-styles-dir))
  365. (other
  366. (user-error "CSL style file not found: %S" other))))
  367. (defun org-cite-csl--locale-getter ()
  368. "Return a locale getter.
  369. The getter looks for locales in `org-cite-csl-locales-dir' directory. If it
  370. cannot find them, it retrieves the default \"en_US\" from
  371. `org-cite-csl--fallback-locales-dir'."
  372. (lambda (loc)
  373. (or (and org-cite-csl-locales-dir
  374. (ignore-errors
  375. (funcall (citeproc-locale-getter-from-dir org-cite-csl-locales-dir)
  376. loc)))
  377. (funcall (citeproc-locale-getter-from-dir
  378. org-cite-csl--fallback-locales-dir)
  379. loc))))
  380. (defun org-cite-csl--processor (info)
  381. "Return Citeproc processor reading items from current bibliography.
  382. INFO is the export state, as a property list.
  383. Newly created processor is stored as the value of the `:cite-citeproc-processor'
  384. property in INFO."
  385. (or (plist-get info :cite-citeproc-processor)
  386. (let* ((bibliography (plist-get info :bibliography))
  387. (locale (or (plist-get info :language) "en_US"))
  388. (processor
  389. (citeproc-create
  390. (org-cite-csl--style-file info)
  391. (citeproc-hash-itemgetter-from-any bibliography)
  392. (org-cite-csl--locale-getter)
  393. locale)))
  394. (plist-put info :cite-citeproc-processor processor)
  395. processor)))
  396. (defun org-cite-csl--parse-reference (reference info)
  397. "Return Citeproc's structure associated to citation REFERENCE.
  398. INFO is the export state, as a property list.
  399. The result is a association list. Keys are: `id', `prefix',`suffix',
  400. `location', `locator' and `label'."
  401. (let (label location-start locator-start location locator prefix suffix)
  402. ;; Parse suffix. Insert it in a temporary buffer to find
  403. ;; different parts: pre-label, label, locator, location (label +
  404. ;; locator), and suffix.
  405. (with-temp-buffer
  406. (save-excursion
  407. (insert (org-element-interpret-data
  408. (org-element-property :suffix reference))))
  409. (cond
  410. ((re-search-forward org-cite-csl--label-regexp nil t)
  411. (setq location-start (match-beginning 0))
  412. (setq label (cdr (assoc (match-string 1) org-cite-csl--label-alist)))
  413. (goto-char (match-end 1))
  414. (skip-chars-forward "[:space:] ")
  415. (setq locator-start (point)))
  416. ((re-search-forward (rx digit) nil t)
  417. (setq location-start (match-beginning 0))
  418. (setq label "page")
  419. (setq locator-start location-start))
  420. (t
  421. (setq suffix (org-element-property :suffix reference))))
  422. ;; Find locator's end, and suffix, if any. To that effect, look
  423. ;; for the last comma or digit after label, whichever comes
  424. ;; last.
  425. (unless suffix
  426. (goto-char (point-max))
  427. (let ((re (rx (or "," (group digit)))))
  428. (when (re-search-backward re location-start t)
  429. (goto-char (or (match-end 1) (match-beginning 0)))
  430. (setq location (buffer-substring location-start (point)))
  431. (setq locator (org-trim (buffer-substring locator-start (point))))
  432. ;; Skip comma in suffix.
  433. (setq suffix
  434. (org-cite-parse-objects
  435. (buffer-substring (match-end 0) (point-max))
  436. t)))))
  437. (setq prefix
  438. (org-cite-concat
  439. (org-element-property :prefix reference)
  440. (and location-start
  441. (org-cite-parse-objects
  442. (buffer-substring 1 location-start)
  443. t)))))
  444. ;; Return value.
  445. (let ((export
  446. (lambda (data)
  447. (org-string-nw-p
  448. (org-trim
  449. ;; When Citeproc exports to Org syntax, avoid mix and
  450. ;; matching output formats by also generating Org
  451. ;; syntax for prefix and suffix.
  452. (if (eq 'org (org-cite-csl--output-format info))
  453. (org-element-interpret-data data)
  454. (org-export-data data info)))))))
  455. `((id . ,(org-element-property :key reference))
  456. (prefix . ,(funcall export prefix))
  457. (suffix . ,(funcall export suffix))
  458. (locator . ,locator)
  459. (label . ,label)
  460. (location . ,location)))))
  461. (defun org-cite-csl--create-structure (citation info)
  462. "Create Citeproc structure for CITATION object.
  463. INFO is the export state, as a property list."
  464. (let* ((cites (mapcar (lambda (r)
  465. (org-cite-csl--parse-reference r info))
  466. (org-cite-get-references citation)))
  467. (footnote (org-cite-inside-footnote-p citation)))
  468. ;; Global prefix is inserted in front of the prefix of the first
  469. ;; reference.
  470. (let ((global-prefix (org-element-property :prefix citation)))
  471. (when global-prefix
  472. (let* ((first (car cites))
  473. (prefix-item (assq 'prefix first)))
  474. (setcdr prefix-item
  475. (concat (org-element-interpret-data global-prefix)
  476. " "
  477. (cdr prefix-item))))))
  478. ;; Global suffix is appended to the suffix of the last reference.
  479. (let ((global-suffix (org-element-property :suffix citation)))
  480. (when global-suffix
  481. (let* ((last (org-last cites))
  482. (suffix-item (assq 'suffix last)))
  483. (setcdr suffix-item
  484. (concat (cdr suffix-item)
  485. " "
  486. (org-element-interpret-data global-suffix))))))
  487. ;; Check if CITATION needs wrapping, i.e., it should be wrapped in
  488. ;; a footnote, but isn't yet.
  489. (when (and (not footnote) (org-cite-csl--note-style-p info))
  490. (org-cite-adjust-note citation info)
  491. (setq footnote (org-cite-wrap-citation citation info)))
  492. ;; Return structure.
  493. (apply #'citeproc-citation-create
  494. `(:note-index
  495. ,(and footnote (org-export-get-footnote-number footnote info))
  496. :cites ,cites
  497. ,@(org-cite-csl--create-structure-params citation info)))))
  498. (defun org-cite-csl--rendered-citations (info)
  499. "Return the rendered citations as an association list.
  500. INFO is the export state, as a property list.
  501. Return an alist (CITATION . OUTPUT) where CITATION object has been rendered as
  502. OUTPUT using Citeproc."
  503. (or (plist-get info :cite-citeproc-rendered-citations)
  504. (let ((citations (org-cite-list-citations info))
  505. (processor (org-cite-csl--processor info))
  506. normal-citations nocite-ids)
  507. (dolist (citation citations)
  508. (if (org-cite-csl--nocite-p citation info)
  509. (setq nocite-ids (append (org-cite-get-references citation t) nocite-ids))
  510. (push citation normal-citations)))
  511. (let ((structures
  512. (mapcar (lambda (c) (org-cite-csl--create-structure c info))
  513. (nreverse normal-citations))))
  514. (citeproc-append-citations structures processor))
  515. (when nocite-ids
  516. (citeproc-add-uncited nocite-ids processor))
  517. ;; All bibliographies have to be rendered in order to have
  518. ;; correct citation numbers even if there are several
  519. ;; sub-bibliograhies.
  520. (org-cite-csl--rendered-bibliographies info)
  521. (let (result
  522. (rendered (citeproc-render-citations
  523. processor
  524. (org-cite-csl--output-format info)
  525. (org-cite-csl--no-citelinks-p info))))
  526. (dolist (citation citations)
  527. (push (cons citation
  528. (if (org-cite-csl--nocite-p citation info) "" (pop rendered)))
  529. result))
  530. (setq result (nreverse result))
  531. (plist-put info :cite-citeproc-rendered-citations result)
  532. result))))
  533. (defun org-cite-csl--bibliography-filter (bib-props)
  534. "Return the sub-bibliography filter corresponding to bibliography properties.
  535. BIB-PROPS should be a plist representing the properties
  536. associated with a \"print_bibliography\" keyword, as returned by
  537. `org-cite-bibliography-properties'."
  538. (let (result
  539. (remove-keyword-colon (lambda (x) (intern (substring (symbol-name x) 1)))))
  540. (map-do
  541. (lambda (key value)
  542. (pcase key
  543. ((or :keyword :notkeyword :nottype :notcsltype :filter)
  544. (dolist (v (split-string value ","))
  545. (push (cons (funcall remove-keyword-colon key) v) result)))
  546. ((or :type :csltype)
  547. (if (string-match-p "," value)
  548. (user-error "The \"%s\" print_bibliography option does not support comma-separated values" key)
  549. (push (cons (funcall remove-keyword-colon key) value) result)))))
  550. bib-props)
  551. result))
  552. (defun org-cite-csl--rendered-bibliographies (info)
  553. "Return the rendered bibliographies.
  554. INFO is the export state, as a property list.
  555. Return an (OUTPUTS PARAMETERS) list where OUTPUTS is an alist
  556. of (BIB-PROPS . OUTPUT) pairs where each key is a property list
  557. of a \"print_bibliography\" keyword and the corresponding OUTPUT
  558. value is the bibliography as rendered by Citeproc."
  559. (or (plist-get info :cite-citeproc-rendered-bibliographies)
  560. (let (bib-plists bib-filters)
  561. ;; Collect bibliography property lists and the corresponding
  562. ;; Citeproc sub-bib filters.
  563. (org-element-map (plist-get info :parse-tree) 'keyword
  564. (lambda (keyword)
  565. (when (equal "PRINT_BIBLIOGRAPHY" (org-element-property :key keyword))
  566. (let ((bib-plist (org-cite-bibliography-properties keyword)))
  567. (push bib-plist bib-plists)
  568. (push (org-cite-csl--bibliography-filter bib-plist) bib-filters)))))
  569. (setq bib-filters (nreverse bib-filters)
  570. bib-plists (nreverse bib-plists))
  571. ;; Render and return all bibliographies.
  572. (let ((processor (org-cite-csl--processor info)))
  573. (citeproc-add-subbib-filters bib-filters processor)
  574. (pcase-let* ((format (org-cite-csl--output-format info))
  575. (`(,rendered-bibs . ,parameters)
  576. (citeproc-render-bib
  577. (org-cite-csl--processor info)
  578. format
  579. (org-cite-csl--no-citelinks-p info)))
  580. (outputs (cl-mapcar #'cons bib-plists rendered-bibs))
  581. (result (list outputs parameters)))
  582. (plist-put info :cite-citeproc-rendered-bibliographies result)
  583. result)))))
  584. ;;; Export capability
  585. (defun org-cite-csl-render-citation (citation _style _backend info)
  586. "Export CITATION object.
  587. INFO is the export state, as a property list."
  588. (org-cite-csl--barf-without-citeproc)
  589. (let ((output (cdr (assq citation (org-cite-csl--rendered-citations info)))))
  590. (if (not (eq 'org (org-cite-csl--output-format info)))
  591. output
  592. ;; Parse Org output to re-export it during the regular export
  593. ;; process.
  594. (org-cite-parse-objects output))))
  595. (defun org-cite-csl-render-bibliography (_keys _files _style props _backend info)
  596. "Export bibliography.
  597. INFO is the export state, as a property list."
  598. (org-cite-csl--barf-without-citeproc)
  599. (pcase-let* ((format (org-cite-csl--output-format info))
  600. (`(,outputs ,parameters) (org-cite-csl--rendered-bibliographies info))
  601. (output (cdr (assoc props outputs))))
  602. (pcase format
  603. ('html
  604. (concat
  605. (and (cdr (assq 'second-field-align parameters))
  606. (let* ((max-offset (cdr (assq 'max-offset parameters)))
  607. (char-width
  608. (string-to-number org-cite-csl-html-label-width-per-char))
  609. (char-width-unit
  610. (progn
  611. (string-match (number-to-string char-width)
  612. org-cite-csl-html-label-width-per-char)
  613. (substring org-cite-csl-html-label-width-per-char
  614. (match-end 0)))))
  615. (format
  616. "<style>.csl-left-margin{float: left; padding-right: 0em;}
  617. .csl-right-inline{margin: 0 0 0 %d%s;}</style>"
  618. (* max-offset char-width)
  619. char-width-unit)))
  620. (and (cdr (assq 'hanging-indent parameters))
  621. (format
  622. "<style>.csl-entry{text-indent: -%s; margin-left: %s;}</style>"
  623. org-cite-csl-html-hanging-indent
  624. org-cite-csl-html-hanging-indent))
  625. output))
  626. ('latex
  627. (if (cdr (assq 'hanging-indent parameters))
  628. (format "\\begin{hangparas}{%s}{1}\n%s\n\\end{hangparas}"
  629. org-cite-csl-latex-hanging-indent
  630. output)
  631. output))
  632. (_
  633. ;; Parse Org output to re-export it during the regular export
  634. ;; process.
  635. (org-cite-parse-elements output)))))
  636. (defun org-cite-csl-finalizer (output _keys _files _style _backend info)
  637. "Add \"hanging\" package if missing from LaTeX output.
  638. OUTPUT is the export document, as a string. INFO is the export state, as a
  639. property list."
  640. (org-cite-csl--barf-without-citeproc)
  641. (if (not (eq 'latex (org-cite-csl--output-format info)))
  642. output
  643. (with-temp-buffer
  644. (save-excursion (insert output))
  645. (when (search-forward "\\begin{document}" nil t)
  646. (goto-char (match-beginning 0))
  647. ;; Ensure that \citeprocitem is defined for citeproc-el.
  648. (insert "\\makeatletter\n\\newcommand{\\citeprocitem}[2]{\\hyper@linkstart{cite}{citeproc_bib_item_#1}#2\\hyper@linkend}\n\\makeatother\n\n")
  649. ;; Ensure there is a \usepackage{hanging} somewhere or add one.
  650. (let ((re (rx "\\usepackage" (opt "[" (*? nonl) "]") "{hanging}")))
  651. (unless (re-search-backward re nil t)
  652. (insert "\\usepackage[notquote]{hanging}\n"))))
  653. (buffer-string))))
  654. ;;; Register `csl' processor
  655. (org-cite-register-processor 'csl
  656. :export-citation #'org-cite-csl-render-citation
  657. :export-bibliography #'org-cite-csl-render-bibliography
  658. :export-finalizer #'org-cite-csl-finalizer
  659. :cite-styles
  660. '((("author" "a") ("bare" "b") ("caps" "c") ("full" "f") ("bare-caps" "bc") ("caps-full" "cf") ("bare-caps-full" "bcf"))
  661. (("noauthor" "na") ("bare" "b") ("caps" "c") ("bare-caps" "bc"))
  662. (("year" "y") ("bare" "b"))
  663. (("text" "t") ("caps" "c") ("full" "f") ("caps-full" "cf"))
  664. (("nil") ("bare" "b") ("caps" "c") ("bare-caps" "bc"))
  665. (("nocite" "n"))))
  666. (provide 'oc-csl)
  667. ;;; oc-csl.el ends here