emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Eric Abrahamsen <eric@ericabrahamsen.net>
To: emacs-orgmode@gnu.org
Subject: Re: Attributes on HTML tables?
Date: Sat, 13 Apr 2013 13:31:17 +0800	[thread overview]
Message-ID: <8738uvq9ne.fsf@ericabrahamsen.net> (raw)
In-Reply-To: 877gk7qev2.fsf@ericabrahamsen.net

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

Eric Abrahamsen <eric@ericabrahamsen.net> writes:

> François Pinard <pinard@iro.umontreal.ca> writes:
>
>> Bastien <bzg@gnu.org> writes:
>>
>>> Eric Abrahamsen <eric@ericabrahamsen.net> writes:
>>
>>>> The first step is probably to research the differences between xhtml and
>>>> html 5.
>>
>>> Well, I would even skip this step and just hack something usable.


I sort of fudged on the below. The upside is that it should be pretty
forgiving now: you can set all kinds of strings as your :html-doctype,
and it will do a reasonably good job of guessing how to handle it.

Barring actual bugs or poor design decisions, what's left to do is:

1. Make sure that inlined script and style chunks are escaped correctly,
I seem to remember reading that the commenting/escaping syntax for these
chunks varies according to html flavor.

2. I'd like to add the possibility to put an arbitrary :html-container
attribute on HTML elements, so that things that would have been wrapped
in "div"s can be wrapped in "article", "section", "nav", and so on and
so forth.

Rick Frankel kindly provided a test file for this, which I've modified
below:

#+BEGIN_SRC emacs-lisp
#+TITLE:  HTML 5 Test
#+DATE:  {{{modification-time(%Y-%m-%d)}}}
#+HTML_DOCTYPE: html5
#+BIND: org-html-divs ((preamble "header" "preamble") (content "section" "content") (postamble "footer" "postamble"))
*  Org HTML5 Test
ok? This should be a paragraph with a \\
line break in it. I think
* How about a table
| col1 | col2 |
|------+------|
| a    |    1 |
| b    |    2 |
Tables can't have attributes in HTML5.
* And a list
- a
- b
- c
* And an image
[[file:1356810947473.jpg]]
* A definiton list
#+ATTR_HTML: :options html-container article
- a :: foo
- b :: bar
#+END_SRC

Please break! I'll provide a properly-written patch when we've sorted it out.

E


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-First-stab-at-exporting-to-various-flavors-of-HTML.patch --]
[-- Type: text/x-patch, Size: 15168 bytes --]

From 296bbf0f74d0c3d49259e146597a174e7c14fda9 Mon Sep 17 00:00:00 2001
From: Eric Abrahamsen <eric@ericabrahamsen.net>
Date: Sat, 13 Apr 2013 13:22:14 +0800
Subject: [PATCH] First stab at exporting to various flavors of HTML.

---
 lisp/ox-html.el | 151 +++++++++++++++++++++++++++++++++++---------------------
 1 file changed, 96 insertions(+), 55 deletions(-)

diff --git a/lisp/ox-html.el b/lisp/ox-html.el
index 0ad3dc3..af80707 100644
--- a/lisp/ox-html.el
+++ b/lisp/ox-html.el
@@ -142,6 +142,15 @@
 (defvar org-html--pre/postamble-class "status"
   "CSS class used for pre/postamble")
 
