all messages for Emacs-related lists mirrored at yhetil.org
 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; 10+ 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] 10+ messages in thread

end of thread, other threads:[~2024-07-07 15:50 UTC | newest]

Thread overview: 10+ 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
2024-07-07 14:34                               ` Nathan Nichols
2024-07-07 15:28                                 ` Ihor Radchenko

Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.