This article shows you how write a elisp function that changes the character under cursor into several HTML links.
Write a command that changes the Chinese character under cursor into several links. For example, if current char is 魂, then it should become:
<div class="cδ"><span class="w">魂</span> <span class="en"><a href="http://translate.google.com/#zh-CN|en|魂">Translate</a> ◇ <a href="http://en.wiktionary.org/wiki/魂">Wiktionary</a> ◇ <a href="http://www.chineseetymology.org/CharacterEtymology.aspx?submitButton1=Etymology&characterInput=魂">history</a></span></div>
Which appears in browser like this:
This is for my blog on languages and linguistics: Belles-lettres Blog.
I've already wrote ≈20 elisp functions like this the past years. 〔☛ Emacs Lisp Power! Transform Text Under Cursor〕 So, today's problem is simply a matter of 5 minute job of copy-pasting.
Here's the code for today's case:
(defun chinese-linkify () "Make the current Chinese character into several Chinese dictionary links. If there's a text selection, use that for input." (interactive) (let ( ξchar p1 p2 templateStr resultStr) (if (region-active-p) (progn (setq p1 (region-beginning) ) (setq p2 (region-end) ) ) (progn (setq p1 (point) ) (setq p2 (1+ (point)) ) ) ) (setq ξchar (buffer-substring-no-properties p1 p2)) (setq templateStr "<div class=\"cδ\"><span class=\"w\">�</span> <span class=\"en\"><a href=\"http://translate.google.com/#zh-CN|en|�\">Translate</a> ◇ <a href=\"http://en.wiktionary.org/wiki/�\">Wiktionary</a> ◇ <a href=\"http://www.chineseetymology.org/CharacterEtymology.aspx?submitButton1=Etymology&characterInput=�\">history</a></span></div>" ) (setq resultStr (replace-regexp-in-string "�" ξchar templateStr)) (delete-region p1 p2) (insert resultStr) ))
The code is pretty simple. If you are not familiar, see: Emacs Lisp Idioms (for writing interactive commands).
This is truely a time saver. Before, i had to go a chinese reference website, type in the Chinese char to search, copy URL, back to emacs then paste, then call a emacs function to change the URL to a link. Do this for 3 other sites. Now, i just press one button in emacs.
The added advantage is that they'll have consistent form. So if later on i want to add one more dictionary to my dictionary list, or use a different HTML tag/format, i can use a truely regular regular expression in find/replace to do it site-wide.
Note: technically, by spec, you should not have Chinese chars in a URL. They should be URL Encoded 〔☛ URL Percent Encoding and Unicode〕 by bytes from the char's UTF-8 encoding. ⁖ For example, the char 魂's UTF-8 encoding is 3 bytes of the following hexadecimal: E9 AD 82. So, this URL:
http://en.wiktionary.org/wiki/魂
should be:
http://en.wiktionary.org/wiki/%E9%AD%82
However, i think the situation of percent encoding is a abomination. 〔☛ Problems of Symbol Congestion in Computer Languages; ASCII Jam vs Unicode〕 I decided to not botch my Chinese chars in URL. Today's browsers will automatically do the encoding for you when user clicks on it.
The weird ξ you see in my elisp code is Greek x. I use Unicode char in symbol name for easy distinction from builtin symbols. You can just ignore it. 〔☛ Programing Style: Variable Naming: English Words Considered Harmful〕