Python製静的サイトジェネレータ Pelican を使ってブログ作成
Posted on 2019/03/12 in blog , Updated on: 2019/03/30
はじめに
本記事は、python製静的サイトジェネレータである pelican ライブラリを利用して、簡単なブログを作成する方法を紹介する。
pelican の各種設定や、テーマ、プラグインの導入については、別記事で紹介する予定。
pelicanとは
非常に簡単に静的サイトを作成する pyhon ライブラリ。
特長として下記があげられる。
- 記事(ブログ投稿など)と固定ページ(連絡先、サイトポリシーなど)を使える
- 様々なテーマが用意されている
- 各種プラグインが豊富
- コードハイライト機能
- 編集後の高速な再構築
- 外部ツールとの連携
- Atom/RSSフィード
pelican のインストール
venv を使って仮想環境を作成し、環境構築する。
参考リンク ⇒ venv - 仮想環境の使い方
ここで、plican の他に、markdown
とghp-import
もインストールする。
- markdown : ブログに投稿する記事をMarkdown形式で書けるようにするパッケージ
- ghp-import : GitHub Pagesへの公開操作を簡単にしてくれるパッケージ
(venv) $ pip install pelican markdown ghp-import
quickstart
プロジェクトディレクトリ(任意の名前)を作成して、入り、pelican-quickstart
コマンドを実行。
(venv) $ mkdir pelican_blog
(venv) $ cd pelican_blog
(venv) $ pelican-quickstart
すると、設定ファイルを作成するための質問がくる。(後で変更可能)
> Where do you want to create your new web site? [.] .
どこにプロジェクトを作成しますか? [ . ]で、カレントディレクトリ。
> What will be the title of this web site? TestBlog
Webサイトのタイトルは?
> Who will be the author of this web site? TestUser
Webサイトの著者は?
> What will be the default language of this web site? [English] ja
使う言語は?
> Do you want to specify a URL prefix? e.g., https://example.com (Y/n) n
URLプレフィックスを指定するか?
> Do you want to enable article pagination? (Y/n) Y
ページネーションを使うか?
> How many articles per page do you want? [10] 10
ページネーションするページ数は?
> What is your time zone? [Europe/Paris] Asia/Tokyo
タイムゾーンは?
> Do you want to generate a tasks.py/Makefile to automate generation and publishing? (Y/n) Y
自動生成・公開用の'task.py/Makefile'を作成するか?
> Do you want to upload your website using FTP? (y/N) N
アップロードにFTPを使うか?
> Do you want to upload your website using SSH? (y/N) N
アップロードにSSHを使うか?
> Do you want to upload your website using Dropbox? (y/N) N
アップロードにDropboxを使うか?
> Do you want to upload your website using S3? (y/N) N
アップロードにS3を使うか?
> Do you want to upload your website using Rackspace Cloud Files? (y/N) N
アップロードにRackspace Cloud Filesを使うか?
> Do you want to upload your website using GitHub Pages? (y/N) y
アップロードにGitHub Pagesを使うか?
> Is this your personal page (username.github.io)? (y/N) y
個人サイトか?
Done. Your new project is available at /Users/TestUser/project
設定終わり。
上記の設定が終わると、プロジェクトフォルダ内に下記のようなファイル、ディレクトリ群が作成される。
pelican_blog
|- content/
|- (pages/)
|- output/
|- Makefile
|- pelicanconf.py
|- publishconf.py
|- tasks.py
記事の作成
さっそくブログに投稿する記事を書いてみる。ここでは、Markdown形式で書くので、記事の拡張子は.md
としてcontentフォルダに保存する。
pelican の記事ルールとして、初めに Title や Date などの meta 情報を書く。(Category: 以下は省略してもOK)
Title: 最初の投稿
Date: 2019-02-24
Category: テスト
Tags: テスト, 投稿
Slug: FirstPost
Authors: UserName
Summary: 簡単にブログ作成
## 最初の投稿です。
Hello **world!**. My name is *SAIRA* from Japan.
### 表を挿入
|テスト1|テスト2|テスト3|
|---|---|---|
|テスト|テスト|テスト|
### シンタックスハイライト
```python
print('hello world!')
```
ブログを表示
記事を保存したら、下記コマンドを実行して、ブログをローカルサーバで確認してみる。
- make html : content 内にある記事を、pelican のテーマに沿って html化
- make serve : ローカルサーバでブログを表示。
(venv) $ make html
(venv) $ make serve
好みのブラウザで、http://localhost:8000
にアクセスすると、下のようにブログが表示される。 Markdownで記述した内容が反映されてるのがわかる。あとは、同じ手順でどんどん記事をcontent
に追加していき、make html
で HTMLを実行するだけで、投稿ページが追加されていく。
テーマの変更や、Webへの公開方法については、別記事で紹介する。