Cycle N' Craft

PelicanとAmazon S3で脱Wordpressしてみる

Posted on Fri 30 January 2015 ( 2018-08-03 update ) in computing

ホームページの作成といえばWordpressというくらいに、いろんな制作会社で採用されておりますが、僕はあいにくWordpressを何度インストールしても飽きてしまいます。至れり尽くせりなCMSだとは思いますが、筆記用具としては使いにくくて結局使わなくなるんですよね。MovableTypeやNucleus、Mamboなんて試してきましたが、一番長いこと使っていたのはBloggerかも。

サーバやCMSが異常動作して、結構な文字量を書いたのに消えちゃったってことありますよね。それならということで、ローカル環境でテキストエディタ作業という方も少なくないと思います。ローカルで編集するならオンライン上の便利なCMSより、テキストをHTMLに変換してくれるだけに特化したシンプルなものが欲しくなってきます。そんな中で巡り合ってしまったのがPelicanです。

Pythonで記述された静的HTMLジェネレータがPelican

PelicanはPythonで書かれていますので導入は割と簡単です。今時のLinuxやMac OS XならPythonはOS標準で入ってますね。わざわざphpやMySQLを用意する必要はありません。

DebianやUbuntuならコマンド一発で導入可能です。

$ sudo apt-get install python-pelican

非常に簡単ですね。Mac OS X環境でも非常に簡単です。

$ pip install pelican

ただ、Pythonが過渡期にありまして、特定のバージョンのPythonが好きだという方もいらっしゃいます。そんな方はvirtualenvなどを利用してみましょう。僕はデフォルトのバージョンでそのままいってしまいます。ということで、より便利に使うためにもhomebrewを導入してから環境を構築していきます。

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/mkdir /usr/local
Password:
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 227786, done.
remote: Compressing objects: 100% (59857/59857), done.
remote: Total 227786 (delta 166724), reused 227720 (delta 166675)
Receiving objects: 100% (227786/227786), 52.33 MiB | 2.83 MiB/s, done.
Resolving deltas: 100% (166724/166724), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
HEAD is now at 4e41304 Rename libcaf alias to caf.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started
silversquare:~ koichiroyamada$ 

インストールが終わったら動作確認です。

$ brew doctor
Your system is ready to brew.

pythonをガッツリ使い倒すためhomebrewでPythonを導入します。

$ brew install python
==> Installing dependencies for python: readline, sqlite, gdbm, openssl
==> Installing python dependency: readline
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.
######################################################################## 100.0%
==> Pouring readline-6.3.8.yosemite.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X provides similar software, and installing this software in
parallel can cause all kinds of trouble.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
🍺  /usr/local/Cellar/readline/6.3.8: 40 files, 2.1M
==> Installing python dependency: sqlite
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/sqlite-3.8.
######################################################################## 100.0%
==> Pouring sqlite-3.8.8.1.yosemite.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

OS X provides an older sqlite3.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/sqlite/lib
    CPPFLAGS: -I/usr/local/opt/sqlite/include

==> Summary
🍺  /usr/local/Cellar/sqlite/3.8.8.1: 9 files, 2.1M
==> Installing python dependency: gdbm
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/gdbm-1.11.y
######################################################################## 100.0%
==> Pouring gdbm-1.11.yosemite.bottle.1.tar.gz
🍺  /usr/local/Cellar/gdbm/1.11: 17 files, 532K
==> Installing python dependency: openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0
######################################################################## 100.0%
==> Pouring openssl-1.0.2.yosemite.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2: 459 files, 18M
==> Installing python
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/python-2.7.
######################################################################## 100.0%
==> Pouring python-2.7.9.yosemite.bottle.8.tar.gz
==> Caveats
Setuptools and Pip have been installed. To update them
  pip install --upgrade setuptools
  pip install --upgrade pip

You can install Python packages with
  pip install <package>

They will install into the site-package directory
  /usr/local/lib/python2.7/site-packages

See: https://github.com/Homebrew/homebrew/blob/master/share/doc/homebrew/Homebrew-and-Python.md

.app bundles were installed.
Run `brew linkapps python` to symlink these to /Applications.
==> /usr/local/Cellar/python/2.7.9/bin/python -s setup.py --no-user-cfg install 
==> /usr/local/Cellar/python/2.7.9/bin/python -s setup.py --no-user-cfg install 
==> Summary
🍺  /usr/local/Cellar/python/2.7.9: 4808 files, 78M

