wicked_pdf 1.0.6 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2,43 +2,54 @@ require 'wicked_pdf/pdf_helper'
2
2
  require 'wicked_pdf/wicked_pdf_helper'
3
3
  require 'wicked_pdf/wicked_pdf_helper/assets'
4
4
 
5
- if defined?(Rails)
5
+ class WickedPdf
6
+ if defined?(Rails)
6
7
 
7
- if Rails::VERSION::MAJOR == 3
8
+ if Rails::VERSION::MAJOR >= 5
8
9
 
9
- class WickedRailtie < Rails::Railtie
10
- initializer 'wicked_pdf.register' do |_app|
11
- ActionController::Base.send :include, PdfHelper
12
- if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
10
+ class WickedRailtie < Rails::Railtie
11
+ initializer 'wicked_pdf.register' do |_app|
12
+ ActionController::Base.send :prepend, PdfHelper
13
13
  ActionView::Base.send :include, WickedPdfHelper::Assets
14
- else
15
- ActionView::Base.send :include, WickedPdfHelper
16
14
  end
17
15
  end
18
- end
19
16
 
20
- elsif Rails::VERSION::MAJOR == 2
17
+ elsif Rails::VERSION::MAJOR == 4
21
18
 
22
- unless ActionController::Base.instance_methods.include? 'render_with_wicked_pdf'
23
- ActionController::Base.send :include, PdfHelper
24
- end
25
- unless ActionView::Base.instance_methods.include? 'wicked_pdf_stylesheet_link_tag'
26
- ActionView::Base.send :include, WickedPdfHelper
27
- end
19
+ class WickedRailtie < Rails::Railtie
20
+ initializer 'wicked_pdf.register' do |_app|
21
+ ActionController::Base.send :include, PdfHelper
22
+ ActionView::Base.send :include, WickedPdfHelper::Assets
23
+ end
24
+ end
25
+
26
+ elsif Rails::VERSION::MAJOR == 3
28
27
 
29
- else
28
+ class WickedRailtie < Rails::Railtie
29
+ initializer 'wicked_pdf.register' do |_app|
30
+ ActionController::Base.send :include, PdfHelper
31
+ if Rails::VERSION::MINOR > 0 && Rails.configuration.assets.enabled
32
+ ActionView::Base.send :include, WickedPdfHelper::Assets
33
+ else
34
+ ActionView::Base.send :include, WickedPdfHelper
35
+ end
36
+ end
37
+ end
30
38
 
31
- class WickedRailtie < Rails::Railtie
32
- initializer 'wicked_pdf.register' do |_app|
39
+ elsif Rails::VERSION::MAJOR == 2
40
+
41
+ unless ActionController::Base.instance_methods.include? 'render_with_wicked_pdf'
33
42
  ActionController::Base.send :include, PdfHelper
34
- ActionView::Base.send :include, WickedPdfHelper::Assets
35
43
  end
44
+ unless ActionView::Base.instance_methods.include? 'wicked_pdf_stylesheet_link_tag'
45
+ ActionView::Base.send :include, WickedPdfHelper
46
+ end
47
+
36
48
  end
37
49
 
38
- end
50
+ if Mime::Type.lookup_by_extension(:pdf).nil?
51
+ Mime::Type.register('application/pdf', :pdf)
52
+ end
39
53
 
40
- if Mime::Type.lookup_by_extension(:pdf).nil?
41
- Mime::Type.register('application/pdf', :pdf)
42
54
  end
43
-
44
55
  end
@@ -1,11 +1,13 @@
1
1
  require 'tempfile'
2
2
 
3
- class WickedPdfTempfile < Tempfile
4
- # ensures the Tempfile's filename always keeps its extension
5
- def initialize(filename, temp_dir = nil)
6
- temp_dir ||= Dir.tmpdir
7
- extension = File.extname(filename)
8
- basename = File.basename(filename, extension)
9
- super([basename, extension], temp_dir)
3
+ class WickedPdf
4
+ class WickedPdfTempfile < Tempfile
5
+ # ensures the Tempfile's filename always keeps its extension
6
+ def initialize(filename, temp_dir = nil)
7
+ temp_dir ||= Dir.tmpdir
8
+ extension = File.extname(filename)
9
+ basename = File.basename(filename, extension)
10
+ super([basename, extension], temp_dir)
11
+ end
10
12
  end
