Elisp: HTML Link to Dead Link

By Xah Lee. Date: . Last updated: .

Here's a command that turn a link in html into a dead link with strik-thru.

<a href="http://example.com/">http://example.com/</a>

becomes:

<s data-accessed="" data-defunct-date="2019-04-02">http://example.com/</s>

with today's date added to the “data-defunct-date” part.

(defun xah-html-make-link-defunct ()  "Make current HTML link to a defunct form.

current tag means cursor is in any of the positions
<▮a href=\"‹url›\"…▮>…▮<b>x▮</b>▮</a▮>▮
but within 200 chars before the cursor position.

the link is changed like this,
For Example:
<a href=\"‹url›\"…>…</a>
or
<iframe src=\"‹url›\"…>…</iframe>

It becomes:
<s>‹url›</s>
or
<s data-accessed=\"‹access_date›\" data-defunct-date=\"‹now_date›\">‹url›</s>

<a href=\"‹url›\" data-accessed=\"‹access_date›\">…</a>
where ‹access_date› is of this format 2019-04-02

URL `http://xahlee.info/emacs/emacs/elisp_html_dead_link.html'
Created: 2019-04-02
Version: 2024-01-15"
  (interactive)
  (let (xp1 xp2 xtagname xwholeLinkStr xresultStr xurl xaccessedDate)
    (progn
      (forward-word) (forward-char)
      (re-search-backward "<\\([A-Za-z0-9]+\\) " (- (point) 200))
      (setq xp1 (match-beginning 0))
      (setq xtagname (match-string 1))
      (cond
       ((string-equal xtagname "a")
        (search-forward "</a>" (line-end-position))
        (setq xp2 (point))
        (setq xwholeLinkStr (buffer-substring-no-properties xp1 xp2))
        (with-temp-buffer
          ;; generate replacement text
          (insert xwholeLinkStr)
          (goto-char (point-min))
          (re-search-forward "href=\"\\([^\"]+?\\)\"")
          (setq xurl (match-string 1))
          (setq xaccessedDate
                (if (re-search-forward
                     "data-accessed=\"\\([0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]\\)\"" xp2 t)
                    (match-string 1)
                  ""
                  ))
          (setq xresultStr
                (format "<s data-accessed=\"%s\" data-defunct-date=\"%s\">%s</s>" xaccessedDate (format-time-string "%Y-%m-%d") xurl)))
        (delete-region xp1 xp2)
        (goto-char xp1)
        (insert xresultStr))
       ((string-equal xtagname "iframe")
        (goto-char xp1)
        (xah-html-make-iframe-defunct))
       (t (error "the tag under cursor is 「%s」, is not 「a」 or 「iframe」" xtagname))))))

(defun xah-html-make-iframe-defunct ()
  "Make the HTML iframe under cursor to a defunct form.

for example
<iframe src=\"https://www.youtube.com/abc\" allowfullscreen></iframe>
becomes
<s https://www.youtube.com/embed/abc</s>

Created: 2024-01-08
Version: 2024-01-15"
  (interactive)
  (let (xp1 xp2 xwholeLinkStr xresultStr xurl)
    (progn
      (forward-word)
      (forward-char)
      (progn
        ;; get the boundary of opening tag
        (search-backward "<iframe " (- (point) 200))
        (setq xp1 (point))
        (search-forward "</iframe>")
        (setq xp2 (point))
        (setq xwholeLinkStr (buffer-substring-no-properties xp1 xp2))
        (with-temp-buffer
          ;; generate replacement text
          (insert xwholeLinkStr)
          (goto-char (point-min))
          (re-search-forward "src=\"\\([^\"]+?\\)\"")
          (setq xurl (match-string 1))
          (setq xresultStr (format "<s>%s</s>" xurl)))))
    (delete-region xp1 xp2)
    (goto-char xp1)
    (insert xresultStr)))