adzap-wicked_pdf 2.0.0 → 2.0.1
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.
- checksums.yaml +4 -4
- data/lib/wicked_pdf/asset_cache.rb +33 -0
- data/lib/wicked_pdf/asset_helper.rb +31 -20
- data/lib/wicked_pdf/configuration.rb +3 -1
- data/lib/wicked_pdf/version.rb +1 -1
- data/lib/wicked_pdf.rb +6 -0
- data/test/functional/wicked_pdf_asset_helper_test.rb +60 -0
- data/test/unit/wicked_pdf_asset_cache_test.rb +51 -0
- data/test/unit/wicked_pdf_configuration_test.rb +11 -0
- metadata +5 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ee59b976a1f8a763eb2a754baf8727f970fe5f06c88194b42735d5c37e3ac6b7
|
|
4
|
+
data.tar.gz: 543153109daf65bcbe75959aecf5416689fd54481dd31b08bf068322de805551
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: bc98c1fbb159ec1c228f320d8d79d805d8c9e85927fd3f6b4f96b7d0927c05da270a55d3e87f348a4c7602f350ce855761c3a65f671d133b7bab6e7712cf4296
|
|
7
|
+
data.tar.gz: 237bef4134c63adb17a206b197888e7a6f8845a7fa4f637454420ab91590c40706766a3d14f1d50f60b3c4df168158751716258b8865ae80c639c4afb75e128c
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
module WickedPdf
|
|
2
|
+
# Process-wide memo for the output of the inlining asset helpers
|
|
3
|
+
# (wicked_pdf_asset_base64, wicked_pdf_stylesheet_link_tag,
|
|
4
|
+
# wicked_pdf_javascript_include_tag).
|
|
5
|
+
#
|
|
6
|
+
# Reading, concatenating and base64-encoding assets is deterministic for a
|
|
7
|
+
# given deployment, but the helpers are invoked once per rendered document —
|
|
8
|
+
# expensive when many documents are rendered in one request (e.g. printing a
|
|
9
|
+
# batch). Enable with `config.cache_assets = true`; disabled by default so
|
|
10
|
+
# environments that recompile assets on change (development) stay live.
|
|
11
|
+
#
|
|
12
|
+
# Cached values are frozen so a stale reference can't be mutated in place.
|
|
13
|
+
class AssetCache
|
|
14
|
+
def initialize
|
|
15
|
+
@store = {}
|
|
16
|
+
@mutex = Mutex.new
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def fetch(key)
|
|
20
|
+
@mutex.synchronize do
|
|
21
|
+
@store.fetch(key) { @store[key] = yield.freeze }
|
|
22
|
+
end
|
|
23
|
+
end
|
|
24
|
+
|
|
25
|
+
def clear
|
|
26
|
+
@mutex.synchronize { @store.clear }
|
|
27
|
+
end
|
|
28
|
+
|
|
29
|
+
def size
|
|
30
|
+
@mutex.synchronize { @store.size }
|
|
31
|
+
end
|
|
32
|
+
end
|
|
33
|
+
end
|
|
@@ -13,26 +13,30 @@ module WickedPdf
|
|
|
13
13
|
end
|
|
14
14
|
|
|
15
15
|
def wicked_pdf_asset_base64(path)
|
|
16
|
-
|
|
17
|
-
|
|
16
|
+
wicked_pdf_asset_cache(:base64, path) do
|
|
17
|
+
asset = find_asset(path)
|
|
18
|
+
raise "Could not find asset '#{path}'" if asset.nil?
|
|
18
19
|
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
base64 = Base64.encode64(asset.to_s).gsub(/\s+/, '')
|
|
21
|
+
"data:#{asset.content_type};base64,#{Rack::Utils.escape(base64)}"
|
|
22
|
+
end
|
|
21
23
|
end
|
|
22
24
|
|
|
23
25
|
def wicked_pdf_stylesheet_link_tag(*sources)
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
26
|
+
wicked_pdf_asset_cache(:stylesheet, *sources) do
|
|
27
|
+
stylesheet_contents = sources.collect do |source|
|
|
28
|
+
source = WickedPdf::AssetHelper.add_extension(source, 'css')
|
|
29
|
+
"<style type='text/css'>#{read_asset(source)}</style>"
|
|
30
|
+
end.join("\n")
|
|
31
|
+
|
|
32
|
+
stylesheet_contents.gsub(ASSET_URL_REGEX) do
|
|
33
|
+
if Regexp.last_match[1].starts_with?('data:')
|
|
34
|
+
"url(#{Regexp.last_match[1]})"
|
|
35
|
+
else
|
|
36
|
+
"url(#{wicked_pdf_asset_path(Regexp.last_match[1])})"
|
|
37
|
+
end
|
|
38
|
+
end.html_safe
|
|
39
|
+
end
|
|
36
40
|
end
|
|
37
41
|
|
|
38
42
|
def wicked_pdf_image_tag(img, options = {})
|
|
@@ -45,10 +49,12 @@ module WickedPdf
|
|
|
45
49
|
end
|
|
46
50
|
|
|
47
51
|
def wicked_pdf_javascript_include_tag(*sources)
|
|
48
|
-
sources
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
+
wicked_pdf_asset_cache(:javascript, *sources) do
|
|
53
|
+
sources.collect do |source|
|
|
54
|
+
source = WickedPdf::AssetHelper.add_extension(source, 'js')
|
|
55
|
+
"<script type='text/javascript'>#{read_asset(source)}</script>"
|
|
56
|
+
end.join("\n").html_safe
|
|
57
|
+
end
|
|
52
58
|
end
|
|
53
59
|
|
|
54
60
|
def wicked_pdf_asset_path(asset)
|
|
@@ -64,6 +70,11 @@ module WickedPdf
|
|
|
64
70
|
# borrowed from actionpack/lib/action_view/helpers/asset_url_helper.rb
|
|
65
71
|
URI_REGEXP = %r{^[-a-z]+://|^(?:cid|data):|^//}
|
|
66
72
|
|
|
73
|
+
# Inlined asset output is deterministic per deployment; memoized process-wide when config.cache_assets is on.
|
|
74
|
+
def wicked_pdf_asset_cache(*key, &block)
|
|
75
|
+
WickedPdf.config.cache_assets ? WickedPdf.asset_cache.fetch(key, &block) : yield
|
|
76
|
+
end
|
|
77
|
+
|
|
67
78
|
def asset_pathname(source)
|
|
68
79
|
if precompiled_or_absolute_asset?(source)
|
|
69
80
|
asset = asset_path(source)
|
|
@@ -16,7 +16,8 @@ module WickedPdf
|
|
|
16
16
|
:root_url,
|
|
17
17
|
:default_protocol,
|
|
18
18
|
:expect_gzipped_remote_assets,
|
|
19
|
-
:enable_local_file_access
|
|
19
|
+
:enable_local_file_access,
|
|
20
|
+
:cache_assets
|
|
20
21
|
].freeze
|
|
21
22
|
|
|
22
23
|
attr_accessor(*SETTINGS)
|
|
@@ -31,6 +32,7 @@ module WickedPdf
|
|
|
31
32
|
def initialize
|
|
32
33
|
@basic_auth = false
|
|
33
34
|
@enable_local_file_access = true
|
|
35
|
+
@cache_assets = false
|
|
34
36
|
@default_options = {}
|
|
35
37
|
end
|
|
36
38
|
|
data/lib/wicked_pdf/version.rb
CHANGED
data/lib/wicked_pdf.rb
CHANGED
|
@@ -10,6 +10,7 @@ require 'active_support/core_ext/object/blank'
|
|
|
10
10
|
|
|
11
11
|
require 'wicked_pdf/version'
|
|
12
12
|
require 'wicked_pdf/configuration'
|
|
13
|
+
require 'wicked_pdf/asset_cache'
|
|
13
14
|
require 'wicked_pdf/tempfile'
|
|
14
15
|
require 'wicked_pdf/binary'
|
|
15
16
|
require 'wicked_pdf/option_parser'
|
|
@@ -41,6 +42,11 @@ module WickedPdf
|
|
|
41
42
|
def configure
|
|
42
43
|
yield config
|
|
43
44
|
end
|
|
45
|
+
|
|
46
|
+
# Shared store used by the asset helpers when config.cache_assets is on.
|
|
47
|
+
def asset_cache
|
|
48
|
+
@asset_cache ||= AssetCache.new
|
|
49
|
+
end
|
|
44
50
|
end
|
|
45
51
|
|
|
46
52
|
def self.new(command = WickedPdf::Command.new)
|
|
@@ -4,10 +4,70 @@ require 'action_view/test_case'
|
|
|
4
4
|
class WickedPdfAssetHelperTest < ActionView::TestCase
|
|
5
5
|
include WickedPdf::AssetHelper
|
|
6
6
|
|
|
7
|
+
teardown do
|
|
8
|
+
WickedPdf.config.cache_assets = false
|
|
9
|
+
WickedPdf.asset_cache.clear
|
|
10
|
+
end
|
|
11
|
+
|
|
7
12
|
test 'wicked_pdf_asset_base64 returns a base64 encoded asset' do
|
|
8
13
|
assert_match %r{data:text/css;base64,.+}, wicked_pdf_asset_base64('wicked.css')
|
|
9
14
|
end
|
|
10
15
|
|
|
16
|
+
test 'inlining helpers recompute on every call by default' do
|
|
17
|
+
asset = stub(:to_s => 'body { color: red; }', :content_type => 'text/css')
|
|
18
|
+
expects(:find_asset).with('wicked.css').twice.returns(asset)
|
|
19
|
+
|
|
20
|
+
2.times { wicked_pdf_asset_base64('wicked.css') }
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
test 'wicked_pdf_asset_base64 memoizes when cache_assets is enabled' do
|
|
24
|
+
WickedPdf.config.cache_assets = true
|
|
25
|
+
asset = stub(:to_s => 'body { color: red; }', :content_type => 'text/css')
|
|
26
|
+
expects(:find_asset).with('wicked.css').once.returns(asset)
|
|
27
|
+
|
|
28
|
+
first = wicked_pdf_asset_base64('wicked.css')
|
|
29
|
+
|
|
30
|
+
assert_equal first, wicked_pdf_asset_base64('wicked.css')
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
test 'wicked_pdf_stylesheet_link_tag memoizes when cache_assets is enabled' do
|
|
34
|
+
WickedPdf.config.cache_assets = true
|
|
35
|
+
Rails.configuration.assets.stubs(:compile => true)
|
|
36
|
+
expects(:read_asset).with('wicked.css').once.returns('/* Wicked styles */')
|
|
37
|
+
|
|
38
|
+
first = wicked_pdf_stylesheet_link_tag('wicked')
|
|
39
|
+
|
|
40
|
+
assert_equal first, wicked_pdf_stylesheet_link_tag('wicked')
|
|
41
|
+
assert_predicate wicked_pdf_stylesheet_link_tag('wicked'), :html_safe?
|
|
42
|
+
end
|
|
43
|
+
|
|
44
|
+
test 'wicked_pdf_javascript_include_tag memoizes when cache_assets is enabled' do
|
|
45
|
+
WickedPdf.config.cache_assets = true
|
|
46
|
+
expects(:read_asset).with('wicked.js').once.returns('// Wicked js')
|
|
47
|
+
|
|
48
|
+
first = wicked_pdf_javascript_include_tag('wicked')
|
|
49
|
+
|
|
50
|
+
assert_equal first, wicked_pdf_javascript_include_tag('wicked')
|
|
51
|
+
end
|
|
52
|
+
|
|
53
|
+
test 'cached helpers key by helper and sources so results do not collide' do
|
|
54
|
+
WickedPdf.config.cache_assets = true
|
|
55
|
+
stubs(:read_asset).returns('content')
|
|
56
|
+
|
|
57
|
+
refute_equal wicked_pdf_stylesheet_link_tag('wicked'), wicked_pdf_javascript_include_tag('wicked')
|
|
58
|
+
refute_equal wicked_pdf_javascript_include_tag('wicked'), wicked_pdf_javascript_include_tag('wicked', 'other')
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
test 'clearing the asset cache forces recomputation' do
|
|
62
|
+
WickedPdf.config.cache_assets = true
|
|
63
|
+
asset = stub(:to_s => 'x', :content_type => 'text/css')
|
|
64
|
+
expects(:find_asset).with('wicked.css').twice.returns(asset)
|
|
65
|
+
|
|
66
|
+
wicked_pdf_asset_base64('wicked.css')
|
|
67
|
+
WickedPdf.asset_cache.clear
|
|
68
|
+
wicked_pdf_asset_base64('wicked.css')
|
|
69
|
+
end
|
|
70
|
+
|
|
11
71
|
test 'wicked_pdf_stylesheet_link_tag should inline the stylesheets passed in' do
|
|
12
72
|
Rails.configuration.assets.expects(:compile => true)
|
|
13
73
|
# Sprockets versions differ in trailing whitespace when concatenating assets
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
require 'test_helper'
|
|
2
|
+
|
|
3
|
+
class WickedPdfAssetCacheTest < ActiveSupport::TestCase
|
|
4
|
+
setup do
|
|
5
|
+
@cache = WickedPdf::AssetCache.new
|
|
6
|
+
end
|
|
7
|
+
|
|
8
|
+
test 'fetch computes the value once and memoizes it' do
|
|
9
|
+
computations = 0
|
|
10
|
+
2.times do
|
|
11
|
+
@cache.fetch([:base64, 'font.ttf']) do
|
|
12
|
+
computations += 1
|
|
13
|
+
'data:...'
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
assert_equal 1, computations
|
|
18
|
+
assert_equal 1, @cache.size
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
test 'fetch returns the cached value on subsequent calls' do
|
|
22
|
+
@cache.fetch([:stylesheet, 'print']) { '<style>a</style>' }
|
|
23
|
+
|
|
24
|
+
assert_equal '<style>a</style>', @cache.fetch([:stylesheet, 'print']) { flunk 'should not recompute' }
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
test 'fetch freezes cached values' do
|
|
28
|
+
assert_predicate @cache.fetch([:javascript, 'app']) { +'<script></script>' }, :frozen?
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
test 'distinct keys are cached independently' do
|
|
32
|
+
@cache.fetch([:base64, 'a.ttf']) { 'a' }
|
|
33
|
+
@cache.fetch([:base64, 'b.ttf']) { 'b' }
|
|
34
|
+
|
|
35
|
+
assert_equal 2, @cache.size
|
|
36
|
+
assert_equal 'a', @cache.fetch([:base64, 'a.ttf']) { 'wrong' }
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
test 'clear empties the store so values are recomputed' do
|
|
40
|
+
@cache.fetch([:base64, 'font.ttf']) { 'stale' }
|
|
41
|
+
@cache.clear
|
|
42
|
+
|
|
43
|
+
assert_equal 0, @cache.size
|
|
44
|
+
assert_equal 'fresh', @cache.fetch([:base64, 'font.ttf']) { 'fresh' }
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
test 'WickedPdf.asset_cache returns a shared instance' do
|
|
48
|
+
assert_same WickedPdf.asset_cache, WickedPdf.asset_cache
|
|
49
|
+
assert_kind_of WickedPdf::AssetCache, WickedPdf.asset_cache
|
|
50
|
+
end
|
|
51
|
+
end
|
|
@@ -15,6 +15,17 @@ class WickedPdfConfigurationTest < ActiveSupport::TestCase
|
|
|
15
15
|
assert_kind_of WickedPdf::Configuration, WickedPdf.config
|
|
16
16
|
end
|
|
17
17
|
|
|
18
|
+
test 'cache_assets is a typed setting defaulting to false' do
|
|
19
|
+
config = WickedPdf::Configuration.new
|
|
20
|
+
|
|
21
|
+
assert_equal false, config.cache_assets
|
|
22
|
+
|
|
23
|
+
config[:cache_assets] = true
|
|
24
|
+
|
|
25
|
+
assert_equal true, config.cache_assets
|
|
26
|
+
assert_empty config.default_options
|
|
27
|
+
end
|
|
28
|
+
|
|
18
29
|
test 'assigning a Hash routes settings to accessors and unknown keys to default_options' do
|
|
19
30
|
WickedPdf.config = {
|
|
20
31
|
:exe_path => '/usr/bin/wkhtmltopdf',
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: adzap-wicked_pdf
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0.
|
|
4
|
+
version: 2.0.1
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Miles Z. Sterrett
|
|
@@ -9,7 +9,7 @@ authors:
|
|
|
9
9
|
- Adam Meehan
|
|
10
10
|
bindir: bin
|
|
11
11
|
cert_chain: []
|
|
12
|
-
date: 2026-08-
|
|
12
|
+
date: 2026-08-12 00:00:00.000000000 Z
|
|
13
13
|
dependencies:
|
|
14
14
|
- !ruby/object:Gem::Dependency
|
|
15
15
|
name: activesupport
|
|
@@ -155,6 +155,7 @@ files:
|
|
|
155
155
|
- init.rb
|
|
156
156
|
- lib/generators/wicked_pdf_generator.rb
|
|
157
157
|
- lib/wicked_pdf.rb
|
|
158
|
+
- lib/wicked_pdf/asset_cache.rb
|
|
158
159
|
- lib/wicked_pdf/asset_helper.rb
|
|
159
160
|
- lib/wicked_pdf/binary.rb
|
|
160
161
|
- lib/wicked_pdf/command.rb
|
|
@@ -185,6 +186,7 @@ files:
|
|
|
185
186
|
- test/functional/pdf_helper_test.rb
|
|
186
187
|
- test/functional/wicked_pdf_asset_helper_test.rb
|
|
187
188
|
- test/test_helper.rb
|
|
189
|
+
- test/unit/wicked_pdf_asset_cache_test.rb
|
|
188
190
|
- test/unit/wicked_pdf_binary_test.rb
|
|
189
191
|
- test/unit/wicked_pdf_command_test.rb
|
|
190
192
|
- test/unit/wicked_pdf_configuration_test.rb
|
|
@@ -233,6 +235,7 @@ test_files:
|
|
|
233
235
|
- test/functional/pdf_helper_test.rb
|
|
234
236
|
- test/functional/wicked_pdf_asset_helper_test.rb
|
|
235
237
|
- test/test_helper.rb
|
|
238
|
+
- test/unit/wicked_pdf_asset_cache_test.rb
|
|
236
239
|
- test/unit/wicked_pdf_binary_test.rb
|
|
237
240
|
- test/unit/wicked_pdf_command_test.rb
|
|
238
241
|
- test/unit/wicked_pdf_configuration_test.rb
|