Elisp: URL to HTML Link

By Xah Lee. Date: . Last updated: .

Here's a command that change a URL into a HTML link.




<a class="sorc" href="http://some.example.com/xyz.html" data-accessed="2015-09-12">http://some.example.com/xyz.html</a>

Note that the date is automatically inserted.

Note: a access date in the URL link is useful, because when the link becomes dead, at least you know when it was up.

The data- are Custom Data Attribute introduced in HTML5. [see HTML5 Custom Data Attribute]

Here's the code.

(defun xah-html-source-url-linkify (@prefixArg)
  "Make URL at cursor point into a HTML link.
If there's a text selection, use the text selection as input.

Example: http://example.com/xyz.htm
<a class=\"sorc\" href=\"http://example.com/xyz.htm\" data-accessed=\"2008-12-25\">example.com…</a>

The anchor text may be of 4 possibilities, depending on value of `universal-argument'.

1 → 「‹full url›」
2 or 4 → 「‹domain›…」
3 → 「img src」
0 or any → smartly decide.

URL `http://ergoemacs.org/emacs/elisp_html-linkify.html'
Version 2018-02-17"
  (interactive "P")
  (let (
        $url $domainName $linkText )
    (if (use-region-p)
          (setq $p1 (region-beginning))
          (setq $p2 (region-end)))
      (let (($bds (bounds-of-thing-at-point 'url)))
        (setq $p1 (car $bds))
        (setq $p2 (cdr $bds))))
    (setq $input (buffer-substring-no-properties $p1 $p2))
    (setq $url (replace-regexp-in-string "&" "&" $input nil "LITERAL"))
    ;; in case it's already encoded. TODO this is only 99% correct.
    (setq $domainName
            (string-match "://\\([^\/]+?\\)/?" $url)
            (match-string 1 $url)))
    (setq $linkText
           ((equal @prefixArg 1) $url)
           ((or (equal @prefixArg 2) (equal @prefixArg 4) (equal @prefixArg '(4))) (concat $domainName "…"))
           ((equal @prefixArg 3) "image source")
           (t (if
                  (let ((case-fold-search t))
                    (string-match "\\(wikimedia\\.org\\|wikipedia\\.org\\).+\\(jpg\\|png\\|svg\\)$" $url))
                  "image source"
    (setq $url (replace-regexp-in-string "&" "&" $url))
    ;; delete URL and insert the link
    (delete-region $p1 $p2)
    (insert (format
             "<a class=\"sorc\" href=\"%s\" data-accessed=\"%s\">%s</a>"
             $url (format-time-string "%Y-%m-%d") $linkText

For a simpler version, see Emacs: URL to Link Command

HTML Link to Dead Link

Elisp: HTML Link to Dead Link

Change URL into Citation Format

If you need a citation format, such with {author, title, date} info, see:

Elisp: Writing a make-citation Command

Elisp HTML Commands

  1. wrap-url
  2. URL to Link
  3. Link to Dead Link
  4. Make Image Links
  5. Extract URL
  6. Word to Wikipedia Linkify
  7. Wikipedia URL Linkify
  8. URL Percent Decode/Encode
  9. Lines to HTML Table
  10. Markup Function Names
  11. Color Source Code
  12. Transform Text Under Cursor
  13. Chinese Char Reference Linkify
  14. HTML make-citation
  15. update-title
  16. Google Map Linkify 🌐
  17. CSS Compressor
  18. Make Ruby Annotation
  19. Move Image File

If you have a question, put $5 at patreon and message me.
Or Buy Xah Emacs Tutorial
Or buy a nice keyboard: Best Keyboards for Emacs


Emacs Lisp