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 53a2bf8e86d1db991e8d2c09214d38b8c67c622e 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 before the closing head tag. For LaTeX, insert preamble before \begin{document}. Only alters output for HTML and LaTeX backends. --- lisp/oc-csl.el | 100 ++++++++++++++++++++++++++++--------------------- 1 file changed, 57 insertions(+), 43 deletions(-) diff --git a/lisp/oc-csl.el b/lisp/oc-csl.el index 898fb40f8..b81885b60 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,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 HTML head +fragment or nil if no fragment should be inserted." + (let* ((parameters (cadr (org-cite-csl--rendered-bibliographies info))) + (fragment + (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))))) + (unless (string= fragment "") (concat "<style>\n" fragment "</style>\n")))) + ;;; Export capability (defun org-cite-csl-render-citation (citation _style _backend info) @@ -839,53 +868,38 @@ 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 + (if-let* ((part (org-cite-csl--generate-html-head info))) + (funcall insert-part "</head>" part))) + ('org-latex + (funcall insert-part + "\\begin{document}" (org-cite-csl--generate-latex-preamble info)))))))) ;;; Register `csl' processor -- 2.43.0
