Emacs: Lines to HTML Table

By Xah Lee. Date: . Last updated: .

Here's a command that turns lines into HTML table.

For example,

a  b  c
1  2  3


<table class="nrm">

and is rendered in browser like this:


when you use CSS code similar to this:

table.nrm {border:solid 1px #000000;border-collapse:collapse;margin:.5ex}
table.nrm th, table.nrm td {border:solid thin #808080;padding:.5ex}
table.nrm th {background-color:#d7e4f2}


(defun xah-html-make-html-table (*separator)
  "Transform the current text block or selection into a HTML table.

If there's a text selection, use the selection as input.
Otherwise, used current text block delimited by empty lines.

*SEPARATOR is a string used as a delimitor for columns.

For example:


with “.” as separator, becomes

<table class=\"nrm\">

URL `http://ergoemacs.org/emacs/elisp_make-html-table.html'
Version 2017-03-03"
  (interactive "sEnter the string for column separation:")
  (let ($bds $p1 $p2)
    (setq $bds (xah-get-bounds-of-thing-or-region 'block))
    (setq $p1 (car $bds))
    (setq $p2 (cdr $bds))
        (narrow-to-region $p1 $p2)
        (let ((case-fold-search nil))

          (goto-char (point-max))
          (insert "\n")

          (goto-char (point-min))
          (while (search-forward *separator nil "NOERROR")
            (replace-match "</td><td>"))

          (goto-char (point-min))
          (while (search-forward "\n" nil "NOERROR")
            (replace-match "</td></tr>\n<tr><td>"))

          (goto-char (point-max))
          (delete-char 8)

          (goto-char (point-min))
          (insert "<table class=\"nrm\">\n<tr><td>")

          (goto-char (point-max))
          (insert "</table>")

You'll need the package xah-get-thing.el

〔►see Emacs: xah-get-thing.el

Text Transform Topic

  1. Emacs: Toggle Letter Case
  2. Emacs: Change to Title Case
  3. Emacs: Upcase Sentences
  4. Emacs: Cycle Replace Space Hyphen Underscore
  5. Emacs: Remove Accent Marks
  6. Emacs: Escape Quotes Command
  7. Emacs: Quote Lines
  8. Emacs: Change Brackets and Quotes
  9. Emacs: CSS Compressor
  10. Emacs: Replace Greek Letter Names to Unicode
  11. Emacs: Convert Straight/Curly Quotes
  12. Emacs: Convert English/Chinese Punctuations
  13. Emacs: Lines to HTML Table
Like it? Buy Xah Emacs Tutorial. Thanks.