generalize rendering to tmp

This commit is contained in:
Loic Nageleisen 2014-02-20 18:18:21 +01:00
parent 7d99252332
commit f09e3f5247

View file

@ -13,9 +13,9 @@ module Tilt
def evaluate(scope, locals, &block) def evaluate(scope, locals, &block)
main = render_html(main_html_file, scope, locals, &block) main = render_html(main_html_file, scope, locals, &block)
render_css(*css_files, scope, locals, block) do |css| render_to_tmp(*css_files, scope, locals, block) do |tmp|
kit = PDFKit.new(main, pdfkit_options) kit = PDFKit.new(main, pdfkit_options)
css.each { |f| kit.stylesheets << f } tmp.each { |t, f| kit.stylesheets << f if t == 'text/css' }
@output = kit.to_pdf @output = kit.to_pdf
end end
@ -72,21 +72,24 @@ module Tilt
Tilt.new(file).render(scope, locals, &block) Tilt.new(file).render(scope, locals, &block)
end end
def render_css(*files, scope, locals, block) def render_to_tmp(*files, scope, locals, block)
tmps = [] tmps = []
noop = %w[html css]
css = files.map do |file| css = files.map do |file|
case file if noop.include?(ext = File.extname(file).sub(/^\./, ''))
when /.*\.css$/ ["text/#{ext}", file]
file
else else
tmp = Tempfile.new(File.basename(file)) tmp = Tempfile.new(File.basename(file))
tmps << tmp tmps << tmp
css = Tilt.new(file).render(scope, locals, &block) template = Tilt.new(file)
tmp.write(css) rendered = template.render(scope, locals, &block)
tmp.write(rendered)
tmp.close tmp.close
tmp.path mime = template.class.default_mime_type
[mime, tmp.path]
end end
end end