Python2.7.9がインストールされました。早速pelicanをインストールです。

$ pip install pelican
Collecting pelican
  Using cached pelican-3.5.0-py2.py3-none-any.whl
Collecting pygments (from pelican)
  Using cached Pygments-2.0.2-py2-none-any.whl
Collecting feedgenerator>=1.6 (from pelican)
  Using cached feedgenerator-1.7.tar.gz
Collecting unidecode (from pelican)
  Using cached Unidecode-0.04.17.tar.gz
    /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-11.3.1-py2.7.egg/setuptools/dist.py:283: UserWarning: The version specified requires normalization, consider using '0.4.17' instead of '0.04.17'.
Collecting pytz>=0a (from pelican)
  Downloading pytz-2014.10-py2.py3-none-any.whl (477kB)
    100% |################################| 479kB 4.7MB/s 
Collecting python-dateutil (from pelican)
  Downloading python_dateutil-2.4.0-py2.py3-none-any.whl (175kB)
    100% |################################| 176kB 6.0MB/s 
Collecting six>=1.4 (from pelican)
  Downloading six-1.9.0-py2.py3-none-any.whl
Collecting jinja2>=2.7 (from pelican)
  Using cached Jinja2-2.7.3.tar.gz
Collecting docutils (from pelican)
  Using cached docutils-0.12.tar.gz
Collecting blinker (from pelican)
  Using cached blinker-1.3.tar.gz
Collecting markupsafe (from jinja2>=2.7->pelican)
  Using cached MarkupSafe-0.23.tar.gz
Installing collected packages: markupsafe, blinker, docutils, jinja2, six, python-dateutil, pytz, unidecode, feedgenerator, pygments, pelican
  Running setup.py install for markupsafe
    building 'markupsafe._speedups' extension
    clang -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -I/usr/local/opt/sqlite/include -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -I/usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c markupsafe/_speedups.c -o build/temp.macosx-10.10-x86_64-2.7/markupsafe/_speedups.o
    clang -bundle -undefined dynamic_lookup -L/usr/local/lib -L/usr/local/opt/sqlite/lib build/temp.macosx-10.10-x86_64-2.7/markupsafe/_speedups.o -o build/lib.macosx-10.10-x86_64-2.7/markupsafe/_speedups.so
  Running setup.py install for blinker
  Running setup.py install for docutils
    changing mode of build/scripts-2.7/rst2html.py from 644 to 755
    changing mode of build/scripts-2.7/rst2s5.py from 644 to 755
    changing mode of build/scripts-2.7/rst2latex.py from 644 to 755
    changing mode of build/scripts-2.7/rst2xetex.py from 644 to 755
    changing mode of build/scripts-2.7/rst2man.py from 644 to 755
    changing mode of build/scripts-2.7/rst2xml.py from 644 to 755
    changing mode of build/scripts-2.7/rst2pseudoxml.py from 644 to 755
    changing mode of build/scripts-2.7/rstpep2html.py from 644 to 755
    changing mode of build/scripts-2.7/rst2odt.py from 644 to 755
    changing mode of build/scripts-2.7/rst2odt_prepstyles.py from 644 to 755
    changing mode of /usr/local/bin/rst2html.py to 755
    changing mode of /usr/local/bin/rst2latex.py to 755
    changing mode of /usr/local/bin/rst2man.py to 755
    changing mode of /usr/local/bin/rst2odt.py to 755
    changing mode of /usr/local/bin/rst2odt_prepstyles.py to 755
    changing mode of /usr/local/bin/rst2pseudoxml.py to 755
    changing mode of /usr/local/bin/rst2s5.py to 755
    changing mode of /usr/local/bin/rst2xetex.py to 755
    changing mode of /usr/local/bin/rst2xml.py to 755
    changing mode of /usr/local/bin/rstpep2html.py to 755
  Running setup.py install for jinja2



  Running setup.py install for unidecode
    /usr/local/Cellar/python/2.7.9/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/setuptools-11.3.1-py2.7.egg/setuptools/dist.py:283: UserWarning: The version specified requires normalization, consider using '0.4.17' instead of '0.04.17'.
  Running setup.py install for feedgenerator


Successfully installed blinker-1.3 docutils-0.12 feedgenerator-1.7 jinja2-2.7.3 markupsafe-0.23 pelican-3.5.0 pygments-2.0.2 python-dateutil-2.4.0 pytz-2014.10 six-1.9.0 unidecode-0.4.17

