forked from schacon/git-scribe
/
generate.rb
222 lines (193 loc) · 6.34 KB
/
generate.rb
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
class GitScribe
module Generate
# generate the new media
def gen(args = [])
type = first_arg(args) || 'all'
prepare_output_dir
gather_and_process
types = type == 'all' ? OUTPUT_TYPES : [type]
ret = false
output = []
Dir.chdir("output") do
types.each do |out_type|
call = 'do_' + out_type
if self.respond_to? call
ret = self.send call
else
die "NOT A THING: #{call}"
end
end
# clean up
`rm #{BOOK_FILE}`
ret
end
end
def prepare_output_dir
Dir.mkdir('output') rescue nil
Dir.chdir('output') do
Dir.mkdir('stylesheets') rescue nil
from_stdir = File.join(SCRIBE_ROOT, 'stylesheets')
FileUtils.cp_r from_stdir, '.'
end
end
def a2x(type)
"a2x -f #{type} -d book "
end
def a2x_wss(type)
a2x(type) + " --stylesheet=stylesheets/handbookish.css"
end
def do_pdf
info "GENERATING PDF"
# TODO: syntax highlighting (fop?)
ex("asciidoc -b docbook #{BOOK_FILE}")
strparams = {'callout.graphics' => 0,
'navig.graphics' => 0,
'admon.textlabel' => 1,
'admon.graphics' => 0}
param = strparams.map { |k, v| "--stringparam #{k} #{v}" }.join(' ')
cmd = "xsltproc --nonet #{param} --output #{local('book.fo')} #{base('docbook-xsl/fo.xsl')} #{local('book.xml')}"
ex(cmd)
cmd = "fop -fo #{local('book.fo')} -pdf #{local('book.pdf')}"
ex(cmd)
#puts `#{a2x('pdf')} -v --fop #{BOOK_FILE}`
if $?.exitstatus == 0
'book.pdf'
end
end
def do_epub
info "GENERATING EPUB"
# TODO: look for custom stylesheets
cmd = "#{a2x_wss('epub')} -v #{BOOK_FILE}"
if ex(cmd)
'book.epub'
end
end
def do_html
info "GENERATING HTML"
# TODO: look for custom stylesheets
#puts `#{a2x_wss('xhtml')} -v #{BOOK_FILE}`
styledir = local('stylesheets')
cmd = "asciidoc -a stylesdir=#{styledir} -a theme=handbookish #{BOOK_FILE}"
ex(cmd)
'book.html'
end
def do_site
info "GENERATING SITE"
# TODO: check if html was already done
ex("asciidoc -b docbook #{BOOK_FILE}")
xsldir = base('docbook-xsl/xhtml')
ex("xsltproc --stringparam html.stylesheet stylesheets/handbookish.css --nonet #{xsldir}/chunk.xsl book.xml")
source = File.read('index.html')
html = Nokogiri::HTML.parse(source, nil, 'utf-8')
sections = []
c = -1
# each chapter
html.css('.toc > dl').each do |section|
section.children.each do |item|
if item.name == 'dt' # section
c += 1
sections[c] ||= {'number' => c}
link = item.css('a').first
sections[c]['title'] = title = link.text
sections[c]['href'] = href = link['href']
clean_title = title.downcase.gsub(/[^a-z0-9\-_]+/, '_') + '.html'
sections[c]['link'] = clean_title
if href[0, 10] == 'index.html'
sections[c]['link'] = 'title.html'
end
sections[c]['sub'] = []
end
if item.name == 'dd' # subsection
item.css('dt').each do |sub|
link = sub.css('a').first
data = {}
data['title'] = title = link.text
data['href'] = href = link['href']
data['link'] = sections[c]['link'] + '#' + href.split('#').last
sections[c]['sub'] << data
end
end
end
end
book_title = html.css('head > title').text
content = html.css('body > div')[1]
content.css('.toc').first.remove
content = content.inner_html
sections.each do |s|
content.gsub!(s['href'], s['link'])
end
template_dir = File.join(SCRIBE_ROOT, 'site', 'default')
# copy the template files in
files = Dir.glob(template_dir + '/*')
FileUtils.cp_r files, '.'
Liquid::Template.file_system = Liquid::LocalFileSystem.new(template_dir)
index_template = Liquid::Template.parse(File.read(File.join(template_dir, 'index.html')))
page_template = Liquid::Template.parse(File.read(File.join(template_dir, 'page.html')))
# write the index page
main_data = {
'book_title' => book_title,
'sections' => sections
}
File.open('index.html', 'w+') do |f|
f.puts index_template.render( main_data )
end
# write the title page
File.open('title.html', 'w+') do |f|
data = {
'title' => sections.first['title'],
'sub' => sections.first['sub'],
'prev' => {'link' => 'index.html', 'title' => "Main"},
'home' => {'link' => 'index.html', 'title' => "Home"},
'next' => sections[1],
'content' => content
}
data.merge!(main_data)
f.puts page_template.render( data )
end
# write the other pages
sections.each_with_index do |section, i|
if i > 0 # skip title page
source = File.read(section['href'])
html = Nokogiri::HTML.parse(source, nil, 'utf-8')
content = html.css('body > div')[1].to_html
sections.each do |s|
content.gsub!(s['href'], s['link'])
end
File.open(section['link'], 'w+') do |f|
next_section = nil
if i <= sections.size
next_section = sections[i+1]
end
data = {
'title' => section['title'],
'sub' => section['sub'],
'prev' => sections[i-1],
'home' => {'link' => 'index.html', 'title' => "Home"},
'next' => next_section,
'content' => content
}
data.merge!(main_data)
f.puts page_template.render( data )
end
#File.unlink(section['href'])
info i
info section['title']
info section['href']
info section['link']
end
#File.unlink
end
sections
end
# create a new file by concatenating all the ones we find
def gather_and_process
files = Dir.glob("book/*")
FileUtils.cp_r files, 'output'
end
def ex(command)
out = `#{command} 2>&1`
info out
$?.exitstatus == 0
end
end
end