Emacs: HTML. Lines to Table 📜
Here's a command that turns lines into HTML table.
For example,
a b c 1 2 3
becomes a HTML table and is rendered in browser like this:
| a | b | c |
| 1 | 2 | 3 |
with CSS code.
Solution
(defun xah-html-lines-to-table () "Transform the current block or selection into a 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> URL `http://xahlee.info/emacs/emacs/elisp_make-html-table.html' Created: 2019-06-07 Version: 2025-03-27" (interactive) (let (xsep xbeg xend xinput xlines xrows xresultStr) (setq xsep (read-string "Table column separation string:" "|" nil "|")) (seq-setq (xbeg xend) (if (region-active-p) (list (region-beginning) (region-end)) (list (save-excursion (if (re-search-backward "\n[ \t]*\n" nil 1) (match-end 0) (point))) (save-excursion (if (re-search-forward "\n[ \t]*\n" nil 1) (match-beginning 0) (point)))))) (setq xinput (buffer-substring-no-properties xbeg xend)) (setq xlines (split-string xinput "\n" t " +")) (setq xrows (mapcar (lambda (x) (format "<tr><td>%s</td></tr>" (mapconcat 'identity (split-string x xsep) "</td><td>"))) xlines)) (setq xresultStr (concat "<table class=\"nrm\">\n" (mapconcat 'identity xrows "\n") "\n</table>")) (delete-region xbeg xend) (goto-char xbeg) (insert xresultStr)))