-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathRakefile
54 lines (42 loc) · 1.27 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
require 'rake'
require "github/markdown"
require "slim"
require "pdfkit"
task :default => ['show']
task :show => [:build] do
sh 'open build/*.pdf'
end
task :build do
rmtree 'build'
mkdir_p 'build'
FileList['src/*.md'].each do |src|
html_filename = out_filename(src, 'html')
pdf_filename = out_filename(src, 'pdf')
to_html(src, html_filename)
to_pdf(html_filename, pdf_filename)
end
end
def out_filename (src, new_extension)
'build/' + File.basename(src, '.md') + '.' + new_extension
end
def to_html (src, out)
source = File.read(src)
content = add_divs(GitHub::Markdown.render_gfm(source))
output = template('base', content)
File.open(out,'w') {|file| file.puts output}
puts "#{src} => #{out}"
end
# replace text such as [block] with <div class="block">
def add_divs (html)
html.gsub!(/^<p>\[(.*?)\/\]/m, '<div class="\1"></div>')
html.gsub!(/^<p>\[\/(.*?)\]/m, '</div>')
html.gsub!(/^<p>\[(.*?)\]/m, '<div class="\1">')
html
end
def to_pdf (src, out)
PDFKit.new(File.new(src), 'lowquality' => true, 'margin-left' => '1.2cm', 'margin-right' => '1.2cm', 'margin-top' => '1.2cm', 'margin-bottom' => '1.2cm').to_file(out)
puts "#{src} => #{out}"
end
def template(tpl, content)
Slim::Template.new("templates/" + tpl+'.slim').render(Object.new, :content => content)
end