-
Notifications
You must be signed in to change notification settings - Fork 29
/
make-pdf
executable file
·118 lines (97 loc) · 3.03 KB
/
make-pdf
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
#!/usr/bin/env ruby
# -*- coding: utf-8 -*-
load 'make-common'
def usage
puts <<USAGE
Usage:
make-pdf [markdown]
Options:
-d, --debug : debuging
USAGE
exit
end
def latex_pre_pandoc(string, config)
replace(string) do
# Inc one category depth
s /^\#/, '##'
# title
s /^%[ ]*title:(.*)/, '# \1'
# fix image path
# XXX. direct to specially crafted image folder for pdf
s '../img/', 'img/'
end
end
def latex_post_pandoc(string, config)
replace(string) do
space = /\s/
# Reformat for the book documentclass as opposed to article
s '\section', '\chap'
s '\sub', '\\'
s /SUBSUBSECTION: (.*)/, '\subsubsection{\1}'
# Enable proper cross-reference
s /#{config['fig'].gsub(space, '\s')}\s*(\d+)\-\-(\d+)/, '\imgref{\1.\2}'
s /#{config['tab'].gsub(space, '\s')}\s*(\d+)\-\-(\d+)/, '\tabref{\1.\2}'
s /#{config['prechap'].gsub(space, '\s')}\s*(\d+)(\s*)#{config['postchap'].gsub(space, '\s')}/, '\chapref{\1}\2'
# Miscellaneous fixes
s /FIG: (.*)/, '\img{\1}'
s '\begin{enumerate}[1.]', '\begin{enumerate}'
s /(\w)--(\w)/, '\1-\2'
s /``(.*?)''/, "#{config['dql']}\\1#{config['dqr']}"
# Typeset the maths in the book with TeX
s '\verb!p = (n(n-1)/2) * (1/2^160))!', '$p = \frac{n(n-1)}{2} \times \frac{1}{2^{160}}$)'
s '2\^{}80', '$2^{80}$'
s /\sx\s10\\\^\{\}(\d+)/, '\e{\1}'
# Convert inline-verbatims into \texttt (which is able to wrap)
s /\\verb(\W)(.*?)\1/ do
"{\\texttt{#{verbatim_sanitize($2)}}}"
end
# Ensure monospaced stuff is in a smaller font
s /(\\verb(\W).*?\2)/, '{\footnotesize\1}'
s /(\\begin\{verbatim\}.*?\\end\{verbatim\})/m, '{\footnotesize\1}'
# Shaded verbatim block
s /(\\begin\{verbatim\}.*?\\end\{verbatim\})/m, '\begin{shaded}\1\end{shaded}'
end
end
# enforce
lang = "ko"
figures do
config = $config['default'].merge($config[lang])
markdown = Dir["#$root/chap*/doc.markdown"].sort.map do |file|
File.read(file)
end.join("\n\n")
print "\tParsing markdown... "
latex = pipe('pandoc -p --no-wrap -f markdown -t latex') do |stdin, stdout, stderr|
# latex specific pre-processing
markdown = latex_pre_pandoc(markdown, config)
# common pre-processing
stdin.write(pre_pandoc(markdown, config))
stdin.close
latex_post_pandoc(stdout.read, config)
end
puts "done"
print "\tCreating main.tex ... "
dir = "#$here/latex"
File.open("#{dir}/main.tex", 'w') do |file|
file.write($latex_template.result(binding))
end
puts "done"
abort = false
puts "\tRunning XeTeX:"
cd($root)
3.times do |i|
print "\t\tPass #{i + 1}... "
pipe("xelatex -output-directory='#{dir}' '#{dir}/main.tex' 2>&1") do |_, stdout|
unless $DEBUG
if ~ /^!\s/
puts "failed with:\n\t\t\t#{$_.strip}"
puts "\tConsider running this again with --debug."
abort = true
end while stdout.gets and not abort
else
STDERR.print while stdout.gets rescue abort = true
end
end
break if abort
puts "done"
end
end