Browse Source

HTML export: Make table row tag customizable

Xin Shi writes:

> Hello Experts,
>
> I use org-mode to produce a lot of big tables with numbers in
> them. When I present these tables by HTML, I found it's hard to
> keep track which row it is. I'm wondering if it's possible to
> implement additional class attribute to the <tr>, such as:
>
> <table class="sample">
> <tr class="d0"><td>One</td><td>Fish</td></tr>
> <tr class="d1"><td>Two</td><td>Fish</td></tr>
>
> <tr class="d0"><td>Red</td><td>Fish</td></tr>
> <tr class="d1"><td>Blue</td><td>Fish</td></tr>
> </table>
>
> So, that in the CSS file, it'll be easier to implement the color:
>
>
> <style type="text/css">
> table.sample {
> 	border: 6px inset #8B8378;
> 	-moz-border-radius: 6px;
> }
> table.sample td {
> 	border: 1px solid black;
> 	padding: 0.2em 2ex 0.2em 2ex;
>
> 	color: black;
> }
> table.sample tr.d0 td {
> 	background-color: #FCF6CF;
> }
> table.sample tr.d1 td {
> 	background-color: #FEFEF2;
> }
> </style>

This commit introduces a new variable `org-export-table-row-tags'
that can be used for this and similar purposes.  For the example
of the poster, one could use:

 (setq org-export-table-row-tags
       (cons '(if head "<tr>"
                (if (= (mod nline 2) 1)
                    "<tr class=\"d1\">"
                  "<tr class=\"d0\">"))
             "</tr>"))
Carsten Dominik 16 years ago
parent
commit
8e34ea7b48
2 changed files with 45 additions and 7 deletions
  1. 7 0
      lisp/ChangeLog
  2. 38 7
      lisp/org-html.el

+ 7 - 0
lisp/ChangeLog

@@ -1,3 +1,10 @@
+2009-06-18  Carsten Dominik  <carsten.dominik@gmail.com>
+
+	* org-html.el (org-export-table-row-tags): New option.
+	(org-format-org-table-html): Rename `nlines' to `nline', use new
+	option.
+
+
 2009-06-17  Carsten Dominik  <carsten.dominik@gmail.com>
 
 	* org-exp-blocks.el: Declare functions and variables.

+ 38 - 7
lisp/org-html.el

@@ -283,6 +283,35 @@ This is customizable so that alignment options can be specified."
   :group 'org-export-tables
   :type '(cons (string :tag "Opening tag") (string :tag "Closing tag")))
 
+(defcustom org-export-table-row-tags '("<tr>" . "</tr>")
+  "The opening tag for table data fields.
+This is customizable so that alignment options can be specified.
+Instead of strings, these ca be Lisp forms that will be evaluated
+for each row in order to construct the table row tags.  During evaluation,
+the variable `head' will be true when this is a header line, nil when this
+is a body line.  And the variable `nline' will contain the line number,
+starting from 1 in the first header line.  For example
+
+  (setq org-export-table-row-tags
+        (cons '(if head
+                   \"<tr>\"
+                 (if (= (mod nline 2) 1)
+                     \"<tr class=\\\"tr-odd\\\">\"
+                   \"<tr class=\\\"tr-even\\\">\"))
+              \"</tr>\"))
+
+will give even lines the class \"tr-even\" and odd lines the class \"tr-odd\"."
+  :group 'org-export-tables
+  :type '(cons 
+	  (choice :tag "Opening tag"
+		  (string :tag "Specify")
+		  (sexp))
+	  (choice :tag "Closing tag"
+		  (string :tag "Specify")
+		  (sexp))))
+
+
+
 (defcustom org-export-html-table-use-header-tags-for-first-column nil
   "Non-nil means, format column one in tables with header tags.
 When nil, also column one will use data tags."
@@ -1506,8 +1535,8 @@ lang=\"%s\" xml:lang=\"%s\">
 			       (lambda (x) (string-match "^[ \t]*|-" x))
 			       (cdr lines)))))
 
-	 (nlines 0) fnum i
-	 tbopen line fields html gr colgropen)
+	 (nline 0) fnum i
+	 tbopen line fields html gr colgropen rowstart rowend)
     (if splice (setq head nil))
     (unless splice (push (if head "<thead>" "<tbody>") html))
     (setq tbopen t)
@@ -1524,12 +1553,14 @@ lang=\"%s\" xml:lang=\"%s\">
 	;; Break the line into fields
 	(setq fields (org-split-string line "[ \t]*|[ \t]*"))
 	(unless fnum (setq fnum (make-vector (length fields) 0)))
-	(setq nlines (1+ nlines) i -1)
-	(push (concat "<tr>"
+	(setq nline (1+ nline) i -1
+	      rowstart (eval (car org-export-table-row-tags))
+	      rowend (eval (cdr org-export-table-row-tags)))
+	(push (concat rowstart
 		      (mapconcat
 		       (lambda (x)
 			 (setq i (1+ i))
-			 (if (and (< i nlines)
+			 (if (and (< i nline)
 				  (string-match org-table-number-regexp x))
 			     (incf (aref fnum i)))
 			 (cond
@@ -1547,7 +1578,7 @@ lang=\"%s\" xml:lang=\"%s\">
 			   (concat (car org-export-table-data-tags) x
 				   (cdr org-export-table-data-tags)))))
 		       fields "")
-		      "</tr>")
+		      rowend)
 	      html)))
     (unless splice (if tbopen (push "</tbody>" html)))
     (unless splice (push "</table>\n" html))
@@ -1560,7 +1591,7 @@ lang=\"%s\" xml:lang=\"%s\">
 	      (lambda (x)
 		(setq gr (pop org-table-colgroup-info))
 		(format "<col align=\"%s\" />"
-			(if (> (/ (float x) nlines) org-table-number-fraction)
+			(if (> (/ (float x) nline) org-table-number-fraction)
 			    "right" "left")))
 	      fnum "")
 	     "</colgroup>")