使用Gitbook写开源书籍,过一把做家瘾

初级(适合没有编程基础的童鞋看)

使用GitBook Editor 写开源书籍

这个是gitbook官方出的编辑器,使用简单html

首先下载GitBook Editor

GitBook Editor

下载地址:https://www.gitbook.com/editorjava

支持Mac、Linux、Windowsnode

安装
Windows
  • 下载gitbook-win.zip
  • 解压;
  • 打开GitBook.exe安装;
Mac
  • 下载 gitbook-mac.dmg安装便可
Linux
  • 下载 gitbook-linux32.tar.gz
  • 使用tar -xvzf gitbook-linux32.tar.gz解压
  • 运行脚本 cd GitBook && ./install.sh安装

使用

安装完成以后,若是有github帐号直接,登陆,可是用github登陆以后也须要设置gitbook的邮箱和密码,等会儿要用到,没有帐号的直接注册就好;python

登陆后,能够直接在gitbook上建立书籍,建议在gitbook网站上建立书籍,而后经过GitBook Editor clone 下来;linux

clone 下来以后 能够直接写书了,先在感受是否是特别简单?别急,为了更爽的写做还须要继续看看下面。git

怎么和github同步?github

若是没有github帐号,先注册,以后绑定github帐号 https://www.gitbook.com/@quanke/settings#social(把链接中的quanke修改成本身的用户名)web

若是绑定了了github帐号,须要设置权限,https://www.gitbook.com/@quanke/settings#github(把链接中的quanke修改成本身的用户名), Reconnect GitHub Account 选择 With access to public repositoriessql

当受权好了以后,咱们来到书的设置( Settings )页面,点击设置github https://www.gitbook.com/book/quanke/vert-x-core-manual-for-java/settings/github(把quanke替换成你的用户名,把vert-x-core-manual-for-java替换成书名)npm

若是原来这本书是github里写的,直接填入github的地址就好,若是帐号都是新建的,什么都没有,那就点击Export to GitHub (注意填入gitbook的用户名密码不是github的)

若是上面的步骤没问题,点击 Add webhook 就OK了,给github增长了一个钩子,也就是说当你的github更新的时候,会告诉gitbook

还有一步很关键,在你的GitBook Editor中,打开咱们刚才clone下来的书,点击菜单里的 Book -> Repository Settings ,把这个设置成为刚才导到github的地址,以后就能够在GitBook Editor写做了。。有什么不懂的,直接能够留言问我

进阶(适合有编程基础的童鞋看)

gitbook 安装

  1. 安装npm 从网站 https://nodejs.org/#download 下载node.js源代码(点击绿色的INSTALL), 解压
./configure
make
make install

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

  1. gitbook 安装
npm install -g gitbook-cli
gitbook -V

查看gitbook是否安装成功。

gitbook 使用

  1. 根据目录生成图书结构

    1.1 README.md 与 SUMMARY编写

README.md

这个文件至关于一本Gitbook的简介。

$ mkdir test_gitbook
$ touch README.md

SUMMARY.md

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

$ touch SUMMARY.md
$ vim 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)

1.2 生成图书结构

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

$ gitbook init
$ 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文件,至关于一章的说明。

  1. 生成图书

2.1 输出为静态网站

你有两种方式输出一个静态网站:

2.1.1 本地预览时自动生成

当你在本身的电脑上编辑好图书以后,你可使用Gitbook 的命令行进行本地预览:

$ gitbook serve .

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

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

使用build参数生成到指定目录 与直接预览生成的静态网站文件不同的是,使用这个命令, 你能够将内容输入到你所想要的目录中去:

$ mkdir /tmp/gitbook
$ gitbook build --output=/tmp/gitbook

2.2 输出PDF

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

$ sudo npm install gitbook-pdf -g

我在执行上面这条命令的时候出现了下面的错误:

***************************************************
Downloading http://cdn.bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-i686.tar.bz2
Saving to /usr/local/lib/node_modules/gitbook-pdf/node_modules/phantomjs/phantomjs/phantomjs-1.9.7-linux-i686.tar.bz2
Error: connect ETIMEDOUT
    at exports._errnoException (util.js:746:11)
    at TCPConnectWrap.afterConnect [as oncomplete] (net.js:1010:19)
