chupa-text-decomposer-webkit 1.0.3 → 1.0.4
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/chupa-text-decomposer-webkit.gemspec +1 -1
- data/doc/text/news.md +8 -0
- data/lib/chupa-text/decomposers/webkit.rb +139 -94
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0522e247250744ce0a2df3317cc3226948f152a6
|
4
|
+
data.tar.gz: 1162b7e926467f66728378fb4b64cb6928afef96
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7f7030778edcfe73c3a7ef5211c48d7218efc7e10f38cbf6f4a6c9a1e3374c202501669503ded21b5db483b18e48b3406a0890fbb725859d3aca6338a59cd3c2
|
7
|
+
data.tar.gz: 0b6aa0901239e94e35d65d70054dcd5d0d58e584034f17208d989ffc51848f3782069b04e45890fb7661697511480d4d1f43fefb9565ff948c5d05cbf5a5b4a2
|
@@ -22,7 +22,7 @@ end
|
|
22
22
|
|
23
23
|
Gem::Specification.new do |spec|
|
24
24
|
spec.name = "chupa-text-decomposer-webkit"
|
25
|
-
spec.version = "1.0.
|
25
|
+
spec.version = "1.0.4"
|
26
26
|
spec.homepage = "https://github.com/ranguba/chupa-text-decomposer-webkit"
|
27
27
|
spec.authors = ["Kouhei Sutou"]
|
28
28
|
spec.email = ["kou@clear-code.com"]
|
data/doc/text/news.md
CHANGED
@@ -19,7 +19,15 @@ require "webkit2-gtk"
|
|
19
19
|
module ChupaText
|
20
20
|
module Decomposers
|
21
21
|
class WebKit < Decomposer
|
22
|
+
module LogTag
|
23
|
+
private
|
24
|
+
def log_tag
|
25
|
+
"[decomposer][webkit]"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
22
29
|
include Loggable
|
30
|
+
include LogTag
|
23
31
|
|
24
32
|
registry.register("webkit", self)
|
25
33
|
|
@@ -50,126 +58,163 @@ module ChupaText
|
|
50
58
|
end
|
51
59
|
|
52
60
|
def decompose(data)
|
53
|
-
|
61
|
+
screenshoter = Screenshoter.new(data)
|
62
|
+
screenshoter.run
|
54
63
|
data[AVAILABLE_ATTRIBUTE_NAME] = !data.screenshot.nil?
|
55
64
|
yield(data)
|
56
65
|
end
|
57
66
|
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
@@view_context ||= WebKit2Gtk::WebContext.new(ephemeral: true)
|
63
|
-
view = WebKit2Gtk::WebView.new(context: @@view_context)
|
64
|
-
window = Gtk::OffscreenWindow.new
|
65
|
-
window.set_default_size(800, 600)
|
66
|
-
window.add(view)
|
67
|
-
window.show_all
|
68
|
-
|
69
|
-
status = {
|
70
|
-
finished: false,
|
71
|
-
screenshot: nil,
|
72
|
-
}
|
73
|
-
prepare_screenshot(data, view, status)
|
74
|
-
debug do
|
75
|
-
"#{log_tag}[load][html] #{data.uri}"
|
76
|
-
end
|
77
|
-
view.load_html(data.body, data.uri.to_s)
|
67
|
+
class Screenshoter
|
68
|
+
include Loggable
|
69
|
+
include LogTag
|
78
70
|
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
71
|
+
def initialize(data)
|
72
|
+
@data = data
|
73
|
+
@@view_context ||= create_view_context
|
74
|
+
@main_loop = GLib::MainLoop.new(nil, false)
|
75
|
+
@timeout_second = compute_timeout_second
|
84
76
|
end
|
85
77
|
|
86
|
-
|
78
|
+
def run
|
79
|
+
view = WebKit2Gtk::WebView.new(context: @@view_context)
|
80
|
+
window = Gtk::OffscreenWindow.new
|
81
|
+
window.set_default_size(800, 600)
|
82
|
+
window.add(view)
|
83
|
+
window.show_all
|
87
84
|
|
88
|
-
|
89
|
-
end
|
85
|
+
setup_callbacks(view)
|
90
86
|
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
87
|
+
timeout(view) do
|
88
|
+
debug do
|
89
|
+
"#{log_tag}[load][HTML] #{@data.uri}"
|
90
|
+
end
|
91
|
+
view.load_html(@data.source.body, @data.source.uri.to_s)
|
92
|
+
@main_loop.run
|
95
93
|
end
|
96
94
|
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
95
|
+
window.destroy
|
96
|
+
end
|
97
|
+
|
98
|
+
private
|
99
|
+
def create_view_context
|
100
|
+
context = WebKit2Gtk::WebContext.new(ephemeral: true)
|
101
|
+
http_proxy = ENV["http_proxy"]
|
102
|
+
https_proxy = ENV["https_proxy"]
|
103
|
+
ftp_proxy = ENV["ftp_proxy"]
|
104
|
+
if http_proxy or https_proxy or ftp_proxy
|
105
|
+
proxy_settings = WebKit2Gtk::NetworkProxySettings.new
|
106
|
+
if http_proxy
|
107
|
+
proxy_settings.add_proxy_for_scheme("http", http_proxy)
|
108
|
+
end
|
109
|
+
if https_proxy
|
110
|
+
proxy_settings.add_proxy_for_scheme("https", https_proxy)
|
111
|
+
end
|
112
|
+
if ftp_proxy
|
113
|
+
proxy_settings.add_proxy_for_scheme("ftp", ftp_proxy)
|
108
114
|
end
|
115
|
+
context.set_network_proxy_settings(:custom, proxy_settings)
|
109
116
|
end
|
117
|
+
context
|
110
118
|
end
|
111
|
-
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
|
119
|
+
|
120
|
+
def setup_callbacks(view)
|
121
|
+
view.signal_connect("load-changed") do |_, load_event|
|
122
|
+
debug do
|
123
|
+
"#{log_tag}[load][#{load_event.nick}] #{view.uri}"
|
124
|
+
end
|
125
|
+
|
126
|
+
case load_event
|
127
|
+
when WebKit2Gtk::LoadEvent::FINISHED
|
128
|
+
debug do
|
129
|
+
"#{log_tag}[screenshot][start] #{view.uri}"
|
130
|
+
end
|
131
|
+
view.get_snapshot(:full_document, :none) do |_, result|
|
132
|
+
@main_loop.quit
|
133
|
+
snapshot_surface = view.get_snapshot_finish(result)
|
134
|
+
debug do
|
135
|
+
size = "#{snapshot_surface.width}x#{snapshot_surface.height}"
|
136
|
+
"#{log_tag}[screenshot][finish] #{view.uri}: #{size}"
|
137
|
+
end
|
138
|
+
unless snapshot_surface.width.zero?
|
139
|
+
png = convert_snapshot_surface_to_png(snapshot_surface)
|
140
|
+
@data.screenshot = Screenshot.new("image/png",
|
141
|
+
[png].pack("m*"),
|
142
|
+
"base64")
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
view.signal_connect("load-failed") do |_, _, failed_uri, error|
|
148
|
+
@main_loop.quit
|
149
|
+
error do
|
150
|
+
message = "failed to load URI: #{failed_uri}: "
|
151
|
+
message << "#{error.class}(#{error.code}): #{error.message}"
|
152
|
+
"#{log_tag}[load][failed] #{message}"
|
153
|
+
end
|
154
|
+
true
|
117
155
|
end
|
118
|
-
true
|
119
156
|
end
|
120
|
-
end
|
121
157
|
|
122
|
-
|
123
|
-
|
158
|
+
def convert_snapshot_surface_to_png(snapshot_surface)
|
159
|
+
screenshot_width, screenshot_height = @data.expected_screenshot_size
|
124
160
|
|
125
|
-
|
126
|
-
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
|
161
|
+
screenshot_surface = Cairo::ImageSurface.new(:argb32,
|
162
|
+
screenshot_width,
|
163
|
+
screenshot_height)
|
164
|
+
context = Cairo::Context.new(screenshot_surface)
|
165
|
+
context.set_source_color(:white)
|
166
|
+
context.paint
|
131
167
|
|
132
|
-
|
133
|
-
|
134
|
-
|
135
|
-
|
168
|
+
ratio = screenshot_width.to_f / snapshot_surface.width
|
169
|
+
context.scale(ratio, ratio)
|
170
|
+
context.set_source(snapshot_surface)
|
171
|
+
context.paint
|
136
172
|
|
137
|
-
|
138
|
-
|
139
|
-
|
140
|
-
|
173
|
+
png = StringIO.new
|
174
|
+
screenshot_surface.write_to_png(png)
|
175
|
+
png.string
|
176
|
+
end
|
177
|
+
|
178
|
+
def timeout(view)
|
179
|
+
timeout_id = GLib::Timeout.add_seconds(@timeout_second) do
|
180
|
+
timeout_id = nil
|
181
|
+
error do
|
182
|
+
message = "timeout to load URI: #{@timeout_second}s: #{view.uri}"
|
183
|
+
message << ": loading" if view.loading?
|
184
|
+
"#{log_tag}[load][timeout] #{message}"
|
185
|
+
end
|
186
|
+
if view.loading?
|
187
|
+
view.signal_connect("close") do
|
188
|
+
@main_loop.quit
|
189
|
+
error do
|
190
|
+
"#{log_tag}[load][closed] #{view.uri}"
|
191
|
+
end
|
192
|
+
end
|
193
|
+
view.try_close
|
194
|
+
else
|
195
|
+
@main_loop.quit
|
196
|
+
end
|
197
|
+
GLib::Source::REMOVE
|
198
|
+
end
|
141
199
|
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
timeout_id = nil
|
147
|
-
status[:finished] = true
|
148
|
-
error do
|
149
|
-
message = "timeout to load URI: #{second}s: #{view.uri}"
|
150
|
-
"#{log_tag}[load][timeout] #{message}"
|
200
|
+
begin
|
201
|
+
yield
|
202
|
+
ensure
|
203
|
+
GLib::Source.remove(timeout_id) if timeout_id
|
151
204
|
end
|
152
|
-
GLib::Source::REMOVE
|
153
205
|
end
|
154
|
-
timeout_id = timeout_source.attach(main_context)
|
155
|
-
yield
|
156
|
-
GLib::Source.remove(timeout_id) if timeout_id
|
157
|
-
end
|
158
206
|
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
207
|
+
def compute_timeout_second
|
208
|
+
default_timeout = 5
|
209
|
+
timeout_string =
|
210
|
+
ENV["CHUPA_TEXT_DECOMPOSER_WEBKIT_TIMEOUT"] || default_timeout.to_s
|
211
|
+
begin
|
212
|
+
Integer(timeout_string)
|
213
|
+
rescue ArgumentError
|
214
|
+
default_timeout
|
215
|
+
end
|
167
216
|
end
|
168
217
|
end
|
169
|
-
|
170
|
-
def log_tag
|
171
|
-
"[decomposer][webkit]"
|
172
|
-
end
|
173
218
|
end
|
174
219
|
end
|
175
220
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: chupa-text-decomposer-webkit
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Kouhei Sutou
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-07-
|
11
|
+
date: 2017-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: chupa-text
|