oc-csl.el 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. ;;; oc-csl.el --- csl citation processor for Org -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 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 caps (c), full (f), and caps-full (cf) variants,
  47. ;; - noauthor (na), including bare (b), caps (c) and bare-caps (bc) variants,
  48. ;; - year (y), including a bare (b) variant,
  49. ;; - text (t). including caps (c), full (f), and caps-full (cf) variants,
  50. ;; - default style, including bare (b), caps (c) and bare-caps (bc) variants.
  51. ;; CSL styles recognize "locator" in citation references' suffix. For example,
  52. ;; in the citation
  53. ;;
  54. ;; [cite:see @Tarski-1965 chapter 1, for an example]
  55. ;;
  56. ;; "chapter 1" is the locator. The whole citation is rendered as
  57. ;;
  58. ;; (see Tarski 1965, chap. 1 for an example)
  59. ;;
  60. ;; in the default CSL style.
  61. ;;
  62. ;; The locator starts with a locator term, among "bk.", "bks.", "book", "chap.",
  63. ;; "chaps.", "chapter", "col.", "cols.", "column", "figure", "fig.", "figs.",
  64. ;; "folio", "fol.", "fols.", "number", "no.", "nos.", "line", "l.", "ll.",
  65. ;; "note", "n.", "nn.", "opus", "op.", "opp.", "page", "p.", "pp.", "paragraph",
  66. ;; "para.", "paras.", "¶", "¶¶", "§", "§§", "part", "pt.", "pts.", "section",
  67. ;; "sec.", "secs.", "sub verbo", "s.v.", "s.vv.", "verse", "v.", "vv.",
  68. ;; "volume", "vol.", and "vols.". It ends with the last comma or digit in the
  69. ;; suffix, whichever comes last, or runs till the end of the suffix.
  70. ;;
  71. ;; The part of the suffix before the locator is appended to reference's prefix.
  72. ;; If no locator term is used, but a number is present, then "page" is assumed.
  73. ;; This library was heavily inspired by and borrows from András Simonyi's
  74. ;; Citeproc Org (<https://github.com/andras-simonyi/citeproc-org>) library.
  75. ;; Many thanks to him!
  76. ;;; Code:
  77. (require 'bibtex)
  78. (require 'json)
  79. (require 'oc)
  80. (require 'citeproc nil t)
  81. (declare-function citeproc-style-cite-note "ext:citeproc")
  82. (declare-function citeproc-proc-style "ext:citeproc")
  83. (declare-function citeproc-bt-entry-to-csl "ext:citeproc")
  84. (declare-function citeproc-locale-getter-from-dir "ext:citeproc")
  85. (declare-function citeproc-create "ext:citeproc")
  86. (declare-function citeproc-citation-create "ext:citeproc")
  87. (declare-function citeproc-append-citations "ext:citeproc")
  88. (declare-function citeproc-render-citations "ext:citeproc")
  89. (declare-function citeproc-render-bib "ext:citeproc")
  90. (declare-function citeproc-hash-itemgetter-from-any "ext:citeproc")
  91. (declare-function org-element-interpret-data "org-element" (data))
  92. (declare-function org-element-map "org-element" (data types fun &optional info first-match no-recursion with-affiliated))
  93. (declare-function org-element-property "org-element" (property element))
  94. (declare-function org-element-put-property "org-element" (element property value))
  95. (declare-function org-export-data "org-export" (data info))
  96. (declare-function org-export-derived-backend-p "org-export" (backend &rest backends))
  97. (declare-function org-export-get-footnote-number "org-export" (footnote info &optional data body-first))
  98. ;;; Customization
  99. ;;;; Location of CSL directories
  100. (defcustom org-cite-csl-locales-dir nil
  101. "Directory of CSL locale files.
  102. If nil then only the fallback en-US locale will be available."
  103. :group 'org-cite
  104. :package-version '(Org . "9.5")
  105. :type '(choice
  106. (directory :tag "Locales directory")
  107. (const :tag "Use en-US locale only" nil))
  108. :safe t)
  109. (defcustom org-cite-csl-styles-dir nil
  110. "Directory of CSL style files.
  111. When non-nil, relative style file names are expanded relatively to this
  112. directory. This variable is ignored when style file is absolute."
  113. :group 'org-cite
  114. :package-version '(Org . "9.5")
  115. :type '(choice
  116. (directory :tag "Styles directory")
  117. (const :tag "Use absolute file names" nil))
  118. :safe t)
  119. ;;;; Citelinks
  120. (defcustom org-cite-csl-link-cites t
  121. "When non-nil, link cites to references."
  122. :group 'org-cite
  123. :package-version '(Org . "9.5")
  124. :type 'boolean
  125. :safe #'booleanp)
  126. (defcustom org-cite-csl-no-citelinks-backends '(ascii)
  127. "List of export back-ends for which cite linking is disabled.
  128. Cite linking for export back-ends derived from any of the back-ends listed here,
  129. is also disabled."
  130. :group 'org-cite
  131. :package-version '(Org . "9.5")
  132. :type '(repeat symbol)
  133. :safe t)
  134. ;;;; Output-specific variables
  135. (defcustom org-cite-csl-html-hanging-indent "1.5em"
  136. "Size of hanging-indent for HTML output in valid CSS units."
  137. :group 'org-cite
  138. :package-version '(Org . "9.5")
  139. :type 'string
  140. :safe #'stringp)
  141. (defcustom org-cite-csl-html-label-width-per-char "0.6em"
  142. "Character width in CSS units for calculating entry label widths.
  143. Used only when `second-field-align' is activated by the used CSL style."
  144. :group 'org-cite
  145. :package-version '(Org . "9.5")
  146. :type 'string
  147. :safe #'stringp)
  148. (defcustom org-cite-csl-latex-hanging-indent "1.5em"
  149. "Size of hanging-indent for LaTeX output in valid LaTeX units."
  150. :group 'org-cite
  151. :package-version '(Org . "9.5")
  152. :type 'string
  153. :safe #'stringp)
  154. ;;; Internal variables
  155. (defconst org-cite-csl--etc-dir
  156. (let* ((oc-root (file-name-directory (locate-library "oc")))
  157. (oc-etc-dir-1 (expand-file-name "../etc/csl/" oc-root)))
  158. ;; package.el and straight will put all of org-mode/lisp/ in org-mode/.
  159. ;; This will cause .. to resolve to the directory above Org.
  160. ;; To make life easier for people using package.el or straight, we can
  161. ;; check to see if ../etc/csl exists, and if it doesn't try ./etc/csl.
  162. (if (file-exists-p oc-etc-dir-1) oc-etc-dir-1
  163. (expand-file-name "etc/csl/" oc-root)))
  164. "Directory \"etc/\" from repository.")
  165. (defconst org-cite-csl--fallback-locales-dir org-cite-csl--etc-dir
  166. "Fallback CSL locale files directory.")
  167. (defconst org-cite-csl--fallback-style-file
  168. (expand-file-name "chicago-author-date.csl"
  169. org-cite-csl--etc-dir)
  170. "Default CSL style file, or nil.
  171. If nil then the Chicago author-date style is used as a fallback.")
  172. (defconst org-cite-csl--label-alist
  173. '(("bk." . "book")
  174. ("bks." . "book")
  175. ("book" . "book")
  176. ("chap." . "chapter")
  177. ("chaps." . "chapter")
  178. ("chapter" . "chapter")
  179. ("col." . "column")
  180. ("cols." . "column")
  181. ("column" . "column")
  182. ("figure" . "figure")
  183. ("fig." . "figure")
  184. ("figs." . "figure")
  185. ("folio" . "folio")
  186. ("fol." . "folio")
  187. ("fols." . "folio")
  188. ("number" . "number")
  189. ("no." . "number")
  190. ("nos." . "number")
  191. ("line" . "line")
  192. ("l." . "line")
  193. ("ll." . "line")
  194. ("note" . "note")
  195. ("n." . "note")
  196. ("nn." . "note")
  197. ("opus" . "opus")
  198. ("op." . "opus")
  199. ("opp." . "opus")
  200. ("page" . "page")
  201. ("p" . "page")
  202. ("p." . "page")
  203. ("pp." . "page")
  204. ("paragraph" . "paragraph")
  205. ("para." . "paragraph")
  206. ("paras." . "paragraph")
  207. ("¶" . "paragraph")
  208. ("¶¶" . "paragraph")
  209. ("§" . "paragraph")
  210. ("§§" . "paragraph")
  211. ("part" . "part")
  212. ("pt." . "part")
  213. ("pts." . "part")
  214. ("section" . "section")
  215. ("sec." . "section")
  216. ("secs." . "section")
  217. ("sub verbo" . "sub verbo")
  218. ("s.v." . "sub verbo")
  219. ("s.vv." . "sub verbo")
  220. ("verse" . "verse")
  221. ("v." . "verse")
  222. ("vv." . "verse")
  223. ("volume" . "volume")
  224. ("vol." . "volume")
  225. ("vols." . "volume"))
  226. "Alist mapping locator names to locators.")
  227. (defconst org-cite-csl--label-regexp
  228. ;; Prior to Emacs-27.1 argument of `regexp' form must be a string literal.
  229. ;; It is the reason why `rx' is avoided here.
  230. (rx-to-string `(seq word-start
  231. (regexp ,(regexp-opt (mapcar #'car org-cite-csl--label-alist) t))
  232. (0+ digit)
  233. (or word-start line-end (any ?\s ?\t)))
  234. t)
  235. "Regexp matching a label in a citation reference suffix.
  236. Label is in match group 1.")
  237. ;;; Internal functions
  238. (defun org-cite-csl--barf-without-citeproc ()
  239. "Raise an error if Citeproc library is not loaded."
  240. (unless (featurep 'citeproc) "Citeproc library is not loaded"))
  241. (defun org-cite-csl--note-style-p (info)
  242. "Non-nil when bibliography style implies wrapping citations in footnotes.
  243. INFO is the export state, as a property list."
  244. (citeproc-style-cite-note
  245. (citeproc-proc-style
  246. (org-cite-csl--processor info))))
  247. (defun org-cite-csl--create-structure-params (citation info)
  248. "Return citeproc structure creation params for CITATION object.
  249. STYLE is the citation style, as a string or nil. INFO is the export state, as
  250. a property list."
  251. (let ((style (org-cite-citation-style citation info)))
  252. (pcase style
  253. ;; "author" style.
  254. (`(,(or "author" "a") . ,variant)
  255. (pcase variant
  256. ((or "caps" "c") '(:mode author-only :capitalize-first t))
  257. ((or "full" "f") '(:mode author-only :ignore-et-al t))
  258. ((or "caps-full" "cf") '(:mode author-only :capitalize-first t :ignore-et-al t))
  259. (_ '(:mode author-only))))
  260. ;; "noauthor" style.
  261. (`(,(or "noauthor" "na") . ,variant)
  262. (pcase variant
  263. ((or "bare" "b") '(:mode suppress-author :suppress-affixes t))
  264. ((or "caps" "c") '(:mode suppress-author :capitalize-first t))
  265. ((or "bare-caps" "bc")
  266. '(:mode suppress-author :suppress-affixes t :capitalize-first t))
  267. (_ '(:mode suppress-author))))
  268. ;; "year" style.
  269. (`(,(or "year" "y") . ,variant)
  270. (pcase variant
  271. ((or "bare" "b") '(:mode year-only :suppress-affixes t))
  272. (_ '(:mode year-only))))
  273. ;; "text" style.
  274. (`(,(or "text" "t") . ,variant)
  275. (pcase variant
  276. ((or "caps" "c") '(:mode textual :capitalize-first t))
  277. ((or "full" "f") '(:mode textual :ignore-et-al t))
  278. ((or "caps-full" "cf") '(:mode textual :ignore-et-al t :capitalize-first t))
  279. (_ '(:mode textual))))
  280. ;; Default "nil" style.
  281. (`(,_ . ,variant)
  282. (pcase variant
  283. ((or "caps" "c") '(:capitalize-first t))
  284. ((or "bare" "b") '(:suppress-affixes t))
  285. ((or "bare-caps" "bc") '(:suppress-affixes t :capitalize-first t))
  286. (_ nil)))
  287. ;; This should not happen.
  288. (_ (error "Invalid style: %S" style)))))
  289. (defun org-cite-csl--no-citelinks-p (info)
  290. "Non-nil when export BACKEND should not create cite-reference links."
  291. (or (not org-cite-csl-link-cites)
  292. (and org-cite-csl-no-citelinks-backends
  293. (apply #'org-export-derived-backend-p
  294. (plist-get info :back-end)
  295. org-cite-csl-no-citelinks-backends))
  296. ;; No references are being exported anyway.
  297. (not (org-element-map (plist-get info :parse-tree) 'keyword
  298. (lambda (k)
  299. (equal "PRINT_BIBLIOGRAPHY" (org-element-property :key k)))
  300. info t))))
  301. (defun org-cite-csl--output-format (info)
  302. "Return expected Citeproc's output format.
  303. INFO is the export state, as a property list. The return value is a symbol
  304. corresponding to one of the output formats supported by Citeproc: `html',
  305. `latex', or `org'."
  306. (let ((backend (plist-get info :back-end)))
  307. (cond
  308. ((org-export-derived-backend-p backend 'html) 'html)
  309. ((org-export-derived-backend-p backend 'latex) 'latex)
  310. (t 'org))))
  311. (defun org-cite-csl--style-file (info)
  312. "Return style file associated to current export process.
  313. INFO is the export state, as a property list.
  314. When file name is relative, expand it according to `org-cite-csl-styles-dir',
  315. or raise an error if the variable is unset."
  316. (pcase (org-cite-bibliography-style info)
  317. ('nil org-cite-csl--fallback-style-file)
  318. ((and (pred file-name-absolute-p) file) file)
  319. ((and (guard org-cite-csl-styles-dir) file)
  320. (expand-file-name file org-cite-csl-styles-dir))
  321. (other
  322. (user-error "Cannot handle relative style file name" other))))
  323. (defun org-cite-csl--locale-getter ()
  324. "Return a locale getter.
  325. The getter looks for locales in `org-cite-csl-locales-dir' directory. If it
  326. cannot find them, it retrieves the default \"en_US\" from
  327. `org-cite-csl--fallback-locales-dir'."
  328. (lambda (loc)
  329. (or (and org-cite-csl-locales-dir
  330. (ignore-errors
  331. (funcall (citeproc-locale-getter-from-dir org-cite-csl-locales-dir)
  332. loc)))
  333. (funcall (citeproc-locale-getter-from-dir
  334. org-cite-csl--fallback-locales-dir)
  335. loc))))
  336. (defun org-cite-csl--processor (info)
  337. "Return Citeproc processor reading items from current bibliography.
  338. INFO is the export state, as a property list.
  339. Newly created processor is stored as the value of the `:cite-citeproc-processor'
  340. property in INFO."
  341. (or (plist-get info :cite-citeproc-processor)
  342. (let* ((bibliography (plist-get info :bibliography))
  343. (locale (or (plist-get info :language) "en_US"))
  344. (processor
  345. (citeproc-create
  346. (org-cite-csl--style-file info)
  347. (citeproc-hash-itemgetter-from-any bibliography)
  348. (org-cite-csl--locale-getter)
  349. locale)))
  350. (plist-put info :cite-citeproc-processor processor)
  351. processor)))
  352. (defun org-cite-csl--parse-reference (reference info)
  353. "Return Citeproc's structure associated to citation REFERENCE.
  354. INFO is the export state, as a property list.
  355. The result is a association list. Keys are: `id', `prefix',`suffix',
  356. `location', `locator' and `label'."
  357. (let (label location-start locator-start location locator prefix suffix)
  358. ;; Parse suffix. Insert it in a temporary buffer to find
  359. ;; different parts: pre-label, label, locator, location (label +
  360. ;; locator), and suffix.
  361. (with-temp-buffer
  362. (save-excursion
  363. (insert (org-element-interpret-data
  364. (org-element-property :suffix reference))))
  365. (cond
  366. ((re-search-forward org-cite-csl--label-regexp nil t)
  367. (setq location-start (match-beginning 0))
  368. (setq label (cdr (assoc (match-string 1) org-cite-csl--label-alist)))
  369. (setq locator-start (match-end 1)))
  370. ((re-search-forward (rx digit) nil t)
  371. (setq location-start (match-beginning 0))
  372. (setq label "page")
  373. (setq locator-start location-start))
  374. (t
  375. (setq suffix (org-element-property :suffix reference))))
  376. ;; Find locator's end, and suffix, if any. To that effect, look
  377. ;; for the last comma or digit after label, whichever comes
  378. ;; last.
  379. (unless suffix
  380. (goto-char (point-max))
  381. (let ((re (rx (or "," (group digit)))))
  382. (when (re-search-backward re location-start t)
  383. (goto-char (or (match-end 1) (match-beginning 0)))
  384. (setq location (buffer-substring location-start (point)))
  385. (setq locator (org-trim (buffer-substring locator-start (point))))
  386. ;; Skip comma in suffix.
  387. (setq suffix
  388. (org-cite-parse-objects
  389. (buffer-substring (match-end 0) (point-max))
  390. t)))))
  391. (setq prefix
  392. (org-cite-concat
  393. (org-element-property :prefix reference)
  394. (and location-start
  395. (org-cite-parse-objects
  396. (buffer-substring 1 location-start)
  397. t)))))
  398. ;; Return value.
  399. (let ((export
  400. (lambda (data)
  401. (org-string-nw-p
  402. (org-trim
  403. ;; When Citeproc exports to Org syntax, avoid mix and
  404. ;; matching output formats by also generating Org
  405. ;; syntax for prefix and suffix.
  406. (if (eq 'org (org-cite-csl--output-format info))
  407. (org-element-interpret-data data)
  408. (org-export-data data info)))))))
  409. `((id . ,(org-element-property :key reference))
  410. (prefix . ,(funcall export prefix))
  411. (suffix . ,(funcall export suffix))
  412. (locator . ,locator)
  413. (label . ,label)
  414. (location . ,location)))))
  415. (defun org-cite-csl--create-structure (citation info)
  416. "Create Citeproc structure for CITATION object.
  417. INFO is the export state, as a property list."
  418. (let* ((cites (mapcar (lambda (r)
  419. (org-cite-csl--parse-reference r info))
  420. (org-cite-get-references citation)))
  421. (footnote (org-cite-inside-footnote-p citation)))
  422. ;; Global prefix is inserted in front of the prefix of the first
  423. ;; reference.
  424. (let ((global-prefix (org-element-property :prefix citation)))
  425. (when global-prefix
  426. (let* ((first (car cites))
  427. (prefix (org-element-property :prefix first)))
  428. (org-element-put-property
  429. first :prefix (org-cite-concat global-prefix prefix)))))
  430. ;; Global suffix is appended to the suffix of the last reference.
  431. (let ((global-suffix (org-element-property :suffix citation)))
  432. (when global-suffix
  433. (let* ((last (org-last cites))
  434. (suffix (org-element-property :suffix last)))
  435. (org-element-put-property
  436. last :suffix (org-cite-concat suffix global-suffix)))))
  437. ;; Check if CITATION needs wrapping, i.e., it should be wrapped in
  438. ;; a footnote, but isn't yet.
  439. (when (and (not footnote) (org-cite-csl--note-style-p info))
  440. (org-cite-adjust-note citation info)
  441. (org-cite-wrap-citation citation info))
  442. ;; Return structure.
  443. (apply #'citeproc-citation-create
  444. `(:note-index
  445. ,(and footnote (org-export-get-footnote-number footnote info))
  446. :cites ,cites
  447. ,@(org-cite-csl--create-structure-params citation info)))))
  448. (defun org-cite-csl--rendered-citations (info)
  449. "Return the rendered citations as an association list.
  450. INFO is the export state, as a property list.
  451. Return an alist (CITATION . OUTPUT) where CITATION object has been rendered as
  452. OUTPUT using Citeproc."
  453. (or (plist-get info :cite-citeproc-rendered-citations)
  454. (let* ((citations (org-cite-list-citations info))
  455. (processor (org-cite-csl--processor info))
  456. (structures
  457. (mapcar (lambda (c) (org-cite-csl--create-structure c info))
  458. citations)))
  459. (citeproc-append-citations structures processor)
  460. (let* ((rendered
  461. (citeproc-render-citations
  462. processor
  463. (org-cite-csl--output-format info)
  464. (org-cite-csl--no-citelinks-p info)))
  465. (result (seq-mapn #'cons citations rendered)))
  466. (plist-put info :cite-citeproc-rendered-citations result)
  467. result))))
  468. ;;; Export capability
  469. (defun org-cite-csl-render-citation (citation _style _backend info)
  470. "Export CITATION object.
  471. INFO is the export state, as a property list."
  472. (org-cite-csl--barf-without-citeproc)
  473. (let ((output (cdr (assq citation (org-cite-csl--rendered-citations info)))))
  474. (if (not (eq 'org (org-cite-csl--output-format info)))
  475. output
  476. ;; Parse Org output to re-export it during the regular export
  477. ;; process.
  478. (org-cite-parse-objects output))))
  479. (defun org-cite-csl-render-bibliography (_keys _files _style _props _backend info)
  480. "Export bibliography.
  481. INFO is the export state, as a property list."
  482. (org-cite-csl--barf-without-citeproc)
  483. (pcase-let* ((format (org-cite-csl--output-format info))
  484. (`(,output . ,parameters)
  485. (citeproc-render-bib
  486. (org-cite-csl--processor info)
  487. format
  488. (org-cite-csl--no-citelinks-p info))))
  489. (pcase format
  490. ('html
  491. (concat
  492. (and (cdr (assq 'second-field-align parameters))
  493. (let* ((max-offset (cdr (assq 'max-offset parameters)))
  494. (char-width
  495. (string-to-number org-cite-csl-html-label-width-per-char))
  496. (char-width-unit
  497. (progn
  498. (string-match (number-to-string char-width)
  499. org-cite-csl-html-label-width-per-char)
  500. (substring org-cite-csl-html-label-width-per-char
  501. (match-end 0)))))
  502. (format
  503. "<style>.csl-left-margin{float: left; padding-right: 0em;}
  504. .csl-right-inline{margin: 0 0 0 %d%s;}</style>"
  505. (* max-offset char-width)
  506. char-width-unit)))
  507. (and (cdr (assq 'hanging-indent parameters))
  508. (format
  509. "<style>.csl-entry{text-indent: -%s; margin-left: %s;}</style>"
  510. org-cite-csl-html-hanging-indent
  511. org-cite-csl-html-hanging-indent))
  512. output))
  513. ('latex
  514. (if (cdr (assq 'hanging-indent parameters))
  515. (format "\\begin{hangparas}{%s}{1}\n%s\n\\end{hangparas}"
  516. org-cite-csl-latex-hanging-indent
  517. output)
  518. output))
  519. (_
  520. ;; Parse Org output to re-export it during the regular export
  521. ;; process.
  522. (org-cite-parse-elements output)))))
  523. (defun org-cite-csl-finalizer (output _keys _files _style _backend info)
  524. "Add \"hanging\" package if missing from LaTeX output.
  525. OUTPUT is the export document, as a string. INFO is the export state, as a
  526. property list."
  527. (org-cite-csl--barf-without-citeproc)
  528. (if (not (eq 'latex (org-cite-csl--output-format info)))
  529. output
  530. (with-temp-buffer
  531. (save-excursion (insert output))
  532. (when (search-forward "\\begin{document}" nil t)
  533. ;; Ensure that \citeprocitem is defined for citeproc-el
  534. (insert "\\makeatletter\n\\newcommand{\\citeprocitem}[2]{\\hyper@linkstart{cite}{citeproc_bib_item_#1}#2\\hyper@linkend}\n\\makeatother\n\n")
  535. ;; Ensure there is a \usepackage{hanging} somewhere or add one.
  536. (goto-char (match-beginning 0))
  537. (let ((re (rx "\\usepackage" (opt "[" (*? nonl) "]") "{hanging}")))
  538. (unless (re-search-backward re nil t)
  539. (insert "\\usepackage[notquote]{hanging}\n"))))
  540. (buffer-string))))
  541. ;;; Register `csl' processor
  542. (org-cite-register-processor 'csl
  543. :export-citation #'org-cite-csl-render-citation
  544. :export-bibliography #'org-cite-csl-render-bibliography
  545. :export-finalizer #'org-cite-csl-finalizer
  546. :cite-styles
  547. '((("author" "a") ("full" "f") ("caps" "c") ("caps-full" "cf"))
  548. (("noauthor" "na") ("bare" "b") ("caps" "c") ("bare-caps" "bc"))
  549. (("year" "y") ("bare" "b"))
  550. (("text" "t") ("caps" "c") ("full" "f") ("caps-full" "cf"))
  551. (("nil") ("bare" "b") ("caps" "c") ("bare-caps" "bc"))))
  552. (provide 'oc-csl)
  553. ;;; oc-csl.el ends here