all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: TEC <tecosaur@gmail.com>
To: Jens Lechtenboerger <lechten@wi.uni-muenster.de>
Cc: Bastien <bzg@gnu.org>, org-mode-email <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Enhance org-html--build-meta-info
Date: Thu, 14 Jan 2021 18:36:54 +0800	[thread overview]
Message-ID: <87pn27st06.fsf@gmail.com> (raw)
In-Reply-To: <87mtxgplz8.fsf@gmail.com>

[-- Attachment #1: Type: text/plain, Size: 311 bytes --]


TEC <tecosaur@gmail.com> writes:

>> Sorry, I still see the flycheck warning and "&amp;amp;" for "&".
> Maybe I accidently sent you the old patches? I'll check tomorrow.

Hah, I check and guess what I see? The changes were unstaged 😂.

Sorry about that, here's an actual revision.

--
Timothy


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-lisp-ox-html.el-make-html-meta-tag-builder-nicer.patch --]
[-- Type: text/x-patch, Size: 6034 bytes --]

From 3ab8b4f108c8cfa4b0bf11842907c31846832f1a Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Mon, 14 Dec 2020 17:41:33 +0800
Subject: [PATCH 1/2] lisp/ox-html.el: make html meta tag builder nicer

* lisp/ox-html.el (org-html--build-meta-info): Multi-line repeated
structure extracted to new function `org-html--build-meta-entry'.
The keyword value formatting is changed from `org-export-data' to
`org-html-encode-plain-text' to avoid potentially nesting HTML tags in
meta tags and the <title> element, which would violate W3C.
---
 lisp/ox-html.el | 118 ++++++++++++++++++++++++------------------------
 1 file changed, 60 insertions(+), 58 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 03145e35c..f18f8a2ef 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -1835,78 +1835,80 @@ INFO is a plist used as a communication channel."
 \f
 ;;; Template
 
+(defun org-html--build-meta-entry
+    (label identity &optional content-format &rest content-formatters)
+  "Build a meta tag using the provided information.
+
+Construct <meta> tag of form <meta LABEL=\"IDENTITY\" />, or when CONTENT-FORMAT
+is present: <meta LABEL=\"IDENTITY\" content=\"{content}\" />
+
+Here {content} is determined by applying any CONTENT-FORMATTERS to the
+CONTENT-FORMAT and encoding the result as plain text."
+  (concat "<meta "
+	  (format "%s=\"%s" label identity)
+	  (when content-format
+	    (concat "\" content=\""
+		    (replace-regexp-in-string
+		     "\"" "&quot;"
+		     (org-html-encode-plain-text
+		      (if content-formatters
+			  (apply #'format content-format content-formatters)
+			content-format)))))
+	  "\" />\n"))
+
 (defun org-html--build-meta-info (info)
   "Return meta tags for exported document.
 INFO is a plist used as a communication channel."
-  (let* ((protect-string
-          (lambda (str)
-            (replace-regexp-in-string
-             "\"" "&quot;" (org-html-encode-plain-text str))))
-         (title (org-export-data (plist-get info :title) info))
-         ;; Set title to an invisible character instead of leaving it
-         ;; empty, which is invalid.
-         (title (if (org-string-nw-p title) title "&lrm;"))
-         (author (and (plist-get info :with-author)
-                      (let ((auth (plist-get info :author)))
+  (let* ((title (org-html-plain-text
+		 (org-element-interpret-data (plist-get info :title)) info))
+	 ;; Set title to an invisible character instead of leaving it
+	 ;; empty, which is invalid.
+	 (title (if (org-string-nw-p title) title "&lrm;"))
+	 (author (and (plist-get info :with-author)
+		      (let ((auth (plist-get info :author)))
 			;; Return raw Org syntax.
-                        (and auth (org-element-interpret-data auth)))))
-         (description (plist-get info :description))
-         (keywords (plist-get info :keywords))
-         (charset (or (and org-html-coding-system
-                           (fboundp 'coding-system-get)
-                           (coding-system-get org-html-coding-system
-                                              'mime-charset))
-                      "iso-8859-1")))
+			(and auth (org-html-plain-text
+				   (org-element-interpret-data auth) info)))))
+	 (charset (or (and org-html-coding-system
+			   (fboundp 'coding-system-get)
+			   (symbol-name
+			    (coding-system-get org-html-coding-system
+					       'mime-charset)))
+		      "iso-8859-1")))
     (concat
      (when (plist-get info :time-stamp-file)
        (format-time-string
 	(concat "<!-- "
 		(plist-get info :html-metadata-timestamp-format)
 		" -->\n")))
-     (format
-      (if (org-html-html5-p info)
-	  (org-html-close-tag "meta" "charset=\"%s\"" info)
-	(org-html-close-tag
-	 "meta" "http-equiv=\"Content-Type\" content=\"text/html;charset=%s\""
-	 info))
-      charset) "\n"
+
+     (if (org-html-html5-p info)
+	 (org-html--build-meta-entry "charset" charset)
+       (org-html--build-meta-entry "http-equiv" "Content-Type"
+				   (concat "text/html;charset=" charset)))
+
      (let ((viewport-options
 	    (cl-remove-if-not (lambda (cell) (org-string-nw-p (cadr cell)))
 			      (plist-get info :html-viewport))))
-       (and viewport-options
-	    (concat
-	     (org-html-close-tag
-	      "meta"
-	      (format "name=\"viewport\" content=\"%s\""
-		      (mapconcat
-		       (lambda (elm) (format "%s=%s" (car elm) (cadr elm)))
-		       viewport-options ", "))
-	      info)
-	     "\n")))
+       (if viewport-options
+	   (org-html--build-meta-entry "name" "viewport"
+				       (mapconcat
+					(lambda (elm)
+                                          (format "%s=%s" (car elm) (cadr elm)))
+					viewport-options ", "))))
+
      (format "<title>%s</title>\n" title)
-     (org-html-close-tag "meta" "name=\"generator\" content=\"Org mode\"" info)
-     "\n"
-     (and (org-string-nw-p author)
-	  (concat
-	   (org-html-close-tag "meta"
-			       (format "name=\"author\" content=\"%s\""
-				       (funcall protect-string author))
-			       info)
-	   "\n"))
-     (and (org-string-nw-p description)
-	  (concat
-	   (org-html-close-tag "meta"
-			       (format "name=\"description\" content=\"%s\"\n"
-				       (funcall protect-string description))
-			       info)
-	   "\n"))
-     (and (org-string-nw-p keywords)
-	  (concat
-	   (org-html-close-tag "meta"
-			       (format "name=\"keywords\" content=\"%s\""
-				       (funcall protect-string keywords))
-			       info)
-	   "\n")))))
+
+     (when (org-string-nw-p author)
+       (org-html--build-meta-entry "name" "author" author))
+
+     (when (org-string-nw-p (plist-get info :description))
+       (org-html--build-meta-entry "name" "description" (plist-get info :description)))
+
+     (when (org-string-nw-p (plist-get info :keywords))
+	(org-html--build-meta-entry "keywords" (plist-get info :keywords)))
+
+     (org-html--build-meta-entry "name" "generator" "Org Mode"))))
 
 (defun org-html--build-head (info)
   "Return information for the <head>..</head> of the HTML output.
-- 
2.29.2


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-lisp-ox-html.el-make-html-meta-tags-customizable.patch --]
[-- Type: text/x-patch, Size: 4357 bytes --]

From 6fa5075b04f6996571f8ac5fc19ef8780e9272da Mon Sep 17 00:00:00 2001
From: TEC <tec@tecosaur.com>
Date: Mon, 14 Dec 2020 17:50:15 +0800
Subject: [PATCH 2/2] lisp/ox-html.el: make html meta tags customizable

* lisp/ox-html.el (org-html-meta-tags): Introduce this as a new option
which can be modified to set the meta tags added in HTML exports.
(org-html--build-meta-info): Make use of `org-html-meta-tags' instead of
hardcoded meta tags.  This is leveraging the earlier restructuring of
`org-html--build-meta-info' into a much DRYer form, such that this
modification has a negligible impact on complexity and readability.
---
 lisp/ox-html.el | 59 ++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 44 insertions(+), 15 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index f18f8a2ef..bd2ca1753 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -1425,6 +1425,23 @@ not be modified."
 
 ;;;; Template :: Styles
 
+(defcustom org-html-meta-tags #'org-html-meta-tags-default
+  "Form that is used to produce meta tags in the HTML head.
+
+Can be a list where each item is a list of arguments to be passed
+to `org-html--build-meta-entry'.  Any nil items are ignored.
+
+Also accept a function which gives such a list when called with with
+a single argument (INFO, a communication plist)."
+  :group 'org-export-html
+  :package-version '(Org . "9.5")
+  :type '(choice
+	  (repeat
+	   (list (string :tag "Meta label")
+		 (string :tag "label value")
+		 (string :tag "Content value")))
+	  function))
+
 (defcustom org-html-head-include-default-style t
   "Non-nil means include the default style in exported HTML files.
 The actual style is defined in `org-html-style-default' and
@@ -1835,6 +1852,28 @@ INFO is a plist used as a communication channel."
 \f
 ;;; Template
 
+(defun org-html-meta-tags-default (info)
+  "A default value for `org-html-meta-tags'.
+
+Generate a list items, each of which is a list of arguments that can
+be passed to `org-html--build-meta-entry', to generate meta tags to be
+included in the HTML head.
+
+Use document's plist INFO to derive relevant information for the tags."
+  (let ((author (and (plist-get info :with-author)
+                     (let ((auth (plist-get info :author)))
+                       ;; Return raw Org syntax.
+                       (and auth (org-element-interpret-data auth))))))
+    (list
+     (when (org-string-nw-p author)
+       (list "name" "author" author))
+     (when (org-string-nw-p (plist-get info :description))
+       (list "name" "description"
+             (plist-get info :description)))
+     (when (org-string-nw-p (plist-get info :keywords))
+       (list "name" "keywords" (plist-get info :keywords)))
+     '("name" "generator" "Org Mode"))))
+
 (defun org-html--build-meta-entry
     (label identity &optional content-format &rest content-formatters)
   "Build a meta tag using the provided information.
@@ -1864,11 +1903,6 @@ INFO is a plist used as a communication channel."
 	 ;; Set title to an invisible character instead of leaving it
 	 ;; empty, which is invalid.
 	 (title (if (org-string-nw-p title) title "&lrm;"))
-	 (author (and (plist-get info :with-author)
-		      (let ((auth (plist-get info :author)))
-			;; Return raw Org syntax.
-			(and auth (org-html-plain-text
-				   (org-element-interpret-data auth) info)))))
 	 (charset (or (and org-html-coding-system
 			   (fboundp 'coding-system-get)
 			   (symbol-name
@@ -1899,16 +1933,11 @@ INFO is a plist used as a communication channel."
 
      (format "<title>%s</title>\n" title)
 
-     (when (org-string-nw-p author)
-       (org-html--build-meta-entry "name" "author" author))
-
-     (when (org-string-nw-p (plist-get info :description))
-       (org-html--build-meta-entry "name" "description" (plist-get info :description)))
-
-     (when (org-string-nw-p (plist-get info :keywords))
-	(org-html--build-meta-entry "keywords" (plist-get info :keywords)))
-
-     (org-html--build-meta-entry "name" "generator" "Org Mode"))))
+     (mapconcat
+      (lambda (args) (apply #'org-html--build-meta-entry args))
+      (delq nil (if (functionp org-html-meta-tags)
+		    (funcall org-html-meta-tags info)
+		  org-html-meta-tags)) ""))))
 
 (defun org-html--build-head (info)
   "Return information for the <head>..</head> of the HTML output.
-- 
2.29.2


  reply	other threads:[~2021-01-14 10:40 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-09-17 13:50 [PATCH] Enhance org-html--build-meta-info TEC
2020-09-17 14:21 ` TEC
2020-09-17 15:53 ` Jens Lechtenboerger
2020-09-17 16:14   ` TEC
2020-09-18  8:11     ` Jens Lechtenboerger
2020-09-25 17:48       ` TEC
2020-09-27 15:17         ` Jens Lechtenboerger
2020-09-27 17:39           ` TEC
2020-09-27 18:00             ` Jens Lechtenboerger
2020-09-27 18:35               ` TEC
2020-09-28  8:17                 ` Jens Lechtenboerger
2020-12-13 16:12                   ` TEC
2020-12-14  6:04                     ` Bastien
2020-12-14  6:34                       ` TEC
2020-12-14  7:20                         ` Bastien
2020-12-14  7:27                           ` TEC
2020-12-14  8:11                             ` Bastien
2020-12-14 10:01                               ` TEC
2020-12-14  9:49                       ` Jens Lechtenboerger
2020-12-15 11:39                         ` TEC
2020-12-16  4:13                           ` Tom Gillespie
2020-12-16  5:04                             ` Timothy E Chapman
2020-12-16  6:45                               ` Tom Gillespie
2020-12-16  6:55                           ` Jens Lechtenboerger
2020-12-16  7:22                             ` TEC
2020-12-16  8:37                               ` Jens Lechtenboerger
2020-12-20  5:08                                 ` TEC
2020-12-20 17:59                                   ` Jens Lechtenboerger
2021-01-02 18:51                                     ` TEC
2021-01-03 13:26                                       ` Jens Lechtenboerger
2021-01-03 14:48                                         ` TEC
2021-01-03 15:41                                           ` Jens Lechtenboerger
2021-01-03 17:17                                             ` TEC
2021-01-04  7:11                                               ` Jens Lechtenboerger
2021-01-10 15:52                                                 ` TEC
2021-01-10 17:02                                                   ` Jens Lechtenboerger
2021-01-10 20:36                                                     ` TEC
2021-01-14 10:36                                                       ` TEC [this message]
2021-01-14 15:59                                                         ` Jens Lechtenboerger
2021-01-14 16:02                                                           ` Ready to merge! " TEC
2021-01-21  4:05                                                         ` Kyle Meyer
2021-01-21  5:55                                                           ` TEC
2020-12-20  5:08                                 ` TEC

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

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

  git send-email \
    --in-reply-to=87pn27st06.fsf@gmail.com \
    --to=tecosaur@gmail.com \
    --cc=bzg@gnu.org \
    --cc=emacs-orgmode@gnu.org \
    --cc=lechten@wi.uni-muenster.de \
    /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 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.