emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [PATCH] Ability to specify :html-head as a function
@ 2024-05-13  0:04 Nathan Nichols
  2024-05-13 15:23 ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Nathan Nichols @ 2024-05-13  0:04 UTC (permalink / raw)
  To: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 583 bytes --]

Hello org-mode users,

Here's a patch that adds the ability to specify :html-head as a function. I
think this is a logical change because:

1. It provides a wider range of options for how to use :html-head (before
:html-head could only be a string, now it can also be a function.)
2. It is consistent with the behavior of :html-preamble and
:html-postamble, which can both either be a string or a function.

I probably did this wrong but anyway here's my attempt at a patch
submission. Please let me know if you need any additional information or
have any questions.

Thanks,

-Nate

[-- Attachment #1.2: Type: text/html, Size: 745 bytes --]

[-- Attachment #2: 0001-Squashed-commit-of-the-following.patch --]
[-- Type: text/x-patch, Size: 5908 bytes --]

From a4c5d3e648898c91858643c27ff6d56cde7af3be Mon Sep 17 00:00:00 2001
From: Nate Nichols <nathannichols454@gmail.com>
Date: Sun, 12 May 2024 19:53:09 -0400
Subject: [PATCH] Squashed commit of the following:

commit 8160b298a544642881fd10c651fd4e736517cf2f
Author: Nate Nichols <nathannichols454@gmail.com>
Date:   Sun May 12 19:03:25 2024 -0400

    Added ability to specify :html-head as a function
---
 lisp/org-element.el | 20 ++++++++++++---
 lisp/ox-html.el     | 59 +++++++++++++++++++--------------------------
 2 files changed, 42 insertions(+), 37 deletions(-)

diff --git a/lisp/org-element.el b/lisp/org-element.el
index cf0982f18..63222c2cc 100644
--- a/lisp/org-element.el
+++ b/lisp/org-element.el
@@ -5578,9 +5578,8 @@ If there is no affiliated keyword, return the empty string."
 ;; global indentation from the contents of the current element.
 
 (defun org-element-normalize-string (s)
-  "Ensure string S ends with a single newline character.
-
-If S isn't a string return it unchanged.  If S is the empty
+  "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
@@ -5589,6 +5588,21 @@ newline character at its end."
    (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
 	   (replace-match "\n" nil nil s)))))
 
