-
Notifications
You must be signed in to change notification settings - Fork 0
/
Rules
194 lines (154 loc) Β· 5.71 KB
/
Rules
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
#!/usr/bin/env ruby
# frozen_string_literal: true
require 'mimemagic'
require 'image_size'
require 'i18n'
# mainly used for Android "Add to Homescreen" feature
favicon_reprs = %i[default favicon152 favicon36 favicon48 favicon72 favicon96 favicon144 favicon192 favicon512]
preprocess do
# by default, no locale available, I18n::InvalidLocale: "fr" is not a valid locale
I18n.available_locales = [ @config[:site][:lang] ]
I18n.default_locale = @config[:site][:lang]
@config[:favicon_reprs] = favicon_reprs
missing_covers = 0
@items.select { |item| item.identifier =~ '/index.*' || book?(item) }.each do |item|
raise "item #{item.identifier} doesn't have a uuid (suggested: #{SecureRandom.uuid})" unless item[:uuid]
if book?(item)
item_name = item.identifier.without_exts.delete_prefix('/book/')
raise "mismatch between identifier name '#{item_name}' & isbn '#{item[:isbn]}'" if item[:isbn] != item_name
if @items["/cover/#{item[:isbn]}.*"].nil?
missing_covers += 1
query = CGI.escape("#{item[:title]} #{item[:author]}")
google_q = "https://www.google.fr/search?q=#{query}&udm=2"
amazon_q = "https://www.amazon.fr/s?k=#{query}&i=stripbooks"
puts "- missing cover for #{format_isbn(item[:isbn])} #{item[:title]} #{item[:author]} | #{google_q} | #{amazon_q}"
end
end
end
@items.select { |item| item.identifier =~ '/cover/*' }.each do |cover|
isbn = cover.identifier.without_exts.delete_prefix('/cover/')
raise "cover for unavailable book #{isbn}" if @items.select { |item| book?(item) }.none? { |item| item[:isbn] == isbn }
end
@items.select { |i| i.identifier =~ '/static/pwa-screenshot-*' }.each do |picture|
desktop_screenshot = picture.identifier =~ '/static/pwa-screenshot-desktop-*'
phone_screenshot = picture.identifier =~ '/static/pwa-screenshot-phone-*'
picture_path = picture.raw_filename
picture[:mime_type] = MimeMagic.by_path(picture_path).type
picture_size = ImageSize.new(IO.read(picture_path))
# TODO size depends on target (phone or desktop)
# isolate each set in its own directory to ease choice
picture_width = @config[:pwa_screenshot_width][phone_screenshot ? :phone : :desktop]
picture_height = picture_size.height * picture_width / picture_size.width
picture[:width] = picture_width
picture[:height] = picture_height
picture[:pwa_platform] = phone_screenshot ? 'android' : 'macos'
picture[:pwa_form_factor] = desktop_screenshot ? 'wide' : 'narrow'
end
@items.select { |i| i.identifier =~ '/cover/*' }.each do |cover|
cover_path = cover.raw_filename
cover[:mime_type] = MimeMagic.by_path(cover_path).type
cover_size = ImageSize.new(IO.read(cover_path))
%i[mini default].each do |repr|
cover_repr_width = @config[:cover][:width][repr]
cover_repr_height = cover_size.height * cover_repr_width / cover_size.width
cover["width_#{repr}"] = cover_repr_width
cover["height_#{repr}"] = cover_repr_height
end
end
puts "#{@items.count { |item| book?(item) }} books"
puts " - #{missing_covers} missing covers" if missing_covers > 0
end
favicon_reprs.each do |repr|
compile '/static/favicon.*', rep: repr do
route = if repr == :default
filter :favicon
# browser friendly favicon location + avoids 404 errors on server when such browsers are looking for it
'/favicon.ico'
else
# extract size from favicon repr name
size = repr.to_s.delete_prefix('favicon').to_i
filter :thumbnailize, width: size
"/#{repr}.#{item[:extension]}"
end
write route
end
end
compile '/static/pwa-screenshot-*' do
filter :thumbnailize, width: item[:width] unless @config[:dev]
write item.identifier.to_s.gsub(%r/\d+\-/, '')
end
compile '/static/{babelio,senscritique}.*' do
filter :thumbnailize, width: @config[:social][:badge_size] unless @config[:dev]
end
%i[compact default].each do |repr|
compile '/static/avatar.*', rep: repr do
filter :thumbnailize, width: @config[:avatar_size][repr] #unless @config[:dev]
suffix = "-#{repr}" unless repr == :default
write "#{item.identifier.without_exts}#{suffix}.#{item[:extension]}"
end
end
compile '/static/**/*' do
# don't filter or layout
end
%i[mini default].each do |repr|
compile '/cover/**/*', rep: repr do
width = @config[:cover][:width][repr]
filter :thumbnailize, width: width unless @config[:dev]
suffix = "-#{repr}" unless repr == :default
write "#{item.identifier.without_exts}#{suffix}.#{item[:extension]}"
end
end
compile '/book/**/*' do
filter :erb
filter :kramdown
filter :html_compressor, type: item[:extension] unless @config[:dev]
layout '/book.*'
layout '/default.*'
write item.identifier.without_ext + '/index.html'
end
compile '/api/v1/*' do
filter :erb
layout item[:layout] unless item[:layout] == 'none'
end
compile '/**/*.md' do
filter :erb
filter :kramdown
layout '/default.*'
end
compile '/service-worker.*' do
filter :erb
end
compile '/manifest.*' do
filter :erb
end
compile '/**/*' do
unless item.binary?
filter :erb
layout '/default.*'
end
end
route '/robots.txt' do
'/robots.txt'
end
route '/manifest.*' do
'/manifest.json'
end
route '/service-worker.*' do
'/service-worker.js'
end
route '/error/404.*' do
# Firebase hosting expected 404.html at the root (see https://firebase.google.com/docs/hosting/full-config#404)
'/404.html'
end
route '/api/**/*' do
item.identifier.to_s.chomp('.erb')
end
route '/index.*' do
"/index.html"
end
route '/**/*.erb' do
"#{item.identifier.without_ext}/index.html"
end
passthrough '/static/favicon{192,512}-maskable.*'
passthrough '/**/*'
layout '/**/*', :erb, trim_mode: '-'