gitbook安装与使用

gitbook安装与使用

2016.04.10
Ubuntu14.04.4

gitbook文档 GitBook Documentation

1. 安装npm

可以参考 Ubuntu14.04安装nodejs ,这里只用了编译源码的方法,还可以使用apt-get的方法下载软件包。

  1. 下载node.js源代码 :
    下载地址
    这里写图片描述
    当前下载的是 v4.4.2LTS 版本

  2. 编译源码:

    cd node-v4.4.2  
    ./configure
    make
    sudo make install   #这里一定要加sudo

    成功执行后,nodejs、npm就被安装好了。

  3. 查看nodejs、npm版本号,确认安装无误

    ➜ ~: nodejs -v
    v4.4.2
    ➜ ~: npm -v
    2.15.0


2. 安装 gitbook

npm install -g gitbook-cli

查看gitbook是否安装成功:
gitbook -V

➜ ~ sudo npm install -g gitbook-cli
/usr/local/bin/gitbook -> /usr/local/lib/node_modules/gitbook-cli/bin/gitbook.js
gitbook-cli@2.1.3 /usr/local/lib/node_modules/gitbook-cli
├── bash-color@0.0.3
├── q@1.4.1
├── semver@5.1.0
├── user-home@2.0.0 (os-homedir@1.0.1)
├── commander@2.9.0 (graceful-readlink@1.0.1)
├── tmp@0.0.28 (os-tmpdir@1.0.1)
├── optimist@0.6.1 (wordwrap@0.0.3, minimist@0.0.10)
├── lodash@4.5.1
├── fs-extra@0.26.5 (path-is-absolute@1.0.0, klaw@1.1.3, jsonfile@2.2.3, graceful-fs@4.1.3, rimraf@2.5.2)
├── npm@3.7.5
└── npmi@1.0.1 (semver@4.3.6, npm@2.15.3)

➜ ~ gitbook -V
2.1.3


3. gitbook使用

3.1 根据目录生成图书结构

3.1.1 README.md 与 SUMMARY编写

README.md 这个文件相当于一本Gitbook的简介。
touch README.md

SUMMARY.md 这个文件是一本书的目录结构,使用Markdown语法
touch SUMMARY.md

在SUMMARY.md 输入:

* [简介](README.md)
* [第一章](chapter1/README.md)
 - [第一节](chapter1/section1.md)
 - [第二节](chapter1/section2.md)
* [第二章](chapter2/README.md)
 - [第一节](chapter2/section1.md)
 - [第二节](chapter2/section2.md)
* [结束](end/README.md)
3.1.2 生成图书结构

当这个目录文件创建好之后,我们可以使用Gitbook的命令行工具将这个目录结构生成相应的目录及文件:

输入命令 gitbook init

➜  gitbook init
info: init book at /home/michael/test1/book 
info: detect structure from SUMMARY (if it exists) 
info: found README.md 
info: create chapter1/README.md 
info: create chapter1/section1.md 
info: create chapter1/section2.md 
info: create chapter2/README.md 
info: create chapter2/section1.md 
info: create chapter2/section2.md 
info: create end/README.md 
info: initialization is finished 

Done, without error
➜  book ls
chapter1  chapter2  end  README.md  SUMMARY.md

tree . #查看建立的目录和文件
.
├── chapter1
│ ├── README.md
│ ├── section1.md
│ └── section2.md
├── chapter2
│ ├── README.md
│ ├── section1.md
│ └── section2.md
├── end
│ └── README.md
├── README.md
└── SUMMARY.md

我们可以看到,gitbook给我们生成了与SUMMARY.md所对应的目录及文件。
每个目录中,都有一个README.md文件,相当于一章的说明。


3.2 生成图书

3.2.1 输出为静态网站
  • 本地预览时自动生成
    当你在自己的电脑上编辑好图书之后,你可以使用Gitbook的命令行进行本地预览: gitbook serve .
➜  book gitbook serve .
Live reload server started on port: 35729
Press CTRL+C to quit ...

