URL → id記法

なんか,URL(http://d.hatena.ne.jp/clairvy/20080506) とかの文字列を d:id:clairvy:20080506 とかに変換してくれるelisp が欲しいと思った.

d:id:ha-tan:20070316:1174089454 ら辺にありそう.
もちょっと見てみる.

d.hatena ら辺を見てないのかな.参考になります.

とりあえず.こう変えてみた.

;;; transrate URL to id notation
(defun my-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))
	       (idstr (my-hatena-convert-url-to-id-1
		       (buffer-substring beg end))))
	  (delete-region beg end)
	  (insert idstr)))))

(defun my-hatena-convert-url-to-id-1 (url)
  "与えられた URL をはてなID記法に変換する"
  (let* ((urls (split-string url "/"))
	 (authority (nth 2 urls))
	 (user (nth 3 urls))
	 (date (nth 4 urls))
	 (item (nth 5 urls))
	 (idstr (concat "id:" user)))
    (if (string-match "\\<\\(.\\)\\.hatena\\.ne\\.jp\\>" authority)
	(setq idstr (concat (match-string 1 authority) ":" idstr)))
    (if (and date (not (string-equal date "")))
	(setq idstr (concat idstr ":" date)))
    (if (and item (not (string-equal item "")))
	(setq idstr (concat idstr ":" item)))
    idstr))

あとは,hatena のURL じゃないときはreject するとか?