Dear All,

I gave a bit more thought to Ihor's suggestion and managed to solve the
problem using the export communication channel, which looks like a more
elegant solution indeed. I'm attaching the (somewhat tentative) patch in
this direction. Note that I had to introduce a flag property to indicate
whether the style blocks were already added
because org-cite-csl-render-bibliography is called more than once when
there are several print_bibliography keywords in the document.

best wishes,
András

On Sun, 13 Jul 2025 at 17:17, András Simonyi <[email protected]>
wrote:

> Dear All,
>
> Ihor Radchenko <[email protected]> writes:
>>
>> > ...
>> > This looks fishy to me.
>> > There is no guarantee that </style> is always included into the exported
>> > HTML, so your code may not always add style.
>> > I suspect that a more robust approach could be adding to :html-head.
>>
>> András, any chance you can try with :html-head?
>>
>
> Dear Ihor, thanks for the remarks -- you are absolutely right that I
> didn't consider the possibility that there is no style element in the html
> head at all. I'm attaching a variant which injects a new style element with
> the CSL-related styles before the closing head tag, I think that should be
> sufficiently robust. Or did you refer to something else by "trying with
> :html-head"? I can see that an alternative could be to manipulate the
> export parameters in the communication channel but I'm not sure about the
> pros and cons in that particular case.
>
> best wishes,
> András
>
>
From b4c76875c446e62b7c247343b3cbdc41a9153e53 Mon Sep 17 00:00:00 2001
From: Andras Simonyi <[email protected]>
Date: Mon, 14 Jul 2025 10:27:08 +0200
Subject: [PATCH] lisp/oc-csl.el: Insert CSS styles into the head of exported
 HTML

* lisp/oc-csl.el (org-cite-csl--generate-html-head): New function to
generate CSL-related CSS for HTML exported files.
(org-cite-csl-render-bibliography): In case of HTML export insert
CSL-specific <style> blocks into the HTML head instead of the rendered
bibliography.
---
 lisp/oc-csl.el | 66 +++++++++++++++++++++++++++++---------------------
 1 file changed, 39 insertions(+), 27 deletions(-)

diff --git a/lisp/oc-csl.el b/lisp/oc-csl.el
index 898fb40f8..8de05d1f5 100644
--- a/lisp/oc-csl.el
+++ b/lisp/oc-csl.el
@@ -822,6 +822,35 @@ INFO is the export state, as a property list."
               "\\[CSL-MAXLABEL-CHARS\\]" ,(number-to-string max-offset)))
     result))
 
+(defun org-cite-csl--generate-html-head (info)
+  "Generate the CSL-related part of the HTML head.
+INFO is the export state, as a property list.  Return the generated head
+fragment or nil if no fragment should be inserted."
+  (let* ((parameters (cadr (org-cite-csl--rendered-bibliographies info)))
+	 (head-part
+	  (concat
+	   (and (cdr (assq 'second-field-align parameters))
+		(let* ((max-offset (cdr (assq 'max-offset parameters)))
+		       (char-width
+			(string-to-number org-cite-csl-html-label-width-per-char))
+		       (char-width-unit
+			(progn
+			  (string-match (number-to-string char-width)
+					org-cite-csl-html-label-width-per-char)
+			  (substring org-cite-csl-html-label-width-per-char
+				     (match-end 0)))))
+		  (format
+		   "<style>.csl-left-margin{float: left; padding-right: 0em;}
+ .csl-right-inline{margin: 0 0 0 %d%s;}</style>"
+		   (* max-offset char-width)
+		   char-width-unit)))
+	   (and (cdr (assq 'hanging-indent parameters))
+		(format
+		 "<style>.csl-entry{text-indent: -%s; margin-left: %s;}</style>"
+		 org-cite-csl-html-hanging-indent
+		 org-cite-csl-html-hanging-indent)))))
+    (and (not (string= "" head-part)) head-part)))
+
 
 ;;; Export capability
 (defun org-cite-csl-render-citation (citation _style _backend info)
@@ -839,37 +868,20 @@ INFO is the export state, as a property list."
   "Export bibliography.
 INFO is the export state, as a property list."
   (org-require-package 'citeproc)
-  (pcase-let*  ((format (org-cite-csl--output-format info))
-                (`(,outputs ,parameters) (org-cite-csl--rendered-bibliographies info))
-                (output (cdr (assoc props outputs))))
+  (let* ((format (org-cite-csl--output-format info))
+	 (outputs (car (org-cite-csl--rendered-bibliographies info)))
+	 (output (cdr (assoc props outputs))))
     (pcase format
       ('html
-       (concat
-        (and (cdr (assq 'second-field-align parameters))
-             (let* ((max-offset (cdr (assq 'max-offset parameters)))
-                    (char-width
-                     (string-to-number org-cite-csl-html-label-width-per-char))
-                    (char-width-unit
-                     (progn
-                       (string-match (number-to-string char-width)
-                                     org-cite-csl-html-label-width-per-char)
-                       (substring org-cite-csl-html-label-width-per-char
-                                  (match-end 0)))))
-               (format
-                "<style>.csl-left-margin{float: left; padding-right: 0em;}
- .csl-right-inline{margin: 0 0 0 %d%s;}</style>"
-                (* max-offset char-width)
-                char-width-unit)))
-        (and (cdr (assq 'hanging-indent parameters))
-             (format
-              "<style>.csl-entry{text-indent: -%s; margin-left: %s;}</style>"
-              org-cite-csl-html-hanging-indent
-              org-cite-csl-html-hanging-indent))
-        output))
+       (unless (plist-get info :html-head-csl-styles-added)
+	 (if-let* ((head-part (org-cite-csl--generate-html-head info)))
+	     (plist-put info :html-head
+                        (concat (plist-get info :html-head) head-part)))
+	 (plist-put info :html-head-csl-styles-added t))
+       output)
       ('org-latex output)
       (_
-       ;; Parse Org output to re-export it during the regular export
-       ;; process.
+       ;; Parse Org output to re-export it during the regular export process.
        (org-cite-parse-elements output)))))
 
 (defun org-cite-csl-finalizer (output _keys _files _style _backend info)
-- 
2.43.0

Reply via email to