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

becomes

<table class="nrm">
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>

and is rendered in browser like this:

abc
123

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}

Solution

(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:

a.b.c
1.2.3

with “.” as separator, becomes

<table class=\"nrm\">
<tr><td>a</td><td>b</td><td>c</td></tr>
<tr><td>1</td><td>2</td><td>3</td></tr>
</table>

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))
    (save-excursion
      (save-restriction
        (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))
          (beginning-of-line)
          (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]

HTML Topic

  1. Emacs HTML Tips
  2. Insert HTML Tag
  3. URL to Link Command
  4. Lines to HTML Table
  5. Replace HTML Entities
  6. CSS Compressor
  7. Color CSS Color Values
  8. View Image Thumbnails
  9. HTML Image Path to Img Tag
  10. Convert Image Files in Dired
  11. Xah HTML Mode

Emacs Text Transform Under Cursor

  1. Toggle Letter Case
  2. Title Case
  3. Upcase Sentences
  4. Cycle Space Hyphen Underscore
  5. Escape Quotes
  6. Quote Lines
  7. Spaces to New Lines
  8. Change Brackets/Quotes
  9. Remove Accent Marks
  10. Convert Straight/Curly Quotes
  11. Convert English/Chinese Punctuations
  12. Color Conversion (RGB, HSL, HSV)
  13. Decimal to Hexadecimal
  14. Replace Greek Letter Names to Unicode
  15. Twitterfy Text
  16. Toggle line wrap
  17. Clean White Space

Elisp HTML Commands

  1. wrap-url
  2. Make URL links
  3. Make Image Links
  4. Extract URL
  5. Word to Wikipedia Linkify
  6. Wikipedia URL Linkify
  7. URL Percent Decode/Encode
  8. Lines to HTML Table
  9. Markup Function Names
  10. Color Source Code
  11. Transform Text Under Cursor
  12. Chinese Char Reference Linkify
  13. HTML make-citation
  14. update-title
  15. Google Map Linkify 🌐
  16. CSS Compressor
  17. Make Ruby Annotation
  18. Move Image File

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs

Emacs

Emacs Lisp

Misc