grover 0.11.2 → 0.11.3

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 49891589eac94bdb0602aefdeaf488bd7df1c40c69c75da1eee473016cb6eefc
4
- data.tar.gz: 14b8fd25ee3eafaa76f5b5da6f0591c9cfe8af077ff042f6e2b804e1c12b7e70
3
+ metadata.gz: 12e04baf78ab0c0339d181c17bdb5c4d9678633c81497e4764cbe9db95b23d1d
4
+ data.tar.gz: a8333251651e40035fef07784c45e4a9f4e8ae7e2d4c13ac6c7fa98b3bf0667e
5
5
  SHA512:
6
- metadata.gz: e140285264c56edc46390938f72ff0cd402285a81040c011fac94a2b019753357b238b7a0bc869c0d1e3e741857f21bb5446b51de1fe5f920aba722f23ecb3bc
7
- data.tar.gz: 6db55a620a3099e945d31fe0da78ca4d73396c2d2544e0985d836595f4d63620e9856388fa8525b328322125b820738a76931cd348d562974f3e0e1fa82c3b3c
6
+ metadata.gz: 05e07a53425a21e8e17026e75348cfd8ccbe0a6d8daba269a01d5d825a632fb8b260c6529c74eb023babefd8c8e8cce4105e915ddca1084c2c7a1e03ca492747
7
+ data.tar.gz: 5d732c36ff47b07abe30f32dd2544f264d0e68c09789b4c00da06af9dbbc0c8576aef25cc245d7fc606b358446169e985bde9f710499b1379019bbefc850a1be
@@ -8,6 +8,7 @@ require 'active_support_ext/object/deep_dup' unless defined?(ActiveSupport)
8
8
  require 'grover/html_preprocessor'
9
9
  require 'grover/middleware'
10
10
  require 'grover/configuration'
11
+ require 'grover/options_builder'
11
12
 
12
13
  require 'nokogiri'
13
14
  require 'schmooze'
@@ -151,8 +152,7 @@ class Grover
151
152
  #
152
153
  def initialize(url, options = {})
153
154
  @url = url
154
- @options = combine_options options
155
-
155
+ @options = OptionsBuilder.new(options, url)
156
156
  @root_path = @options.delete 'root_path'
157
157
  @front_cover_path = @options.delete 'front_cover_path'
158
158
  @back_cover_path = @options.delete 'back_cover_path'
@@ -258,73 +258,6 @@ class Grover
258
258
  Processor.new(root_path)
259
259
  end
260
260
 
