Xah Talk Show 2025-06-26 Ep669 Emacs Lisp, HTML ul to dl, and More

xah talk show ep669 1d52c
xah talk show ep669 1d52c
(defun xah-html-ul-to-dl (&optional SepChar)
  "Change html unordered list to definition list.

Cursor must be on the left of ▮<ul>

SepChar is a string that separates term and definition, in a <li> item.
KeepSep if true, keep the SepChar in result.

Warning, if you have nested list, remember that each li must have a SepChar in its inner text.

Created: 2020-09-05
Version: 2025-06-29"
  (interactive (list (read-string "Seperator:" " -- ")))
  (when (not (string-equal
              "<ul"
              (buffer-substring-no-properties
               (point)
               (+ (point) (length "<ul")))))
    (search-backward "<ul" nil t)
    (user-error "%s: Cursor must be on the left of ▮<ul.
Cursor is moved to there now.
" this-command))
  (when
      (not (string-equal "<ul" (buffer-substring-no-properties (point) (+ (point) (length "<ul"))))) (user-error "%s: Cursor must be on the left of ▮<ul" this-command))
  (let (xbeg xend)
    (seq-setq (xbeg xend) (list (point) (progn (xah-html-skip-tag-forward) (point))))
    (save-restriction
      (narrow-to-region xbeg xend)
      (progn
        (goto-char (point-min))
        (while (re-search-forward "<\\(/?\\)ul\\(?: [^>]+\\)?>" nil t)
          (replace-match "<\\1dl\\2>" t)))
      (progn
        (goto-char (point-min))
        (while (re-search-forward "<li\\(?: [^>]+\\)?>" nil t)
          (replace-match "<dt\\2>" t)))
      (progn
        (goto-char (point-min))
        (while (re-search-forward "</li>" nil t)
          (replace-match "</dd>\n" t)))
      (progn
        (goto-char (point-min))
        (while (re-search-forward (concat " *" SepChar " *") nil t)
          (replace-match "</dt>\n<dd>" t))))))