emacs-pretest-bug
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

XHTML support in table.el


From: Per Cederqvist
Subject: XHTML support in table.el
Date: Fri, 11 Mar 2005 18:46:53 +0100

I just found table.el in the CVS version of Emacs (updated earlier
today).  I really like it.  The patch below adds support for
generating XHTML 1.0 tables.  The generated table validates as both
1.0 Transitional and 1.0 Strict.

An alternative to this patch would be to just lowercase the TABLE, TR,
TH and TD strings in the generated 'html.  That would cause the output
to be both valid HTML and XHTML.  Maybe that is a better thing to do.

Many thanks for a great library!

Yours,
    /Per Cederqvist

--- table.el    08 Mar 2005 14:41:25 +0100      1.11
+++ table.el    11 Mar 2005 18:39:24 +0100      
@@ -790,6 +790,39 @@
   :type 'string
   :group 'table)
 
+(defcustom table-xhtml-delegate-spacing-to-user-agent nil
+  "*Non-nil delegates cell contents spacing entirely to user agent.
+Otherwise, when nil, it preserves the original spacing and line breaks."
+  :tag "XHTML delegate spacing"
+  :type 'boolean
+  :group 'table)
+
+(defcustom table-xhtml-th-rows 0
+  "*Number of top rows to become header cells automatically in XHTML 
generation."
+  :tag "XHTML Header Rows"
+  :type 'integer
+  :group 'table)
+
+(defcustom table-xhtml-th-columns 0
+  "*Number of left columns to become header cells automatically in XHTML 
generation."
+  :tag "XHTML Header Columns"
+  :type 'integer
+  :group 'table)
+
+(defcustom table-xhtml-table-attribute "border=\"1\""
+  "*Table attribute that applies to the table in XHTML generation."
+  :tag "XHTML table attribute"
+  :type 'string
+  :group 'table)
+
+(defcustom table-xhtml-cell-attribute ""
+  "*Cell attribute that applies to all cells in XHTML generation.
+Do not specify \"align\" and \"valign\" because they are determined by
+the cell contents dynamically."
+  :tag "XHTML cell attribute"
+  :type 'string
+  :group 'table)
+
 (defcustom table-cals-thead-rows 1
   "*Number of top rows to become header rows in CALS table."
   :tag "CALS Header Rows"
@@ -909,7 +942,7 @@
   "For mode line indicator")
 (defvar table-fixed-mode-indicator nil
   "For mode line indicator")
-(defconst table-source-languages '(html latex cals)
+(defconst table-source-languages '(xhtml html latex cals)
   "Supported source languages.")
 (defvar table-source-info-plist nil
   "General storage for temporary information used while generating source.")
@@ -943,7 +976,7 @@
 (defvar table-sequence-increment-history '("1"))
 (defvar table-sequence-interval-history '("1"))
 (defvar table-sequence-justify-history '("left"))
-(defvar table-source-language-history '("html"))
+(defvar table-source-language-history '("xhtml"))
 (defvar table-col-delim-regexp-history '(""))
 (defvar table-row-delim-regexp-history '(""))
 (defvar table-capture-justify-history '("left"))
@@ -3003,20 +3036,20 @@
 (defun table-generate-source (language &optional dest-buffer caption)
   "Generate source of the current table in the specified language.
 LANGUAGE is a symbol that specifies the language to describe the
-structure of the table.  It must be either 'html, 'latex or 'cals.
-The resulted source text is inserted into DEST-BUFFER and the buffer
-object is returned.  When DEST-BUFFER is omitted or nil the default
-buffer specified in `table-dest-buffer-name' is used.  In this case
-the content of the default buffer is erased prior to the generation.
-When DEST-BUFFER is non-nil it is expected to be either a destination
-buffer or a name of the destination buffer.  In this case the
-generated result is inserted at the current point in the destination
-buffer and the previously existing contents in the buffer are
-untouched.
+structure of the table.  It must be either 'html, 'xhtml, 'latex
+or 'cals.  The resulted source text is inserted into DEST-BUFFER
+and the buffer object is returned.  When DEST-BUFFER is omitted
+or nil the default buffer specified in `table-dest-buffer-name'
+is used.  In this case the content of the default buffer is
+erased prior to the generation.  When DEST-BUFFER is non-nil it
+is expected to be either a destination buffer or a name of the
+destination buffer.  In this case the generated result is
+inserted at the current point in the destination buffer and the
+previously existing contents in the buffer are untouched.
 
 References used for this implementation:
 
-HTML:
+HTML and XHTML:
         http://www.w3.org
 
 LaTeX:
@@ -3126,6 +3159,13 @@
                       (not (string= caption "")))
                  (format "  <CAPTION>%s</CAPTION>\n" caption)
                "")))
+     ((eq language 'xhtml)
+      (insert (format "<!-- This XHTML table template is generated by emacs %s 
-->\n" emacs-version)
+             (format "<table %s>\n" table-xhtml-table-attribute)
+             (if (and (stringp caption)
+                      (not (string= caption "")))
+                 (format "  <caption>%s</caption>\n" caption)
+               "")))
      ((eq language 'latex)
       (insert (format "%% This LaTeX table template is generated by emacs 
%s\n" emacs-version)
              "\\begin{tabular}{|" (apply 'concat (make-list (length col-list) 
"l|")) "}\n"
@@ -3149,6 +3189,8 @@
     (cond
      ((eq language 'html)
       (insert "</TABLE>\n"))
+     ((eq language 'xhtml)
+      (insert "</table>\n"))
      ((eq language 'latex)
       (insert "\\end{tabular}\n"))
      ((eq language 'cals)
@@ -3170,6 +3212,8 @@
       (cond
        ((eq language 'html)
        (insert "  <TR>\n"))
+       ((eq language 'xhtml)
+       (insert "  <tr>\n"))
        ((eq language 'cals)
        (insert "      <row>\n"))
        ))
@@ -3178,6 +3222,8 @@
       (cond
        ((eq language 'html)
        (insert "  </TR>\n"))
+       ((eq language 'xhtml)
+       (insert "  </tr>\n"))
        ((eq language 'cals)
        (insert "      </row>\n")
        (unless (/= (table-get-source-info 'current-row) table-cals-thead-rows)
@@ -3232,6 +3278,20 @@
              (insert (format " align=\"%s\"" (if alignment (symbol-name 
alignment) "left")))
              (insert (format " valign=\"%s\"" (if valign (symbol-name valign) 
"top")))
              (insert ">\n"))
+            ((eq language 'xhtml)
+             (insert (format "    <%s"
+                             (table-put-source-info
+                              'cell-type
+                              (if (or (<= (table-get-source-info 'current-row) 
table-xhtml-th-rows)
+                                      (<= (table-get-source-info 
'current-column) table-xhtml-th-columns))
+                                  "th" "td"))))
+             (if (and table-xhtml-cell-attribute (not (string= 
table-xhtml-cell-attribute "")))
+                 (insert " " table-xhtml-cell-attribute))
+             (if (> colspan 1) (insert (format " colspan=\"%d\"" colspan)))
+             (if (> rowspan 1) (insert (format " rowspan=\"%d\"" rowspan)))
+             (insert (format " align=\"%s\"" (if alignment (symbol-name 
alignment) "left")))
+             (insert (format " valign=\"%s\"" (if valign (symbol-name valign) 
"top")))
+             (insert ">\n"))
             ((eq language 'cals)
              (insert "        <entry")
              (if (> colspan 1)
@@ -3257,6 +3317,8 @@
            (cond
             ((eq language 'html)
              (insert (format"    </%s>\n" (table-get-source-info 'cell-type))))
+            ((eq language 'xhtml)
+             (insert (format"    </%s>\n" (table-get-source-info 'cell-type))))
             ((eq language 'cals)
              (insert "        </entry>\n"))
             ))
@@ -3289,6 +3351,23 @@
                     (progn
                       (goto-char (point-min))
                       (looking-at "\\s *\\'")))))
+       ((eq language 'xhtml)
+       (if table-xhtml-delegate-spacing-to-user-agent
+           (progn
+             (table--remove-eol-spaces (point-min) (point-max))
+             (if (re-search-forward "\\s +\\'" nil t)
+                 (replace-match "")))
+         (while (search-forward " " nil t)
+           (replace-match "&nbsp;"))
+         (goto-char (point-min))
+         (while (and (re-search-forward "$" nil t)
+                     (not (eobp)))
+           (insert "<br />")
+           (forward-char 1)))
+       (unless (and table-html-delegate-spacing-to-user-agent
+                    (progn
+                      (goto-char (point-min))
+                      (looking-at "\\s *\\'")))))
        ((eq language 'cals)
        (table--remove-eol-spaces (point-min) (point-max))
        (if (re-search-forward "\\s +\\'" nil t)
@@ -3301,6 +3380,7 @@
        (indent-rigidly beg (point)
                        (cond
                         ((eq language 'html) 6)
+                        ((eq language 'xhtml) 6)
                         ((eq language 'cals) 10)))
        (insert ?\n)))))
 




reply via email to

[Prev in Thread] Current Thread [Next in Thread]