カーソル付近のURLをはてなID記法に変換する
日記を書くときに、僕はEmacsで下書きをしてからWebブラウザにCopy & Pasteして登録しているのですが、他人の日記の参照する際にいつも
http://d.hatena.ne.jp/ha-tan/20050820/1124488573
の形式のURLを
id:ha-tan:20050820:1124488573
のようなはてなのID記法に編集していました。これは面倒です。
で、これに対処するためにEmacs Lispを書いてみました。
(defun hatena-convert-url-to-id () "カーソル付近のURLをはてなID記法に変換する。" (interactive) (let ((bounds (bounds-of-thing-at-point 'url))) (if bounds (let* ((beg (car bounds)) (end (cdr bounds)) (urls (split-string (buffer-substring beg end) "/")) (user (nth 3 urls)) (date (nth 4 urls)) (item (nth 5 urls)) (idstr (concat "id:" user))) (if (and date (not (string-equal date ""))) (setq idstr (concat idstr ":" date))) (if (and item (not (string-equal item ""))) (setq idstr (concat idstr ":" item))) (delete-region beg end) (insert idstr)))))
んー、センスないLispですねー。添削希望。
BUGS: 以下のパターンの挙動がいまいちです。やっぱりまじめに正規表現でURL解析しないとダメ?
http://d.hatena.ne.jp/ha-tan/あ => id:ha-tan:あ (「id:ha-tanあ」になって欲しい) http://d.hatena.ne.jp/ha-tan/20050820/あ => id:ha-tan:20050820:あ (「id:ha-tan:20050820あ」になって欲しい) http://d.hatena.ne.jp/ha-tan/20050820/1124488573/あ => id:ha-tan:20050820:1124488573 (「id:ha-tan:20050820:1124488573あ」になって欲しい)