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'
Created: ~2013
Version: 2024-03-19"
  (let (xp1 xp2 xinput xsep xitems xsList xlistStr xhasEmptyLine)
    (seq-setq (xp1 xp2) (xah-get-pos-of-block-or))
    (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")))
      (narrow-to-region xp1 xp2)
      (delete-region (point-min) (point-max))
       (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))))
        (goto-char (point-min))
            (re-search-forward "<li>[-.•*] " nil :move)
          (replace-match "<li>" t t)))
        (goto-char (point-min))
        (while (re-search-forward "<br /></li>" nil :move)
          (replace-match "</li>" t t))))
    (skip-chars-forward " \n")))

requires package Emacs: xah-get-thing.el 📦