npm ERR! Linux 3.2.0-4-686-pae
npm ERR! argv "/usr/local/bin/node" "/usr/local/bin/npm" "install" "gitbook-pdf" "-g"
npm ERR! node v0.12.7
npm ERR! npm  v2.11.3
npm ERR! code ELIFECYCLE


npm ERR! phantomjs@1.9.7-5 install: `node install.js`
npm ERR! Exit status 1
npm ERR! 
npm ERR! Failed at the phantomjs@1.9.7-5 install script 'node install.js'.
npm ERR! This is most likely a problem with the phantomjs package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node install.js
npm ERR! You can get their info via:
npm ERR!     npm owner ls phantomjs
npm ERR! There is likely additional logging output above.


npm ERR! Please include the following file with any support request:
npm ERR!     /home/wangxq/repository/phantomjs/npm-debug.log


***************************************************

由错误报告内容的第一行能够知道,发生错误的缘由是下载 phantomjs发生了错误,所以咱们须要手动下载和安装。

解决方法:[1]

$ git clone git://github.com/ariya/phantomjs.git
$ sudo apt-get install build-essential g++ flex bison gperf ruby perl \
  libsqlite3-dev libfontconfig1-dev libicu-dev libfreetype6 libssl-dev \
  libpng-dev libjpeg-dev python libx11-dev libxext-dev       #安装编译须要的工具和依赖
$ cd phantomjs
$ git checkout 1.9.7  #注意:这里的1.9.7是phantom的版本号,能够由错误报告的第一行找出 
$ ./build.sh --jobs 4
$ sudo cp bin/phantomjs /bin/
$ sudo npm install gitbook-pdf -g  #从新进行安装

而后,使用下面的命令,要PDF文件,首先安装依赖库:

$ sudo apt-get install calibre  
$ gitbook -v 2.1.0 pdf .

发生错误:

*****************************************************
info: start conversion to pdf ....ERROR


Error: Command failed: /bin/sh -c ebook-convert /tmp/tmp-29384ctltwbk/SUMMARY.html /tmp/tmp-29384ctltwbk/index.pdf --title="" --comments="这本书是gitbook的一个例子" --language="en" --book-producer="GitBook" --publisher="GitBook" --chapter="descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter ')]" --chapter-mark="pagebreak" --page-breaks-before="/" --level1-toc="descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-1 ')]" --level2-toc="descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-2 ')]" --level3-toc="descendant-or-self::*[contains(concat(' ', normalize-space(@class), ' '), ' book-chapter-3 ')]" --no-chapters-in-toc --max-levels="1" --breadth-first --margin-left="62" --margin-right="62" --margin-top="56" --margin-bottom="56" --pdf-default-font-size="12" --pdf-mono-font-size="12" --paper-size="a4" --pdf-header-template="<p class='header'><span></span></p>" --pdf-footer-template="<p class='footer'><span>_SECTION_</span> <span style='float:right;'>_PAGENUM_</span></p>"
Usage: ebook-convert input_file output_file [options]


Convert an ebook from one format to another.


input_file is the input and output_file is the output. Both must be specified as the first two arguments to the command.


The output ebook format is guessed from the file extension of output_file. output_file can also be of the special format .EXT where EXT is the output file extension. In this case, the name of the output file is derived the name of the input file. Note that the filenames must not start with a hyphen. Finally, if output_file has no extension, then it is treated as a directory and an "open ebook" (OEB) consisting of HTML files is written to that directory. These files are the files that would normally have been passed to the output plugin.


After specifying the input and output file you can customize the conversion by specifying various options. The available options depend on the input and output file types. To get help on them specify the input and output file and then use the -h option.


For full documentation of the conversion system see
http://manual.calibre-ebook.com/conversion.html


Whenever you pass arguments to ebook-convert that have spaces in them, enclose the arguments in quotation marks.


ebook-convert: error: no such option: --pdf-default-font-size


*****************************************************

解决方法[2]

$ sudo -v && wget -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()"


$ gitbook -v 2.1.0 pdf .  # 从新执行命令生成pdf,目标文件为book.pdf

问题

若是输入gitbook init命令,出现Installing version 2.1.0, 须要耐性等待安装。

全科龙婷

相关文章
相关标签/搜索