-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathmakepdfs.rb
executable file
·66 lines (62 loc) · 2.51 KB
/
makepdfs.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
#!/usr/bin/env ruby
# --disable-web-security does not seem to work anymore since Chrome 111
command_base = %{
google-chrome-stable
--headless
--run-all-compositor-stages-before-draw
--disable-gpu
--window-size=1920,1080
--virtual-time-budget=1000000
--disable-audio-output
--incognito
--print-to-pdf=
}.gsub(/\s+/, " ").strip
command_end = '?print-pdf&pdfSeparateFragments=false'
root = ARGV[0] || raise("Bad usage, missing argument: launch as ./makepdfs.rb ROOT_OF_THE_WEBSITE")
puts "Working inside #{root}"
remote = `git -C '#{root}' remote get-url origin`
puts "Detected remote: #{remote}"
url = remote.match(/^(git@|https:\/\/)github\.com(\/|:)(?<owner>[^\/]+)\/(?<repo>[^\/]+?)(\.git)?$/)
owner = url[:owner].downcase
repo = (url[:repo] == "#{owner}.github.io" ? "" : "/#{url[:repo]}").downcase
puts "Detected repo: #{owner}/#{repo}"
files = Dir.glob("#{root}/**/index.html")
puts "Detected the following HTML roots:"
puts files
paths = Hash[
files.map { |path|
route = path.sub(root, '').gsub(/^\/?(.*)\/index.html$/) { $1 }
[route.nil? || route.strip.empty? ? "index" : route.gsub('/', '_'), "https://#{owner}.github.io#{repo}/#{route}#{command_end}"]
}
]
def is_letter_format(file)
`pdfinfo #{file} | grep 'Page size'`.include?('letter')
end
max_attempts = 25
max_attempts_format = 10
for name, path in paths do
puts "Working on #{name} built from #{path}"
output = "#{name}_slides.pdf"
command = "timeout 5m #{command_base}#{output} '#{path}'"
attempt = 0
attempts_for_format = 1
size = 0
is_letter = true
while (size / 1024 < 3 || is_letter) && attempt < max_attempts && attempts_for_format < max_attempts_format do
attempt = attempt + 1
puts "ATTEMPT #{attempt} at #{Time.now}: launching #{command}"
`#{command}`
size = File.size?(output) || 0
is_letter = size > 1024 && is_letter_format(output)
if is_letter then attempts_for_format += 1 end
puts "Produced a file of #{size} bytes with the #{is_letter ? 'wrong' : 'correct'} format"
end
if attempt >= max_attempts then
puts "::error ::Giving up after #{max_attempts} attempts, the URL #{path} produced an invalid file too many times."
File.delete(output)
end
if attempts_for_format >= max_attempts_format then
puts "::warning ::Website at #{path} does not seem to be rendering slides, saving as other document"
File.rename(output, "#{name}_page_format.pdf")
end
end