Module: WickedPdfHelper::Assets

Defined in:
lib/wicked_pdf/wicked_pdf_helper/assets.rb

Constant Summary collapse

ASSET_URL_REGEX =
/url\(['"]?([^'"]+?)['"]?\)/

Instance Method Summary collapse

Instance Method Details

#wicked_pdf_asset_base64(path) ⇒ Object



7
8
9
10
11
12
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 7

def wicked_pdf_asset_base64(path)
  asset = Rails.application.assets.find_asset(path)
  throw "Could not find asset '#{path}'" if asset.nil?
  base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
  "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
end

#wicked_pdf_asset_path(asset) ⇒ Object



46
47
48
49
50
51
52
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 46

def wicked_pdf_asset_path(asset)
  if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
    pathname
  else
    "file:///#{pathname}"
  end
end

#wicked_pdf_image_tag(img, options = {}) ⇒ Object



30
31
32
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 30

def wicked_pdf_image_tag(img, options = {})
  image_tag wicked_pdf_asset_path(img), options
end

#wicked_pdf_javascript_include_tag(*sources) ⇒ Object



39
40
41
42
43
44
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 39

def wicked_pdf_javascript_include_tag(*sources)
  sources.collect do |source|
    source = WickedPdfHelper.add_extension(source, 'js')
    "<script type='text/javascript'>#{read_asset(source)}</script>"
  end.join("\n").html_safe
end

#wicked_pdf_javascript_src_tag(jsfile, options = {}) ⇒ Object



34
35
36
37
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 34

def wicked_pdf_javascript_src_tag(jsfile, options = {})
  jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
  javascript_include_tag wicked_pdf_asset_path(jsfile), options
end


14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 14

def wicked_pdf_stylesheet_link_tag(*sources)
  stylesheet_contents = sources.collect do |source|
    source = WickedPdfHelper.add_extension(source, 'css')
    "<style type='text/css'>#{read_asset(source)}</style>"
  end.join("\n")

  stylesheet_contents.gsub(ASSET_URL_REGEX) do
    if Regexp.last_match[1].starts_with?('data:')
      "url(#{Regexp.last_match[1]})"
    else
      asset = Regexp.last_match[1]
      "url(#{wicked_pdf_asset_path(asset)})" if asset_exists?(asset)
    end
  end.html_safe
end