Emacs: HTML. URL to Link with Date ๐
Here's a command that change a URL into a HTML link, with access date.
example:
http://some.example.com/xyz.html
becomes:
<a href="http://example.com/x.html" data-accessed="2015-09-12">http://example.com/x.html</a>
Note that the date is automatically inserted. Access date in the URL link is useful, because when the content changed or becomes dead, a access date helps you find the original content. The data- are Custom Data Attribute introduced in HTML5.
[see HTML: Custom Data Attribute]
Here's the code.
(defvar xah-html-link-noreferrer t "if t, add rel=\"noreferrer\". Used by `xah-html-url-to-dated-link' and possibly some others. Version: 2022-03-22") (defvar xah-html-link-target-blank t "if t, add target=\"_blank\". Used by `xah-html-url-to-dated-link' and possibly some others. Version: 2022-03-25")
(defun xah-html-url-to-dated-link (&optional Begin End) "Change URL under cursor into a link with data-accessed attribute. If there is a selection, use the selection as input. Example: http://example.com/x.htm becomes <a href=\"http://example.com/x.htm\" data-accessed=\"2008-12-25\">http://example.com/x.htm</a> Use the option `xah-html-link-noreferrer' and `xah-html-link-target-blank' URL `http://xahlee.info/emacs/emacs/elisp_html-linkify.html' Created: 2008-12-25 Version: 2025-03-25" (interactive) (let (xbeg xend xinput) (seq-setq (xbeg xend) (if (and Begin End) (list Begin End) (if (region-active-p) (list (region-beginning) (region-end)) (let ((xurlstops "^ ย \t\n\"`'|()[]{}<>โโโโใใใใใใใใใใใใยซยปโนโบโฎโฏ โฐโฑ")) (list (save-excursion (skip-chars-backward xurlstops) (point)) (save-excursion (skip-chars-forward xurlstops) (point))))))) (setq xinput (buffer-substring-no-properties xbeg xend)) (delete-region xbeg xend) (insert (format "<a href=\"%s\" data-accessed=\"%s\"%s%s>%s</a>" xinput (format-time-string "%Y-%m-%d") (if xah-html-link-target-blank " target=\"_blank\"" "") (if xah-html-link-noreferrer " rel=\"noreferrer\"" "") xinput ))))