mkbok 0.0.10 → 0.0.11

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,195 @@
1
+ #!/usr/bin/env ruby
2
+ # -*- coding: utf-8 -*-
3
+ # please use ruby1.8.*, there is problem for utf-8 in ruby 1.9
4
+ require 'fileutils'
5
+ require 'erb'
6
+ require 'yaml'
7
+
8
+ include FileUtils
9
+
10
+ $here = File.expand_path(File.dirname(__FILE__))
11
+ $root = File.join($here, '..')
12
+ $outDir = File.join($root, 'pdf')
13
+
14
+ def figures(&block)
15
+ begin
16
+ Dir["#$root/figures/18333*.png"].each do |file|
17
+ cp(file, file.sub(/18333fig0(\d)0?(\d+)\-tn/, '\1.\2'))
18
+ end
19
+ block.call
20
+ ensure
21
+ Dir["#$root/figures/18333*.png"].each do |file|
22
+ rm(file.gsub(/18333fig0(\d)0?(\d+)\-tn/, '\1.\2'))
23
+ end
24
+ end
25
+ end
26
+
27
+ def usage
28
+ puts <<USAGE
29
+ Usage:
30
+ makepdf [OPTION...] LANGUAGE [LANGUAGE 2...]
31
+
32
+ Options:
33
+ -d, --debug prints TeX and other output
34
+ USAGE
35
+ exit
36
+ end
37
+
38
+ def command_exists?(command)
39
+ if File.executable?(command) then
40
+ return command
41
+ end
42
+ ENV['PATH'].split(File::PATH_SEPARATOR).map do |path|
43
+ cmd = "#{path}/#{command}"
44
+ File.executable?(cmd) || File.executable?("#{cmd}.exe") || File.executable?("#{cmd}.cmd")
45
+ end.inject{|a, b| a || b}
46
+ end
47
+
48
+ def replace(string, &block)
49
+ string.instance_eval do
50
+ alias :s :gsub!
51
+ instance_eval(&block)
52
+ end
53
+ string
54
+ end
55
+
56
+ def verbatim_sanitize(string)
57
+ string.gsub('\\', '{\textbackslash}').
58
+ gsub('~', '{\textasciitilde}').
59
+ gsub(/([\$\#\_\^\%])/, '\\\\' + '\1{}')
60
+ end
61
+
62
+ def pre_pandoc(string, config)
63
+ replace(string) do
64
+ s /\#\#\#\#\# (.*?) \#\#\#\#\#/, 'PARASECTION: \1'
65
+ # Pandoc discards #### subsubsections #### - this hack recovers them
66
+ s /\#\#\#\# (.*?) \#\#\#\#/, 'SUBSUBSECTION: \1'
67
+
68
+ # Turns URLs into clickable links
69
+ s /\`(http:\/\/[A-Za-z0-9\/\%\&\=\-\_\\\.]+)\`/, '<\1>'
70
+ s /(\n\n)\t(http:\/\/[A-Za-z0-9\/\%\&\=\-\_\\\.]+)\n([^\t]|\t\n)/, '\1<\2>\1'
71
+
72
+ # Process figures
73
+ s /Insert\s18333fig\d+\.png\s*\n.*?\d{1,2}-\d{1,2}\. (.*)/, 'FIG: \1'
74
+ end
75
+ end
76
+
77
+ def post_pandoc(string, config)
78
+ replace(string) do
79
+ space = /\s/
80
+
81
+ # Reformat for the book documentclass as opposed to article
82
+ s '\section', '\chap'
83
+ s '\sub', '\\'
84
+ s /SUBSUBSECTION: (.*)/, '\subsubsection{\1}'
85
+ s /PARASECTION: (.*)/, '\paragraph{\1}'
86
+
87
+ # Enable proper cross-reference
88
+ s /#{config['fig'].gsub(space, '\s')}\s*(\d+)\-\-(\d+)/, '\imgref{\1.\2}'
89
+ s /#{config['tab'].gsub(space, '\s')}\s*(\d+)\-\-(\d+)/, '\tabref{\1.\2}'
90
+ s /#{config['prechap'].gsub(space, '\s')}\s*(\d+)(\s*)#{config['postchap'].gsub(space, '\s')}/, '\chapref{\1}\2'
91
+
92
+ # Miscellaneous fixes
93
+ s /FIG: (.*)/, '\img{\1}'
94
+ s '\begin{enumerate}[1.]', '\begin{enumerate}'
95
+ s /(\w)--(\w)/, '\1-\2'
96
+ s /``(.*?)''/, "#{config['dql']}\\1#{config['dqr']}"
97
+
98
+ # Typeset the maths in the book with TeX
99
+ s '\verb!p = (n(n-1)/2) * (1/2^160))!', '$p = \frac{n(n-1)}{2} \times \frac{1}{2^{160}}$)'
100
+ s '2\^{}80', '$2^{80}$'
101
+ s /\sx\s10\\\^\{\}(\d+)/, '\e{\1}'
102
+
103
+ # Convert inline-verbatims into \texttt (which is able to wrap)
104
+ s /\\verb(\W)(.*?)\1/ do
105
+ "{\\texttt{#{verbatim_sanitize($2)}}}"
106
+ end
107
+
108
+ # Ensure monospaced stuff is in a smaller font
109
+ s /(\\verb(\W).*?\2)/, '{\footnotesize\1}'
110
+ s /(\\begin\{verbatim\}.*?\\end\{verbatim\})/m, '{\footnotesize\1}'
111
+
112
+ # Shaded verbatim block
113
+ s /(\\begin\{verbatim\}.*?\\end\{verbatim\})/m, '\begin{shaded}\1\end{shaded}'
114
+
115
+ # http://www.devdaily.com/blog/post/latex/control-line-spacing-in-itemize-enumerate-tags
116
+ # http://wiki.ctex.org/index.php/LaTeX/%E5%88%97%E8%A1%A8
117
+ # set the space of itemsize
118
+ s /(\\begin\{itemize\})/m,'\begin{itemize}\setlength{\itemsep}{1pt}\setlength{\parskip}{0pt}\setlength{\parsep}{0pt}'
119
+ s /(\\begin\{enumerate\})/m,'\begin{enumerate}\setlength{\itemsep}{1pt}\setlength{\parskip}{0pt}\setlength{\parsep}{0pt}'
120
+ # hardcode for itemize to use * instead of dot, which is missed in some chinese fonts
121
+ s /(^\\item)/m,'\item[*]'
122
+
123
+ end
124
+ end
125
+
126
+ ARGV.delete_if{|arg| $DEBUG = true if arg == '-d' or arg == '--debug'}
127
+ languages = ARGV.select{|arg| File.directory?("#$root/#{arg}")}
128
+ usage if languages.empty?
129
+
130
+ $config = YAML.load_file("#$here/config.yml")
131
+ template = ERB.new(File.read("#$here/template.tex"))
132
+
133
+ missing = ['pandoc', 'xelatex'].reject{|command| command_exists?(command)}
134
+ unless missing.empty?
135
+ puts "Missing dependencies: #{missing.join(', ')}."
136
+ puts "Install these and try again."
137
+ exit
138
+ end
139
+
140
+ figures do
141
+ languages.each do |lang|
142
+ config = $config['default'].merge($config[lang]) rescue $config['default']
143
+
144
+ puts "#{lang}:"
145
+ markdown = Dir["#$root/#{lang}/*/*.markdown"].sort.map do |file|
146
+ File.read(file)
147
+ end.join("\n\n")
148
+
149
+ print "\tParsing markdown... "
150
+ latex = IO.popen('pandoc -p --no-wrap -f markdown -t latex', 'w+') do |pipe|
151
+ pipe.write(pre_pandoc(markdown, config))
152
+ pipe.close_write
153
+ post_pandoc(pipe.read, config)
154
+ end
155
+ puts "done"
156
+
157
+ print "\tCreating main.tex for #{lang}... "
158
+ dir = "#$here/#{lang}"
159
+ mkdir_p(dir)
160
+ File.open("#{dir}/main.tex", 'w') do |file|
161
+ file.write(template.result(binding))
162
+ end
163
+ puts "done"
164
+
165
+ abort = false
166
+
167
+ puts "\tRunning XeTeX:"
168
+ cd($root)
169
+ 3.times do |i|
170
+ print "\t\tPass #{i + 1}... "
171
+ IO.popen("xelatex -output-directory=\"#{dir}\" \"#{dir}/main.tex\" 2>&1") do |pipe|
172
+ unless $DEBUG
173
+ if ~ /^!\s/
174
+ puts "failed with:\n\t\t\t#{$_.strip}"
175
+ puts "\tConsider running this again with --debug."
176
+ abort = true
177
+ end while pipe.gets and not abort
178
+ else
179
+ STDERR.print while pipe.gets rescue abort = true
180
+ end
181
+ end
182
+ break if abort
183
+ puts "done"
184
+ end
185
+
186
+ unless abort
187
+ print "\tMoving output to kaiyuanbook.#{lang}.pdf... "
188
+ mv("#{dir}/main.pdf", "#$root/kaiyuanbook.#{lang}.pdf")
189
+ puts "done"
190
+ else
191
+ print "\tConvent error, exit !"
192
+ exit 1
193
+ end
194
+ end
195
+ end
@@ -0,0 +1,209 @@
1
+ \documentclass[a4paper]{book}
2
+ \usepackage{tocbibind} % for toc show inside pdf
3
+ \usepackage[
4
+ %urlbordercolor = {1 1 1},
5
+ %linkbordercolor = {1 1 1},
6
+ %citebordercolor = {1 1 1},
7
+ bookmarksnumbered, % add bookmark number in pdf output
8
+ urlcolor = blue,
9
+ colorlinks = true,
10
+ citecolor = black,
11
+ linkcolor = black]{hyperref}
12
+ \usepackage{graphicx}
13
+ \usepackage{xltxtra}
14
+ \usepackage{fancyhdr}
15
+ \usepackage{booktabs}
16
+ \usepackage{indentfirst}
17
+ \usepackage{framed,color}
18
+ \usepackage{footnpag}
19
+ \usepackage{array}
20
+ \usepackage[font=small,format=plain,labelfont=bf,up,textfont=it,up]{caption}
21
+
22
+ \usepackage{titlesec} % texlive-latex-extra package
23
+ \usepackage[titletoc]{appendix} % this is used for \appendices
24
+
25
+ \definecolor{colorchapter}{RGB}{70,130,180} % SteelBlue
26
+ \definecolor{colorsection}{RGB}{95,158,160} % CadetBlue
27
+ \definecolor{colorsubsection}{RGB}{139,0,0} % DarkRed
28
+ \definecolor{colorheader}{RGB}{70,130,180} % SteelBlue
29
+
30
+
31
+ \titleformat{\section}
32
+ {\color{colorsection}\normalfont\Large\bfseries}
33
+ {\color{colorsection}\thesection}{1em}{}
34
+ \titleformat{\subsection}
35
+ {\color{colorsubsection}\normalfont\large\bfseries}
36
+ {\color{colorsubsection}\thesubsection}{1em}{}
37
+
38
+ \definecolor{shadecolor}{gray}{0.90}
39
+
40
+ \setromanfont[Mapping=tex-text,BoldFont=<%= config['bold'] %>]{<%= config['font'] %>}
41
+ \setmonofont{<%= config['mono'] %>}
42
+
43
+ \XeTeXlinebreaklocale{<%= lang %>}
44
+ <%= config['langrule'] %>
45
+
46
+ \settowidth{\parindent}{<%= config['indent'] %>}
47
+ \setcounter{footnote}{0}
48
+
49
+ \title{{<%= config['title'] %>}}
50
+ \author{Larry Cai}
51
+
52
+ \makeatletter
53
+ \let\savedauthor=\@author
54
+ \let\savedtitle=\@title
55
+ \def\imgwidth{.6\linewidth}
56
+ \def\maxwidth{\ifdim\Gin@nat@width>\imgwidth\imgwidth
57
+ \else\Gin@nat@width\fi}
58
+ \makeatother
59
+
60
+ \title{\huge{\savedtitle}}
61
+
62
+ \author{\textbf{\savedauthor}\thanks{<%= config['thanks'] %>}}
63
+ \def\w3cdtfymd{\the\year-\ifnum\month<10 0\fi\the\month-\ifnum\day<10 0\fi\the\day}
64
+ \date{\w3cdtfymd}
65
+ %\renewcommand{\thefootnote}{\fnsymbol{footnote}}
66
+
67
+ \newcommand{\PreserveBackslash}[1]{\let\temp=\\#1\let\\=\temp}
68
+ \let\PBS=\PreserveBackslash
69
+ \makeatletter
70
+ \setlength\headheight{12\p@}
71
+ \setlength\headsep {.25in}
72
+ \setlength\topskip {10\p@}
73
+ \setlength\footskip{.35in}
74
+ \setlength\textwidth{400\p@}
75
+
76
+ \setlength\@tempdima{\paperheight}
77
+ \addtolength\@tempdima{-2in}
78
+ \divide\@tempdima\baselineskip
79
+ \@tempcnta=\@tempdima
80
+ \setlength\textheight{\@tempcnta\baselineskip}
81
+ \addtolength\textheight{\topskip}
82
+
83
+ \setlength\@tempdima {\paperwidth}
84
+ \addtolength\@tempdima {-\textwidth}
85
+ \setlength\oddsidemargin {\paperwidth}
86
+ \addtolength\oddsidemargin {-2.35in}
87
+ \addtolength\oddsidemargin {-\textwidth}
88
+ \setlength\marginparwidth {0pt}
89
+ \@settopoint\oddsidemargin
90
+ \@settopoint\marginparwidth
91
+ \setlength\evensidemargin {\paperwidth}
92
+ \addtolength\evensidemargin{-2.35in}
93
+ \addtolength\evensidemargin{-\textwidth}
94
+ \@settopoint\evensidemargin
95
+
96
+ \setlength\topmargin{\paperheight}
97
+ \addtolength\topmargin{-2in}
98
+ \addtolength\topmargin{-\headheight}
99
+ \addtolength\topmargin{-\headsep}
100
+ \addtolength\topmargin{-\textheight}
101
+ \addtolength\topmargin{-\footskip} % this might be wrong!
102
+ \addtolength\topmargin{-.5\topmargin}
103
+ \@settopoint\topmargin
104
+ %\@addtoreset{footnote}{page}
105
+ \makeatother
106
+
107
+ %\fancypagestyle{plain}{\fancyhf{}\fancyfoot[LE,RO]{\footnotesize\textbf\thepage}}
108
+ \fancypagestyle{plain}{\fancyhf{}\fancyfoot{}} % make sure no page number in page of first chapter
109
+
110
+ \pagestyle{plain}
111
+
112
+
113
+ \renewcommand{\headrulewidth}{0pt}
114
+ \renewcommand{\footrulewidth}{0pt}
115
+
116
+ \newcounter{img}[chapter]
117
+ \renewcommand{\theimg}{\thechapter.\arabic{img}}
118
+ \newcommand{\img}[1]{\begin{figure}[h!]
119
+ \refstepcounter{img}
120
+ \label{img:\theimg}
121
+ \centering\includegraphics[width=\maxwidth]{figures/\theimg.png}
122
+ \caption{#1}
123
+ \end{figure}}
124
+
125
+ \newcounter{tab}[chapter]
126
+ \renewcommand{\thetab}{\thechapter.\arabic{tab}}
127
+
128
+ \newcommand{\prechap}{<%= config['prechap'] %>}
129
+ \newcommand{\postchap}{<%= config['postchap'] %>}
130
+ \newcommand{\presect}{<%= config['presect'] %>}
131
+ \newcommand{\postsect}{<%= config['postsect'] %>}
132
+ \renewcommand{\chaptermark}[1]{\markboth{\textbf{\prechap \thechapter \postchap}\hspace*{1ex}#1}{}}
133
+ \renewcommand{\sectionmark}[1]{\markright{\textbf{\presect \thesection \postsect}\hspace*{1ex}#1}}
134
+ \newcommand{\chap}[1]{\newpage\thispagestyle{empty}\chapter{#1}\label{chap:\thechapter}}
135
+ \newcommand{\chapref}[1]{\hyperref[chap:#1]{\prechap #1\postchap}}
136
+ \newcommand{\imgref}[1]{\hyperref[img:#1]{<%= config['fig'] %>#1}}
137
+ \newcommand{\tabref}[1]{\hyperref[tab:#1]{<%= config['tab'] %>#1}}
138
+ \newcommand{\e}[1]{$ \times 10^{#1}$}
139
+ \renewcommand{\contentsname}{<%= config['con'] %>}
140
+ \renewcommand{\figurename}{<%= config['fig'] %>}
141
+ \renewcommand{\tablename}{<%= config['tab'] %>}
142
+ \renewcommand{\appendixname}{<%= config['appendix'] %>}
143
+
144
+ % chapter
145
+ \makeatletter
146
+ \def\@makechapterhead#1{%
147
+ \vspace*{50\p@}%
148
+ {\parindent \z@ \raggedright \normalfont
149
+ \ifnum \c@secnumdepth >\m@ne
150
+ \if@mainmatter
151
+ \color{colorchapter}\normalfont\huge\bfseries\prechap{ }\thechapter{ }\postchap
152
+ \par\nobreak
153
+ \vskip 20\p@
154
+ \fi
155
+ \fi
156
+ \interlinepenalty\@M
157
+ \color{colorchapter}\normalfont\Huge\bfseries #1\par\nobreak
158
+ \vskip 40\p@
159
+ }}
160
+
161
+ % this is for non-normal chapter like Acknownledgement, Preface, Contents
162
+ \def\@makeschapterhead#1{%
163
+ \vspace*{50\p@}%
164
+ {\parindent \z@ \raggedright \normalfont
165
+ \ifnum \c@secnumdepth >\m@ne
166
+ \if@mainmatter
167
+ \color{colorchapter}\normalfont\huge\bfseries \thechapter{ }
168
+ \par\nobreak
169
+ \vskip 20\p@
170
+ \fi
171
+ \fi
172
+ \interlinepenalty\@M
173
+ \color{colorchapter}\normalfont\Huge\bfseries #1\par\nobreak
174
+ \vskip 40\p@
175
+ }}
176
+ \makeatother
177
+
178
+ \linespread{1.3}
179
+
180
+ \begin{document}
181
+ \maketitle
182
+ \thispagestyle{empty}
183
+ \setcounter{tocdepth}{4}
184
+
185
+ \frontmatter
186
+ <%= preface %>
187
+ \tableofcontents\newpage\thispagestyle{empty}
188
+
189
+ % customize header & footer
190
+
191
+ \fancyhf{}
192
+ \fancyhead[LE]{\color{colorheader}\quad\small\textbf\thepage\quad\quad\small\leftmark}
193
+ \fancyhead[RO]{\color{colorheader}\small\rightmark\quad\quad\small\textbf\thepage\quad}
194
+ %\fancyhead[RE,LO]{\color{colorheader}\small{\savedtitle}} % book title
195
+ %\fancyfoot[LE,RO]{\small\textbf\thepage} % page number
196
+ %\fancyfoot[C]{\small\textbf{hello}} % could add release information
197
+
198
+ %\renewcommand{\headrulewidth}{0.4pt} % add one line
199
+ %\renewcommand{\headrule}{\color{red}} % conflict with headrulewidth
200
+ \pagestyle{fancy}
201
+
202
+ \mainmatter
203
+ <%= latex %>
204
+
205
+ \appendices
206
+ \renewcommand{\prechap}{\appendixname}
207
+ \renewcommand{\postchap}{}
208
+ <%= appendix %>
209
+ \end{document}
@@ -0,0 +1,29 @@
1
+ #! /usr/bin/env ruby
2
+ #
3
+
4
+ command = ARGV[0]
5
+ exclude = ['figures', 'figures-dia', 'figures-source', 'couchapp', 'latex', 'pdf', 'epub', 'en', 'ebooks']
6
+
7
+ data = []
8
+ original_lines=`grep -r -h '^[^[:space:]#]' en/[0]* | grep -v '^Insert'| wc -l`.to_i
9
+ Dir.glob("*").each do |dir|
10
+ if !File.file?(dir) && !exclude.include?(dir)
11
+ lines = `git diff-tree -r -p --diff-filter=M master:en master:#{dir} | grep '^-[^[:space:]#-]' | grep -v '^-Insert' | wc -l`.strip.to_i
12
+ last_commit = `git log -1 --no-merges --format="%ar" #{dir}`.chomp
13
+ authors = ""
14
+ if command == 'authors'
15
+ authors = `git shortlog --no-merges -s -n #{dir}`.chomp
16
+ end
17
+ data << [dir, lines, authors, last_commit]
18
+ end
19
+ end
20
+
21
+ d = data.sort { |a, b| b[1] <=> a[1] }
22
+ d.each do |dir, lines, authors, last|
23
+ puts "#{dir.ljust(10)} - #{(lines*100)/original_lines}% (#{last})"
24
+ if command == 'authors'
25
+ puts "Authors: #{authors.split("\n").size}"
26
+ puts authors
27
+ puts
28
+ end
29
+ end
File without changes
@@ -0,0 +1,89 @@
1
+ # 基础知识和10分钟写出第一本开源书 #
2
+ ## 先从Pro Git说起 ##
3
+
4
+ 如果你了解Git,或者想了解Git。那么你就应该知道[Pro Git](http://progit.org/),它是Git的书中写得最好的一本(至少是之一),可是你是否知道它有网络中文版,而且能在iPad上极其漂亮得阅读。并且是免费的,不是盗版的免费!如果你想要最新的,你甚至可以自己生成它。哈哈,我就是这么干的。
5
+
6
+ 这一切就归功于开源社区和它后面用到的技术。
7
+
8
+ ###开源书###
9
+ 这里我不用多讲,开源书就像其他的开源产品(如维基百科)一样,只要是开放的,社区就有人会贡献。[Pro Git](http://progit.org/)的作者Scott很慷慨得把书的内容全部共享在[github/progit](http://github.com/progit/progit)库中,使用得是[CC BY-NC-SA 3.0](http://creativecommons.org/licenses/by-nc-sa/3.0/us/)。
10
+
11
+ Scott只负责英文版,其他许许多多语言的翻译都是社区贡献的,中文翻译相当有质量,你可以在线读[Pro Git中文版](http://progit.org/book/zh/)。
12
+
13
+ ### 开源技术生成电子书 ###
14
+ 这本书不仅仅开源了内容,使用的技术也是开源的。让我们看看他是怎么做的。
15
+
16
+ ### Markdown原始文件 ###
17
+ 首先书的内容是用Markdown格式写的。Markdown格式的普及要归功于[Github](github.com)和[StackOverflow](http://stackoverflow.com/)。因为它们越来越流行,它们支持Markdown格式也越来越流行。这里要赞一个的是,国内的[图灵社区](http://www.ituring.com.cn/)也支持Markdown,用起来超级方便。
18
+
19
+ 简单来说,Markdown格式的文件看着像一般的文本文件,里面只是加了很少的格式标记,因此看文本文件也不影响理解,这种格式也有很多工具帮你去转化,而且很容易自动化解决。并且这些技术大多数是开源或免费的。
20
+
21
+ 松本行弘在他的Ruby书中说的好,想象一下几十年后,你是否还能找到软件来打开你的Word老格式的文档,没有那些软件,你的文档也就没用了。文本文件就没有这个问题。
22
+
23
+ 你可以直接看一下【Pro Git】的[“第一章 介绍” 的Markdown原始文件](https://raw.github.com/progit/progit/master/zh/01-introduction/01-chapter1.markdown),顺便看看github自动生成的简单[“第一章 介绍” 的html](https://github.com/progit/progit/blob/master/zh/01-introduction/01-chapter1.markdown)。
24
+
25
+ ## 产生电子书 ##
26
+ ### PDF格式 ###
27
+ 为了能达到出版的质量,Latex是一个常用的格式,PDF也能很容易地转换出来,有关Latex,自己看看参考链接学习吧。
28
+
29
+ [Pandoc](http://johnmacfarlane.net/pandoc/)能帮着从Markdown转换出Latex格式,然后再用[TexLive](http://www.tug.org/texlive/)软件中的`xelatex`转成PDF格式。
30
+
31
+ ### Epub/Mobi格式 ###
32
+ Ruby的[rdiscount](https://github.com/rtomayko/rdiscount)能帮你从markdown转成html格式,然后有[Calibre](calibre)附带的命令`ebook-convert`生成最终的`.mobi` (Kindle) 和 `.epub` (iPad)。
33
+
34
+ 从1.8版本开始,Pandoc也开始支持生成Epub格式了。
35
+
36
+ ## 工作环境 ##
37
+ 你只需要一台Linux机器(虚拟机就可以了)和熟悉简单的Linux命令就可以试验了。有Git和Ruby的知识那就更方便了。
38
+
39
+ 我用的试验环境是Ubuntu Oneiric (11.10)
40
+
41
+ ### 下载中文开源书 ###
42
+ 很简单,`git clone`一下这本书就可以了,下载它的源文件包我觉得还是烦了点。
43
+
44
+ $ git clone git@github.com/larrycai/kaiyuanbook.git
45
+
46
+ ### PDF格式 ###
47
+ 生成PDF是一个比较复杂的东西,[pandoc](http://johnmacfarlane.net/pandoc/)用Ubuntu库里1.8.x版本,[TexLive](http://www.tug.org/texlive/)用缺省Ubuntu源里的2009版也够了。当然也可下载最新的[TexLive](http://www.tug.org/texlive/)包安装,并配置到搜索路径中。
48
+
49
+ ~~~~~~~~~~ {.bash}
50
+ $ sudo apt-get install ruby1.9.1
51
+ $ sudo apt-get install pandoc
52
+ $ sudo apt-get install texlive-xetex
53
+ $ sudo apt-get install texlive-latex-recommended # 主要的Latex包
54
+ $ sudo apt-get install texlive-latex-extra # titlesec包,先不用知道
55
+ ~~~~~~~~~~~~~~~~~~~~
56
+
57
+ 因为是中文PDF,需要把字体嵌入在文件中,因此需要安装字体文件,幸运的是在源里有不错的字体。
58
+
59
+ ~~~~~~~~~~ {.bash}
60
+ $ sudo apt-get install ttf-arphic-gbsn00lp ttf-arphic-ukai # 文鼎字体
61
+ $ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei # 文泉驿字体
62
+ ~~~~~~~~~~
63
+
64
+ 现在你就可以用`mkbok`命令生成Pdf文件了,`mkbok`会自动调用Pandoc和Latex工具生成Pdf、Html、Epub格式。
65
+
66
+ $ ./mkbok
67
+
68
+ 怎么样,打开看看Pdf文件,很漂亮了吧。
69
+
70
+ ## 自己试试 ##
71
+ 一定要做,搜索一下,改掉一些内容,再运行一遍。
72
+
73
+ 好了,你可以把书扔到一遍,写你自己的书了。照样画葫芦,你行的。在下一章会对书的结构和怎么对应地用Markdown写进行详细地解释。
74
+
75
+ ## 其他常用的格式 ##
76
+
77
+ 计算机类图书对格式要求不是很多,图文、章节、源代码基本就够了,就算有些复杂公式,也可用图来显示。这也从理论上说明,它不需要复杂的格式。现在对这类技术书出版我的理解主要有几种:
78
+
79
+ 1. Microsoft的Word格式,虽然国内出版界如日中天,缺省就认它(对技术没追求,鄙视)。简单好学,但是不擅长自动化,是开源的死敌。
80
+ 2. Latex格式(就是Donald E. Knuth(高德纳)发明的,这是很棒的东西,特别适合学术类的各种复杂的公式等,不过学习曲线很高,直接写还是很有难度的。国内也只有几家学术期刊使用。
81
+ 3. Docbook格式是最有名的(从SGML演化过来?),Orielly和Pragmatic出版社缺省就用它,它能 很方便的转化出出版要的各种样式。如[Jenkins - the definition guide](http://www.wakaleo.com/books/jenkins-the-definitive-guide)开源书就是采用Docbook。但由于是XML格式,很多人不习惯,而且多人网上协作不是很方便。
82
+ 4. 通过蒋鑫的[Got Github](http://www.worldhello.net/gotgithub/)开源书,我也了解reStructureText也是和Markdown差不多纯文本的,也是蛮流行的。
83
+
84
+ ## 参考 ##
85
+ 1. Pro Git: <http://progit.org/>
86
+ 2. LaTeX2e完全学习手册: <http://book.douban.com/subject/5450816/>
87
+
88
+
89
+
@@ -0,0 +1,105 @@
1
+ # 用Markdown来写 #
2
+ 希望你已经照着上一章生成出了第一个Pdf文件,看到了一本蛮标准的书的样子。
3
+
4
+ 这一章详细点介绍这是如何用Markdown写出来的。
5
+
6
+ 首先简单介绍一下一本书的组成
7
+
8
+ ## 标准书稿的组成 ##
9
+
10
+ 一部完整的书稿,通常按顺序由封面、扉页、版权页(含内容简介)、序、前言、目录、正文(含图稿)、附录(可选项)、参考文献(可选项)、符号表(可选项)、索引(可选项)等组成。
11
+
12
+ 详细请看电子出版社的《作译者手册》<http://www.phei.com.cn/wstg/zyzxz>
13
+
14
+ 封面、扉页、版权页(含内容简介)封底一般有设计师用图形软件做出来单独印刷的。
15
+
16
+ 序、前言、目录、正文(含图稿)、附录(可选项)都是标准提交格式写的。
17
+
18
+ 参考文献(可选项)、符号表(可选项)、索引(可选项)一般应该是自动生成的。
19
+
20
+ ## 怎么从Markdown到书 ##
21
+ 前一章提到过,技术书籍对排版要求不高,不同级别的章节,代码显示和一些图示就可以了。因此有机会用文本的方式一一对应过去。
22
+
23
+ 最[基本的Markdown](http://daringfireball.net/projects/markdown/)可以完成上面的功能了。
24
+
25
+ 在Latex中设置好书的模板(`latex/template.tex`),如页眉、页脚、目录、颜色等等,一般有经验的人可以帮你搞定。
26
+
27
+ Pandoc软件会把Markdown文件转换成Latex格式,然后套上上面的模板。
28
+
29
+ `mkbok`是一个小工具,做了一些额外的定义和调整。
30
+
31
+ ### Markdown扩展 ###
32
+
33
+ 基本的Markdown功能不是很全(如没有脚注),因此可以考虑用一些Markdown的扩展。
34
+
35
+ 由于会用Pandoc转换,我建议推荐用Pandoc的Markdown扩展<http://johnmacfarlane.net/pandoc/README.html>
36
+
37
+ 你有兴趣也可以看看Github的<http://github.github.com/github-flavored-markdown/>
38
+
39
+ ## 如何使用Markdown写书 ##
40
+ 现在可以看看结构了。
41
+
42
+ ~~~~~~~~~ {.bash}
43
+ $ find zh
44
+ zh/preface # 序和前言
45
+ zh/chapters # 正文
46
+ zh/appendix # 附录
47
+ ~~~~~~~~~~~~
48
+
49
+ ### 标准章节 ###
50
+ 每一章的第一行基本就是章节名字,应该只出现一次
51
+
52
+ ~~~~~~~
53
+ # 用Markdown来写 #
54
+ ~~~~~~~
55
+
56
+ 其他的小章节用`##`和`###`表示,最好不要有更多的层次。
57
+
58
+ ### 序、前言、附录 ###
59
+ 这和其他章节是一样的,只是在PDF的目录显示中章节号和计数不同。
60
+
61
+ ### 页眉、页脚 ###
62
+ 这是有Latex设定的,不需要Markdown参与。
63
+
64
+ ### 目录 ###
65
+ 这是有Latex自动生成的,不需要Markdown参与。
66
+
67
+ ### 图片 ###
68
+ 把图片放在`figures`目录中。
69
+
70
+ ### 脚注 ###
71
+ 这是Pandoc扩展Markdown才能支持
72
+
73
+ ### 代码 ###
74
+ 基本的Markdown用空四格的方式,不支持代码高亮显示。
75
+
76
+ 我建议使用Pandoc扩展Markdown,它在生成的Epub和Html中支持代码高亮显示(还没搞定)
77
+
78
+ ## 中文字体 ##
79
+ 首先,我用的是Linux环境并且选用的是**UTF-8**的编码,而不是GBK,否则在github上显示会有问题,不了解这方面的朋友自己找找资料吧,够讲个把小时的。
80
+
81
+ 在产生PDF时,一般建议内嵌中文字体的,但是真正能用的中文字体实际很少,极大多数是有版权的:
82
+
83
+ * [文鼎](http://www.arphic.com.tw/)开放的四套字体(简报宋、细上海宋、简中楷、中楷),没有一点版权问题,是大部分的中文Linux的缺省安装。
84
+ * [文泉驿](http://wenq.org/)的几套字体(微米黑、正黑、点阵宋体)是开放但是GPL性质的,所以不是随便可以商用的。
85
+ * Adobe有两套开放字体(宋体、黑体)我认为是可以随便用的,忘了在哪里看到这个解释的了。
86
+
87
+ 可以看看[Ubuntu免费中文字体](http://wiki.ubuntu.org.cn/免费中文字体)的介绍有个认识。
88
+
89
+ ## 怎么选择对应字体 ##
90
+ 一般缺省中文正文字体是宋体、细明体,对应英文Serif类的英文字体:Georgia、Times New Roman等。
91
+
92
+ 标题和重要内容可以选楷体和黑体,对应英文Sans Serif类的英文字体:Arial、Tahoma、Verdana等
93
+
94
+ 技术文章中常见的代码典型的等宽体用黑体,对应英文Monospace类的英文字体:Courier New等
95
+
96
+ 所以对应的在[我的中文Latex配置](https://github.com/larrycai/sdcamp/blob/master/latex/config.yml)中可选的是:
97
+
98
+ * font:文鼎的简报宋、细上海宋,文泉驿的点阵宋体,Adobe的宋体
99
+ * bold: 文鼎的简中楷、中楷,文泉驿的微米黑、正黑,Adobe的黑体
100
+ * mono: 文泉驿的微米黑、正黑,Adobe的黑体
101
+
102
+
103
+
104
+
105
+