Emacs: HTML, Lines to List 🚀

By Xah Lee. Date: . Last updated: .

Here's a command that turn lines or paragraphs into HTML list.

(defun xah-html-lines-to-list ()
  "Make each line into a HTML list item, of current text block or selection.

If the region contain empty line, each text block is made into a list item. Else, each line is a list item.

If `universal-argument' is called first, use ordered list ol instead of ul.

URL `http://xahlee.info/emacs/emacs/elisp_lines_to_list.html'
Version: ~2013 2023-05-14 2023-06-28 2023-07-20"
  (interactive)
  (let (xp1 xp2 xinput xsep xitems xsList xlistStr xhasEmptyLine)
    (let ((xbds (xah-get-bounds-of-thing-or-region 'block)))
      (setq xp1 (car xbds) xp2 (cdr xbds)))
    (setq xinput (buffer-substring-no-properties xp1 xp2))
    (setq xhasEmptyLine (if (string-match "\n\n" xinput) t nil))
    (setq xsep (if xhasEmptyLine "\n\n+" "\n"))
    (setq xitems (split-string xinput xsep t " +"))
    (setq xsList (mapcar (lambda (x) (format "<li>%s</li>" (string-trim x))) xitems))
    (setq xlistStr (mapconcat 'identity xsList (if xhasEmptyLine "\n\n" "\n")))
    (save-restriction
      (narrow-to-region xp1 xp2)
      (delete-region (point-min) (point-max))
      (insert
       (let ((xsep2 (if xhasEmptyLine "\n\n" "\n")))
         (if current-prefix-arg
             (concat "\n<ol>" xsep2 xlistStr xsep2  "</ol>" xsep2)
           (concat "\n<ul>" xsep2 xlistStr xsep2 "</ul>" xsep2))))
      (progn
        (goto-char (point-min))
        (while (re-search-forward "<li>[-.•*] " nil :move) (replace-match "<li>" t t))))
    (skip-chars-forward " \n")))

requires package Emacs: xah-get-thing.el