-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpdf_generator.rb
139 lines (112 loc) · 4.11 KB
/
pdf_generator.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
require 'pdfkit'
require 'fileutils'
require 'combine_pdf'
class PdfGenerator < Middleman::Extension
SITE_URL = 'https://mss.band/'.freeze
PDF_BUILD_PATH = 'build/pdfs'.freeze
TIMESTAMP = Time.new.strftime("%Y%m%d.%H%M").freeze
COMMON_PDF_OPTIONS = {
page_size: 'letter',
margin_top: '10mm',
margin_bottom: '10mm',
margin_left: '20mm',
margin_right: '20mm',
print_media_type: true,
lowquality: true,
dpi: 62,
zoom: 1.0,
outline_depth: 1,
footer_font_size: 9,
footer_left: TIMESTAMP,
footer_right: SITE_URL,
enable_local_file_access: true
}.freeze
TOC_PDF_OPTIONS = {
footer_center: "MSS Songbook - Table of Contents",
}.freeze
NUMBERING_FORMAT = {
number_format: '%s',
location: :bottom_right,
margin_from_height: 5,
font_size: 9
}.freeze
SONGS = Dir.glob('source/songs/*.html.sng').sort.collect do |filename|
song = SongPro.parse(File.read(filename))
{
title: song.title,
artist: song.artist,
difficulty: song.custom[:difficulty],
filename: song.title.gsub(/[^\w\s]/i, '').parameterize,
short: song.custom[:short],
order: song.custom[:order],
}
end.sort_by! { |song| song[:order].to_i }.freeze
def manipulate_resource_list(resources)
FileUtils::mkdir_p(PDF_BUILD_PATH)
add_pdf_file_to_resource_list("mss-guitar.pdf", resources)
add_pdf_file_to_resource_list("mss-ukulele.pdf", resources)
add_song_pdfs_to_resource_list(resources)
resources
end
def after_build(builder)
FileUtils::mkdir_p(PDF_BUILD_PATH)
generate_static_pdf("toc.pdf", TOC_PDF_OPTIONS, 'build/songbook/toc/index.html')
generate_static_pdf("blank.pdf", COMMON_PDF_OPTIONS, 'build/songbook/blank_page/index.html')
generate_song_pdfs
generate_songbook_pdf("guitar")
generate_songbook_pdf("ukulele")
end
private
def add_song_pdfs_to_resource_list(resources)
SONGS.each do |song|
add_pdf_file_to_resource_list("#{song[:filename]}-guitar.pdf", resources)
add_pdf_file_to_resource_list("#{song[:filename]}-ukulele.pdf", resources)
end
end
def add_pdf_file_to_resource_list(filename, resources)
song_path = "pdfs/#{filename}"
song_source = "#{__dir__}/#{PDF_BUILD_PATH}/#{filename}"
FileUtils.touch(song_source)
resources << Middleman::Sitemap::Resource.new(@app.sitemap, song_path, song_source)
end
def generate_static_pdf(filename, options, source_path)
File.open(source_path, 'rb') do |file|
pdf = PDFKit.new(file.read, COMMON_PDF_OPTIONS.merge(options))
pdf.to_file("#{PDF_BUILD_PATH}/#{filename}")
end
end
def generate_song_pdfs
SONGS.each do |song|
generate_song_pdf("#{song[:filename]}/guitar/index.html", "#{song[:filename]}-guitar.pdf", song)
generate_song_pdf("#{song[:filename]}/ukulele/index.html", "#{song[:filename]}-ukulele.pdf", song)
end
end
def generate_song_pdf(source_filename, target_filename, song)
html_path = "build/songs/#{source_filename}"
pdf_path = "#{PDF_BUILD_PATH}/#{target_filename}"
if pdf_up_to_date(html_path, pdf_path)
puts "Skipping #{pdf_path} - already up to date"
else
puts "Generating #{pdf_path}"
File.open(html_path, 'rb') do |file|
pdf = PDFKit.new(file.read, COMMON_PDF_OPTIONS.merge(footer_center: "#{song[:title]}"))
pdf.stylesheets << Dir.glob('build/stylesheets/*.css')[0]
pdf.to_file(pdf_path)
end
end
end
def pdf_up_to_date(html_path, pdf_path)
File.file?(pdf_path) && (File.mtime(pdf_path) > File.mtime(html_path))
end
def generate_songbook_pdf(instrument)
songbook_pdf = CombinePDF.new
songbook_pdf << CombinePDF.load("#{__dir__}/#{PDF_BUILD_PATH}/toc.pdf")
SONGS.each do |song|
songbook_pdf << CombinePDF.load("#{__dir__}/#{PDF_BUILD_PATH}/#{song[:filename]}-#{instrument}.pdf")
songbook_pdf << CombinePDF.load("#{__dir__}/#{PDF_BUILD_PATH}/blank.pdf") if song[:short]
end
songbook_pdf.number_pages(NUMBERING_FORMAT)
songbook_pdf.save("#{__dir__}/#{PDF_BUILD_PATH}/mss-#{instrument}.pdf")
end
end
::Middleman::Extensions.register(:pdf_generator, PdfGenerator)