gitbook安裝與使用
2016.04.10
Ubuntu14.04.4
gitbook文檔 GitBook Documentation
1. 安裝npm
可以參考 Ubuntu14.04安裝nodejs ,這裡隻用了編譯源碼的方法,還可以使用apt-get的方法下載下傳軟體包。
-
下載下傳node.js源代碼 :
下載下傳位址
目前下載下傳的是 v4.4.2LTS 版本gitbook安裝與使用gitbook安裝與使用 - 編譯源碼:
成功執行後,nodejs、npm就被安裝好了。cd node-v4. ./configure make sudo make install #這裡一定要加sudo
- 檢視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