Elisp: URL to HTML Link + Date

By Xah Lee. Date: . Last updated: .

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 HTML5 Custom Data Attribute]

Here's the code.

(defun xah-html-source-url-linkify ()
  "Change URL under cursor into a HTML link.
If there's a text selection, use the text 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>
URL `http://ergoemacs.org/emacs/elisp_html-linkify.html'
Version 2008-12-25 2021-05-20"
  (interactive)
  (let (($p0 (point)) $p1 $p2 $input $url $domainName $linkText )
    (if (use-region-p)
        (setq $p1 (region-beginning) $p2 (region-end))
      (save-excursion
        ;; chars that are likely to be delimiters of full path, e.g. space, tabs, brakets.
        (skip-chars-backward "^  \"\t\n'|[]{}<>〔〕“”〈〉《》【】〖〗〘〙«»‹›·。\\`")
        (setq $p1 (point))
        (goto-char $p0)
        (skip-chars-forward "^  \"\t\n'|[]{}<>〔〕“”〈〉《》【】〖〗〘〙«»‹›·。\\'")
        (setq $p2 (point))))
    (setq $input (buffer-substring-no-properties $p1 $p2))
    ;; (setq $url (replace-regexp-in-string "&amp;" "&" $input nil "LITERAL"))
    (delete-region $p1 $p2)
    (insert (format
             "<a href=\"%s\" data-accessed=\"%s\">%s</a>"
             $input (format-time-string "%Y-%m-%d") $input
             ))))

HTML

Tags

Links

CSS

Misc

If you have a question, put $5 at patreon and message me on xah discord.
Or support me by Buy Xah Emacs Tutorial