> This looks like a copy-paste of `org-element-normalize-string'.
> Why not simply calling `org-element-normalize-string'?

I changed it at one point, but then changed it back and didn't realize that
it was ultimately unchanged.
Here's a patch that uses `org-element-normalize-string` instead.

> Also, may we move this discussion to Org mailing list? I prefer the
> discussions and development to be public (it helps future maintainers).

Yes, of course. I think I just forgot to hit "reply all".

On Fri, Jun 21, 2024 at 4:47 AM Ihor Radchenko <yanta...@posteo.net> wrote:

> Nathan Nichols <nathannichols...@gmail.com> writes:
>
> > Subject: [PATCH] Added ability to specify :html-head as a string or
> function
> > ...
> > +(defun org-html-normalize-str (s)
> > +  "Return S, or evaluate to a string ending with a single newline
> character.
> > +If S isn't a string or a function, return it unchanged.  If S is the
> empty
> > +string, return it.  Otherwise, return a new string with a single
> > +newline character at its end."
> > +  (cond
> > +   ((not (stringp s)) s)
> > +   ((string= "" s) "")
> > +   (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
> > +        (replace-match "\n" nil nil s)))))
>
> This looks like a copy-paste of `org-element-normalize-string'.
> Why not simply calling `org-element-normalize-string'?
>
> Also, may we move this discussion to Org mailing list? I prefer the
> discussions and development to be public (it helps future maintainers).
>
> --
> Ihor Radchenko // yantar92,
> Org mode contributor,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
>
From 4f4f5c76a490eaab86d721a1331dcee5307687d0 Mon Sep 17 00:00:00 2001
From: Nate Nichols <nathannichols...@gmail.com>
Date: Thu, 20 Jun 2024 14:25:35 -0400
Subject: [PATCH] Added ability to specify :html-head as a string or function

---
 lisp/ox-html.el              | 13 ++++++++++---
 testing/lisp/test-ox-html.el |  6 ++++++
 2 files changed, 16 insertions(+), 3 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index d1687cf5a..0139aa88c 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -2001,15 +2001,22 @@ INFO is a plist used as a communication channel."
 		  org-html-meta-tags))
       ""))))
 
+(defun org-html-normalize-str-or-fn (input &rest trailing)
+  "If INPUT is a string, it is passed to `org-element-normalize-string'.
+If INPUT is a function, it is applied to arguments TRAILING, and the result is
+passed to `org-element-normalize-string'."
+  (let ((s (if (functionp input) (format "%s" (apply input trailing)) input)))
+    (org-element-normalize-str s)))
+
 (defun org-html--build-head (info)
   "Return information for the <head>..</head> of the HTML output.
 INFO is a plist used as a communication channel."
   (org-element-normalize-string
    (concat
     (when (plist-get info :html-head-include-default-style)
-      (org-element-normalize-string org-html-style-default))
-    (org-element-normalize-string (plist-get info :html-head))
-    (org-element-normalize-string (plist-get info :html-head-extra))
+      (org-element-normalize-str org-html-style-default))
+    (org-html-normalize-str-or-fn (plist-get info :html-head) info)
+    (org-html-normalize-str-or-fn (plist-get info :html-head-extra) info)
     (when (and (plist-get info :html-htmlized-css-url)
 	       (eq org-html-htmlize-output-type 'css))
       (org-html-close-tag "link"
diff --git a/testing/lisp/test-ox-html.el b/testing/lisp/test-ox-html.el
index 0959d1441..d079cd271 100644
--- a/testing/lisp/test-ox-html.el
+++ b/testing/lisp/test-ox-html.el
@@ -996,5 +996,11 @@ entirely."
         (should (= 0 (how-many "Created: ")))
         (should (= 1 (how-many "Author=Monsieur Oeuf")))))))
 
+(ert-deftest ox-html/test-normalize-str-or-fn ()
+  ;; Test cases for `org-element-normalize-str-or-fn'
+  (should (string= (org-html-normalize-str-or-fn (lambda (_res) "abcdefg") nil) "abcdefg\n"))
+  (should (string= (org-html-normalize-str-or-fn "abcdefg") "abcdefg\n"))
+  (should (= (org-element-normalize-str-or-fn 123 nil) 123)))
+
 (provide 'test-ox-html)
 ;;; test-ox-html.el ends here
-- 
2.34.1

Reply via email to