Pelican + Github Pages でブログを作った話 1 05, 2016

Pelicanでこのサイトを作った時のメモ.

Pelicanとは?

getpelican/pelican at 3.4.0は,Pythonで作られた静的webサイトジェネレータです.

フランス語の"notebook"を意味する"calepin"から名付けられたらしい.
reStructuredText か Markdownでページ内容を記述できるのが魅力的です.

テーマはWordpressほどじゃないですが,それなりにあります.
Pelican themesから好きなものを選ぶ事ができます.

Pythonユーザとして,

  • やる気出せばいじれないこともない
  • テーマがそこそこカッコいい
  • Markdownで記事が書ける
  • ドキュメントが充実している という理由で採用しました.

Pelicanの導入

pipを使えばあっという間.Markdownも入れておきます.

$ pip install pelican Markdown

インタラクティブなコマンドツールで初期設定を済ましてくれます.

$ mkdir ./blog
$ cd blog
$ pelican-quickstart

以下のような形で質問に回答しました.

Welcome to pelican-quickstart v3.6.3.

This script will help you create a new Pelican-based website.

Please answer the following questions so this script can generate the files
needed by Pelican.


> Where do you want to create your new web site? [.] .
> > What will be the title of this web site? Futon note
> > Who will be the author of this web site? DaikiShimada
> > What will be the default language of this web site? [en] ja
> > Do you want to specify a URL prefix? e.g., http://example.com   (Y/n) y
> > What is your URL prefix? (see above example; no trailing slash) daikishimada.github.io
> > Do you want to enable article pagination? (Y/n) y
> > How many articles per page do you want? [10] 5
> > What is your time zone? [Europe/Paris] Asia/Tokyo
> > Do you want to generate a Fabfile/Makefile to automate generation and publishing? (Y/n) y
> > Do you want an auto-reload & simpleHTTP script to assist with theme and site development? (Y/n) n
> > Do you want to upload your website using FTP? (y/N) n
> > Do you want to upload your website using SSH? (y/N) n
> > Do you want to upload your website using Dropbox? (y/N) n
> > Do you want to upload your website using S3? (y/N) n
> > Do you want to upload your website using Rackspace Cloud Files? (y/N) n
> > Do you want to upload your website using GitHub Pages? (y/N) y
> > Is this your personal page (username.github.io)? (y/N) y
> Done. Your new project is available at /Users/sheema/Documents/web/pelican-test

ここで,

> > Do you want to generate a Fabfile/Makefile to automate generation and publishing? (Y/n) y

と答えておくと,ディレクトリにHTML生成用のMakefileとfabfile.pyを生成してくれます.
makeコマンドやfabコマンドでHTMLを生成出来るようになるので,pelicanのオプションを毎回叩く必要がなくなる(はず).

あとは,content/以下に記事を書いていけばok.

ちなみに,ブログじゃないページはcontent/pages/にファイルを置いていけば良いみたいです.

記事の生成と確認

Markdownからhtmlに書き出して欲しい時は,

$ make html

でhtmlを吐き出してくれます.初期設定ではoutput/以下に内容が出力されます.

htmlの確認をする時は,

$ make serve

とすると,http://localhost:8000でwebサーバを走らせてくれるので,表示などをローカル環境で確認できます.

公開用にhtmlを吐き出したい時は,

$ make publish

でやってくれます.

テーマの導入

ロシア製のテーマSamael500/w3-personal-blogをお借りします.

$ git clone https://github.com/Samael500/w3-personal-blog.git

ところどころ文言がロシア語なので,w3-personal-blog/template/内のファイルの一部を適宜書き換える.
ここが地味に大変だった…

pelican にテーマを反映

pelicanconf.pyにはサイトの設定を書き込む様になっています.
以下の記述をして,pelicanに使うテーマのディレクトリを知らせる.

THEME = './w3-personal-blog-master'

Github Pages のリポジトリの準備

Github Pagesにはユーザが1つだけ持てるサイトとリポジトリ毎に作れるサイト(Project page)の2種類があります.
今回はユーザ用のサイトを作成しました.

公開用リポジトリの作成

ユーザ用サイトはユーザ名.github.ioというリポジトリを作成すれば良いので,daikihsimada.github.ioというリポジトリを作成.

このリポジトリのmasterブランチにhtmlファイルをアップロードすると,自動的にユーザ名.github,ioというURLのwebサイトとしてアクセスができるようになります.

ソース管理用リポジトリの作成

webサイトの公開自体はユーザ名.github.ioで行いますが,サイト生成に必要なMarkdownファイルやテーマ,pelicanの諸々のファイルは公開する必要が無いので,別のprivateなリポジトリで管理することにしました.

pelicanをセットアップしたblog/を管理用リポジトリで管理することにしました.

git init
git remote add origin 管理用リポジトリ

それと,.gitignoreは以下のように.

*.pyc
output/

サイトの公開

davisp/ghp-importというPythonモジュールを利用すれば,便利にGithub Pagesへのページ公開が行えます.

pip install ghp-import

適当に記事を書いて,公開してみる.

$ make publish

# outputディレクトリをgh-pagesブランチに
$ ghp-import output

# gh-pagesブランチを公開リポジトリのmasterへpush
$ git push https://github.com/DaikiShimada/daikishimada.github.io.git gh-pages:master

テーマのカスタマイズにこだわると,時間はかかってしまいますが, 比較的簡単にサイトを公開できます.

テーマのカスタマイズも,Pythonユーザなら比較的わかりやすい記述になっているので,少しコードを読めば大丈夫だと思います.
vim で記事を書いてGitで記事のソースまで管理できてしまうので,精神衛生上すごく快適です.

参考文献