Re: [PATCH] Ability to specify :html-head as a function
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 wrote: > Nathan Nichols 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 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 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 ~~ 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 tha
Re: [PATCH] Ability to specify :html-head as a function
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č wrote: > Nathan Nichols writes: > > > +(defun org-html-normalize-str-or-fn (input 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č [he/him] > http://adamkovic.org > From 153ee0e8ff2a0b17fa7d928af66a608dac18d31b Mon Sep 17 00:00:00 2001 From: Nate Nichols 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 ~~ 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 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 .. 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-
Re: [PATCH] Ability to specify :html-head as a function
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 wrote: > Nathan Nichols 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 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 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> > From 3b3ad45f506c3446fda0d7702913f1badfd20d46 Mon Sep 17 00:00:00 2001 From: Nate Nichols 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 ~~ 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." o
Re: [PATCH] Ability to specify :html-head as a function
> 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 wrote: > Nathan Nichols 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 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 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 .. 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
[PATCH] Ability to specify :html-head as a function
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 From a4c5d3e648898c91858643c27ff6d56cde7af3be Mon Sep 17 00:00:00 2001 From: Nate Nichols Date: Sun, 12 May 2024 19:53:09 -0400 Subject: [PATCH] Squashed commit of the following: commit 8160b298a544642881fd10c651fd4e736517cf2f Author: Nate Nichols 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 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 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 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: - - -