info: loading book configuration....OK 
info: load plugin gitbook-plugin-highlight ....OK 
info: load plugin gitbook-plugin-search ....OK 
info: load plugin gitbook-plugin-sharing ....OK 
info: load plugin gitbook-plugin-fontsettings ....OK 
info: load plugin gitbook-plugin-livereload ....OK 
info: >> 5 plugins loaded 
info: start generation with website generator 
info: clean website generatorOK 
info: generation is finished 

Starting server ...
Serving book on http://localhost:4000

然后浏览器中输入 http://localhost:4000 就可以预览生成的以网页形式组织的书籍。

  • 直接执行 gitbook build .命令直接生成静态网站
➜  book gitbook build . 
info: loading book configuration....OK 
info: load plugin gitbook-plugin-highlight ....OK 
info: load plugin gitbook-plugin-search ....OK 
info: load plugin gitbook-plugin-sharing ....OK 
info: load plugin gitbook-plugin-fontsettings ....OK 
info: >> 4 plugins loaded 
info: start generation with website generator 
info: clean website generatorOK 
info: generation is finished 

Done, without error
➜  book ls
_book  chapter1  chapter2  end  README.md  SUMMARY.md

这里你会发现,你在你的图书项目的目录中多了一个名为_book的文件目录,而这个目录中的文件,即是生成的静态网站内容。

可以指定输出目录 gitbook build . /home/michael/book_output

3.2.2 输出PDF

输入为PDF文件,需要先使用NPM安装上gitbook-pdf:

$ sudo npm install gitbook-pdf -g

这个命令会出现错误,需要手动下载及编译phantomjs工具

git clone git://github.com/ariya/phantomjs.git

安装编译需要的工具和依赖

sudo apt-get install ruby bison flex gperf m4 python
sudo apt-get install libbison-dev libfl-dev libruby1.9.1 libyaml-0-2
sudo apt-get install libssl-dev libpng-dev libjpeg-dev
sudo apt-get install libicu-dev libx11-dev libxext-dev
sudo apt-get install libfontconfig1-dev libfreetype6 libsqlite3-dev
sudo apt-get install libasound2-dev
sudo apt-get install libwayland-dev

现在可以开始编译源码了:

cd phantomjs
git checkout 1.9.7 注意:这里的1.9.7是phantom的版本号,可以由错误报告的第一行找出
./build.sh –jobs 4 使用jobs时我会出现问题,添加以上最后两个库后,直接不用jobs编译成功
sudo cp bin/phantomjs /bin/
sudo npm install gitbook-pdf -g 重新进行安装

➜  ~ sudo npm install gitbook-pdf -g 
npm WARN deprecated lodash@2.4.1: lodash@<3.0.0 is no longer maintained. Upgrade to lodash@^4.0.0.
npm WARN deprecated npmconf@0.0.24: this package has been reintegrated into npm and is now out of date with respect to npm
-
> phantomjs@1.9.7-5 install /usr/local/lib/node_modules/gitbook-pdf/node_modules/phantomjs
> node install.js

Writing location.js file
PhantomJS is already installed at /usr/bin/phantomjs.
/usr/local/bin/gitbook-pdf -> /usr/local/lib/node_modules/gitbook-pdf/bin/gitbook-pdf.js
gitbook-pdf@0.0.2 /usr/local/lib/node_modules/gitbook-pdf
├── commander@2.2.0
├── q@1.0.1
├── lodash@2.4.1
└── phantomjs@1.9.7-5 (which@1.0.9, kew@0.1.7, rimraf@2.2.8, ncp@0.4.2, adm-zip@0.2.1, mkdirp@0.3.5, npmconf@0.0.24)

最后,安装生成PDF依赖库:

$ sudo apt-get install calibre

使用命令gitbook pdf .生成pdf文档,此时会出现更新步骤:

