Shammer's Philosophy

My private adversaria

HTML変換コマンドその2

昔作成したHTML変換コマンドのバージョンアップ版。特殊文字をHTML用に変換するだけだったが、URLをリンクにする内容を追記した。

(defun text2html (start end)
  (interactive "r")
  (save-excursion
    (save-restriction
      (narrow-to-region start end)
      (goto-char (point-min))
      (while (re-search-forward " " nil t)
        (replace-match " "))
      (goto-char (point-min))
      (while (re-search-forward ">" nil t)
        (replace-match ">"))
      (goto-char (point-min))
      (while (re-search-forward "<" nil t)
        (replace-match "&lt;"))
      (goto-char (point-min))
      (while (re-search-forward "\n" nil t)
        (replace-match "\n<br/>"))
      (goto-char (point-min))
      (while (re-search-forward "http://" nil t)
	(let* ((start (progn
			(match-beginning 0)
			(point)))
	       (end (progn
		      (end-of-line)
		      (point)))
	       (url (buffer-substring start end)))
	(replace-match "<a href='http://")
	(end-of-line)
	(insert (concat "'>" url "</a>")))))))

Mac OS X Lion 上の Emacs 24.3 で動作確認済。近いうちに逆版も作成しよう。