Emacs: HTML, Transpose Table 🚀

By Xah Lee. Date: . Last updated: .

Here's a command to transpose HTML table.

(defun xah-html-table-to-matrix (HtmlTableText)
  "Convert a html table (string) into lisp object of nested vector.
URL `http://xahlee.info/emacs/emacs/elisp_transpose_html_table.html'
Version 2023-02-14"
  (interactive)
  (let* (xlispStr
         (xRand (random #xffff))
         (xQuote (format "⍝%x" xRand))
         (xL1 (format "◖%x" xRand))
         (xR1 (format "◗%x" xRand))
         (xL2 (format "⯊%x" xRand))
         (xR2 (format "⯋%x" xRand)))
    (setq
     xlispStr
     (with-temp-buffer
       (insert HtmlTableText)
       (goto-char (point-min))
       (while (search-forward "\"" nil t) (replace-match xQuote t t))
       (goto-char (point-min))
       (while (re-search-forward "<table *[^>]*>\\|<tr>" nil t) (replace-match xL1 t t))
       (goto-char (point-min))
       (while (re-search-forward "</table>\\|</tr>" nil t) (replace-match xR1 t t))
       (goto-char (point-min))
       (while (re-search-forward "<th>\\|<td>" nil t) (replace-match xL2 t t))
       (goto-char (point-min))
       (while (re-search-forward "</th>\\|</td>" nil t) (replace-match xR2 t t))
       (goto-char (point-min))
       (while (search-forward xQuote nil t) (replace-match "\\\"" t t))
       (goto-char (point-min))
       (while (search-forward xL2 nil t) (replace-match "\"" t t))
       (goto-char (point-min))
       (while (search-forward xR2 nil t) (replace-match "\" " t t))
       (goto-char (point-min))
       (while (search-forward xL1 nil t) (replace-match "[" t t))
       (goto-char (point-min))
       (while (search-forward xR1 nil t) (replace-match "]" t t))
       (buffer-substring (point-min) (point-max))))
    (eval (read xlispStr))))

(defun xah-html-matrix-to-table-text (Matrix)
  "Convert a lisp object of nested vector to html table as string.
URL `http://xahlee.info/emacs/emacs/elisp_transpose_html_table.html'
Version 2023-02-14"
  (interactive)
  (with-temp-buffer
    (insert "<table class=\"nrm\">\n")
    (mapc
     (lambda (x)
       (insert "<tr>")
       (mapc
        (lambda (y)
          (insert (format "<td>%s</td>" y)))
        x)
       (insert "</tr>\n"))
     Matrix)
    (insert "</table>")
    (buffer-substring-no-properties (point-min) (point-max))))

(defun xah-html-transpose-table ()
  "Transpose the html table at cursor.
URL `http://xahlee.info/emacs/emacs/elisp_transpose_html_table.html'
Version 2023-02-14"
  (interactive)
  (let (xp1 xp2 xHtmlText xlispStr xmatrix xtmatrix)
    (if (region-active-p)
        (setq xp1 (region-beginning) xp2 (region-end))
      (progn
        (search-forward ">" nil t)
        (search-backward "<table")
        (setq xp1 (point))
        (search-forward "</table>")
        (setq xp2 (point))))
    (setq xHtmlText (buffer-substring-no-properties xp1 xp2))
    (setq xmatrix (xah-html-table-to-matrix xHtmlText))
    (setq xtmatrix (xah-transpose xmatrix))
    (delete-region xp1 xp2)
    (insert (xah-html-matrix-to-table-text xtmatrix))))

requires Emacs: Transpose Matrix 🚀