html_to 1.0.1 → 1.1.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/.github/{PULL_REQUEST_TEMPLATE.md → pull_request_template.md} +1 -1
- data/CHANGELOG.md +2 -0
- data/Gemfile +2 -1
- data/README.md +60 -68
- data/html_to.gemspec +7 -1
- data/lib/generators/html_to/copy_template_generator.rb +18 -0
- data/lib/generators/html_to/install_generator.rb +17 -0
- data/lib/generators/html_to/templates/html_to_serializer.rb +11 -0
- data/lib/html_to/image_generate.rb +11 -7
- data/lib/html_to/meta_image_generate_job.rb +1 -1
- data/lib/html_to/version.rb +1 -1
- data/lib/html_to.rb +1 -1
- data/lib/views/html_to/circle.html.erb +46 -0
- data/lib/views/html_to/image.html.erb +46 -0
- metadata +66 -6
- data/lib/views/html_to/white.html.erb +0 -38
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: cc490495133d2799e907b246a839a9645e81d0097a10006a851cbb8af8343a61
|
4
|
+
data.tar.gz: 46f2beb5850fe8975354763abdce67ea53b0f06bc65a6ed19d9c179cb329f547
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: daeaa75cc74b90d4ff4883c5475559d7b886fb81dcbdea084ae96d1b5fd159e962e1dcf442ad95dabdc3d0298f4f82b2f2bc2d2ac8e10bb70094f2cd0f40abbc
|
7
|
+
data.tar.gz: 9ab97a704bcc78d2274948916e8b382e7cb97a64775a37747ce7abe866564b5126e06cd09f1e56db53c9996c471ddfb1d1b82fead1a8ecf8d3583596b01fe3ba
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,83 +1,75 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
gem
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
1
|
+
[](https://codecov.io/gh/Vchekryzhov/html-to)
|
2
|
+
[](https://github.com/vchekryzhov/html-to/actions/workflows/ruby.yml/badge.svg)
|
3
|
+
[](https://rubygems.org/gems/html_to)
|
4
|
+
|
5
|
+
# HtmlTo 🔥
|
6
|
+
|
7
|
+
HtmlTo is a gem for Rails that allows you to generate images from an HTML file.💡
|
8
|
+
|
9
|
+
## 📋 Table of Contents
|
10
|
+
|
11
|
+
- [Installation](#Installation)🚀
|
12
|
+
- [Usage](#basic-usage) 📖
|
13
|
+
- [Advanced usage](#advanced-usage) 🧰
|
14
|
+
|
15
|
+
|
16
|
+
## Installation
|
17
|
+
|
18
|
+
You need to have Chrome or Chromium installed 🛠️
|
19
|
+
|
20
|
+
|
10
21
|
Ubuntu:
|
11
22
|
``` bash
|
12
|
-
sudo apt install imagemagick
|
13
23
|
sudo apt install -y chromium-browser
|
14
24
|
```
|
15
25
|
Debian:
|
16
26
|
```bash
|
17
|
-
sudo apt install imagemagick
|
18
27
|
apt-get install chromium chromium-l10n
|
19
28
|
```
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
@@share_uploader = "share_image"
|
28
|
-
@@share_template = 'share/post'
|
29
|
-
include HtmlTo
|
29
|
+
add gem to your gemfile
|
30
|
+
```ruby
|
31
|
+
gem 'html_to'
|
32
|
+
```
|
33
|
+
copy example serializer with
|
34
|
+
```bash
|
35
|
+
rails generate html_to:install
|
30
36
|
```
|
31
37
|
|
32
|
-
###
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
<head>
|
41
|
-
<meta charset="UTF-8">
|
42
|
-
<title>Title</title>
|
43
|
-
<style>
|
44
|
-
|
45
|
-
body {
|
46
|
-
margin: 0;
|
47
|
-
}
|
48
|
-
.image {
|
49
|
-
background-size: cover;
|
50
|
-
position: absolute;
|
51
|
-
float: left;
|
52
|
-
top: 0px;
|
53
|
-
width: 1200px;
|
54
|
-
height: 630px;
|
55
|
-
object-fit: y-repeat;
|
56
|
-
|
57
|
-
}
|
58
|
-
.description{
|
59
|
-
position: absolute;
|
60
|
-
font-size: 48px;
|
61
|
-
z-index: 1;
|
62
|
-
}
|
38
|
+
### optional
|
39
|
+
for set path to chromium executable
|
40
|
+
```ruby
|
41
|
+
#confg/initializers/html_to.rb
|
42
|
+
HtmlTo::Configuration.config do |config|
|
43
|
+
config.chromium_path = './path-to-executable'
|
44
|
+
end
|
45
|
+
```
|
63
46
|
|
64
|
-
|
65
|
-
|
66
|
-
<body>
|
67
|
-
<div class="root">
|
68
|
-
<img class='image' src="https://images.unsplash.com/photo-1593642702821-c8da6771f0c6?ixid=MXwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1489&q=80">
|
69
|
-
<p class="description">
|
70
|
-
<%=obj.attributes%>
|
71
|
-
</p>
|
72
|
-
</div>
|
73
|
-
</body>
|
74
|
-
</html>
|
47
|
+
## Basic usage
|
48
|
+
Add to your model 📖
|
75
49
|
|
50
|
+
```ruby
|
51
|
+
include HtmlTo
|
52
|
+
html_to HtmlToSerializer
|
76
53
|
```
|
77
|
-
|
54
|
+
Now after save your model new image will generated and attached to ```meta_image``` in your model
|
78
55
|
|
79
|
-
|
80
|
-
|
56
|
+
## Advanced usage
|
57
|
+
available options for customizations 🧰
|
58
|
+
```ruby
|
59
|
+
html_to HtmlTo::DummySerializer, image_name: :my_image, template: :image
|
60
|
+
```
|
61
|
+
| option | descriotions | default |
|
62
|
+
|--------------------|--------------------------------------------|------------|
|
63
|
+
| `image_name` | name of attachment | meta_image |
|
64
|
+
| `template` | HTML template | circle |
|
65
|
+
| `synchronous` | Run image generation job not in background | false |
|
66
|
+
| `skip_auto_update` | skip auto update after save | false |
|
67
|
+
| `width` | width of image | 1200 |
|
68
|
+
| `height` | height of image | 630 |
|
81
69
|
|
82
|
-
|
83
|
-
|
70
|
+
### template customization
|
71
|
+
there are two templates available `circle` and `image` for copy to your project:
|
72
|
+
```bash
|
73
|
+
rails generate html_to:copy_template
|
74
|
+
```
|
75
|
+
you can add your own template to `app/views/html_to/*`
|
data/html_to.gemspec
CHANGED
@@ -2,7 +2,7 @@ require File.expand_path('lib/html_to/version', __dir__)
|
|
2
2
|
|
3
3
|
Gem::Specification.new do |s|
|
4
4
|
s.name = 'html_to'
|
5
|
-
s.required_ruby_version = '>= 2.
|
5
|
+
s.required_ruby_version = '>= 2.7'
|
6
6
|
s.version = HtmlTo::VERSION
|
7
7
|
s.summary = 'Html-To transforms html to image'
|
8
8
|
s.description = 'Simple gem for transforms html page through chromium headless to image.'
|
@@ -16,6 +16,12 @@ Gem::Specification.new do |s|
|
|
16
16
|
'html_to.gemspec', '.github/*.md',
|
17
17
|
'Gemfile', 'Rakefile']
|
18
18
|
s.extra_rdoc_files = ['README.md']
|
19
|
+
|
20
|
+
s.add_dependency 'activejob', '>= 5'
|
21
|
+
s.add_dependency 'activestorage', '>= 5'
|
22
|
+
s.add_dependency 'english'
|
23
|
+
s.add_dependency 'erb'
|
24
|
+
|
19
25
|
if ENV['TEST_RAILS_VERSION'].nil?
|
20
26
|
s.add_development_dependency 'rails', '~> 7.0.6'
|
21
27
|
else
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
|
5
|
+
module HtmlTo
|
6
|
+
module Generators
|
7
|
+
class CopyTemplateGenerator < Rails::Generators::Base
|
8
|
+
source_root File.expand_path('../../views/html_to', __dir__)
|
9
|
+
|
10
|
+
desc 'Copy templates'
|
11
|
+
|
12
|
+
def copy_initializer
|
13
|
+
copy_file 'image.html.erb', 'app/views/html_to/image.html.erb'
|
14
|
+
copy_file 'circle.html.erb', 'app/views/html_to/circle.html.erb'
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'rails/generators'
|
4
|
+
|
5
|
+
module HtmlTo
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
source_root File.expand_path('./templates', __dir__)
|
9
|
+
|
10
|
+
desc 'Copy example of serializer to your app'
|
11
|
+
|
12
|
+
def copy_initializer
|
13
|
+
copy_file 'html_to_serializer.rb', 'app/serializers/html_to_serializer.rb'
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,11 @@
|
|
1
|
+
class HtmlToSerializer < HtmlTo::Serializer
|
2
|
+
# you can have access to serialized record via object
|
3
|
+
|
4
|
+
def title
|
5
|
+
'object.title'
|
6
|
+
end
|
7
|
+
|
8
|
+
def background_image
|
9
|
+
'https://images.unsplash.com/photo-1593642702821-c8da6771f0c6?ixid=MXwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&q=80'
|
10
|
+
end
|
11
|
+
end
|
@@ -5,7 +5,7 @@ require 'erb'
|
|
5
5
|
class HtmlTo::ImageGenerate
|
6
6
|
include HtmlTo::Chromium
|
7
7
|
def call(record, serializer, options)
|
8
|
-
generate_template(record, serializer, options[:template])
|
8
|
+
generate_template(record, serializer, options[:template], options[:width], options[:height])
|
9
9
|
take_screenshot(options[:width], options[:height])
|
10
10
|
attach_image(record, options[:image_name])
|
11
11
|
rescue StandardError
|
@@ -16,16 +16,18 @@ class HtmlTo::ImageGenerate
|
|
16
16
|
FileUtils.rm_f(screenshot_file_path)
|
17
17
|
end
|
18
18
|
|
19
|
-
def generate_template(record, serializer, template)
|
19
|
+
def generate_template(record, serializer, template, width, height)
|
20
20
|
object = serializer.constantize.new(record)
|
21
|
-
html =
|
21
|
+
html = File.open(template) do |f|
|
22
|
+
ERB.new(f.read).result(binding)
|
23
|
+
end
|
22
24
|
File.write(html_file_path, html)
|
23
25
|
end
|
24
26
|
|
25
27
|
def take_screenshot(width, height)
|
26
28
|
cmd = <<~BASH.chomp
|
27
29
|
#{HtmlTo::Chromium.execute_path} \
|
28
|
-
--headless
|
30
|
+
--headless \
|
29
31
|
--screenshot=#{screenshot_file_path} \
|
30
32
|
--window-size=#{width},#{height} \
|
31
33
|
--disable-gpu \
|
@@ -37,9 +39,12 @@ class HtmlTo::ImageGenerate
|
|
37
39
|
end
|
38
40
|
|
39
41
|
def attach_image(record, image_name)
|
40
|
-
record.send(image_name).purge
|
41
42
|
record.html_to_skip_meta_image_generate = true
|
43
|
+
record.send(image_name).purge
|
42
44
|
record.send(image_name).attach(io: optimize_screenshot, filename: optimize_screenshot, content_type: 'image/png')
|
45
|
+
return unless optimize_screenshot
|
46
|
+
|
47
|
+
optimize_screenshot.close
|
43
48
|
end
|
44
49
|
|
45
50
|
def html_file_path
|
@@ -59,9 +64,8 @@ class HtmlTo::ImageGenerate
|
|
59
64
|
.convert('jpg')
|
60
65
|
.saver(quality: 85)
|
61
66
|
.call
|
62
|
-
|
63
67
|
else
|
64
|
-
screenshot_file_path
|
68
|
+
File.open(screenshot_file_path)
|
65
69
|
end
|
66
70
|
end
|
67
71
|
|
data/lib/html_to/version.rb
CHANGED
data/lib/html_to.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
|
2
|
+
<html>
|
3
|
+
<body>
|
4
|
+
<div class="container">
|
5
|
+
<div>
|
6
|
+
<svg viewBox="0 0 100 100">
|
7
|
+
<defs>
|
8
|
+
<path id="circle"
|
9
|
+
d="
|
10
|
+
M 50, 50
|
11
|
+
m -37, 0
|
12
|
+
a 37,37 0 1,1 74,0
|
13
|
+
a 37,37 0 1,1 -74,0"/>
|
14
|
+
</defs>
|
15
|
+
<text font-size="18">
|
16
|
+
<textPath xlink:href="#circle">
|
17
|
+
<%= object.title %>
|
18
|
+
</textPath>
|
19
|
+
</text>
|
20
|
+
</svg>
|
21
|
+
</div>
|
22
|
+
</div>
|
23
|
+
</body>
|
24
|
+
<style>
|
25
|
+
svg {
|
26
|
+
fill: currentColor;
|
27
|
+
height: auto;
|
28
|
+
max-width: 66vmin;
|
29
|
+
transform-origin: center;
|
30
|
+
width: 100%;
|
31
|
+
}
|
32
|
+
.container {
|
33
|
+
display: flex;
|
34
|
+
justify-content: center;
|
35
|
+
align-items: center;
|
36
|
+
width: 1200px;
|
37
|
+
height: 630px;
|
38
|
+
}
|
39
|
+
body {
|
40
|
+
margin: 0;
|
41
|
+
}
|
42
|
+
|
43
|
+
|
44
|
+
</style>
|
45
|
+
</html>
|
46
|
+
|
@@ -0,0 +1,46 @@
|
|
1
|
+
<!DOCTYPE html>
|
2
|
+
<head>
|
3
|
+
<meta charset="UTF-8">
|
4
|
+
<title>Title</title>
|
5
|
+
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Martian Mono">
|
6
|
+
<style>
|
7
|
+
|
8
|
+
body {
|
9
|
+
margin: 0;
|
10
|
+
font-family: "Martian Mono", sans-serif;
|
11
|
+
}
|
12
|
+
.image {
|
13
|
+
width: <%=width%>px;
|
14
|
+
height: <%=height%>px;
|
15
|
+
position: absolute;
|
16
|
+
float: left;
|
17
|
+
object-fit: cover;
|
18
|
+
}
|
19
|
+
.description{
|
20
|
+
position: absolute;
|
21
|
+
color: white;
|
22
|
+
z-index: 1;
|
23
|
+
font-size: 72px;
|
24
|
+
margin-top: 10%;
|
25
|
+
margin-left: 5%;
|
26
|
+
}
|
27
|
+
.root{
|
28
|
+
background-color: black;
|
29
|
+
width: <%=width%>px;
|
30
|
+
height: <%=height%>px;
|
31
|
+
}
|
32
|
+
.image {
|
33
|
+
opacity: 0.5;
|
34
|
+
}
|
35
|
+
|
36
|
+
</style>
|
37
|
+
</head>
|
38
|
+
<body>
|
39
|
+
<div class="root">
|
40
|
+
<img class='image' src="<%=object.background_image %>" alt="img">
|
41
|
+
<p class="description">
|
42
|
+
<%=object.title%>
|
43
|
+
</p>
|
44
|
+
</div>
|
45
|
+
</body>
|
46
|
+
</html>
|
metadata
CHANGED
@@ -1,15 +1,71 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: html_to
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Chekryzhov Viktor
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-09-
|
11
|
+
date: 2023-09-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: activejob
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '5'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '5'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: activestorage
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '5'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '5'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: english
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: erb
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :runtime
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
13
69
|
- !ruby/object:Gem::Dependency
|
14
70
|
name: rails
|
15
71
|
requirement: !ruby/object:Gem::Requirement
|
@@ -33,13 +89,16 @@ extra_rdoc_files:
|
|
33
89
|
files:
|
34
90
|
- ".github/CODE_OF_CONDUCT.md"
|
35
91
|
- ".github/CONTRIBUTING.md"
|
36
|
-
- ".github/
|
92
|
+
- ".github/pull_request_template.md"
|
37
93
|
- CHANGELOG.md
|
38
94
|
- Gemfile
|
39
95
|
- LICENSE
|
40
96
|
- README.md
|
41
97
|
- Rakefile
|
42
98
|
- html_to.gemspec
|
99
|
+
- lib/generators/html_to/copy_template_generator.rb
|
100
|
+
- lib/generators/html_to/install_generator.rb
|
101
|
+
- lib/generators/html_to/templates/html_to_serializer.rb
|
43
102
|
- lib/html_to.rb
|
44
103
|
- lib/html_to/chromium.rb
|
45
104
|
- lib/html_to/configuration.rb
|
@@ -47,7 +106,8 @@ files:
|
|
47
106
|
- lib/html_to/meta_image_generate_job.rb
|
48
107
|
- lib/html_to/serializer.rb
|
49
108
|
- lib/html_to/version.rb
|
50
|
-
- lib/views/html_to/
|
109
|
+
- lib/views/html_to/circle.html.erb
|
110
|
+
- lib/views/html_to/image.html.erb
|
51
111
|
homepage: https://github.com/Vchekryzhov/html-to
|
52
112
|
licenses:
|
53
113
|
- MIT
|
@@ -61,14 +121,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
61
121
|
requirements:
|
62
122
|
- - ">="
|
63
123
|
- !ruby/object:Gem::Version
|
64
|
-
version: '2.
|
124
|
+
version: '2.7'
|
65
125
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
126
|
requirements:
|
67
127
|
- - ">="
|
68
128
|
- !ruby/object:Gem::Version
|
69
129
|
version: '0'
|
70
130
|
requirements: []
|
71
|
-
rubygems_version: 3.
|
131
|
+
rubygems_version: 3.1.6
|
72
132
|
signing_key:
|
73
133
|
specification_version: 4
|
74
134
|
summary: Html-To transforms html to image
|
@@ -1,38 +0,0 @@
|
|
1
|
-
<!DOCTYPE html>
|
2
|
-
<html lang="en">
|
3
|
-
<head>
|
4
|
-
<meta charset="UTF-8">
|
5
|
-
<title>Title</title>
|
6
|
-
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Martian Mono">
|
7
|
-
<style>
|
8
|
-
|
9
|
-
body {
|
10
|
-
margin: 0;
|
11
|
-
font-family: "Martian Mono", sans-serif;
|
12
|
-
}
|
13
|
-
.image {
|
14
|
-
background-size: cover;
|
15
|
-
position: absolute;
|
16
|
-
float: left;
|
17
|
-
top: 0px;
|
18
|
-
width: 1200px;
|
19
|
-
height: 630px;
|
20
|
-
object-fit: y-repeat;
|
21
|
-
}
|
22
|
-
.description{
|
23
|
-
position: absolute;
|
24
|
-
font-size: 48px;
|
25
|
-
z-index: 1;
|
26
|
-
}
|
27
|
-
|
28
|
-
</style>
|
29
|
-
</head>
|
30
|
-
<body>
|
31
|
-
<div class="root">
|
32
|
-
<img class='image' src="https://images.unsplash.com/photo-1593642702821-c8da6771f0c6?ixid=MXwxMjA3fDF8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1489&q=80" alt="img">
|
33
|
-
<p class="description">
|
34
|
-
<%=object.title%>
|
35
|
-
</p>
|
36
|
-
</div>
|
37
|
-
</body>
|
38
|
-
</html>
|