grover 0.5.5 → 0.6.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/grover.rb +22 -23
- data/lib/grover/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6c26dbbef85a97e2a0efaaac225c73ef4621c3a2
|
4
|
+
data.tar.gz: 644ba163ea5e742ae0f9b245ed1d5a7381968865
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6eb0f8320d0fe7d53c13b9474a014cc9ab1fab300d70ef15c8dae1d6dfd45569005a3073f5339882b28898d6d297b26329c28a152dd9ae6a709bfc9e6de31d5d
|
7
|
+
data.tar.gz: 8135ebc0aae3ba810577be644f2efd184d799f79c943dd8c07d431eb9d1e8fb8eff8fa5a2b3277b17c01cd7554de907abd4b0570304588c99ac55c6782f87eb5
|
data/lib/grover.rb
CHANGED
@@ -24,37 +24,52 @@ class Grover
|
|
24
24
|
ENV['CI'] == 'true' ? "{args: ['--no-sandbox', '--disable-setuid-sandbox']}" : ''
|
25
25
|
end
|
26
26
|
|
27
|
-
method :convert_pdf,
|
28
|
-
async (
|
27
|
+
method :convert_pdf, <<-FUNCTION
|
28
|
+
async (url_or_html, options) => {
|
29
29
|
let browser;
|
30
30
|
try {
|
31
|
+
// Launch the browser and create a page
|
31
32
|
browser = await puppeteer.launch(#{launch_params});
|
32
33
|
const page = await browser.newPage();
|
33
34
|
|
35
|
+
// Set caching flag (if provided)
|
34
36
|
const cache = options.cache; delete options.cache;
|
35
|
-
if (cache != undefined) {
|
37
|
+
if (cache != undefined) {
|
36
38
|
await page.setCacheEnabled(cache);
|
37
39
|
}
|
38
40
|
|
41
|
+
// Setup timeout option (if provided)
|
39
42
|
let request_options = {};
|
40
43
|
const timeout = options.timeout; delete options.timeout;
|
41
44
|
if (timeout != undefined) {
|
42
45
|
request_options.timeout = timeout;
|
43
46
|
}
|
44
47
|
|
45
|
-
if (
|
48
|
+
if (url_or_html.match(/^http/i)) {
|
49
|
+
// Request is for a URL, so request it
|
46
50
|
request_options.waitUntil = 'networkidle2';
|
47
|
-
await page.goto(
|
51
|
+
await page.goto(url_or_html, request_options);
|
48
52
|
} else {
|
53
|
+
// Request is some HTML content. Use request interception to assign the body
|
49
54
|
request_options.waitUntil = 'networkidle0';
|
50
|
-
await page.
|
55
|
+
await page.setRequestInterception(true);
|
56
|
+
page.once('request', request => {
|
57
|
+
request.respond({ body: url_or_html });
|
58
|
+
// Reset the request interception
|
59
|
+
// (we only want to intercept the first request - ie our HTML)
|
60
|
+
page.on('request', request => request.continue());
|
61
|
+
});
|
62
|
+
const displayUrl = options.displayUrl; delete options.displayUrl;
|
63
|
+
await page.goto(displayUrl || 'http://example.com', request_options);
|
51
64
|
}
|
52
65
|
|
66
|
+
// If specified, emulate the media type
|
53
67
|
const emulateMedia = options.emulateMedia; delete options.emulateMedia;
|
54
68
|
if (emulateMedia != undefined) {
|
55
69
|
await page.emulateMedia(emulateMedia);
|
56
70
|
}
|
57
71
|
|
72
|
+
// Return the converted PDF
|
58
73
|
return await page.pdf(options);
|
59
74
|
} finally {
|
60
75
|
if (browser) {
|
@@ -66,11 +81,9 @@ class Grover
|
|
66
81
|
end
|
67
82
|
private_constant :Processor
|
68
83
|
|
69
|
-
DISPLAY_URL_PLACEHOLDER = '{{display_url}}'.freeze
|
70
|
-
|
71
84
|
DEFAULT_HEADER_TEMPLATE = "<div class='date text left'></div><div class='title text center'></div>".freeze
|
72
85
|
DEFAULT_FOOTER_TEMPLATE = Utils.strip_heredoc(<<-HTML).freeze
|
73
|
-
<div class='text left grow'
|
86
|
+
<div class='url text left grow'></div>
|
74
87
|
<div class='text right'><span class='pageNumber'></span>/<span class='totalPages'></span></div>
|
75
88
|
HTML
|
76
89
|
|
@@ -159,7 +172,6 @@ class Grover
|
|
159
172
|
Utils.deep_merge! combined, Utils.deep_stringify_keys(options)
|
160
173
|
Utils.deep_merge! combined, meta_options unless url_source?
|
161
174
|
|
162
|
-
fix_templates! combined
|
163
175
|
fix_boolean_options! combined
|
164
176
|
fix_numeric_options! combined
|
165
177
|
|
@@ -190,19 +202,6 @@ class Grover
|
|
190
202
|
@url.match(/^http/i)
|
191
203
|
end
|
192
204
|
|
193
|
-
def fix_templates!(options)
|
194
|
-
display_url = options.delete 'display_url'
|
195
|
-
return unless display_url
|
196
|
-
|
197
|
-
options['footer_template'] ||= DEFAULT_FOOTER_TEMPLATE
|
198
|
-
|
199
|
-
%w[header_template footer_template].each do |key|
|
200
|
-
next unless options[key].is_a? ::String
|
201
|
-
|
202
|
-
options[key] = options[key].gsub(DISPLAY_URL_PLACEHOLDER, display_url)
|
203
|
-
end
|
204
|
-
end
|
205
|
-
|
206
205
|
def fix_boolean_options!(options)
|
207
206
|
%w[display_header_footer print_background landscape prefer_css_page_size].each do |opt|
|
208
207
|
next unless options.key? opt
|
data/lib/grover/version.rb
CHANGED