➜  test git:(master) gitbook pdf .
Installing GitBook 2.6.7
gitbook@2.6.7 ../../../../../../tmp/tmp-11408QlYw2Y5m0b5R/node_modules/gitbook
├── bash-color@0.0.3
├── escape-string-regexp@1.0.3
├── nunjucks-filter@1.0.0
├── gitbook-plugin-livereload@0.0.1
├── gitbook-plugin-sharing@1.0.1
├── github-slugid@1.0.0
├── gitbook-plugin-fontsettings@1.0.2
├── nunjucks-autoescape@1.0.0
├── jsonschema@1.0.2
├── spawn-cmd@0.0.2
├── q@1.0.1
├── json-schema-defaults@0.1.1
├── graceful-fs@3.0.5
├── semver@5.0.1
├── urijs@1.17.0
├── crc@3.2.1
├── tmp@0.0.24
├── npmi@0.1.1 (semver@4.3.6)
├── resolve@0.6.3
├── merge-defaults@0.2.1 (lodash@2.4.2)
├── dom-serializer@0.1.0 (domelementtype@1.1.3, entities@1.1.1)
├── send@0.2.0 (fresh@0.2.4, range-parser@1.0.3, mime@1.2.11, debug@2.2.0)
├── lodash@3.10.1
├── nunjucks@2.2.0 (asap@2.0.3, optimist@0.6.1)
├── i18n@0.5.0 (sprintf@0.1.5, mustache@2.2.1, debug@2.2.0)
├── fs-extra@0.16.5 (jsonfile@2.2.3, rimraf@2.5.2)
├── fstream-ignore@1.0.2 (inherits@2.0.1, minimatch@2.0.10, fstream@1.0.8)
├── gitbook-plugin-search@1.1.0 (lunr@0.5.12)
├── npm@2.4.1
├── gitbook-plugin-highlight@1.0.3 (highlight.js@8.8.0)
├── cheerio@0.19.0 (entities@1.1.1, css-select@1.0.0, htmlparser2@3.8.3)
├── request@2.51.0 (forever-agent@0.5.2, aws-sign2@0.5.0, caseless@0.8.0, tunnel-agent@0.4.2, oauth-sign@0.5.0, stringstream@0.0.5, json-stringify-safe@5.0.1, tough-cookie@2.2.2, mime-types@1.0.2, qs@2.3.3, node-uuid@1.4.7, combined-stream@0.0.7, http-signature@0.10.1, hawk@1.1.1, form-data@0.2.0, bl@0.9.5)
├── gitbook-parsers@0.8.9 (q@1.4.1, gitbook-restructuredtext@0.2.3, gitbook-markdown@0.5.3, gitbook-asciidoc@0.2.4)
├── chokidar@1.0.6 (path-is-absolute@1.0.0, arrify@1.0.1, is-glob@1.1.3, async-each@0.1.6, is-binary-path@1.0.1, glob-parent@1.3.0, readdirp@1.4.0, anymatch@1.3.0)
├── tiny-lr@0.2.1 (parseurl@1.3.1, livereload-js@2.2.2, qs@5.1.0, debug@2.2.0, faye-websocket@0.10.0, body-parser@1.14.2)
└── juice@1.5.0 (commander@2.3.0, slick@1.12.1, batch@0.5.2, cssom@0.3.0, web-resource-inliner@1.1.4)
info: loading book configuration....OK 
info: load plugin gitbook-plugin-highlight ....OK 
info: load plugin gitbook-plugin-search ....OK 
info: load plugin gitbook-plugin-sharing ....OK 
info: load plugin gitbook-plugin-fontsettings ....OK 
info: >> 4 plugins loaded 
info: start generation with pdf generator 
info: clean pdf generatorOK 
info: write SUMMARY.html 
info: start conversion to pdf ....OK 
info: generation is finished 
info: >> 1 file(s) generated 

Done, without error

➜  test git:(master) ls
book.pdf  chapter1  chapter2  end  README.md  SUMMARY.md

可以看到在当前目录生成book.pdf


参考:
http://blog.csdn.net/xiaocainiaoshangxiao/article/details/46882921
http://www.linuxeden.com/html/softuse/20140409/150458.html
http://bluereader.org/article/159629980

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值