forked from schacon/git-scribe
/
git-scribe.rb
272 lines (225 loc) · 6.65 KB
/
git-scribe.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
require 'rubygems'
require 'nokogiri'
require 'liquid'
require 'fileutils'
require 'pp'
class GitScribe
def initialize(args)
@command = args.shift
@args = args
end
def self.start(args)
GitScribe.new(args).run
end
def run
if @command && self.respond_to?(@command)
self.send @command
else
help
end
end
## COMMANDS ##
def help
puts "No command: #{@command}"
puts "TODO: tons of help"
end
# start a new scribe directory with skeleton structure
def init
end
# check that we have everything needed
def check
# look for a2x (asciidoc, latex, xsltproc)
end
BOOK_FILE = 'book.asc'
OUTPUT_TYPES = ['pdf', 'epub', 'mobi', 'html', 'site']
# generate the new media
def gen
type = @args.shift || 'all'
prepare_output_dir
gather_and_process
types = type == 'all' ? OUTPUT_TYPES : [type]
output = []
Dir.chdir("output") do
types.each do |out_type|
call = 'do_' + out_type
if self.respond_to? call
self.send call
else
puts "NOT A THING: #{call}"
end
end
# clean up
# `rm #{BOOK_FILE}`
# TODO: open media (?)
end
end
def prepare_output_dir
Dir.mkdir('output') rescue nil
Dir.chdir('output') do
Dir.mkdir('stylesheets') rescue nil
from_stdir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '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
puts "GENERATING PDF"
# TODO: syntax highlighting (fop?)
`#{a2x('pdf')} --dblatex-opts "-P latex.output.revhistory=0" #{BOOK_FILE}`
if $?.exitstatus == 0
'book.pdf'
end
end
def do_epub
puts "GENERATING EPUB"
# TODO: look for custom stylesheets
`#{a2x_wss('epub')} -v #{BOOK_FILE}`
puts 'exit status', $?.exitstatus
'book.epub'
end
def do_html
puts "GENERATING HTML"
# TODO: look for custom stylesheets
#puts `#{a2x_wss('xhtml')} -v #{BOOK_FILE}`
styledir = File.expand_path(File.join(Dir.pwd, 'stylesheets'))
puts cmd = "asciidoc -a stylesdir=#{styledir} -a theme=handbookish #{BOOK_FILE}"
`#{cmd}`
puts 'exit status', $?.exitstatus
'book.html'
end
def do_site
puts "GENERATING SITE"
# TODO: check if html was already done
puts `asciidoc -b docbook #{BOOK_FILE}`
xsldir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', 'docbook-xsl', 'xhtml'))
`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
puts
end
pp sections
book_title = html.css('head > title').text
content = html.css('body > div')[1]
content.css('.toc').first.remove
content = content.inner_html
puts content
sections.each do |s|
content.gsub!(s['href'], s['link'])
end
template_dir = File.expand_path(File.join(File.dirname(__FILE__), '..', '..', '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'])
puts i
puts section['title']
puts section['href']
puts section['link']
puts
end
#File.unlink
end
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
# DISPLAY HELPER FUNCTIONS #
def l(info, size)
clean(info)[0, size].ljust(size)
end
def r(info, size)
clean(info)[0, size].rjust(size)
end
def clean(info)
info.to_s.gsub("\n", ' ')
end
# API/DATA HELPER FUNCTIONS #
def git(command)
`git #{command}`.chomp
end
end