-
Notifications
You must be signed in to change notification settings - Fork 35
/
Copy pathRakefile
117 lines (95 loc) · 2.92 KB
/
Rakefile
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
require 'rubygems'
require 'bundler/setup'
require 'yaml'
require 'asciidoctor'
desc 'Build pdf format'
task :pdf do
system 'bundle exec persie build pdf'
end
desc 'Build pdf sample'
task :sample do
require 'colorize'
require 'persie/dependency'
unless Persie::Dependency.which 'cpdf'
puts 'You must install "cpdf"(https://github.com/coherentgraphics/cpdf-binaries) first'
end
puts '=== Build sample ' << '=' * 55
puts 'Generating sample...'
info = File.read('./book.adoc')
info.match /:revnumber:\s*(\d+\.?\d*\.?\d*\.?\w*)/
rev = $1
info.match /:slug:\s*(\w+)/
slug = $1
page_range = '2-76' # EDIT this
system "cpdf ./themes/pdf/sample-cover.pdf ./builds/pdf/#{slug}-#{rev}.pdf #{page_range} -o ./builds/pdf/#{slug}-sample.pdf"
puts ' Done'.colorize(:green)
puts " Location: ./builds/pdf/#{slug}-sample.pdf"
puts '=' * 72
end
desc 'Build epub format'
task :epub do
system 'bundle exec persie build epub -c'
end
desc 'Build mobi format'
task :mobi do
system 'bundle exec persie build mobi'
end
desc 'Build site format'
task :site do
system 'bundle exec persie build html -m'
end
desc 'Convert to Markdown format'
task :md do
FileUtils.mkdir_p 'builds/markdown'
FileUtils.rm Dir['builds/markdown/*']
FileUtils.mkdir_p 'tmp/markdown'
bottle = {}
puts "Collecting refids"
Dir.glob("manuscript/*.adoc") do |f|
doc = Asciidoctor.load_file(f)
refids = doc.references[:ids]
refids.each do |_, v|
v.gsub!(/<code>|<\/code>/, '`')
end
bottle.merge! refids
end
File.open('tmp/markdown/refs.yml', 'w') { |f| f.write(bottle.to_yaml) }
excludes = [
'contributing',
'controllers',
'digging_depper',
'extending_rails',
'foreword',
'maintenance',
'models',
'release_notes',
'start_here',
'supplement',
'views'
]
files = FileList.new('manuscript/*.adoc')
files.exclude(excludes.map { |n| "manuscript/#{n}.adoc"})
files.each do |f|
puts "Converting #{File.basename(f)}"
asciidoc = File.open(f).read
imagesdir = "images"
imagesdir << "/#{File.basename(f, '.adoc')}" if ['getting_started', 'i18n'].include?(File.basename(f, '.adoc'))
markdown = Asciidoctor.convert( asciidoc,
template_dir: 'md_tpl',
template_engine_options: { erb: { trim: '-' } },
attributes: {
'imagesdir' => imagesdir
}
)
# weird, you cannot use `**' etc directly in templates
markdown.gsub!('<!--emphasis-->', '_')
markdown.gsub!('<!--strong-->', '**')
markdown.gsub!('<!--monospaced-->', '`')
filename = "#{File.basename(f,'.*')}.md"
File.open("builds/markdown/#{filename}", 'w') { |f| f.write(markdown) }
end
end
desc 'Preview site'
task :preview do
system 'bundle exec persie preview multiple'
end