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))))

See also: Elisp: Word to Wikipedia HTML Link

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