CentOS7にGitBookの環境を構築

2016/09/04

CentOS7にGitBookの環境を構築してみます。 事前にVirtualBoxとVagrantでCentOS7の仮想環境を作っておきます。

VagrantでCentOS7の仮想環境を作る@Windows7 | backport

Calibreのインストール

GitBookのPDF生成にはcalibreが使われています。まずはcalibreをインストール。作業時の最新版は2.66.0でした。

[vagrant@localhost ~]$ sudo yum install epel-release -y
[vagrant@localhost ~]$ sudo yum install qt-creator -y --enablerepo=epel
[vagrant@localhost ~]$ sudo yum install wget -y
[vagrant@localhost ~]$ sudo -v && wget --no-check-certificate -nv -O- https://raw.githubusercontent.com/kovidgoyal/calibre/master/setup/linux-installer.py | sudo python -c "import sys; main=lambda:sys.stderr.write('Download failed\n'); exec(sys.stdin.read()); main()"

Node.jsのインストール

GitBookはNode.js上で動くアプリケーションなので、先にNode.jsをインストールします。 公式サイトのInstalling Node.js via package manager | Node.jsで案内されている手順に従います。

[vagrant@localhost ~]$ sudo curl --silent --location https://rpm.nodesource.com/setup_6.x | sudo bash -
[vagrant@localhost ~]$ sudo yum install nodejs -y
[vagrant@localhost ~]$ node -v
v6.4.0
[vagrant@localhost ~]$

GitBookのインストール

gitbook-cliというコマンドラインツールをインストールするとGitBookの各コマンドが使えるようになります。gitbook-cliはラッパーみたいなもので、GitBookの本体は何かしらのコマンドを実行するタイミングでインストールされます。

Windowsのディレクトリがマウントされているのは/vagrantなので、ここの中にgitbookという作業ディレクトリを作ります。

[vagrant@localhost ~]$ sudo npm install gitbook-cli -g
[vagrant@localhost vagrant]$ cd ~
[vagrant@localhost ~]$ cd /vagrant
[vagrant@localhost vagrant]$ mkdir gitbook
[vagrant@localhost vagrant]$ cd gitbook
[vagrant@localhost gitbook]$ ls -l
total 0
[vagrant@localhost gitbook]$ gitbook help
[vagrant@localhost gitbook]$ gitbook version
CLI version: 2.3.0
GitBook version: 3.2.0
[vagrant@localhost gitbook]$

インストールできました。 いくつかコマンドを試してみます。initコマンドで雛形を作り、buildコマンドでHTMLを、pdfコマンドでPDFを生成してみます。

[vagrant@localhost gitbook]$ gitbook init
warn: no summary file in this book
info: create README.md
info: create SUMMARY.md
info: initialization is finished
[vagrant@localhost gitbook]$
[vagrant@localhost gitbook]$ gitbook build
info: 7 plugins are installed
info: 6 explicitly listed
info: loading plugin "highlight"... OK
info: loading plugin "search"... OK
info: loading plugin "lunr"... OK
info: loading plugin "sharing"... OK
info: loading plugin "fontsettings"... OK
info: loading plugin "theme-default"... OK
info: found 1 pages
info: found 0 asset files
info: >> generation finished with success in 0.8s !
[vagrant@localhost gitbook]$
[vagrant@localhost gitbook]$ gitbook pdf
info: 7 plugins are installed
info: 6 explicitly listed
info: loading plugin "highlight"... OK
info: loading plugin "search"... OK
info: loading plugin "lunr"... OK
info: loading plugin "sharing"... OK
info: loading plugin "fontsettings"... OK
info: loading plugin "theme-default"... OK
info: found 1 pages
info: found 0 asset files
info: >> generation finished with success in 2.9s !
info: >> 1 file(s) generated
[vagrant@localhost gitbook]$

できたかどうか確認してみましょう。

[vagrant@localhost gitbook]$ ls -l
total 53
drwxrwxrwx. 1 vagrant vagrant     0 Sep  3 07:13 _book
-rwxrwxrwx. 1 vagrant vagrant 52659 Sep  3 07:15 book.pdf
-rwxrwxrwx. 1 vagrant vagrant    16 Sep  3 07:13 README.md
-rwxrwxrwx. 1 vagrant vagrant    40 Sep  3 07:13 SUMMARY.md
[vagrant@localhost gitbook]$ ls -l _book/
total 11
drwxrwxrwx. 1 vagrant vagrant 4096 Sep  3 07:13 gitbook
-rwxrwxrwx. 1 vagrant vagrant 6044 Sep  3 07:13 index.html
-rwxrwxrwx. 1 vagrant vagrant  566 Sep  3 07:13 search_index.json
[vagrant@localhost gitbook]$

HTMLもPDFも無事作成できました。 常用するにはもうちょっとあれこれ設定したほうがいいんですが、長くなったのでいったんここまで。




おススメ