根据源代码生成电子书

##前言
最近想要提高自己的编码能力,所以想要找一些优质的项目来学习一下。

最近刚好海淘了最新的 kindle paperwhite 3 所以立马想到了春哥的 code2ebook

centos 遇到无数的坑,转到 debian 下顺的一B。

##准备工作

###下载code2ebook

1
2
3
hambut@debian:~$ git clone https://github.com/agentzh/code2ebook.git
hambut@debian:~$ cd code2ebook
hambut@debian:~$ export PATH=`pwd`:$PATH

安装code2ebook的依赖

我们需要安装 vimctagsperl 这三个依赖软件,以 debian 为例

1
hambut@debian:~$ sudo apt-get install vim ctags perl

###准备阅读的源代码

1
hambut@debian:~$ git clone https://github.com/idevz/vanilla.git

根据源代码生成html源文件

1
2
3
4
5
6
7
8
9
10
11
12
hambut@debian:~$ cd vanilla

hambut@debian:~/Documents/vanilla$ src2html.pl --tab-width 4 \
--color --cross-reference --navigator --line-numbers . 'vanilla-source-code'
processing "." with ctags...
Wrote html_out/vanilla/v/routes/simple.lua.html
Wrote html_out/vanilla/v/routes/restful.lua.html
Wrote html_out/vanilla/v/routes/index.html
...

Wrote html_out/./index.html
Finished, total file count: 70

其实到这步已经可以很方便的通过浏览器等工具看有高亮模式的代码了

但是我们想要通过 kindle 看代码还需要转换成 .mobi.epub 的格式
这时我们需要另一个软件 Calibre

安装Calibre

1
hambut@debian:~$ sudo apt-get install calibre

转换 html 源文件到 mobi 文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
hambut@debian:~/Documents/vanilla$ ebook-convert html_out/index.html vanilla-source-code.mobi \ 
--output-profile kindle_dx --no-inline-toc --title "vanilla source code" --publisher 'hambut'

1% Converting input to HTML...
InputFormatPlugin: HTML Input running
on /home/hambut/Documents/vanilla/html_out/index.html
Language not specified
Creator not specified
Building file list...
Normalizing filename cases
Rewriting HTML links
Forcing index.html into XHTML namespace
Forcing index1.html into XHTML namespace
...
Forcing index36.html into XHTML namespace
Forcing index37.html into XHTML namespace
34% Running transforms on ebook...
Merging user specified metadata...
Detecting structure...
Maximum TOC links reached, stopping.
Auto generated TOC with 50 entries.
Flattening CSS and remapping font sizes...
Source base font size is 12.00000pt
Removing fake margins...
Cleaning up manifest...
Trimming unused files from manifest...
Creating MOBI Output...
67% Running MOBI Output plugin
Serializing resources...
Creating MOBI 6 output
Applying case-transforming CSS...
Rasterizing SVG images...
Converting XHTML to Mobipocket markup...
Serializing markup content...
Compressing markup content...
Generating MOBI index for a book
MOBI output written to /home/hambut/Documents/vanilla/vanilla-source-code.mobi
Output saved to /home/hambut/Documents/vanilla/vanilla-source-code.mobi

剩下来把生成好的 vanilla-source-code.mobi 发送到你的 kindle 就可以了。