+(defconst org-html-doctype-alist
+  '(("html4" . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">")
+    ("html4-strict" . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">
+\"http://www.w3.org/TR/html4/strict.dtd\"")
+    ("xhtml" . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
+\"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">")
+    ("html5" . "<!DOCTYPE HTML>"))
+  "An alist mapping (x)html flavors to specific doctypes.")
+
 (defconst org-html-special-string-regexps
   '(("\\\\-" . "&#x00ad;")		; shy
     ("---\\([^-]\\)" . "&#x2014;\\1")	; mdash
@@ -747,7 +756,9 @@ in all modes you want.  Then, use the command
   '(:border "2" :cellspacing "0" :cellpadding "6" :rules "groups" :frame "hsides")
   "Default attributes and values which will be used in table tags.
 This is a plist where attributes are symbols, starting with
-colons, and values are strings."
+colons, and values are strings.
+
+When exporting to HTML5, these values will be disregarded."
   :group 'org-export-html
   :version "24.4"
   :package-version '(Org . "8.0")
@@ -855,7 +866,9 @@ CSS classes, then this prefix can be very useful."
   "The extension for exported HTML files.
 %s will be replaced with the charset of the exported file.
 This may be a string, or an alist with export extensions
-and corresponding declarations."
+and corresponding declarations.
+
+This declaration only applies when exporting to XHTML."
   :group 'org-export-html
   :type '(choice
 	  (string :tag "Single declaration")
@@ -871,15 +884,14 @@ Use utf-8 as the default value."
   :package-version '(Org . "8.0")
   :type 'coding-system)
 
-(defcustom org-html-doctype
-  "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">"
+(defcustom org-html-doctype "xhtml"  
   "Document type definition to use for exported HTML files.
 Can be set with the in-buffer HTML_DOCTYPE property or for
 publishing, with :html-doctype."
   :group 'org-export-html
   :version "24.4"
   :package-version '(Org . "8.0")
-  :type 'string)
+  :type 'string) 
 
 (defcustom org-html-container-element "div"
   "HTML element to use for wrapping top level sections.
@@ -1034,7 +1046,7 @@ precedence over this variable."
   '(("en" "<p class=\"author\">Author: %a (%e)</p>
 <p class=\"date\">Date: %d</p>
 <p class=\"creator\">%c</p>
-<p class=\"xhtml-validation\">%v</p>"))
+<p class=\"validation\">%v</p>"))
   "Alist of languages and format strings for the HTML postamble.
 
 The first element of each list is the language code, as used for
@@ -1059,7 +1071,7 @@ like that: \"%%\"."
 		:value-type (string :tag "Format string")))
 
 (defcustom org-html-validation-link
-  "<a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a>"
+  "<a href=\"http://validator.w3.org/check?uri=referer\">Validate</a>"
   "Link to HTML validation service."
   :group 'org-export-html
   :type 'string)
@@ -1239,6 +1251,19 @@ CSS classes, then this prefix can be very useful."
 \f
 ;;; Internal Functions
 
+(defun org-html-xhtml-p (info)
+  (let ((dt (downcase (plist-get info :html-doctype))))
+    (string-match-p dt "xhtml")))  
+
+(defun org-html-html5-p (info)
+  (let ((dt (downcase (plist-get info :html-doctype))))
+        (member dt '("html5" "<!doctype html>"))))  
+
+(defun org-html-close-tag (info)
+  (if (org-html-xhtml-p info)
+      "/>"
+    ">"))
+
 (defun org-html--make-attribute-string (attributes)
   "Return a list of attributes, as a string.
 ATTRIBUTES is a plist where values are either strings or nil. An
@@ -1252,7 +1277,7 @@ attributes with a nil value will be omitted from the result."
                              "\"" "&quot;" (org-html-encode-plain-text item))))
                  (setcar output (format "%s=\"%s\"" key value))))))))
 
-(defun org-html-format-inline-image (src &optional
+(defun org-html-format-inline-image (src &optional info
 					 caption label attr standalone-p)
   "Format an inline image from SRC.
 CAPTION, LABEL and ATTR are optional arguments providing the
@@ -1272,12 +1297,14 @@ When STANDALONE-P is t, wrap the <img.../> into a <div>...</div>."
 				   (file-name-nondirectory src)))))))
     (cond
      (standalone-p
-      (let ((img (format "<img src=\"%s\" %s/>" src attr)))
+      (let ((img (format "<img src=\"%s\" %s%s" src attr
+			 (org-html-close-tag info))))
 	(format "\n<div%s class=\"figure\">%s%s\n</div>"
 		id (format "\n<p>%s</p>" img)
 		(if (and caption (not (string= caption "")))
 		    (format "\n<p>%s</p>" caption) ""))))
-     (t (format "<img src=\"%s\" %s/>" src (concat attr id))))))
+     (t (format "<img src=\"%s\" %s%s" src (concat attr id)
+		(org-html-close-tag info))))))
 
 (defun org-html--textarea-block (element)
   "Transcode ELEMENT into a textarea block.
@@ -1426,7 +1453,8 @@ INFO is a plist used as a communication channel."
 		      (let ((auth (plist-get info :author)))
 			(and auth (org-export-data auth info)))))
 	 (description (plist-get info :description))
