forked from binford2k/showoff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshowoff_ng.rb
99 lines (82 loc) · 3.41 KB
/
showoff_ng.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
class Showoff
require 'showoff/config'
require 'showoff/compiler'
require 'showoff/presentation'
require 'showoff/state'
require 'showoff/locale'
require 'showoff/logger'
# @todo: Do we really need Ruby 2.0 support?
require 'showoff/monkeypatches'
GEMROOT = File.expand_path(File.join(File.dirname(__FILE__), '..'))
def self.do_static(args, options)
Showoff::State.set(:format, args[0] || 'web')
Showoff::State.set(:supplemental, args[1]) if args[0] == 'supplemental'
Showoff::Locale.setContentLocale(options[:language])
presentation = Showoff::Presentation.new(options)
makeSnapshot(presentation)
generatePDF if Showoff::State.get(:format) == 'pdf'
# puts '------------------'
# presentation.sections.each do |section|
# puts section.name
# section.slides.each do |slide|
# puts " - #{slide.name}"
# end
# end
end
# Generate a static HTML snapshot of the presentation in the `static` directory.
# Note that the `Showoff::Presentation` determines the format of the generated
# presentation based on the content requested.
#
# @see
# https://github.com/puppetlabs/showoff/blob/220d6eef4c5942eda625dd6edc5370c7490eced7/lib/showoff.rb#L1506-L1574
def self.makeSnapshot(presentation)
FileUtils.mkdir_p 'static'
File.write(File.join('static', 'index.html'), presentation.static)
['js', 'css'].each { |dir|
src = File.join(GEMROOT, 'public', dir)
dest = File.join('static', dir)
FileUtils.copy_entry(src, dest, false, false, true)
}
# now copy all the files we care about
presentation.assets.each do |path|
src = File.join(Showoff::Config.root, path)
dest = File.join('static', path)
FileUtils.mkdir_p(File.dirname(dest))
begin
FileUtils.copy(src, dest)
rescue Errno::ENOENT => e
Showoff::Logger.warn "Missing source file: #{path}"
end
end
end
# Generate a PDF version of the presentation in the current directory. This
# requires that the HTML snaphot exists, and it will *remove* that snapshot
# if the PDF generation is successful.
#
# @note
# wkhtmltopdf is terrible and will often report hard failures even after
# successfully building a PDF. Therefore, we check file existence and
# display different error messaging.
# @see
# https://github.com/puppetlabs/showoff/blob/220d6eef4c5942eda625dd6edc5370c7490eced7/lib/showoff.rb#L1447-L1471
def self.generatePDF
begin
require 'pdfkit'
output = Showoff::Config.get('name')+'.pdf'
kit = PDFKit.new(File.new('static/index.html'), Showoff::Config.get('pdf_options'))
kit.to_file(output)
FileUtils.rm_rf('static')
rescue RuntimeError => e
if File.exist? output
Showoff::Logger.warn "Your PDF was generated, but PDFkit reported an error. Inspect the file #{output} for suitability."
Showoff::Logger.warn "You might try loading `static/index.html` in a web browser and checking the developer console for 404 errors."
else
Showoff::Logger.error "Generating your PDF with wkhtmltopdf was not successful."
Showoff::Logger.error "Try running the following command manually to see what it's failing on."
Showoff::Logger.error e.message.sub('--quiet', '')
end
rescue LoadError
Showoff::Logger.error 'Generating a PDF version of your presentation requires the `pdfkit` gem.'
end
end
end