emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: "András Simonyi" <andras.simonyi@gmail.com>
To: emacs-orgmode list <emacs-orgmode@gnu.org>
Subject: [PATCH][oc-csl] Improve reference parsing
Date: Wed, 26 Oct 2022 17:40:17 +0200	[thread overview]
Message-ID: <CAOWRwxDdi8L38+=eqXtXrub3vd5dt4RwnKkGw=x48F4+TNarhg@mail.gmail.com> (raw)

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

Dear All,

the attached patch improves the parsing and exporting of cite
prefixes, suffixes and locators -- the most noticeable change is
probably the  support for formatted locators and of underlining in
general.  Comments are welcome.

best wishes,
András

[-- Attachment #2: 0001-oc-csl.el-Improve-reference-parsing.patch --]
[-- Type: text/x-patch, Size: 5233 bytes --]

From 5bec7025f66eb65f13a701dc616aca2440110c1a Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Andr=C3=A1s=20Simonyi?= <andras.simonyi@gmail.com>
Date: Wed, 26 Oct 2022 12:15:42 +0200
Subject: [PATCH] oc-csl.el: Improve reference parsing

* lisp/oc-csl.el (org-cite-csl--export-backend): New constant to
provide a trivial export back-end for exporting reference affixes and
locators with the simple html-based markup expected by citeproc.
(org-cite-csl--parse-reference): Do not construct the reference
locator and include it in the result, since citeproc does not make use
of it.  Start the suffix immediately after the locator's ending,
skipping the ending comma if necessary.  Use
`org-cite-csl--export-backend' to export reference affixes and
locators.
---
 lisp/oc-csl.el | 45 ++++++++++++++++++++++++++++-----------------
 1 file changed, 28 insertions(+), 17 deletions(-)

diff --git a/lisp/oc-csl.el b/lisp/oc-csl.el
index 1ccb74e92..30eac9f1a 100644
--- a/lisp/oc-csl.el
+++ b/lisp/oc-csl.el
@@ -140,9 +140,10 @@
 (declare-function org-element-property "org-element" (property element))
 (declare-function org-element-put-property "org-element" (element property value))
 
-(declare-function org-export-data "org-export" (data info))
+(declare-function org-export-data-with-backend "org-export" (data backend info))
 (declare-function org-export-derived-backend-p "org-export" (backend &rest backends))
 (declare-function org-export-get-footnote-number "org-export" (footnote info &optional data body-first))
+(declare-function org-export-create-backend "org-export" (&key transcoders))
 
 \f
 ;;; Customization
@@ -310,6 +311,16 @@ If nil then the Chicago author-date style is used as a fallback.")
   "Regexp matching a label in a citation reference suffix.
 Label is in match group 1.")
 
+(defconst org-cite-csl--export-backend
+  (org-export-create-backend
+   :transcoders
+   '((plain-text . (lambda (text _info) text))
+     (bold . (lambda (_bold contents _info) (concat "<b>" contents "</b>")))
+     (italic . (lambda (_italic contents _info) (concat "<i>" contents "</i>")))
+     (underline . (lambda (_underline contents _info)
+		    (concat "<span class=\"underline\">" contents "</span>")))))
+  "Custom backend for exporting citation affixes and locators.")
+
 \f
 ;;; Internal functions
 (defun org-cite-csl--barf-without-citeproc ()
@@ -476,11 +487,10 @@ property in INFO."
 INFO is the export state, as a property list.
 
 The result is a association list.  Keys are: `id', `prefix',`suffix',
-`location', `locator' and `label'."
-  (let (label location-start locator-start location locator prefix suffix)
+`locator' and `label'."
+  (let (label location-start locator-start locator prefix suffix)
     ;; Parse suffix.  Insert it in a temporary buffer to find
-    ;; different parts: pre-label, label, locator, location (label +
-    ;; locator), and suffix.
+    ;; different parts: pre-label, label, locator, and suffix.
     (with-temp-buffer
       (save-excursion
         (insert (org-element-interpret-data
@@ -506,12 +516,15 @@ The result is a association list.  Keys are: `id', `prefix',`suffix',
         (let ((re (rx (or "," (group digit)))))
           (when (re-search-backward re location-start t)
             (goto-char (or (match-end 1) (match-beginning 0)))
-            (setq location (buffer-substring location-start (point)))
-            (setq locator (org-trim (buffer-substring locator-start (point))))
+            (setq locator
+                  (org-cite-parse-objects
+                   (buffer-substring locator-start (point))
+                   t))
             ;; Skip comma in suffix.
+            (when (= (following-char) ?,) (forward-char))
             (setq suffix
                   (org-cite-parse-objects
-                   (buffer-substring (match-end 0) (point-max))
+                   (buffer-substring (point) (point-max))
                    t)))))
       (setq prefix
             (org-cite-concat
@@ -525,18 +538,16 @@ The result is a association list.  Keys are: `id', `prefix',`suffix',
            (lambda (data)
              (org-string-nw-p
               (org-trim
-               ;; When Citeproc exports to Org syntax, avoid mix and
-               ;; matching output formats by also generating Org
-               ;; syntax for prefix and suffix.
-               (if (eq 'org (org-cite-csl--output-format info))
-                   (org-element-interpret-data data)
-                 (org-export-data data info)))))))
+               ;; Export the parsed prefix, suffix, and locator  
+               ;; with a custom backend, which produces the simple
+               ;; html markup expected by citeproc.
+               (org-export-data-with-backend
+                data org-cite-csl--export-backend info))))))
       `((id . ,(org-element-property :key reference))
         (prefix . ,(funcall export prefix))
         (suffix . ,(funcall export suffix))
-        (locator . ,locator)
-        (label . ,label)
-        (location . ,location)))))
+        (locator . ,(funcall export locator))
+        (label . ,label)))))
 
 (defun org-cite-csl--create-structure (citation info)
   "Create Citeproc structure for CITATION object.
-- 
2.25.1


             reply	other threads:[~2022-10-26 15:41 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-26 15:40 András Simonyi [this message]
2022-10-27  4:10 ` [PATCH][oc-csl] Improve reference parsing Ihor Radchenko
2022-11-01 15:02   ` András Simonyi
2022-11-02  6:29     ` Ihor Radchenko
2022-11-02 17:58       ` András Simonyi
2022-11-03  6:34         ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CAOWRwxDdi8L38+=eqXtXrub3vd5dt4RwnKkGw=x48F4+TNarhg@mail.gmail.com' \
    --to=andras.simonyi@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).