11
13
  end
@@ -1,3 +1,3 @@
1
1
  class WickedPdf
2
- VERSION = '1.0.6'.freeze
2
+ VERSION = '1.1.0'.freeze
3
3
  end
@@ -1,33 +1,35 @@
1
- module WickedPdfHelper
2
- def self.root_path
3
- String === Rails.root ? Pathname.new(Rails.root) : Rails.root
4
- end
1
+ class WickedPdf
2
+ module WickedPdfHelper
3
+ def self.root_path
4
+ String === Rails.root ? Pathname.new(Rails.root) : Rails.root
5
+ end
5
6
 
6
- def self.add_extension(filename, extension)
7
- filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
8
- end
7
+ def self.add_extension(filename, extension)
8
+ filename.to_s.split('.').include?(extension) ? filename : "#{filename}.#{extension}"
9
+ end
9
10
 
10
- def wicked_pdf_stylesheet_link_tag(*sources)
11
- css_dir = WickedPdfHelper.root_path.join('public', 'stylesheets')
12
- css_text = sources.collect do |source|
13
- source = WickedPdfHelper.add_extension(source, 'css')
14
- "<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
15
- end.join("\n")
16
- css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
17
- end
11
+ def wicked_pdf_stylesheet_link_tag(*sources)
12
+ css_dir = WickedPdfHelper.root_path.join('public', 'stylesheets')
13
+ css_text = sources.collect do |source|
14
+ source = WickedPdfHelper.add_extension(source, 'css')
15
+ "<style type='text/css'>#{File.read(css_dir.join(source))}</style>"
16
+ end.join("\n")
17
+ css_text.respond_to?(:html_safe) ? css_text.html_safe : css_text
18
+ end
18
19
 
19
- def wicked_pdf_image_tag(img, options = {})
20
- image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
21
- end
20
+ def wicked_pdf_image_tag(img, options = {})
21
+ image_tag "file:///#{WickedPdfHelper.root_path.join('public', 'images', img)}", options
22
+ end
22
23
 
23
- def wicked_pdf_javascript_src_tag(jsfile, options = {})
24
- jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
25
- src = "file:///#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}"
26
- content_tag('script', '', { 'type' => Mime::JS, 'src' => path_to_javascript(src) }.merge(options))
27
- end
24
+ def wicked_pdf_javascript_src_tag(jsfile, options = {})
25
+ jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
26
+ src = "file:///#{WickedPdfHelper.root_path.join('public', 'javascripts', jsfile)}"
27
+ content_tag('script', '', { 'type' => Mime::JS, 'src' => path_to_javascript(src) }.merge(options))
28
+ end
28
29
 
