天天看点

gitbook安装与使用gitbook安装与使用

gitbook安装与使用

2016.04.10

Ubuntu14.04.4

gitbook文档 GitBook Documentation

1. 安装npm

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

  1. 下载node.js源代码 :

    下载地址

    gitbook安装与使用gitbook安装与使用
    当前下载的是 v4.4.2LTS 版本
  2. 编译源码:
    cd node-v4.  
    ./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

[email protected] /usr/local/lib/node_modules/gitbook-cli

├── [email protected].0.3

├── [email protected]

├── [email protected]

├── [email protected] ([email protected])

├── [email protected] ([email protected])

├── [email protected] ([email protected])

├── [email protected] ([email protected], [email protected])

├── [email protected]

├── [email protected] ([email protected], [email protected], [email protected], [email protected], [email protected])

├── [email protected]

└── [email protected] ([email protected], [email protected])

➜ ~ 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: 
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: >>  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: >>  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.: lodash@<. is no longer maintained. Upgrade to lodash@^4...
npm WARN deprecated npmconf@0..: this package has been reintegrated into npm and is now out of date with respect to npm
-
> [email protected] 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.. /usr/local/lib/node_modules/gitbook-pdf
├── commander@2.
├── q@1..
├── lodash@2.
└── [email protected] (which@1.., kew@0., rimraf@2., ncp@0., adm-zip@0., mkdirp@0., npmconf@0..)
           

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

$ sudo apt-get install calibre

使用命令

gitbook pdf .

生成pdf文档,此时会出现更新步骤:

➜  test git:(master) gitbook pdf .
Installing GitBook 
gitbook@2 ../../../../../../tmp/tmp-QlYw2Y5m0b5R/node_modules/gitbook
├── bash-color@0
├── escape-string-regexp@1
├── nunjucks-filter@1
├── gitbook-plugin-livereload@0
├── gitbook-plugin-sharing@1
├── github-slugid@1
├── gitbook-plugin-fontsettings@1
├── nunjucks-autoescape@1
├── jsonschema@1
├── spawn-cmd@0
├── q@1
├── json-schema-defaults@0
├── graceful-fs@3
├── semver@5
├── urijs@1
├── crc@3
├── tmp@0
├── npmi@0 (semver@4)
├── resolve@0
├── merge-defaults@0 (lodash@2)
├── dom-serializer@0 (domelementtype@1, entities@1)
├── send@0 (fresh@0, range-parser@1, mime@1, debug@2)
├── lodash@3
├── nunjucks@2 (asap@2, optimist@0)
├── i18n@0 (sprintf@0, mustache@2, debug@2)
├── fs-extra@0 (jsonfile@2, rimraf@2)
├── fstream-ignore@1 (inherits@2, minimatch@2, fstream@1)
├── gitbook-plugin-search@1 (lunr@0)
├── npm@2
├── gitbook-plugin-highlight@1 (highlight.js@8)
├── cheerio@0 (entities@1, css-select@1, htmlparser2@3)
├── request@2 (forever-agent@0, aws-sign2@0, caseless@0, tunnel-agent@0, oauth-sign@0, stringstream@0, json-stringify-safe@5, tough-cookie@2, mime-types@1, qs@2, node-uuid@1, combined-stream@0, http-signature@0, hawk@1, form-data@0, bl@0)
├── gitbook-parsers@0 (q@1, gitbook-restructuredtext@0, gitbook-markdown@0, gitbook-asciidoc@0)
├── chokidar@1 (path-is-absolute@1, arrify@1, is-glob@1, async-each@0, is-binary-path@1, glob-parent@1, readdirp@1, anymatch@1)
├── tiny-lr@0 (parseurl@1, livereload-js@2, qs@5, debug@2, faye-websocket@0, body-parser@1)
└── juice@1 (commander@2, slick@1, batch@0, cssom@0, web-resource-inliner@1)
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: >>  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: >>  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

继续阅读