header

2012年9月7日金曜日

How to post Blogger from Emacs; EmacsからBloggerに投稿する方法

How to post Blogger from Emacs

ブログを始めてみました。
英語の練習を兼ねて日本語で書くか英語で書くか迷い中ですが、
Emacsから、Bloggerにポストするコードを書いたので
紹介したいと思います。
I started this blog today, but I think that write the blog from Emacs.

Ready

pythonの実行環境とgooglecl,python-beautifulsoupが
必要です。下のリンクを参考にしました。


I consulted to below blog.
http://blog.ideabulbs.com/2011/10/howto-publishing-to-blogger-from-emacs.html


私はDebian環境なので以下のようにインストールしました。


Installation
$ sudo apt-get install emacs python googlecl python-beautifulsoup

Paste your .emacs etc..

適当に書いたElispのコードです。
コピペするときはgoogle-blogger-accountとgoogle-blogger-blog-name
に適当な文字を入れてください。
(私はuser-mail-addressにGmailのアドレスを設定しているので下の設定のままです。
setqではなくdefvarを使っているのは、バイトコンパイル時のwarningがいやだからです)


;; set Google account and blog-name for google blog
(defvar google-blogger-account user-mail-address)
(defvar google-blogger-blog-name "memo")
(defun my/create-html-and-fetch-filename ()
(let* ((org->html-file-name
(replace-regexp-in-string
"org$" "html" buffer-file-truename)))
(org-export-as-html 23 nil nil nil 'string) ; 23 = HTML
(my/replace-newline org->html-file-name)
org->html-file-name))
(defun my/replace-newline (file)
(let* ((base (buffer-name)))
(find-file file)
(replace-regexp "^[\n\t]" "" nil (point-min) (point-max))
(replace-regexp "</p>[\n\t]" "</p>" nil (point-min) (point-max))
(replace-regexp "[\n\t]<" "<" nil (point-min) (point-max))
(save-buffer)
(switch-to-buffer base)))
(defun my/search-title-word ()
(goto-char (point-min))
(if (re-search-forward "^.\+TITLE: \\(.+\\)" nil t)
(match-string 1)))
(defun my/blog-post ()
(interactive)
(let* ((title (or (my/search-title-word)
(read-string "title here: ")))
(content (if (string-match "\\.org$" (buffer-name))
(my/create-html-and-fetch-filename)
(buffer-string))))
(async-shell-command
(concat "google blogger post --draft -u " google-blogger-account
" --blog '" google-blogger-blog-name "'"
" --title '" title "' " content))))

Execute cummand

Emacsを再起動するか、C-x C-eなどでコードを評価してください。
その後、xxx.org形式のファイルで編集が終わったら、
そのファイルで M-x my/blog-post コマンドでポストできます。


M-x my/blog-post

be sure that posted it.
of course this post is from Emacs.
下のようにtitle tagを設定しているとそれが設定されるようにしました。
未入力の場合は、ミニバッファから聞かれます。


#+TITLE: How to post Blogger from Emacs

関係ない話ですがorg-modeでは下のようにoptionで色々設定できるようです。
私はtable of contents(toc)がいらないので消してます。


#+OPTIONS: toc:nil \n:nil num:nil
#+STYLE: <link rel="stylesheet" type="text/css" href="org.css">

それとBloggerはorg-modeで生成したHTMLに改行があると<br>タグに変換する
らしいので生成後のHTMLはEmacsから消しています。

最後に

ここまで紹介してきてアレですが
実はbloggerにEmacsからポストするgooglecl.elなるものがあるらしいです。
ただ改行を<br>にするのは直ってない?ようなので(今はわかりません)
http://dreamofminecometrue.blogspot.jp/2012/02/googlecl-emacs-de-blogger.html
少しは紹介する意味があったのではないでしょうか?
よかったら使ってください。
(作ったあとあることを知りました。)


Popular Posts

Blogger templates

Blogger news