261
- def combine_options(options)
262
- combined = Utils.deep_stringify_keys Grover.configuration.options
263
- Utils.deep_merge! combined, Utils.deep_stringify_keys(options)
264
- Utils.deep_merge! combined, meta_options unless url_source?
265
-
266
- fix_boolean_options! combined
267
- fix_integer_options! combined
268
- fix_float_options! combined
269
- fix_array_options! combined
270
-
271
- combined
272
- end
273
-
274
- #
275
- # Extract out options from meta tags in the source - based on code from PDFKit project
276
- #
277
- def meta_options
278
- meta_opts = {}
279
-
280
- meta_tags.each do |meta|
281
- tag_name = meta['name'] && meta['name'][/#{Grover.configuration.meta_tag_prefix}([a-z_-]+)/, 1]
282
- next unless tag_name
283
-
284
- Utils.deep_assign meta_opts, tag_name.split('-'), meta['content']
285
- end
286
-
287
- meta_opts
288
- end
289
-
290
- def meta_tags
291
- Nokogiri::HTML(@url).xpath('//meta')
292
- end
293
-
294
- def url_source?
295
- @url.match(/\Ahttp/i)
296
- end
297
-
298
- def fix_boolean_options!(options)
299
- %w[display_header_footer print_background landscape prefer_css_page_size].each do |opt|
300
- next unless options.key? opt
301
-
302
- options[opt] = !FALSE_VALUES.include?(options[opt])
303
- end
304
- end
305
-
306
- FALSE_VALUES = [nil, false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze
307
-
308
- def fix_integer_options!(options)
309
- ['viewport.width', 'viewport.height'].each do |opt|
310
- keys = opt.split('.')
311
- Utils.deep_assign(options, keys, options.dig(*keys).to_i) if options.dig(*keys)
312
- end
313
- end
314
-
315
- def fix_float_options!(options)
316
- ['viewport.device_scale_factor', 'scale'].each do |opt|
317
- keys = opt.split('.')
318
- Utils.deep_assign(options, keys, options.dig(*keys).to_f) if options.dig(*keys)
319
- end
320
- end
321
-
322
- def fix_array_options!(options)
323
- return unless options['launch_args'].is_a? String
324
-
325
- options['launch_args'] = YAML.safe_load options['launch_args']
326
- end
327
-
328
261
  def normalized_options(path:)
329
262
  normalized_options = Utils.normalize_object @options
330
263
  normalized_options['path'] = path if path.is_a? ::String
@@ -0,0 +1,50 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grover/utils'
4
+ require 'grover/options_fixer'
5
+
6
+ class Grover
7
+ #
8
+ # Build options from Grover.configuration, meta_options, and passed-in options
9
+ #
10
+ class OptionsBuilder < Hash
11
+ def initialize(options, url)
12
+ @url = url
13
+ combined = grover_configuration
14
+ Utils.deep_merge! combined, Utils.deep_stringify_keys(options)
15
+ Utils.deep_merge! combined, meta_options unless url_source?
16
+
17
+ update OptionsFixer.new(combined).run
18
+ end
19
+
20
+ private
21
+
22
+ def grover_configuration
23
+ Utils.deep_stringify_keys Grover.configuration.options
24
+ end
25
+
26
+ #
27
+ # Extract out options from meta tags in the source - based on code from PDFKit project
28
+ #
29
+ def meta_options
30
+ meta_opts = {}
31
+
32
+ meta_tags.each do |meta|
33
+ tag_name = meta['name'] && meta['name'][/#{Grover.configuration.meta_tag_prefix}([a-z_-]+)/, 1]
34
+ next unless tag_name
35
+
36
+ Utils.deep_assign meta_opts, tag_name.split('-'), meta['content']
37
+ end
38
+
39
+ meta_opts
40
+ end
41
+
42
+ def meta_tags
43
+ Nokogiri::HTML(@url).xpath('//meta')
44
+ end
45
+
46
+ def url_source?
47
+ @url.match(/\Ahttp/i)
48
+ end
49
+ end
50
+ end
@@ -0,0 +1,61 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'grover/utils'
4
+
5
+ class Grover
6
+ #
7
+ # Convert string option values to boolean, numeric, and array literals
8
+ #
9
+ class OptionsFixer
10
+ FALSE_VALUES = [nil, false, 0, '0', 'f', 'F', 'false', 'FALSE', 'off', 'OFF'].freeze
11
+
12
+ def initialize(options)
13
+ @options = options
14
+ end
15
+
16
+ def run
17
+ fix_boolean_options!
18
+ fix_integer_options!
19
+ fix_float_options!
20
+ fix_array_options!
21
+ @options
22
+ end
23
+
24
+ private
25
+
26
+ def fix_options!(*option_paths)
27
+ option_paths.each do |option_path|
28
+ keys = option_path.split '.'
29
+ value = @options.dig(*keys)
30
+ Utils.deep_assign(@options, keys, yield(value)) if value
31
+ end
32
+ end
33
+
34
+ def fix_boolean_options!
35
+ fix_options!(
36
+ 'display_header_footer', 'full_page', 'landscape', 'omit_background', 'prefer_css_page_size',
37
+ 'print_background', 'viewport.has_touch', 'viewport.is_landscape', 'viewport.is_mobile'
38
+ ) { |value| !FALSE_VALUES.include?(value) }
39
+ end
40
+
41
+ def fix_integer_options!
42
+ fix_options!(
43
+ 'viewport.height', 'viewport.width',
44
+ &:to_i
45
+ )
46
+ end
47
+
48
+ def fix_float_options!
49
+ fix_options!(
50
+ 'clip.height', 'clip.width', 'clip.x', 'clip.y', 'quality', 'scale', 'viewport.device_scale_factor',
51
+ &:to_f
52
+ )
53
+ end
54
+
55
+ def fix_array_options!
56
+ fix_options!('launch_args') do |value|
57
+ value.is_a?(String) ? YAML.safe_load(value) : value
58
+ end
59
+ end
60
+ end
61
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  class Grover
4
- VERSION = '0.11.2'
4
+ VERSION = '0.11.3'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grover
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.11.2
4
+ version: 0.11.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2020-02-16 00:00:00.000000000 Z
11
+ date: 2020-02-26 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: combine_pdf
@@ -157,6 +157,9 @@ dependencies:
157
157
  - - "~>"
158
158
  - !ruby/object:Gem::Version
159
159
  version: '0.17'
160
+ - - "<"
161
+ - !ruby/object:Gem::Version
162
+ version: '0.18'
160
163
  type: :development
161
164
  prerelease: false
162
165
  version_requirements: !ruby/object:Gem::Requirement
@@ -164,6 +167,9 @@ dependencies:
164
167
  - - "~>"
165
168
  - !ruby/object:Gem::Version
166
169
  version: '0.17'
170
+ - - "<"
171
+ - !ruby/object:Gem::Version
172
+ version: '0.18'
167
173
  description: Transform HTML into PDF/PNG/JPEG using Google Puppeteer/Chromium
168
174
  email:
169
175
  - abromwich@studiosity.com
@@ -177,6 +183,8 @@ files:
177
183
  - lib/grover/configuration.rb
178
184
  - lib/grover/html_preprocessor.rb
179
185
  - lib/grover/middleware.rb
186
+ - lib/grover/options_builder.rb
187
+ - lib/grover/options_fixer.rb
180
188
  - lib/grover/utils.rb
181
189
  - lib/grover/version.rb
182
190
  homepage: https://github.com/Studiosity/grover
@@ -198,8 +206,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
198
206
  - !ruby/object:Gem::Version
199
207
  version: '0'
200
208
  requirements: []
201
- rubyforge_project:
202
- rubygems_version: 2.7.6.2
209
+ rubygems_version: 3.0.6
203
210
  signing_key:
204
211
  specification_version: 4
205
212
  summary: A Ruby gem to transform HTML into PDF, PNG or JPEG by wrapping the NodeJS