himg 0.0.6-x86_64-linux-musl
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 +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +20 -0
- data/.ruby-version +1 -0
- data/Appraisals +40 -0
- data/CHANGELOG.md +50 -0
- data/LICENSE-APACHE.txt +203 -0
- data/LICENSE-MIT.txt +21 -0
- data/README.md +173 -0
- data/Rakefile +26 -0
- data/Steepfile +19 -0
- data/ext/himg/examples/assets/github_profile.html +119 -0
- data/ext/himg/examples/assets/github_profile_offline.html +86 -0
- data/ext/himg/output/.gitkeep +0 -0
- data/gemfiles/plain_ruby.gemfile +5 -0
- data/gemfiles/plain_ruby.gemfile.lock +181 -0
- data/gemfiles/rails_6.gemfile +11 -0
- data/gemfiles/rails_6.gemfile.lock +320 -0
- data/gemfiles/rails_7_0.gemfile +11 -0
- data/gemfiles/rails_7_0.gemfile.lock +318 -0
- data/gemfiles/rails_7_1.gemfile +8 -0
- data/gemfiles/rails_7_1.gemfile.lock +334 -0
- data/gemfiles/rails_7_2.gemfile +8 -0
- data/gemfiles/rails_7_2.gemfile.lock +329 -0
- data/gemfiles/rails_8.gemfile +8 -0
- data/gemfiles/rails_8.gemfile.lock +329 -0
- data/lib/himg/3.2/himg.so +0 -0
- data/lib/himg/3.4/himg.so +0 -0
- data/lib/himg/base_url.rb +21 -0
- data/lib/himg/cli.rb +64 -0
- data/lib/himg/railtie/controller_config.rb +31 -0
- data/lib/himg/railtie/template_handler.rb +32 -0
- data/lib/himg/railtie.rb +49 -0
- data/lib/himg/version.rb +5 -0
- data/lib/himg.rb +26 -0
- data/readme_hero.svg +54 -0
- data/sig/himg.rbs +16 -0
- metadata +211 -0
@@ -0,0 +1,329 @@
|
|
1
|
+
PATH
|
2
|
+
remote: ..
|
3
|
+
specs:
|
4
|
+
himg (0.0.5)
|
5
|
+
rb_sys (~> 0.9)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
actioncable (8.0.2)
|
11
|
+
actionpack (= 8.0.2)
|
12
|
+
activesupport (= 8.0.2)
|
13
|
+
nio4r (~> 2.0)
|
14
|
+
websocket-driver (>= 0.6.1)
|
15
|
+
zeitwerk (~> 2.6)
|
16
|
+
actionmailbox (8.0.2)
|
17
|
+
actionpack (= 8.0.2)
|
18
|
+
activejob (= 8.0.2)
|
19
|
+
activerecord (= 8.0.2)
|
20
|
+
activestorage (= 8.0.2)
|
21
|
+
activesupport (= 8.0.2)
|
22
|
+
mail (>= 2.8.0)
|
23
|
+
actionmailer (8.0.2)
|
24
|
+
actionpack (= 8.0.2)
|
25
|
+
actionview (= 8.0.2)
|
26
|
+
activejob (= 8.0.2)
|
27
|
+
activesupport (= 8.0.2)
|
28
|
+
mail (>= 2.8.0)
|
29
|
+
rails-dom-testing (~> 2.2)
|
30
|
+
actionpack (8.0.2)
|
31
|
+
actionview (= 8.0.2)
|
32
|
+
activesupport (= 8.0.2)
|
33
|
+
nokogiri (>= 1.8.5)
|
34
|
+
rack (>= 2.2.4)
|
35
|
+
rack-session (>= 1.0.1)
|
36
|
+
rack-test (>= 0.6.3)
|
37
|
+
rails-dom-testing (~> 2.2)
|
38
|
+
rails-html-sanitizer (~> 1.6)
|
39
|
+
useragent (~> 0.16)
|
40
|
+
actiontext (8.0.2)
|
41
|
+
actionpack (= 8.0.2)
|
42
|
+
activerecord (= 8.0.2)
|
43
|
+
activestorage (= 8.0.2)
|
44
|
+
activesupport (= 8.0.2)
|
45
|
+
globalid (>= 0.6.0)
|
46
|
+
nokogiri (>= 1.8.5)
|
47
|
+
actionview (8.0.2)
|
48
|
+
activesupport (= 8.0.2)
|
49
|
+
builder (~> 3.1)
|
50
|
+
erubi (~> 1.11)
|
51
|
+
rails-dom-testing (~> 2.2)
|
52
|
+
rails-html-sanitizer (~> 1.6)
|
53
|
+
activejob (8.0.2)
|
54
|
+
activesupport (= 8.0.2)
|
55
|
+
globalid (>= 0.3.6)
|
56
|
+
activemodel (8.0.2)
|
57
|
+
activesupport (= 8.0.2)
|
58
|
+
activerecord (8.0.2)
|
59
|
+
activemodel (= 8.0.2)
|
60
|
+
activesupport (= 8.0.2)
|
61
|
+
timeout (>= 0.4.0)
|
62
|
+
activestorage (8.0.2)
|
63
|
+
actionpack (= 8.0.2)
|
64
|
+
activejob (= 8.0.2)
|
65
|
+
activerecord (= 8.0.2)
|
66
|
+
activesupport (= 8.0.2)
|
67
|
+
marcel (~> 1.0)
|
68
|
+
activesupport (8.0.2)
|
69
|
+
base64
|
70
|
+
benchmark (>= 0.3)
|
71
|
+
bigdecimal
|
72
|
+
concurrent-ruby (~> 1.0, >= 1.3.1)
|
73
|
+
connection_pool (>= 2.2.5)
|
74
|
+
drb
|
75
|
+
i18n (>= 1.6, < 2)
|
76
|
+
logger (>= 1.4.2)
|
77
|
+
minitest (>= 5.1)
|
78
|
+
securerandom (>= 0.3)
|
79
|
+
tzinfo (~> 2.0, >= 2.0.5)
|
80
|
+
uri (>= 0.13.1)
|
81
|
+
appraisal (2.5.0)
|
82
|
+
bundler
|
83
|
+
rake
|
84
|
+
thor (>= 0.14.0)
|
85
|
+
ast (2.4.3)
|
86
|
+
base64 (0.2.0)
|
87
|
+
benchmark (0.4.0)
|
88
|
+
bigdecimal (3.1.9)
|
89
|
+
builder (3.3.0)
|
90
|
+
bump (0.10.0)
|
91
|
+
concurrent-ruby (1.3.5)
|
92
|
+
connection_pool (2.5.1)
|
93
|
+
crass (1.0.6)
|
94
|
+
csv (3.3.4)
|
95
|
+
date (3.4.1)
|
96
|
+
debug (1.10.0)
|
97
|
+
irb (~> 1.10)
|
98
|
+
reline (>= 0.3.8)
|
99
|
+
diff-lcs (1.6.1)
|
100
|
+
drb (2.2.1)
|
101
|
+
erubi (1.13.1)
|
102
|
+
ffi (1.17.2-aarch64-linux-gnu)
|
103
|
+
ffi (1.17.2-aarch64-linux-musl)
|
104
|
+
ffi (1.17.2-arm-linux-gnu)
|
105
|
+
ffi (1.17.2-arm-linux-musl)
|
106
|
+
ffi (1.17.2-arm64-darwin)
|
107
|
+
ffi (1.17.2-x86_64-darwin)
|
108
|
+
ffi (1.17.2-x86_64-linux-gnu)
|
109
|
+
ffi (1.17.2-x86_64-linux-musl)
|
110
|
+
fileutils (1.7.3)
|
111
|
+
globalid (1.2.1)
|
112
|
+
activesupport (>= 6.1)
|
113
|
+
i18n (1.14.7)
|
114
|
+
concurrent-ruby (~> 1.0)
|
115
|
+
io-console (0.8.0)
|
116
|
+
irb (1.15.2)
|
117
|
+
pp (>= 0.6.0)
|
118
|
+
rdoc (>= 4.0.0)
|
119
|
+
reline (>= 0.4.2)
|
120
|
+
json (2.10.2)
|
121
|
+
language_server-protocol (3.17.0.4)
|
122
|
+
lint_roller (1.1.0)
|
123
|
+
listen (3.9.0)
|
124
|
+
rb-fsevent (~> 0.10, >= 0.10.3)
|
125
|
+
rb-inotify (~> 0.9, >= 0.9.10)
|
126
|
+
logger (1.7.0)
|
127
|
+
loofah (2.24.0)
|
128
|
+
crass (~> 1.0.2)
|
129
|
+
nokogiri (>= 1.12.0)
|
130
|
+
mail (2.8.1)
|
131
|
+
mini_mime (>= 0.1.1)
|
132
|
+
net-imap
|
133
|
+
net-pop
|
134
|
+
net-smtp
|
135
|
+
marcel (1.0.4)
|
136
|
+
mini_mime (1.1.5)
|
137
|
+
minitest (5.25.5)
|
138
|
+
mutex_m (0.3.0)
|
139
|
+
net-imap (0.5.6)
|
140
|
+
date
|
141
|
+
net-protocol
|
142
|
+
net-pop (0.1.2)
|
143
|
+
net-protocol
|
144
|
+
net-protocol (0.2.2)
|
145
|
+
timeout
|
146
|
+
net-smtp (0.5.1)
|
147
|
+
net-protocol
|
148
|
+
nio4r (2.7.4)
|
149
|
+
nokogiri (1.18.8-aarch64-linux-gnu)
|
150
|
+
racc (~> 1.4)
|
151
|
+
nokogiri (1.18.8-aarch64-linux-musl)
|
152
|
+
racc (~> 1.4)
|
153
|
+
nokogiri (1.18.8-arm-linux-gnu)
|
154
|
+
racc (~> 1.4)
|
155
|
+
nokogiri (1.18.8-arm-linux-musl)
|
156
|
+
racc (~> 1.4)
|
157
|
+
nokogiri (1.18.8-arm64-darwin)
|
158
|
+
racc (~> 1.4)
|
159
|
+
nokogiri (1.18.8-x86_64-darwin)
|
160
|
+
racc (~> 1.4)
|
161
|
+
nokogiri (1.18.8-x86_64-linux-gnu)
|
162
|
+
racc (~> 1.4)
|
163
|
+
nokogiri (1.18.8-x86_64-linux-musl)
|
164
|
+
racc (~> 1.4)
|
165
|
+
parallel (1.27.0)
|
166
|
+
parser (3.3.8.0)
|
167
|
+
ast (~> 2.4.1)
|
168
|
+
racc
|
169
|
+
pp (0.6.2)
|
170
|
+
prettyprint
|
171
|
+
prettyprint (0.2.0)
|
172
|
+
prism (1.4.0)
|
173
|
+
psych (5.2.3)
|
174
|
+
date
|
175
|
+
stringio
|
176
|
+
racc (1.8.1)
|
177
|
+
rack (3.1.13)
|
178
|
+
rack-session (2.1.0)
|
179
|
+
base64 (>= 0.1.0)
|
180
|
+
rack (>= 3.0.0)
|
181
|
+
rack-test (2.2.0)
|
182
|
+
rack (>= 1.3)
|
183
|
+
rackup (2.2.1)
|
184
|
+
rack (>= 3)
|
185
|
+
rails (8.0.2)
|
186
|
+
actioncable (= 8.0.2)
|
187
|
+
actionmailbox (= 8.0.2)
|
188
|
+
actionmailer (= 8.0.2)
|
189
|
+
actionpack (= 8.0.2)
|
190
|
+
actiontext (= 8.0.2)
|
191
|
+
actionview (= 8.0.2)
|
192
|
+
activejob (= 8.0.2)
|
193
|
+
activemodel (= 8.0.2)
|
194
|
+
activerecord (= 8.0.2)
|
195
|
+
activestorage (= 8.0.2)
|
196
|
+
activesupport (= 8.0.2)
|
197
|
+
bundler (>= 1.15.0)
|
198
|
+
railties (= 8.0.2)
|
199
|
+
rails-dom-testing (2.2.0)
|
200
|
+
activesupport (>= 5.0.0)
|
201
|
+
minitest
|
202
|
+
nokogiri (>= 1.6)
|
203
|
+
rails-html-sanitizer (1.6.2)
|
204
|
+
loofah (~> 2.21)
|
205
|
+
nokogiri (>= 1.15.7, != 1.16.7, != 1.16.6, != 1.16.5, != 1.16.4, != 1.16.3, != 1.16.2, != 1.16.1, != 1.16.0.rc1, != 1.16.0)
|
206
|
+
railties (8.0.2)
|
207
|
+
actionpack (= 8.0.2)
|
208
|
+
activesupport (= 8.0.2)
|
209
|
+
irb (~> 1.13)
|
210
|
+
rackup (>= 1.0.0)
|
211
|
+
rake (>= 12.2)
|
212
|
+
thor (~> 1.0, >= 1.2.2)
|
213
|
+
zeitwerk (~> 2.6)
|
214
|
+
rainbow (3.1.1)
|
215
|
+
rake (13.2.1)
|
216
|
+
rake-compiler (1.3.0)
|
217
|
+
rake
|
218
|
+
rake-compiler-dock (1.9.1)
|
219
|
+
rb-fsevent (0.11.2)
|
220
|
+
rb-inotify (0.11.1)
|
221
|
+
ffi (~> 1.0)
|
222
|
+
rb_sys (0.9.111)
|
223
|
+
rake-compiler-dock (= 1.9.1)
|
224
|
+
rbs (3.9.2)
|
225
|
+
logger
|
226
|
+
rdoc (6.13.1)
|
227
|
+
psych (>= 4.0.0)
|
228
|
+
regexp_parser (2.10.0)
|
229
|
+
reline (0.6.1)
|
230
|
+
io-console (~> 0.5)
|
231
|
+
rspec (3.13.0)
|
232
|
+
rspec-core (~> 3.13.0)
|
233
|
+
rspec-expectations (~> 3.13.0)
|
234
|
+
rspec-mocks (~> 3.13.0)
|
235
|
+
rspec-core (3.13.3)
|
236
|
+
rspec-support (~> 3.13.0)
|
237
|
+
rspec-expectations (3.13.3)
|
238
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
239
|
+
rspec-support (~> 3.13.0)
|
240
|
+
rspec-mocks (3.13.2)
|
241
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
242
|
+
rspec-support (~> 3.13.0)
|
243
|
+
rspec-rails (7.1.1)
|
244
|
+
actionpack (>= 7.0)
|
245
|
+
activesupport (>= 7.0)
|
246
|
+
railties (>= 7.0)
|
247
|
+
rspec-core (~> 3.13)
|
248
|
+
rspec-expectations (~> 3.13)
|
249
|
+
rspec-mocks (~> 3.13)
|
250
|
+
rspec-support (~> 3.13)
|
251
|
+
rspec-support (3.13.2)
|
252
|
+
rubocop (1.75.2)
|
253
|
+
json (~> 2.3)
|
254
|
+
language_server-protocol (~> 3.17.0.2)
|
255
|
+
lint_roller (~> 1.1.0)
|
256
|
+
parallel (~> 1.10)
|
257
|
+
parser (>= 3.3.0.2)
|
258
|
+
rainbow (>= 2.2.2, < 4.0)
|
259
|
+
regexp_parser (>= 2.9.3, < 3.0)
|
260
|
+
rubocop-ast (>= 1.44.0, < 2.0)
|
261
|
+
ruby-progressbar (~> 1.7)
|
262
|
+
unicode-display_width (>= 2.4.0, < 4.0)
|
263
|
+
rubocop-ast (1.44.1)
|
264
|
+
parser (>= 3.3.7.2)
|
265
|
+
prism (~> 1.4)
|
266
|
+
ruby-progressbar (1.13.0)
|
267
|
+
securerandom (0.4.1)
|
268
|
+
steep (1.10.0)
|
269
|
+
activesupport (>= 5.1)
|
270
|
+
concurrent-ruby (>= 1.1.10)
|
271
|
+
csv (>= 3.0.9)
|
272
|
+
fileutils (>= 1.1.0)
|
273
|
+
json (>= 2.1.0)
|
274
|
+
language_server-protocol (>= 3.17.0.4, < 4.0)
|
275
|
+
listen (~> 3.0)
|
276
|
+
logger (>= 1.3.0)
|
277
|
+
mutex_m (>= 0.3.0)
|
278
|
+
parser (>= 3.1)
|
279
|
+
rainbow (>= 2.2.2, < 4.0)
|
280
|
+
rbs (~> 3.9)
|
281
|
+
securerandom (>= 0.1)
|
282
|
+
strscan (>= 1.0.0)
|
283
|
+
terminal-table (>= 2, < 5)
|
284
|
+
uri (>= 0.12.0)
|
285
|
+
stringio (3.1.7)
|
286
|
+
strscan (3.1.3)
|
287
|
+
terminal-table (4.0.0)
|
288
|
+
unicode-display_width (>= 1.1.1, < 4)
|
289
|
+
thor (1.3.2)
|
290
|
+
timeout (0.4.3)
|
291
|
+
tzinfo (2.0.6)
|
292
|
+
concurrent-ruby (~> 1.0)
|
293
|
+
unicode-display_width (3.1.4)
|
294
|
+
unicode-emoji (~> 4.0, >= 4.0.4)
|
295
|
+
unicode-emoji (4.0.4)
|
296
|
+
uri (1.0.3)
|
297
|
+
useragent (0.16.11)
|
298
|
+
websocket-driver (0.7.7)
|
299
|
+
base64
|
300
|
+
websocket-extensions (>= 0.1.0)
|
301
|
+
websocket-extensions (0.1.5)
|
302
|
+
zeitwerk (2.7.2)
|
303
|
+
|
304
|
+
PLATFORMS
|
305
|
+
aarch64-linux-gnu
|
306
|
+
aarch64-linux-musl
|
307
|
+
arm-linux-gnu
|
308
|
+
arm-linux-musl
|
309
|
+
arm64-darwin
|
310
|
+
x86_64-darwin
|
311
|
+
x86_64-linux-gnu
|
312
|
+
x86_64-linux-musl
|
313
|
+
|
314
|
+
DEPENDENCIES
|
315
|
+
appraisal
|
316
|
+
bump
|
317
|
+
debug
|
318
|
+
himg!
|
319
|
+
irb
|
320
|
+
rails (~> 8.0)
|
321
|
+
rake
|
322
|
+
rake-compiler
|
323
|
+
rspec
|
324
|
+
rspec-rails (~> 7.0)
|
325
|
+
rubocop
|
326
|
+
steep
|
327
|
+
|
328
|
+
BUNDLED WITH
|
329
|
+
2.6.7
|
Binary file
|
Binary file
|
@@ -0,0 +1,21 @@
|
|
1
|
+
require "uri"
|
2
|
+
|
3
|
+
module Himg
|
4
|
+
class BaseUrl
|
5
|
+
def initialize(path_or_url)
|
6
|
+
path_or_url = path_or_url.to_s.strip
|
7
|
+
return if path_or_url&.empty?
|
8
|
+
|
9
|
+
@url = URI.parse(path_or_url)
|
10
|
+
@url.scheme = "file" unless @url.scheme
|
11
|
+
|
12
|
+
raise Himg::Error, "Invalid base_url #{path_or_url}" if @url.path.empty?
|
13
|
+
|
14
|
+
@url.path += "/" unless @url.path.end_with?("/")
|
15
|
+
end
|
16
|
+
|
17
|
+
def to_s
|
18
|
+
@url&.to_s
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/himg/cli.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
require "thor"
|
2
|
+
require "himg"
|
3
|
+
require "open-uri"
|
4
|
+
|
5
|
+
module Himg
|
6
|
+
class CLI < Thor
|
7
|
+
desc "screenshot SOURCE_HTML DESTINATION_PNG [OPTIONS]", "Render HTML to a png screenshot"
|
8
|
+
|
9
|
+
option :width, type: :numeric, desc: "Sets the width of the rendered content.", default: 720
|
10
|
+
option :height, type: :numeric, desc: "Sets the desired height of the rendered output.", default: 405
|
11
|
+
option :truncate, type: :boolean, desc: "Keeps the image height fixed instead of expanding to include the full page.", default: true
|
12
|
+
option :verbose, type: :boolean, desc: "Enables detailed logging for debugging and profiling.", default: false
|
13
|
+
option :disable_fetch, type: :boolean, desc: "Skip fetching file/http resources (stylesheets, images, fonts, etc)", default: false
|
14
|
+
option :http_headers, desc: "HTTP Headers to use when fetching remote resource"
|
15
|
+
option :base_url, desc: "Base URL used to resolve relative URLs"
|
16
|
+
|
17
|
+
long_desc <<-LONGDESC
|
18
|
+
`himg screenshot` takes a path to an HTML file and will render a png image with the output.
|
19
|
+
|
20
|
+
It takes a SOURCE, which can be a file path or a URL to fetch.
|
21
|
+
|
22
|
+
The DESTINATION_PNG must be a local file path.
|
23
|
+
|
24
|
+
CAVEATS: This uses a lightweight HTML parser instead of a full browser, so does not support all features.
|
25
|
+
Additionally it does not use a JavaScript engine, so will screenshot the page as-is and would not work for all webpages.
|
26
|
+
LONGDESC
|
27
|
+
def screenshot(url, destination)
|
28
|
+
Document.new(url, options).load do |content|
|
29
|
+
png = Himg.render(content, **options.transform_keys(&:to_sym))
|
30
|
+
|
31
|
+
File.open(destination, "wb") { |f| f.write(png) }
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
private
|
36
|
+
|
37
|
+
def self.exit_on_failure?
|
38
|
+
true
|
39
|
+
end
|
40
|
+
|
41
|
+
class Document
|
42
|
+
def initialize(source, options)
|
43
|
+
@source = source
|
44
|
+
@options = options
|
45
|
+
end
|
46
|
+
|
47
|
+
def http_url?
|
48
|
+
@source =~ %r{\Ahttps?\://}
|
49
|
+
end
|
50
|
+
|
51
|
+
def load(&block)
|
52
|
+
if http_url?
|
53
|
+
URI.send(:open, @source) do |input|
|
54
|
+
yield(input.binmode.read)
|
55
|
+
end
|
56
|
+
else
|
57
|
+
File.open(@source) do |f|
|
58
|
+
yield(f.read)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
module Himg
|
2
|
+
class Railtie
|
3
|
+
module ControllerConfig
|
4
|
+
extend ActiveSupport::Concern
|
5
|
+
|
6
|
+
included do
|
7
|
+
before_action :_apply_himg_config
|
8
|
+
end
|
9
|
+
|
10
|
+
class_methods do
|
11
|
+
def himg_config(options = {})
|
12
|
+
@_himg_global_config ||= {}
|
13
|
+
@_himg_global_config.merge!(options)
|
14
|
+
end
|
15
|
+
|
16
|
+
def _himg_global_config
|
17
|
+
@_himg_global_config&.dup || {}
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
def himg_config(options = {})
|
22
|
+
@_himg_config.merge!(options)
|
23
|
+
@_himg_config
|
24
|
+
end
|
25
|
+
|
26
|
+
def _apply_himg_config
|
27
|
+
@_himg_config = self.class._himg_global_config
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,32 @@
|
|
1
|
+
module Himg
|
2
|
+
class Railtie
|
3
|
+
# Handles .himg templates by calling through to Himg.render
|
4
|
+
class TemplateHandler
|
5
|
+
def self.call(_template, source)
|
6
|
+
<<-CODE
|
7
|
+
Himg.render(#{source.inspect}, **@_himg_config)
|
8
|
+
CODE
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
# Handles .himg.erb templates by pre-processing with Erb, possibly appending
|
13
|
+
# to @output_buffer or calling .safe_append, and then passing the result to
|
14
|
+
# Himg.render
|
15
|
+
class ErbTemplateHandler
|
16
|
+
def self.call(template, source)
|
17
|
+
erb_handler = ActionView::Template.registered_template_handler(:erb)
|
18
|
+
preprocessed_view_code = erb_handler.call(template, source)
|
19
|
+
|
20
|
+
<<-CODE
|
21
|
+
output = begin
|
22
|
+
#{preprocessed_view_code}
|
23
|
+
end
|
24
|
+
Himg.render(output, **@_himg_config)
|
25
|
+
CODE
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
ActionView::Template.register_template_handler :himg, Himg::Railtie::TemplateHandler
|
32
|
+
ActionView::Template.register_template_handler "himg.erb", Himg::Railtie::ErbTemplateHandler
|
data/lib/himg/railtie.rb
ADDED
@@ -0,0 +1,49 @@
|
|
1
|
+
module Himg
|
2
|
+
# Himg::Railtie
|
3
|
+
#
|
4
|
+
# Allows you to render Png images directly from rails using HTML based templates.
|
5
|
+
#
|
6
|
+
# View templates can be registered with `.himg` and will be converted from
|
7
|
+
# HTML to an image, or with `.himg.erb` to insert dynamic content. A renderer
|
8
|
+
# also allows direct rendering of HTML without going through a template.
|
9
|
+
#
|
10
|
+
# Example usage:
|
11
|
+
# - `show.himg.erb` renders an image based on controller data.
|
12
|
+
# - `render himg: '<div>My Data</div>'` generates an image from HTML.
|
13
|
+
#
|
14
|
+
# This works by registering a template handler, which will be called by Rails'
|
15
|
+
# default_render method when a view template exists matching the current action
|
16
|
+
# in a request where the format extension is .himg or a png is requested.
|
17
|
+
#
|
18
|
+
# The direct render works similarly, but instead of checking the request format
|
19
|
+
# it looks for the :himg key passed into a Rails render function.
|
20
|
+
class Railtie < Rails::Railtie
|
21
|
+
initializer "himg.configure_rails_initialization" do
|
22
|
+
ActiveSupport.on_load(:action_view) do
|
23
|
+
require "himg/railtie/template_handler"
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
initializer "himg.mime_types" do
|
28
|
+
Mime::Type.register "image/png", :himg
|
29
|
+
end
|
30
|
+
|
31
|
+
initializer "himg.controller_renderer" do
|
32
|
+
ActionController::Renderers.add :himg do |obj, options = {}|
|
33
|
+
configured_options = options[:config] || {}
|
34
|
+
direct_options = options.symbolize_keys.slice(*Himg::RENDER_OPTIONS)
|
35
|
+
merged_options = configured_options.merge(direct_options)
|
36
|
+
|
37
|
+
png_data = Himg.render(obj, **merged_options)
|
38
|
+
send_data png_data, type: "image/png", disposition: "inline"
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
initializer 'himg.controller_config' do
|
43
|
+
require "himg/railtie/controller_config"
|
44
|
+
ActiveSupport.on_load(:action_controller) do
|
45
|
+
include Himg::Railtie::ControllerConfig
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
data/lib/himg/version.rb
ADDED
data/lib/himg.rb
ADDED
@@ -0,0 +1,26 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "himg/version"
|
4
|
+
require_relative "himg/base_url"
|
5
|
+
require "himg/railtie" if defined?(Rails::Railtie)
|
6
|
+
|
7
|
+
# Attempt to load a versioned extension based on the Ruby version.
|
8
|
+
# Fall back to loading the non-versioned extension if version-specific loading fails.
|
9
|
+
begin
|
10
|
+
RUBY_VERSION =~ /(\d+\.\d+)/
|
11
|
+
require "#{Regexp.last_match(1)}/himg/himg"
|
12
|
+
rescue LoadError
|
13
|
+
require "himg/himg"
|
14
|
+
end
|
15
|
+
|
16
|
+
# The Hyper Image Generator
|
17
|
+
#
|
18
|
+
# Converts HTML to an Image for a minimal subset of HTML and CSS
|
19
|
+
module Himg
|
20
|
+
RENDER_OPTIONS = %i[width height truncate verbose].freeze
|
21
|
+
class Error < StandardError; end
|
22
|
+
|
23
|
+
def self.render(html, width: 720, height: 405, truncate: true, verbose: false, base_url: nil, disable_fetch: false)
|
24
|
+
render_to_string(html, "width" => width.to_i, "height" => height.to_i, "truncate" => truncate, "verbose" => verbose, "base_url" => BaseUrl.new(base_url).to_s, "disable_fetch" => disable_fetch)
|
25
|
+
end
|
26
|
+
end
|