emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Nathan Nichols <nathannichols454@gmail.com>
To: Ihor Radchenko <yantar92@posteo.net>
Cc: emacs-orgmode@gnu.org
Subject: Re: [PATCH] Ability to specify :html-head as a function
Date: Sat, 29 Jun 2024 10:19:07 -0400	[thread overview]
Message-ID: <CACJZKxhR-hdAEokC9kHydcmByXnqXSk=TwiX1OcRQOD76KaZqw@mail.gmail.com> (raw)
In-Reply-To: <87a5jdi29k.fsf@localhost>


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


  reply	other threads:[~2024-06-29 14:20 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2024-06-30  8:30                         ` Rudolf Adamkovič
2024-06-30 20:13                           ` Nathan Nichols
2024-07-02 11:59                             ` Ihor Radchenko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='CACJZKxhR-hdAEokC9kHydcmByXnqXSk=TwiX1OcRQOD76KaZqw@mail.gmail.com' \
    --to=nathannichols454@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=yantar92@posteo.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).