-	 (keywords (plist-get info :keywords)))
+	 (keywords (plist-get info :keywords))
+	 (close (org-html-close-tag info)))
     (concat
      (format "<title>%s</title>\n" title)
      (format
@@ -1434,17 +1462,18 @@ INFO is a plist used as a communication channel."
 	(format-time-string
 	 (concat "<!-- " org-html-metadata-timestamp-format " -->\n"))))
      (format
-      "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"/>\n"
+      "<meta http-equiv=\"Content-Type\" content=\"text/html;charset=%s\"%s\n"
       (or (and org-html-coding-system
 	       (fboundp 'coding-system-get)
 	       (coding-system-get org-html-coding-system 'mime-charset))
-	  "iso-8859-1"))
-     (format "<meta name=\"generator\" content=\"Org-mode\"/>\n")
-     (and author (format "<meta name=\"author\" content=\"%s\"/>\n" author))
+	  "iso-8859-1") close) 
+     (format "<meta name=\"generator\" content=\"Org-mode\"%s\n" close) 
+     (and author (format "<meta name=\"author\" content=\"%s\"%s\n" author close)) 
      (and description
-	  (format "<meta name=\"description\" content=\"%s\"/>\n" description))
+	  (format "<meta name=\"description\" content=\"%s\"%s\n" description
+		  close))
      (and keywords
-	  (format "<meta name=\"keywords\" content=\"%s\"/>\n" keywords)))))
+	  (format "<meta name=\"keywords\" content=\"%s\"%s\n" keywords close))))) 
 
 (defun org-html--build-head (info)
   "Return information for the <head>..</head> of the HTML output.
@@ -1457,8 +1486,8 @@ INFO is a plist used as a communication channel."
     (org-element-normalize-string (plist-get info :html-head-extra))
     (when (and (plist-get info :html-htmlized-css-url)
 	       (eq org-html-htmlize-output-type 'css))
-      (format "<link rel=\"stylesheet\" href=\"%s\" type=\"text/css\" />\n"
-	      (plist-get info :html-htmlized-css-url)))
+      (format "<link rel=\"stylesheet\" href=\"%s\" type=\"text/css\" %s>\n"
+	      (plist-get info :html-htmlized-css-url) (org-html-close-tag info)))
     (when (plist-get info :html-head-include-scripts) org-html-scripts))))
 
 (defun org-html--build-mathjax-config (info)
@@ -1555,7 +1584,7 @@ communication channel."
 		       (format-time-string org-html-metadata-timestamp-format)))
 		    (when (plist-get info :with-creator)
 		      (format "<p class=\"creator\">%s</p>\n" creator))
