Dear Nikolaos and List Members,

On Wed, 28 May 2025 at 20:27, Nikolaos Chatzikonstantinou
<[email protected]> wrote:
> The
> issue I have is with the function `org-cite-csl-render-bibliography':
> it should not output <style> elements because when one exports an
> org-mode document

Thanks for reporting! Unfortunately, I'm not well-versed enough in
HTML matters to assess how norm violating the current behaviour is,
but I'm attaching a tentative patch which moves the CSL-related CSS
fragment to the head of the exported HTML using the same mechanism
that was injecting the CSL-related LaTeX preamble. Comments on whether
we should change the current behaviour and the concrete implementation
in the patch are welcome!

best wishes,
András
From 968f30cf9bef7877893e98ece6735a4b110df1b2 Mon Sep 17 00:00:00 2001
From: Andras Simonyi <[email protected]>
Date: Sat, 31 May 2025 18:20:09 +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): Remove inserting CSL-specific
<style> blocks directly into the HTML-formatted bibliography.
(org-cite-csl-finalizer): Refactor to insert CSL-related HTML or LaTeX
formatting at export finalization. For HTML, insert CSS by searching
for the end of the head's style block. For LaTeX, insert preamble
before \begin{document}.  Only alters output for HTML and LaTeX
backends.
---
 lisp/oc-csl.el | 96 ++++++++++++++++++++++++++++----------------------
 1 file changed, 53 insertions(+), 43 deletions(-)

diff --git a/lisp/oc-csl.el b/lisp/oc-csl.el
index 898fb40f8..d869081c6 100644
--- a/lisp/oc-csl.el
+++ b/lisp/oc-csl.el
@@ -227,7 +227,7 @@ example,
 
     indentation length
 <------------------------->
-max.  label width  separator
+max. label width  separator
 <---------------><-------->
 [Doe22]                    John Doe.  A title...
 [DoeSmithJones19]          John Doe, Jane Smith and...
@@ -822,6 +822,32 @@ 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."
+  (let ((parameters (cadr (org-cite-csl--rendered-bibliographies info))))
+   (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
+            "  .csl-left-margin{float: left; padding-right: 0em;}
+ .csl-right-inline{margin: 0 0 0 %d%s;}\n"
+            (* max-offset char-width)
+            char-width-unit)))
+    (and (cdr (assq 'hanging-indent parameters))
+         (format
+          "  .csl-entry{text-indent: -%s; margin-left: %s;}\n"
+          org-cite-csl-html-hanging-indent
+          org-cite-csl-html-hanging-indent))))) 
+
 
 ;;; Export capability
 (defun org-cite-csl-render-citation (citation _style _backend info)
@@ -839,53 +865,37 @@ 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))))
-    (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))
-      ('org-latex output)
-      (_
-       ;; Parse Org output to re-export it during the regular export
-       ;; process.
-       (org-cite-parse-elements output)))))
+  (let* ((format (org-cite-csl--output-format info))
+	 (outputs (car (org-cite-csl--rendered-bibliographies info)))
+	 (output (cdr (assoc props outputs))))
+    (if (memq format '(html org-latex))
+	output
+      ;; 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)
-  "Add \"hanging\" package if missing from LaTeX output.
+  "Add missing CLS-specific parts to HTML and LaTeX output.
 OUTPUT is the export document, as a string.  INFO is the export state, as a
 property list."
   (org-require-package 'citeproc)
-  (if (not (eq 'org-latex (org-cite-csl--output-format info)))
-      output
-    (with-temp-buffer
-      (save-excursion (insert output))
-      (when (search-forward "\\begin{document}" nil t)
-	(goto-char (match-beginning 0))
-	;; Insert the CSL-specific parts of the LaTeX preamble.
-	(insert (org-cite-csl--generate-latex-preamble info)))
-      (buffer-string))))
+  (let ((output-format (org-cite-csl--output-format info)))
+    (if (not (memq output-format '(html org-latex)))
+	output
+      (let ((insert-part (lambda (anchor part) 
+			   (with-temp-buffer
+			     (save-excursion (insert output))
+			     (when (search-forward anchor nil t)
+			       (goto-char (match-beginning 0))
+			       ;; Insert the CSL-specific parts.
+			       (insert part))
+			     (buffer-string)))))
+	(pcase output-format
+	  ('html
+           (funcall insert-part "</style>" (org-cite-csl--generate-html-head info)))
+	  ('org-latex
+           (funcall insert-part
+                    "\\begin{document}" (org-cite-csl--generate-latex-preamble info))))))))
 
 
 ;;; Register `csl' processor
-- 
2.43.0

Reply via email to