grover 0.3.0 → 0.3.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0f700a9f110114c8de7c64f68ada3c60b183e002
4
- data.tar.gz: 10038b11d2dc0e3a7deb9c8be59c17ce9fe75f5a
3
+ metadata.gz: c7d5ea905a539c2326c8f58e7bade296648fa300
4
+ data.tar.gz: c87e18907dab9181629998302b3e88b90c064f58
5
5
  SHA512:
6
- metadata.gz: '08d9ea6100709712c0ac3a3adaf6c00c80ae45fca0fe38176d808e4f4c89efe8b900128d750056ca9b4a7db73dae1f099cec4d5f97851501459728480d181ab4'
7
- data.tar.gz: df95c29b666dd3eb53bbb5c23acfd328f3750cdd2931dd08a9432805ec4089a9f13f17303b7f3a1cffe7655179f1ea97fb16d270f51287736692dc38fc085227
6
+ metadata.gz: 5490e256b29f0684a5c41ea0cdb193347ef9b01eb69e35d52d03302a6d49b4fb9aeb3e3dd7ea77b69bff789fa1491f0c71a6db08bc72b189a7b72daf6cb92a79
7
+ data.tar.gz: fe20dafde8aa7987f09e60691d9dcfafab7af3366ea6e0892971a4e9b3f29b1d8d9cb37ef04f3fd33d152ad40b1771af243e08000c3181a2120d7f35648dc672
data/lib/grover.rb CHANGED
@@ -32,6 +32,12 @@ class Grover
32
32
  } else {
33
33
  await page.goto(`data:text/html,${url}`, { waitUntil: 'networkidle0' });
34
34
  }
35
+
36
+ const emulateMedia = options.emulateMedia; delete options.emulateMedia;
37
+ if (emulateMedia) {
38
+ await page.emulateMedia(emulateMedia);
39
+ }
40
+
35
41
  return await page.pdf(options);
36
42
  } finally {
37
43
  if (browser) {
@@ -98,9 +104,30 @@ class Grover
98
104
  Processor.new(root_path)
99
105
  end
100
106
 
107
+ def options_with_template_fix
108
+ options = @options.dup
109
+ display_url = options.delete :display_url
110
+ if display_url
111
+ options[:footer_template] ||= DEFAULT_FOOTER_TEMPLATE
112
+
113
+ %i[header_template footer_template].each do |key|
114
+ next unless options[key].is_a? String
115
+ options[key] = options[key].gsub(DISPLAY_URL_PLACEHOLDER, display_url)
116
+ end
117
+ end
118
+ options
119
+ end
120
+
101
121
  def normalized_options(path)
102
- options = Utils.normalize_object @options
122
+ options = Utils.normalize_object options_with_template_fix
103
123
  options['path'] = path if path
104
124
  options
105
125
  end
126
+
127
+ DISPLAY_URL_PLACEHOLDER = '{{display_url}}'.freeze
128
+
129
+ DEFAULT_FOOTER_TEMPLATE = Utils.strip_heredoc(<<-HTML).freeze
130
+ <div class='text left grow'>#{DISPLAY_URL_PLACEHOLDER}</div>
131
+ <div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
132
+ HTML
106
133
  end
@@ -47,8 +47,8 @@ class Grover
47
47
  body = response.respond_to?(:body) ? response.body : response.join
48
48
  body = body.join if body.is_a?(Array)
49
49
 
50
- body = HTMLPreprocessor.process body, request_url, protocol
51
- Grover.new(body).to_pdf
50
+ body = HTMLPreprocessor.process body, root_url, protocol
51
+ Grover.new(body, display_url: request_url).to_pdf
52
52
  end
53
53
 
54
54
  def update_headers(headers, body)
@@ -63,11 +63,8 @@ class Grover
63
63
  def configure_env_for_pdf_request(env)
64
64
  @render_pdf = true
65
65
 
66
- path = @request.path.sub(PDF_REGEX, '')
67
- path = path.sub(@request.script_name, '')
68
-
69
- %w[PATH_INFO REQUEST_URI].each { |e| env[e] = path }
70
-
66
+ env['PATH_INFO'] = path_without_extension
67
+ env['REQUEST_URI'] = path_without_extension
71
68
  env['HTTP_ACCEPT'] = concat(env['HTTP_ACCEPT'], Rack::Mime.mime_type('.html'))
72
69
  end
73
70
 
@@ -75,14 +72,24 @@ class Grover
75
72
  (accepts || '').split(',').unshift(type).compact.join(',')
76
73
  end
77
74
 
78
- def request_url
79
- "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
75
+ def root_url
76
+ @root_url ||= "#{env['rack.url_scheme']}://#{env['HTTP_HOST']}/"
80
77
  end
81
78
 
82
79
  def protocol
83
80
  env['rack.url_scheme']
84
81
  end
85
82
 
83
+ def path_without_extension
84
+ return @path_without_extension if defined? @path_without_extension
85
+ path = @request.path.sub(PDF_REGEX, '')
86
+ @path_without_extension = path.sub(@request.script_name, '')
87
+ end
88
+
89
+ def request_url
90
+ "#{root_url.sub(%r{/\z}, '')}#{path_without_extension}"
91
+ end
92
+
86
93
  def env
87
94
  @request.env
88
95
  end
data/lib/grover/utils.rb CHANGED
@@ -20,6 +20,16 @@ class Grover
20
20
  gsub(/[[:space:]]+/, ' ')
21
21
  end
22
22
 
23
+ #
24
+ # Remove minimum spaces from the front of all lines within a string
25
+ #
26
+ # Based on active_support/core_ext/string/strip.rb
27
+ #
28
+ def self.strip_heredoc(string, inline: false)
29
+ string = string.gsub(/^#{string.scan(/^[ \t]*(?=\S)/).min}/, ''.freeze)
30
+ inline ? string.delete("\n") : string
31
+ end
32
+
23
33
  #
24
34
  # Recursively normalizes hash objects with camelized string keys
25
35
  #
@@ -1,3 +1,3 @@
1
1
  class Grover
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '0.3.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grover
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.3.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Bromwich
@@ -25,33 +25,33 @@ dependencies:
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0.2'
27
27
  - !ruby/object:Gem::Dependency
28
- name: activesupport
28
+ name: bundler
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
31
  - - "~>"
32
32
  - !ruby/object:Gem::Version
33
- version: '5.2'
33
+ version: '1.3'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
38
  - - "~>"
39
39
  - !ruby/object:Gem::Version
40
- version: '5.2'
40
+ version: '1.3'
41
41
  - !ruby/object:Gem::Dependency
42
- name: bundler
42
+ name: pdf-reader
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
45
  - - "~>"
46
46
  - !ruby/object:Gem::Version
47
- version: '1.3'
47
+ version: '2.1'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
52
  - - "~>"
53
53
  - !ruby/object:Gem::Version
54
- version: '1.3'
54
+ version: '2.1'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rack-test
57
57
  requirement: !ruby/object:Gem::Requirement