-		    (format "<p class=\"xhtml-validation\">%s</p>\n"
+		    (format "<p class=\"validation\">%s</p>\n"
 			    validation-link))))
 		(t (format-spec
 		    (or (cadr (assoc
@@ -1597,23 +1626,31 @@ holding export options."
 CONTENTS is the transcoded contents string.  INFO is a plist
 holding export options."
   (concat
-   (format
-    (or (and (stringp org-html-xml-declaration)
-	     org-html-xml-declaration)
-	(cdr (assoc (plist-get info :html-extension)
-		    org-html-xml-declaration))
-	(cdr (assoc "html" org-html-xml-declaration))
-
-	"")
-    (or (and org-html-coding-system
-	     (fboundp 'coding-system-get)
-	     (coding-system-get org-html-coding-system 'mime-charset))
-	"iso-8859-1"))
-   "\n"
-   (plist-get info :html-doctype)
+   (when (org-html-xhtml-p info)
+     (format "%s\n"
+	     (format (or (and (stringp org-html-xml-declaration)
+			      org-html-xml-declaration)
+			 (cdr (assoc (plist-get info :html-extension)
+				     org-html-xml-declaration))
+			 (cdr (assoc "html" org-html-xml-declaration))
+			 
+			 "")
+		     (or (and org-html-coding-system
+			      (fboundp 'coding-system-get)
+			      (coding-system-get org-html-coding-system 'mime-charset))
+			 "iso-8859-1"))))
+   (let* ((dt (plist-get info :html-doctype))
+	  (dt-cons (assoc dt org-html-doctype-alist)))
+     (if dt-cons
+	 (cdr dt-cons)
+       dt))
    "\n"
-   (format "<html xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\">\n"
-	   (plist-get info :language) (plist-get info :language))
+   (concat "<html"
+	   (when (org-html-xhtml-p info)
+	     (format
+	      " xmlns=\"http://www.w3.org/1999/xhtml\" lang=\"%s\" xml:lang=\"%s\""
+	      (plist-get info :language) (plist-get info :language)))
+	   ">\n")
    "<head>\n"
    (org-html--build-meta-info info)
    (org-html--build-head info)
@@ -2164,7 +2201,7 @@ holding contextual information."
       ;; Build the real contents of the sub-tree.
       (let* ((type (if numberedp 'ordered 'unordered))
 	     (itemized-body (org-html-format-list-item
-			     contents type nil nil full-text)))
+			     contents type nil info nil full-text)))
 	(concat
 	 (and (org-export-first-sibling-p headline info)
 	      (org-html-begin-plain-list type))
@@ -2222,7 +2259,7 @@ holding contextual information."
 (defun org-html-horizontal-rule (horizontal-rule contents info)
   "Transcode an HORIZONTAL-RULE  object from Org to HTML.
 CONTENTS is nil.  INFO is a plist holding contextual information."
-  "<hr/>")
+  (format "<hr%s" (org-html-close-tag info)))
 
 ;;;; Inline Src Block
 
@@ -2279,11 +2316,12 @@ contextual information."
 	(trans "<code>[-]</code>")
 	(t "")))
 
-(defun org-html-format-list-item (contents type checkbox
+(defun org-html-format-list-item (contents type checkbox info
 					     &optional term-counter-id
 					     headline)
   "Format a list item into HTML."
-  (let ((checkbox (concat (org-html-checkbox checkbox) (and checkbox " "))))
+  (let ((checkbox (concat (org-html-checkbox checkbox) (and checkbox " ")))
+	(br (format "<br%s" (org-html-close-tag info))))
     (concat
      (case type
        (ordered
@@ -2291,13 +2329,13 @@ contextual information."
 	       (extra (if counter (format " value=\"%s\"" counter) "")))
 	  (concat
 	   (format "<li%s>" extra)
-	   (when headline (concat headline "<br/>")))))
+	   (when headline (concat headline br))))) 
        (unordered
 	(let* ((id term-counter-id)
 	       (extra (if id (format " id=\"%s\"" id) "")))
 	  (concat
 	   (format "<li%s>" extra)
-	   (when headline (concat headline "<br/>")))))
+	   (when headline (concat headline br))))) 
        (descriptive
 	(let* ((term term-counter-id))
 	  (setq term (or term "(no term)"))
@@ -2323,7 +2361,7 @@ contextual information."
 	 (tag (let ((tag (org-element-property :tag item)))
 		(and tag (org-export-data tag info)))))
     (org-html-format-list-item
-     contents type checkbox (or tag counter))))
+     contents type checkbox info (or tag counter))))
 
 ;;;; Keyword
 
@@ -2382,7 +2420,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 	(when (and formula-link
 		   (string-match "file:\\([^]]*\\)" formula-link))
 	  (org-html-format-inline-image
-	   (match-string 1 formula-link) caption label attr t))))
+	   (match-string 1 formula-link) info caption label attr t))))
      (t latex-frag))))
 
 ;;;; Latex Fragment
@@ -2401,7 +2439,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 	 (when (and formula-link
 		    (string-match "file:\\([^]]*\\)" formula-link))
 	   (org-html-format-inline-image
-	    (match-string 1 formula-link)))))
+	    (match-string 1 formula-link) info))))
       (t latex-frag))))
 
 ;;;; Line Break
@@ -2409,7 +2447,7 @@ CONTENTS is nil.  INFO is a plist holding contextual information."
 (defun org-html-line-break (line-break contents info)
   "Transcode a LINE-BREAK object from Org to HTML.
 CONTENTS is nil.  INFO is a plist holding contextual information."
-  "<br/>\n")
+  (format "<br%s\n" (org-html-close-tag info)))
 
 ;;;; Link
 
@@ -2434,7 +2472,7 @@ Inline images can have these attributes:
 	 (label (org-element-property :name parent)))
     ;; Return proper string, depending on DISPOSITION.
     (org-html-format-inline-image
-     path caption label
+     path info caption label
      (org-html--make-attribute-string
       (org-export-read-attribute :attr_html parent))
      (org-html-standalone-image-p link info))))
@@ -2755,7 +2793,8 @@ contextual information."
     (when (plist-get info :preserve-breaks)
       (setq output
 	    (replace-regexp-in-string
-	     "\\(\\\\\\\\\\)?[ \t]*\n" "<br/>\n" output)))
+	     "\\(\\\\\\\\\\)?[ \t]*\n"
+	     (format "<br%s\n" (org-html-close-tag info)) output)))
     ;; Return value.
     output))
 
