OK, I fixed the commit message and other issues that you mentioned. Please
let me know if theres anything else.

On Tue, Jul 2, 2024 at 7:58 AM Ihor Radchenko <yanta...@posteo.net> wrote:

> Nathan Nichols <nathannichols...@gmail.com> writes:
>
> > Ok here's an updated patch. Please let me know if there's anything else.
>
> Thanks!
>
> > Subject: [PATCH] Added ability to specify :html-head as a string or
> function
>
> Please add changelog to the commit message.
> See https://orgmode.org/worg/org-contribute.html#commit-messages
>
> > --- a/lisp/ox-html.el
> > +++ b/lisp/ox-html.el
> > @@ -1531,7 +1531,8 @@ style information."
> >  This variable can contain the full HTML structure to provide a
> >  style, including the surrounding HTML tags.  You can consider
> >  including definitions for the following classes: title, todo,
> > -done, timestamp, timestamp-kwd, tag, target.
> > +done, timestamp, timestamp-kwd, tag, target.  Can be a string, or
> > +a function that accepts the project p-list and returns a string.
> > ...
> >  (defcustom org-html-head-extra ""
> >    "More head information to add in the HTML output.
> >
> > -You can set this on a per-file basis using #+HTML_HEAD_EXTRA:,
> > -or for publication projects using the :html-head-extra property."
> > +You can set this on a per-file basis using #+HTML_HEAD_EXTRA:, or
> > +for publication projects using the :html-head-extra property.
> > +Can be a string, or a function that accepts the project p-list
> > +and returns a string."
>
> We usually say INFO plist or INFO channel. "Project p-list" is not a
> term we use.
>
> >    :group 'org-export-html
> >    :version "24.4"
> >    :package-version '(Org . "8.0")
>
> Please change :package-version to 9.8. This slot indicates the most
> recent version when the meaning of default value of the option has been
> changed.
>
> > +(defun org-html-normalize-string-or-function (input &rest args)
> > +  "Normalize INPUT function or string.  Return a string or nil.
>
> "String or nil" is not accurate, actually. Because
> `org-element-normalize-stirng' either returns the argument unchanged or
> returns a string. Maybe better just drop "Return a string or nil"
> completely.
>
> --
> 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 7ef67df45357dac1758d68c6106a0e0add7bc005 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: Changed :html-head option to accept a string or a
function.
* etc/ORG-NEWS: Added news item for changes to :html-head option.
* testing/lisp/test-ox-html.el: Added tests for
`org-html-normalize-string-or-function`
---
 etc/ORG-NEWS                 |  6 ++++++
 lisp/ox-html.el              | 31 +++++++++++++++++++++++--------
 testing/lisp/test-ox-html.el |  8 ++++++++
 3 files changed, 37 insertions(+), 8 deletions(-)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index b9f51667d..59aceea02 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -92,7 +92,13 @@ This results in an error such as:
 Runtime error near line 2: attempt to write a readonly database (8)
 [ Babel evaluation exited with code 1 ]
 #+end_example
+*** ~org-html-head~ and ~org-html-head-extra~ can now be specified as functions
 
+Previously, ~org-html-head~ and ~org-html-head-extra~ could only be
+specified directly as strings.  Now, they can be set to functions that
+accept the project p-list and return a string.  This makes it possible
+to dynamically generate the content of the resulting ~<head>~ tag in
+the resulting HTML document.
 
 ** Miscellaneous
 *** Trailing =-= is now allowed in plain links
diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index d1687cf5a..ed39a8834 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -1520,7 +1520,7 @@ should not be modified.  Use `org-html-head' to use your own
 style information."
   :group 'org-export-html
   :version "24.4"
-  :package-version '(Org . "8.0")
+  :package-version '(Org . "9.8")
   :type 'boolean)
 ;;;###autoload
 (put 'org-html-head-include-default-style 'safe-local-variable 'booleanp)
@@ -1531,7 +1531,8 @@ style information."
 This variable can contain the full HTML structure to provide a
 style, including the surrounding HTML tags.  You can consider
 including definitions for the following classes: title, todo,
-done, timestamp, timestamp-kwd, tag, target.
+done, timestamp, timestamp-kwd, tag, target.  Can be a string, or
+a function that accepts the INFO p-list and returns a string.
 
 For example, a valid value would be:
 
@@ -1556,19 +1557,23 @@ or for publication projects using the :html-head property."
   :group 'org-export-html
   :version "24.4"
   :package-version '(Org . "8.0")
-  :type 'string)
+  :type '(choice (string :tag "Literal text to insert")
+                 (function :tag "Function evaluating to a string")))
 ;;;###autoload
 (put 'org-html-head 'safe-local-variable 'stringp)
 
 (defcustom org-html-head-extra ""
   "More head information to add in the HTML output.
 
-You can set this on a per-file basis using #+HTML_HEAD_EXTRA:,
-or for publication projects using the :html-head-extra property."
+You can set this on a per-file basis using #+HTML_HEAD_EXTRA:, or
+for publication projects using the :html-head-extra property.
+Can be a string, or a function that accepts the project p-list
+and returns a string."
   :group 'org-export-html
   :version "24.4"
   :package-version '(Org . "8.0")
-  :type 'string)
+  :type '(choice (string :tag "Literal text to insert")
+                 (function :tag "Function evaluating to a string")))
 ;;;###autoload
 (put 'org-html-head-extra 'safe-local-variable 'stringp)
 
@@ -2001,6 +2006,15 @@ INFO is a plist used as a communication channel."
 		  org-html-meta-tags))
       ""))))
 
+(defun org-html-normalize-string-or-function (input &rest args)
+  "Normalize INPUT function or string.
+If INPUT is a string, it is passed to
+`org-element-normalize-string'.  If INPUT is a function, it is
+applied to arguments ARGS, and the result is passed to
+`org-element-normalize-string'."
+  (let ((s (if (functionp input) (format "%s" (apply input args)) input)))
+    (org-element-normalize-string 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."
@@ -2008,8 +2022,9 @@ INFO is a plist used as a communication channel."
    (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-html-normalize-string-or-function (plist-get info :html-head) info)
+    (org-html-normalize-string-or-function (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..af3007392 100644
--- a/testing/lisp/test-ox-html.el
+++ b/testing/lisp/test-ox-html.el
@@ -996,5 +996,13 @@ entirely."
         (should (= 0 (how-many "Created: ")))
         (should (= 1 (how-many "Author=Monsieur Oeuf")))))))
 
+(ert-deftest ox-html/test-normalize-string-or-function ()
+  ;; Test cases for `org-element-normalize-string-or-function'
+  (should (string= (org-html-normalize-string-or-function
+                    (lambda (_res) "abcdefg") nil) "abcdefg\n"))
+  (should (string= (org-html-normalize-string-or-function "abcdefg")
+                   "abcdefg\n"))
+  (should (= (org-element-normalize-string-or-function 123 nil) 123)))
+
 (provide 'test-ox-html)
 ;;; test-ox-html.el ends here
-- 
2.34.1

Reply via email to