Elisp: Wikipedia URL Linkify

By Xah Lee. Date:

Here's a command to change a Wikipedia URL into a HTML link.

For example:



<a class="wikipedia-69128" href="http://en.wikipedia.org/wiki/Saint_Jerome_in_His_Study_(D%C3%BCrer)" data-accessed="2015-09-14">Saint Jerome in His Study (Dürer)</a>

and this:



<a class="wikipedia-69128" href="http://zh.wikipedia.org/wiki/%E6%96%87%E6%9C%AC%E7%BC%96%E8%BE%91%E5%99%A8" data-accessed="2015-09-14">文本编辑器</a>

The advantage of this command are:

Here's the code.

(defun xah-html-wikipedia-url-linkify ()
  "Change Wikipedia URL under cursor into a HTML link.
If there is a text selection, use that as input.

<a class=\"wikipedia-69128\" href=\"http://en.wikipedia.org/wiki/Emacs\" data-accessed=\"2015-09-14\">Emacs</a>.

URL `http://ergoemacs.org/emacs/elisp_html_wikipedia_url_linkify.html'
Version 2015-09-14."
  (let ($boundaries
        $p1 $p2
    (if (use-region-p)
          (setq $p1 (region-beginning))
          (setq $p2 (region-end)))
        (let ($p0)
            (setq $p0 (point))
            (skip-chars-backward "^ \t\n<>[]")
            (setq $p1 (point))
            (goto-char $p0)
            (skip-chars-forward "^ \t\n<>[]")
            (setq $p2 (point))))

        ;; (setq ξboundaries (bounds-of-thing-at-point 'url))
        ;; (setq ξp1 (car ξboundaries))
        ;; (setq ξp2 (cdr ξboundaries))
    (setq $input-str (buffer-substring-no-properties $p1 $p2))
    (require 'url-util)
    (setq $link-text
           "_" " "
           (decode-coding-string (url-unhex-string (file-name-nondirectory $input-str)) 'utf-8)))
    (setq $output-str
           "<a class=\"wikipedia-69128\" href=\"%s\" data-accessed=\"%s\">%s</a>"
           (url-encode-url $input-str)
           (format-time-string "%Y-%m-%d")
      (delete-region $p1 $p2)
      (insert $output-str))))

In Emacs: Xah HTML Mode

See also: Elisp: Word to Wikipedia HTML Link

Patreon me $5 patreon

Or Buy Xah Emacs Tutorial

Or buy a nice keyboard: Best Keyboard for Emacs

If you have a question, put $5 at patreon and message me.