@@ -3030,11 +3069,12 @@ contextual information."
      (let* ((label (org-element-property :name table))
 	    (caption (org-export-get-caption table))
 	    (attributes
-	     (org-html--make-attribute-string
-	      (org-combine-plists
-	       (and label (list :id (org-export-solidify-link-text label)))
-	       (plist-get info :html-table-attributes)
-	       (org-export-read-attribute :attr_html table))))
+	     (if (org-html-html5-p info) ""
+	       (org-html--make-attribute-string
+		(org-combine-plists
+		 (and label (list :id (org-export-solidify-link-text label)))
+		 (plist-get info :html-table-attributes)
+		 (org-export-read-attribute :attr_html table)))))
 	    (alignspec
 	     (if (and (boundp 'org-html-format-table-no-css)
 		      org-html-format-table-no-css)
@@ -3114,9 +3154,10 @@ contextual information."
   ;; Replace each newline character with line break.  Also replace
   ;; each blank line with a line break.
   (setq contents (replace-regexp-in-string
-		  "^ *\\\\\\\\$" "<br/>\n"
+		  "^ *\\\\\\\\$" (format "<br%s\n" (org-html-close-tag info))
 		  (replace-regexp-in-string
-		   "\\(\\\\\\\\\\)?[ \t]*\n" " <br/>\n" contents)))
+		   "\\(\\\\\\\\\\)?[ \t]*\n"
+		   (format " <br%s\n" (org-html-close-tag info)) contents)))
   ;; Replace each white space at beginning of a line with a
   ;; non-breaking space.
   (while (string-match "^[ \t]+" contents)
-- 
1.8.2.1


[-- Attachment #3: Type: text/plain, Size: 1292 bytes --]




> Okay, I've got a nearly-working patch for this, but I'm falling down
> hard on the defcustom. Here's what I thought to do:
>
> #+BEGIN_SRC emacs-lisp
> (defconst org-html-doctype-alist
>   '((html4 . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">")
>     (html4-strict . "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">
> \"http://www.w3.org/TR/html4/strict.dtd\"")
>     (xhtml . "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\"
> \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">")
>     (html5 . "<!DOCTYPE HTML>"))
>   "An alist mapping (x)html flavors to specific doctypes.")
>
> (defcustom org-html-doctype 'xhtml  
>   "Document type definition to use for exported HTML files.
> Can be set with the in-buffer HTML_DOCTYPE property or for
> publishing, with :html-doctype."
>   :group 'org-export-html
>   :version "24.4"
>   :package-version '(Org . "8.0")
>   :type 'i-dont-know-how-to-work-this) 
> #+END_SRC
>
> The end result I'm after is: the user can either set org-html-doctype to
> a symbol from among the cars org-html-doctype-alist, or he/she can set
> it directly to the doctype string. I don't know how to represent that in
> a defcustom.
>
> And of course, if anyone has any better approaches, then speak now or...
> speak later.
>
> Eric

  reply	other threads:[~2013-04-13  5:25 UTC|newest]

Thread overview: 27+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-04-07  2:55 Attributes on HTML tables? François Pinard
2013-04-07  6:27 ` Bastien
2013-04-07 14:42   ` Christian Moe
2013-04-07 18:46     ` Nicolas Goaziou
2013-04-07 20:39       ` Christian Moe
2013-04-07 20:48         ` Nicolas Goaziou
2013-04-08 21:09           ` Christian Moe
2013-04-10 17:44             ` François Pinard
2013-04-12  2:06               ` Eric Abrahamsen
2013-04-12  5:42                 ` Bastien
2013-04-12  6:01                   ` Eric Abrahamsen
2013-04-12  8:46                     ` Bastien
2013-04-12 14:36                       ` François Pinard
2013-04-13  3:38                         ` Eric Abrahamsen
2013-04-13  5:31                           ` Eric Abrahamsen [this message]
2013-04-13 20:42                             ` Rick Frankel
2013-04-14 10:13                               ` Eric Abrahamsen
2013-04-14 18:01                                 ` Rick Frankel
2013-04-15 18:03                                 ` Rick Frankel
2013-04-13 17:10                 ` Rick Frankel
2013-04-14  8:41                   ` Bastien
2013-04-10 19:37             ` Nicolas Goaziou
2013-04-11  8:28               ` Christian Moe
2013-04-11 10:55                 ` Nicolas Goaziou
2013-04-11 12:14                   ` Christian Moe
2013-04-11 15:34                   ` Bastien
2013-04-11 17:29                     ` Nicolas Goaziou

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=8738uvq9ne.fsf@ericabrahamsen.net \
    --to=eric@ericabrahamsen.net \
    --cc=emacs-orgmode@gnu.org \
    /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).