関連ツールなどもインストールされました。あわせてMarkdownで記述できるようにします。

$ pip install Markdown
Collecting Markdown
  Downloading Markdown-2.5.2.tar.gz (291kB)
    100% |################################| 294kB 4.6MB/s 
Installing collected packages: Markdown
  Running setup.py install for Markdown
    changing mode of build/scripts-2.7/markdown_py from 644 to 755
    Converting docs/authors.txt -> build/docs/authors.html
    Converting docs/change_log.txt -> build/docs/change_log.html
    Converting docs/cli.txt -> build/docs/cli.html
    Converting docs/index.txt -> build/docs/index.html
    Converting docs/install.txt -> build/docs/install.html
    Converting docs/reference.txt -> build/docs/reference.html
    Converting docs/release-2.0.1.txt -> build/docs/release-2.0.1.html
    Converting docs/release-2.0.2.txt -> build/docs/release-2.0.2.html
    Converting docs/release-2.0.txt -> build/docs/release-2.0.html
    Converting docs/release-2.1.0.txt -> build/docs/release-2.1.0.html
    Converting docs/release-2.1.1.txt -> build/docs/release-2.1.1.html
    Converting docs/release-2.2.0.txt -> build/docs/release-2.2.0.html
    Converting docs/release-2.2.1.txt -> build/docs/release-2.2.1.html
    Converting docs/release-2.3.txt -> build/docs/release-2.3.html
    Converting docs/release-2.4.txt -> build/docs/release-2.4.html
    Converting docs/release-2.5.txt -> build/docs/release-2.5.html
    Converting docs/siteindex.txt -> build/docs/siteindex.html
    Converting docs/test_suite.txt -> build/docs/test_suite.html
    Converting docs/extensions/abbreviations.txt -> build/docs/extensions/abbreviations.html
    Converting docs/extensions/admonition.txt -> build/docs/extensions/admonition.html
    Converting docs/extensions/api.txt -> build/docs/extensions/api.html
    Converting docs/extensions/attr_list.txt -> build/docs/extensions/attr_list.html
    Converting docs/extensions/code_hilite.txt -> build/docs/extensions/code_hilite.html
    Converting docs/extensions/definition_lists.txt -> build/docs/extensions/definition_lists.html
    Converting docs/extensions/extra.txt -> build/docs/extensions/extra.html
    Converting docs/extensions/fenced_code_blocks.txt -> build/docs/extensions/fenced_code_blocks.html
    Converting docs/extensions/footnotes.txt -> build/docs/extensions/footnotes.html
    Converting docs/extensions/header_id.txt -> build/docs/extensions/header_id.html
    Converting docs/extensions/index.txt -> build/docs/extensions/index.html
    Converting docs/extensions/meta_data.txt -> build/docs/extensions/meta_data.html
    Converting docs/extensions/nl2br.txt -> build/docs/extensions/nl2br.html
    Converting docs/extensions/sane_lists.txt -> build/docs/extensions/sane_lists.html
    Converting docs/extensions/smart_strong.txt -> build/docs/extensions/smart_strong.html
    Converting docs/extensions/smarty.txt -> build/docs/extensions/smarty.html
    Converting docs/extensions/tables.txt -> build/docs/extensions/tables.html
    Converting docs/extensions/toc.txt -> build/docs/extensions/toc.html
    Converting docs/extensions/wikilinks.txt -> build/docs/extensions/wikilinks.html
    changing mode of /usr/local/bin/markdown_py to 755
Successfully installed Markdown-2.5.2

さらにAmazon Web Serviceのs3にファイルをホストしますので、アップロード用のツールを導入します。

静的ファイルを後悔するにはAmazonのS3は非常にシンプルで簡単ですね。

$ pip install s3cmd
Collecting s3cmd
  Downloading s3cmd-1.0.1.tar.gz (56kB)
    100% |################################| 57kB 4.5MB/s 
    Using xml.etree.ElementTree for XML processing
Installing collected packages: s3cmd
  Running setup.py install for s3cmd
    Using xml.etree.ElementTree for XML processing
    changing mode of build/scripts-2.7/s3cmd from 644 to 755
    changing mode of /usr/local/bin/s3cmd to 755
Successfully installed s3cmd-1.0.1

あとは作業フォルダを作ってもう少し。

$ pelican-quickstart
$ s3cmd --configure

アップロードは簡単です。

$ make publish
$ make s3_upload