29
- def wicked_pdf_javascript_include_tag(*sources)
30
- js_text = sources.collect { |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
31
- js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
30
+ def wicked_pdf_javascript_include_tag(*sources)
31
+ js_text = sources.collect { |source| wicked_pdf_javascript_src_tag(source, {}) }.join("\n")
32
+ js_text.respond_to?(:html_safe) ? js_text.html_safe : js_text
33
+ end
32
34
  end
33
35
  end
@@ -1,120 +1,130 @@
1
1
  require 'open-uri'
2
2
 
3
- module WickedPdfHelper
4
- module Assets
5
- ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
6
-
7
- def wicked_pdf_asset_base64(path)
8
- asset = Rails.application.assets.find_asset(path)
9
- throw "Could not find asset '#{path}'" if asset.nil?
10
- base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
11
- "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
12
- end
13
-
14
- def wicked_pdf_stylesheet_link_tag(*sources)
15
- stylesheet_contents = sources.collect do |source|
16
- source = WickedPdfHelper.add_extension(source, 'css')
17
- "<style type='text/css'>#{read_asset(source)}</style>"
18
- end.join("\n")
19
-
20
- stylesheet_contents.gsub(ASSET_URL_REGEX) do
21
- if Regexp.last_match[1].starts_with?('data:')
22
- "url(#{Regexp.last_match[1]})"
23
- else
24
- "url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
25
- end
26
- end.html_safe
27
- end
3
+ class WickedPdf
4
+ module WickedPdfHelper
5
+ module Assets
6
+ ASSET_URL_REGEX = /url\(['"]?([^'"]+?)['"]?\)/
7
+
8
+ def wicked_pdf_asset_base64(path)
9
+ asset = find_asset(path)
10
+ raise "Could not find asset '#{path}'" if asset.nil?
11
+ base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
12
+ "data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
13
+ end
28
14
 
29
- def wicked_pdf_image_tag(img, options = {})
30
- image_tag wicked_pdf_asset_path(img), options
31
- end
15
+ def wicked_pdf_stylesheet_link_tag(*sources)
16
+ stylesheet_contents = sources.collect do |source|
17
+ source = WickedPdfHelper.add_extension(source, 'css')
18
+ "<style type='text/css'>#{read_asset(source)}</style>"
19
+ end.join("\n")
20
+
21
+ stylesheet_contents.gsub(ASSET_URL_REGEX) do
22
+ if Regexp.last_match[1].starts_with?('data:')
23
+ "url(#{Regexp.last_match[1]})"
24
+ else
25
+ "url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
26
+ end
27
+ end.html_safe
28
+ end
32
29
 
33
- def wicked_pdf_javascript_src_tag(jsfile, options = {})
34
- jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
35
- javascript_include_tag wicked_pdf_asset_path(jsfile), options
36
- end
30
+ def wicked_pdf_image_tag(img, options = {})
31
+ image_tag wicked_pdf_asset_path(img), options
32
+ end
37
33
 
38
- def wicked_pdf_javascript_include_tag(*sources)
39
- sources.collect do |source|
40
- source = WickedPdfHelper.add_extension(source, 'js')
41
- "<script type='text/javascript'>#{read_asset(source)}</script>"
42
- end.join("\n").html_safe
43
- end
34
+ def wicked_pdf_javascript_src_tag(jsfile, options = {})
35
+ jsfile = WickedPdfHelper.add_extension(jsfile, 'js')
36
+ javascript_include_tag wicked_pdf_asset_path(jsfile), options
37
+ end
44
38
 
45
- def wicked_pdf_asset_path(asset)
46
- if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
47
- pathname
48
- else
49
- "file:///#{pathname}"
39
+ def wicked_pdf_javascript_include_tag(*sources)
40
+ sources.collect do |source|
41
+ source = WickedPdfHelper.add_extension(source, 'js')
42
+ "<script type='text/javascript'>#{read_asset(source)}</script>"
43
+ end.join("\n").html_safe
50
44
  end
51
- end
52
45
 
53
- private
46
+ def wicked_pdf_asset_path(asset)
47
+ if (pathname = asset_pathname(asset).to_s) =~ URI_REGEXP
48
+ pathname
49
+ else
50
+ "file:///#{pathname}"
51
+ end
52
+ end
54
53
 
55
- # borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
56
- URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
54
+ private
55
+
56
+ # borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
57
+ URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
58
+
59
+ def asset_pathname(source)
60
+ if precompiled_or_absolute_asset?(source)
61
+ asset = asset_path(source)
62
+ pathname = prepend_protocol(asset)
63
+ if pathname =~ URI_REGEXP
64
+ # asset_path returns an absolute URL using asset_host if asset_host is set
65
+ pathname
66
+ else
67
+ File.join(Rails.public_path, asset.sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
68
+ end
69
+ else
70
+ asset = find_asset(source)
71
+ asset ? asset.pathname : File.join(Rails.public_path, source)
72
+ end
73
+ end
57
74
 
58
- def asset_pathname(source)
59
- if precompiled_or_absolute_asset?(source)
60
- asset = asset_path(source)
61
- pathname = prepend_protocol(asset)
62
- if pathname =~ URI_REGEXP
63
- # asset_path returns an absolute URL using asset_host if asset_host is set
64
- pathname
75
+ def find_asset(path)
76
+ if Rails.application.assets.respond_to?(:find_asset)
77
+ Rails.application.assets.find_asset(path)
65
78
  else
66
- File.join(Rails.public_path, asset.sub(/\A#{Rails.application.config.action_controller.relative_url_root}/, ''))
79
+ Sprockets::Railtie.build_environment(Rails.application).find_asset(path)
67
80
  end
68
- else
69
- asset = Rails.application.assets.find_asset(source)
70
- asset ? asset.pathname : File.join(Rails.public_path, source)
71
81
  end
72
- end
73
82
 
74
- # will prepend a http or default_protocol to a protocol relative URL
75
- # or when no protcol is set.
76
- def prepend_protocol(source)
77
- protocol = WickedPdf.config[:default_protocol] || 'http'
78
- if source[0, 2] == '//'
79
- source = [protocol, ':', source].join
80
- elsif source[0] != '/' && !source[0, 8].include?('://')
81
- source = [protocol, '://', source].join
83
+ # will prepend a http or default_protocol to a protocol relative URL
84
+ # or when no protcol is set.
85
+ def prepend_protocol(source)
86
+ protocol = WickedPdf.config[:default_protocol] || 'http'
87
+ if source[0, 2] == '//'
88
+ source = [protocol, ':', source].join
89
+ elsif source[0] != '/' && !source[0, 8].include?('://')
90
+ source = [protocol, '://', source].join
91
+ end
92
+ source
82
93
  end
83
- source
84
- end
85
94
 
86
- def precompiled_or_absolute_asset?(source)
87
- Rails.configuration.assets.compile == false ||
88
- source.to_s[0] == '/' ||
89
- source.to_s.match(/\Ahttps?\:\/\//)
90
- end
95
+ def precompiled_or_absolute_asset?(source)
96
+ Rails.configuration.assets.compile == false ||
97
+ source.to_s[0] == '/' ||
98
+ source.to_s.match(/\Ahttps?\:\/\//)
99
+ end
91
100
 
92
- def read_asset(source)
93
- if precompiled_or_absolute_asset?(source)
94
- pathname = asset_pathname(source)
95
- if pathname =~ URI_REGEXP
96
- read_from_uri(pathname)
97
- elsif File.file?(pathname)
98
- IO.read(pathname)
101
+ def read_asset(source)
102
+ if precompiled_or_absolute_asset?(source)
103
+ pathname = asset_pathname(source)
104
+ if pathname =~ URI_REGEXP
105
+ read_from_uri(pathname)
106
+ elsif File.file?(pathname)
107
+ IO.read(pathname)
108
+ end
109
+ else
110
+ find_asset(source).to_s
99
111
  end
100
- else
101
- Rails.application.assets.find_asset(source).to_s
102
112
  end
103
- end
104
113
 
105
- def read_from_uri(uri)
106
- encoding = ':UTF-8' if RUBY_VERSION > '1.8'
107
- asset = open(uri, "r#{encoding}", &:read)
108
- asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
109
- asset
110
- end
114
+ def read_from_uri(uri)
115
+ encoding = ':UTF-8' if RUBY_VERSION > '1.8'
116
+ asset = open(uri, "r#{encoding}", &:read)
117
+ asset = gzip(asset) if WickedPdf.config[:expect_gzipped_remote_assets]
118
+ asset
119
+ end
111
120
 
112
- def gzip(asset)
113
- stringified_asset = StringIO.new(asset)
114
- gzipper = Zlib::GzipReader.new(stringified_asset)
115
- gzipper.read
116
- rescue Zlib::GzipFile::Error
117
- nil
121
+ def gzip(asset)
122
+ stringified_asset = StringIO.new(asset)
123
+ gzipper = Zlib::GzipReader.new(stringified_asset)
124
+ gzipper.read
125
+ rescue Zlib::GzipFile::Error
126
+ nil
127
+ end
118
128
  end
119
129
  end
120
130
  end
@@ -0,0 +1 @@
1
+ // Wicked js
@@ -17,11 +17,9 @@ class PdfHelperTest < ActionController::TestCase
17
17
  @ac = nil
18
18
  end
19
19
 
20
- if Rails::VERSION::MAJOR == 2
21
- test 'should prerender header and footer :template options' do
22
- options = @ac.send(:prerender_header_and_footer,
23
- :header => { :html => { :template => 'hf.html.erb' } })
24
- assert_match %r(^file:\/\/\/.*wicked_header_pdf.*\.html), options[:header][:html][:url]
25
- end
20
+ test 'should prerender header and footer :template options' do
21
+ options = @ac.send(:prerender_header_and_footer,
22
+ :header => { :html => { :template => 'hf.html.erb' } })
23
+ assert_match %r{^file:\/\/\/.*wicked_header_pdf.*\.html}, options[:header][:html][:url]
26
24
  end
27
25
  end
@@ -2,11 +2,29 @@ require 'test_helper'
2
2
  require 'action_view/test_case'
3
3
 
4
4
  class WickedPdfHelperAssetsTest < ActionView::TestCase
5
- include WickedPdfHelper::Assets
5
+ include WickedPdf::WickedPdfHelper::Assets
6
6
 
7
7
  if Rails::VERSION::MAJOR > 3 || (Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR > 0)
8
8
  test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
9
- assert_match %r(data:text\/css;base64,.+), wicked_pdf_asset_base64('wicked.css')
9
+ assert_match %r{data:text\/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
10
+ end
11
+
12
+ test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
13
+ Rails.configuration.assets.expects(:compile => true)
14
+ assert_equal "<style type='text/css'>/* Wicked styles */\n\n</style>",
15
+ wicked_pdf_stylesheet_link_tag('wicked')
16
+ end
17
+
18
+ test 'wicked_pdf_image_tag should return the same as image_tag when passed a full path' do
19
+ Rails.configuration.assets.expects(:compile => true)
20
+ assert_equal image_tag("file:///#{Rails.root.join('public', 'pdf')}"),
21
+ wicked_pdf_image_tag('pdf')
22
+ end
23
+
24
+ test 'wicked_pdf_javascript_include_tag should inline the javascripts passed in' do
25
+ Rails.configuration.assets.expects(:compile => true)
26
+ assert_equal "<script type='text/javascript'>// Wicked js\n;\n</script>",
27
+ wicked_pdf_javascript_include_tag('wicked')
10
28
  end
11
29
 
12
30
  test 'wicked_pdf_asset_path should return a url when assets are served by an asset server' do
@@ -86,13 +104,13 @@ class WickedPdfHelperAssetsTest < ActionView::TestCase
86
104
  end
87
105
 
88
106
  test 'WickedPdfHelper::Assets::ASSET_URL_REGEX should match various URL data type formats' do
89
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
90
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
91
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
92
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
93
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
94
- assert_match WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
95
- assert_no_match WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
107
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'/asset/stylesheets/application.css\');'
108
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("/asset/stylesheets/application.css");'
109
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(/asset/stylesheets/application.css);'
110
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(\'http://assets.domain.com/dummy.png\');'
111
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url("http://assets.domain.com/dummy.png");'
112
+ assert_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, 'url(http://assets.domain.com/dummy.png);'
113
+ assert_no_match WickedPdf::WickedPdfHelper::Assets::ASSET_URL_REGEX, '.url { \'http://assets.domain.com/dummy.png\' }'
96
114
  end
97
115
 
98
116
  test 'prepend_protocol should properly set the protocol when the asset is precompiled' do