Module: WickedPdf::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



10
11
12
13
14
15
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 10

def wicked_pdf_asset_base64(path)
  asset = find_asset(path)
  raise "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



74
75
76
77
78
79
80
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 74

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



58
59
60
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 58

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

#wicked_pdf_javascript_include_tag(*sources) ⇒ Object



67
68
69
70
71
72
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 67

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_pack_tag(*sources) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 45

def wicked_pdf_javascript_pack_tag(*sources)
  return unless defined?(Webpacker)

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

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



62
63
64
65
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 62

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


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

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
      "url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
    end
  end.html_safe
end

#wicked_pdf_stylesheet_pack_tag(*sources) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/wicked_pdf/wicked_pdf_helper/assets.rb', line 32

def wicked_pdf_stylesheet_pack_tag(*sources)
  return unless defined?(Webpacker)
  if running_in_development?
    stylesheet_pack_tag(*sources)
  else
    css_text = sources.collect do |source|
      source = WickedPdfHelper.add_extension(source, 'css')
      wicked_pdf_stylesheet_link_tag(webpacker_source_url(source))
    end.join("\n")
    css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
  end
end