+
+(defun org-element-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-string s)))
+
+
+;; Test cases for `org-element-normalize-str-or-fn'
+(cl-assert (string= (org-element-normalize-str-or-fn (lambda (_res) "abcdefg") nil) "abcdefg\n"))
+(cl-assert (string= (org-element-normalize-str-or-fn "abcdefg") "abcdefg\n") nil)
+(cl-assert (= (org-element-normalize-str-or-fn 123 nil) 123))
+
+
 (defun org-element-normalize-contents (element &optional ignore-first)
   "Normalize plain text in ELEMENT's contents.
 
diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index ec0add65e..72a8590c4 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -131,7 +131,11 @@
     (:html-equation-reference-format "HTML_EQUATION_REFERENCE_FORMAT" nil org-html-equation-reference-format t)
     (:html-postamble nil "html-postamble" org-html-postamble)
     (:html-preamble nil "html-preamble" org-html-preamble)
-    (:html-head "HTML_HEAD" nil org-html-head newline)
+    ;; You should be able to use multiple headline properties "#+EXPORT_HTML_HEAD" in a file.
+    ;; The results of each occurrence will be joined by a newline to form the final string
+    ;; included in the <head> section.
+    ;; TODO: Test/verify this works still. See: `org-export-options-alist'.
+    (:html-head "HTML_HEAD" "html-head" org-html-head newline)
     (:html-head-extra "HTML_HEAD_EXTRA" nil org-html-head-extra newline)
     (:subtitle "SUBTITLE" nil nil parse)
     (:html-head-include-default-style
@@ -1402,6 +1406,24 @@ This option can also be set on with the CREATOR keyword."
   :package-version '(Org . "8.0")
   :type '(string :tag "Creator string"))
 
+
+;;;; Template :: Head
+
+(defcustom org-html-head ""
+  "When set to a string, include that string in the HTML header.
+When set to a function, apply this function and insert the
+returned string.  The function takes the property list of export
+options as its only argument.
+
+Setting :html-preamble in publishing projects will take
+precedence over this variable."
+  :group 'org-export-html
+  :type '(choice (const :tag "Default (empty)" "")
+                 (string :tag "Fixed string")
+		 (function :tag "Function (must return a string)")))
+
+
+
 ;;;; Template :: Preamble
 
 (defcustom org-html-preamble t
@@ -1525,38 +1547,7 @@ style information."
 ;;;###autoload
 (put 'org-html-head-include-default-style 'safe-local-variable 'booleanp)
 
-(defcustom org-html-head ""
-  "Org-wide head definitions for exported HTML files.
-
-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.
-
-For example, a valid value would be:
-
-   <style>
-      p { font-weight: normal; color: gray; }
-      h1 { color: black; }
-      .title { text-align: center; }
-      .todo, .timestamp-kwd { color: red; }
-      .done { color: green; }
-   </style>
-
-If you want to refer to an external style, use something like
 
-   <link rel=\"stylesheet\" type=\"text/css\" href=\"mystyles.css\" />
-
-As the value of this option simply gets inserted into the HTML
-<head> header, you can use it to add any arbitrary text to the
-header.
-
-You can set this on a per-file basis using #+HTML_HEAD:,
-or for publication projects using the :html-head property."
-  :group 'org-export-html
-  :version "24.4"
-  :package-version '(Org . "8.0")
-  :type 'string)
 ;;;###autoload
 (put 'org-html-head 'safe-local-variable 'stringp)
 
@@ -2008,8 +1999,8 @@ 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-element-normalize-str-or-fn (plist-get info :html-head) info)
+    (org-element-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"
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
  2024-05-13  0:04 [PATCH] Ability to specify :html-head as a function Nathan Nichols
@ 2024-05-13 15:23 ` Ihor Radchenko
       [not found]   ` <CACJZKxjn-z2g81zJ2N1_-GMxd2stqC-4wZPVpNSiwtMj1V+jKQ@mail.gmail.com>
  0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2024-05-13 15:23 UTC (permalink / raw)
  To: Nathan Nichols; +Cc: emacs-orgmode


Nathan Nichols <nathannichols454@gmail.com> writes:
> Here's a patch that adds the ability to specify :html-head as a function. I
> think this is a logical change because:
>
> 1. It provides a wider range of options for how to use :html-head (before
> :html-head could only be a string, now it can also be a function.)
> 2. It is consistent with the behavior of :html-preamble and
> :html-postamble, which can both either be a string or a function.

Thanks!
This addition makes sense.

> I probably did this wrong but anyway here's my attempt at a patch
> submission. Please let me know if you need any additional information or
> have any questions.
>
> Thanks,
>
> --- a/lisp/org-element.el
> +++ b/lisp/org-element.el
> @@ -5578,9 +5578,8 @@ If there is no affiliated keyword, return the empty string."
>  ;; global indentation from the contents of the current element.
>  
>  (defun org-element-normalize-string (s)
> -  "Ensure string S ends with a single newline character.
> -
> -If S isn't a string return it unchanged.  If S is the empty
> +  "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
> @@ -5589,6 +5588,21 @@ newline character at its end."
>     (t (and (string-match "\\(\n[ \t]*\\)*\\'" s)
>  	   (replace-match "\n" nil nil s)))))

Please, do not remove existing functions.
Also, please do not modify org-element library when it is just for the
purposes of a single exporter. org-element library is the Org mode
parser, used by all parts of Org mode.

Instead, you can simply define a helper function inside ox-html.el.
  
> +;; Test cases for `org-element-normalize-str-or-fn'
> +(cl-assert (string= (org-element-normalize-str-or-fn (lambda (_res) "abcdefg") nil) "abcdefg\n"))
> +(cl-assert (string= (org-element-normalize-str-or-fn "abcdefg") "abcdefg\n") nil)
> +(cl-assert (= (org-element-normalize-str-or-fn 123 nil) 123))

Tests should go to testing/lisp/..., which see.

>  (defun org-element-normalize-contents (element &optional ignore-first)
>    "Normalize plain text in ELEMENT's contents.
>  
> diff --git a/lisp/ox-html.el b/lisp/ox-html.el
> index ec0add65e..72a8590c4 100644
> --- a/lisp/ox-html.el
> +++ b/lisp/ox-html.el
> @@ -131,7 +131,11 @@
>      (:html-equation-reference-format "HTML_EQUATION_REFERENCE_FORMAT" nil org-html-equation-reference-format t)
>      (:html-postamble nil "html-postamble" org-html-postamble)
>      (:html-preamble nil "html-preamble" org-html-preamble)
> -    (:html-head "HTML_HEAD" nil org-html-head newline)
> +    ;; You should be able to use multiple headline properties "#+EXPORT_HTML_HEAD" in a file.
> +    ;; The results of each occurrence will be joined by a newline to form the final string
> +    ;; included in the <head> section.
> +    ;; TODO: Test/verify this works still. See: `org-export-options-alist'.
> +    (:html-head "HTML_HEAD" "html-head" org-html-head newline)

What is the purpose of "html-head" #+options keyword addition?
Also, is TODO comment for you or for others?

-- 
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>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
       [not found]                 ` <87jziiptoh.fsf@localhost>
@ 2024-06-21 19:21                   ` Nathan Nichols
  2024-06-22 12:35                     ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Nathan Nichols @ 2024-06-21 19:21 UTC (permalink / raw)
  To: Ihor Radchenko, emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1763 bytes --]

> 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 <yantar92@posteo.net> wrote:

> Nathan Nichols <nathannichols454@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>
>

[-- Attachment #1.2: Type: text/html, Size: 2639 bytes --]

[-- Attachment #2: 0001-Added-ability-to-specify-html-head-as-a-string-or-fu.patch --]
[-- Type: text/x-patch, Size: 2526 bytes --]

From 4f4f5c76a490eaab86d721a1331dcee5307687d0 Mon Sep 17 00:00:00 2001
From: Nate Nichols <nathannichols454@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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
  2024-06-21 19:21                   ` Nathan Nichols
@ 2024-06-22 12:35                     ` Ihor Radchenko
  2024-06-29 14:19                       ` Nathan Nichols
  0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2024-06-22 12:35 UTC (permalink / raw)
  To: Nathan Nichols; +Cc: emacs-orgmode

Nathan Nichols <nathannichols454@gmail.com> writes:

>> 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.

Thanks!

> +(defun org-html-normalize-str-or-fn (input &rest trailing)
> +  "If INPUT is a string, it is passed to `org-element-normalize-string'.

Ideally, the first line of the docstring should fully describe what
function does.

Maybe you can add something like

   Normalize INPUT function or string.  Return a string or nil.

> +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)))
       ^org-element-normalize-string

TRAILING name is confusing because it is not what one expects to be a
name of function arguments.  Maybe

    (defun org-html-normalize-str-or-fn (input &rest args)


Also, you need to update docstrings and type definitions for
`org-html-head' and `org-html-head-extra', update the Org manual, and
announce the new allowed values in etc/ORG-NEWS.

-- 
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>


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
  2024-06-22 12:35                     ` Ihor Radchenko
@ 2024-06-29 14:19                       ` Nathan Nichols
  2024-06-30  8:30                         ` Rudolf Adamkovič
  0 siblings, 1 reply; 8+ messages in thread
From: Nathan Nichols @ 2024-06-29 14:19 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 1827 bytes --]

Ok, here's a patch. Please let me know if this is acceptable or not.

On Sat, Jun 22, 2024 at 8:33 AM Ihor Radchenko <yantar92@posteo.net> wrote:

> Nathan Nichols <nathannichols454@gmail.com> writes:
>
> >> 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.
>
> Thanks!
>
> > +(defun org-html-normalize-str-or-fn (input &rest trailing)
> > +  "If INPUT is a string, it is passed to `org-element-normalize-string'.
>
> Ideally, the first line of the docstring should fully describe what
> function does.
>
> Maybe you can add something like
>
>    Normalize INPUT function or string.  Return a string or nil.
>
> > +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)))
>        ^org-element-normalize-string
>
> TRAILING name is confusing because it is not what one expects to be a
> name of function arguments.  Maybe
>
>     (defun org-html-normalize-str-or-fn (input &rest args)
>
>
> Also, you need to update docstrings and type definitions for
> `org-html-head' and `org-html-head-extra', update the Org manual, and
> announce the new allowed values in etc/ORG-NEWS.
>
> --
> 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>
>

[-- Attachment #1.2: Type: text/html, Size: 2621 bytes --]

[-- Attachment #2: 0001-Added-ability-to-specify-html-head-as-a-string-or-fu.patch --]
[-- Type: text/x-patch, Size: 4946 bytes --]

From 3b3ad45f506c3446fda0d7702913f1badfd20d46 Mon Sep 17 00:00:00 2001
From: Nate Nichols <nathannichols454@gmail.com>
Date: Thu, 20 Jun 2024 14:25:35 -0400
Subject: [PATCH] Added ability to specify :html-head as a string or function

---
 etc/ORG-NEWS                 |  6 ++++++
 lisp/ox-html.el              | 28 +++++++++++++++++++++-------
 testing/lisp/test-ox-html.el |  6 ++++++
 3 files changed, 33 insertions(+), 7 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..6119bc82a 100644
--- 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.
 
 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-str-or-fn (input &rest args)
+  "Normalize INPUT function or string.  Return a string or nil.
+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,8 @@ 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-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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
  2024-06-29 14:19                       ` Nathan Nichols
@ 2024-06-30  8:30                         ` Rudolf Adamkovič
  2024-06-30 20:13                           ` Nathan Nichols
  0 siblings, 1 reply; 8+ messages in thread
From: Rudolf Adamkovič @ 2024-06-30  8:30 UTC (permalink / raw)
  To: Nathan Nichols, Ihor Radchenko; +Cc: emacs-orgmode

Nathan Nichols <nathannichols454@gmail.com> writes:

> +(defun org-html-normalize-str-or-fn (input &rest args)

How about

  org-html-normalize-string-or-function

instead of

  org-html-normalize-str-or-fn

to match

  org-element-normalize-string

and make searching easier?

Rudy
-- 
"Great minds discuss ideas; average minds discuss events; small minds
discuss people."  --- Anna Eleanor Roosevelt (1884-1962)

Rudolf Adamkovič <rudolf@adamkovic.org> [he/him]
http://adamkovic.org


^ permalink raw reply	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
  2024-06-30  8:30                         ` Rudolf Adamkovič
@ 2024-06-30 20:13                           ` Nathan Nichols
  2024-07-02 11:59                             ` Ihor Radchenko
  0 siblings, 1 reply; 8+ messages in thread
From: Nathan Nichols @ 2024-06-30 20:13 UTC (permalink / raw)
  To: Rudolf Adamkovič; +Cc: Ihor Radchenko, emacs-orgmode


[-- Attachment #1.1: Type: text/plain, Size: 715 bytes --]

Ok here's an updated patch. Please let me know if there's anything else.

On Sun, Jun 30, 2024 at 4:30 AM Rudolf Adamkovič <rudolf@adamkovic.org>
wrote:

> Nathan Nichols <nathannichols454@gmail.com> writes:
>
> > +(defun org-html-normalize-str-or-fn (input &rest args)
>
> How about
>
>   org-html-normalize-string-or-function
>
> instead of
>
>   org-html-normalize-str-or-fn
>
> to match
>
>   org-element-normalize-string
>
> and make searching easier?
>
> Rudy
> --
> "Great minds discuss ideas; average minds discuss events; small minds
> discuss people."  --- Anna Eleanor Roosevelt (1884-1962)
>
> Rudolf Adamkovič <rudolf@adamkovic.org> [he/him]
> http://adamkovic.org
>

[-- Attachment #1.2: Type: text/html, Size: 1279 bytes --]

[-- Attachment #2: 0001-Added-ability-to-specify-html-head-as-a-string-or-fu.patch --]
[-- Type: text/x-patch, Size: 5106 bytes --]

From 153ee0e8ff2a0b17fa7d928af66a608dac18d31b Mon Sep 17 00:00:00 2001
From: Nate Nichols <nathannichols454@gmail.com>
Date: Thu, 20 Jun 2024 14:25:35 -0400
Subject: [PATCH] Added ability to specify :html-head as a string or function

---
 etc/ORG-NEWS                 |  6 ++++++
 lisp/ox-html.el              | 29 ++++++++++++++++++++++-------
 testing/lisp/test-ox-html.el |  8 ++++++++
 3 files changed, 36 insertions(+), 7 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..304671f7e 100644
--- 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.
 
 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.  Return a string or nil.
+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


^ permalink raw reply related	[flat|nested] 8+ messages in thread

* Re: [PATCH] Ability to specify :html-head as a function
  2024-06-30 20:13                           ` Nathan Nichols
@ 2024-07-02 11:59                             ` Ihor Radchenko
  0 siblings, 0 replies; 8+ messages in thread
From: Ihor Radchenko @ 2024-07-02 11:59 UTC (permalink / raw)
  To: Nathan Nichols; +Cc: Rudolf Adamkovič, emacs-orgmode

Nathan Nichols <nathannichols454@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>


^ permalink raw reply	[flat|nested] 8+ messages in thread

end of thread, other threads:[~2024-07-02 11:59 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-13  0:04 [PATCH] Ability to specify :html-head as a function Nathan Nichols
2024-05-13 15:23 ` Ihor Radchenko
     [not found]   ` <CACJZKxjn-z2g81zJ2N1_-GMxd2stqC-4wZPVpNSiwtMj1V+jKQ@mail.gmail.com>
     [not found]     ` <87zfriscml.fsf@localhost>
     [not found]       ` <CACJZKxgwmRd_CewmqQX8e+PEM=XX=93zasL3JsAovwgSAgRguQ@mail.gmail.com>
     [not found]         ` <87v824904a.fsf@localhost>
     [not found]           ` <CACJZKxgjLizWkJ6qpoh759mmM5XjCviJiPbCmuRHiJph2Hbddg@mail.gmail.com>
     [not found]             ` <87bk3v78zq.fsf@localhost>
     [not found]               ` <CACJZKxgSGUikb8ecGjtVXi05gYfme8-POAiEWq-6cyWDnXozRg@mail.gmail.com>
     [not found]                 ` <87jziiptoh.fsf@localhost>
2024-06-21 19:21                   ` Nathan Nichols
2024-06-22 12:35                     ` Ihor Radchenko
2024-06-29 14:19                       ` Nathan Nichols
2024-06-30  8:30                         ` Rudolf Adamkovič
2024-06-30 20:13                           ` Nathan Nichols
2024-07-02 11:59                             ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).