Xah Talk Show 2024-07-22 Ep564, Emacs Lisp Coding, highlight duplicate links

vidthumb x0lketAZ4P8
(defun xah-html-show-duplicate-links ()
  "highlight and print any duplicate links in current html file.
Created: 2024-07-22
Version: 2024-07-22"
  (interactive)
  (let (xp1 xp2 xlink
            (xlinksTable (make-hash-table :test 'equal)))
    (seq-setq (xp1 xp2)
              (if (region-active-p)
                  (vector (region-beginning) (region-end))
                (vector (point-min) (point-max))))
    (save-restriction
      (narrow-to-region xp1 xp2)
      ;; algorithm. Find each link, then for each, extract the link, if it ;; is local, then expand to file absolute path. check if the path is ;; in hashtable. If so, highlight the link Else, add to hashtable. ;; Repeat to next link.
      (goto-char (point-min))
      (while (search-forward "href=\"" nil t)
        (let (xboundary xbeg xend xhrefval)
          (setq xboundary (bounds-of-thing-at-point 'filename))
          (setq xbeg (car xboundary) xend (cdr xboundary))
          (setq xhrefval (buffer-substring-no-properties xbeg xend))
          (setq xlink
                (if (string-match "^http" xhrefval)
                    xhrefval
                  (expand-file-name xhrefval default-directory)))
          (if (gethash xlink xlinksTable)
              (progn
                (overlay-put (make-overlay xbeg xend) 'face 'font-lock-warning-face)
                (message "DUPLICATE: %s" xlink))
            (progn
              (puthash xlink 1 xlinksTable))))))))