html_to 1.0.1 → 1.1.2

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
  SHA256:
3
- metadata.gz: cce279f63f00703d7d960adcc2917ebe0a3e58f01c03fe91680ba162afe617bb
4
- data.tar.gz: f917e33be2c191482272351aeb26ec01913e6e499a286f691b1aace06972ef0a
3
+ metadata.gz: c9080c727397bbccad429a3664f96041b84d0d175ceacee85d9724d414b67eb6
4
+ data.tar.gz: 14040b78505b0bb0dbf26830eff84c0529af19b8590e0b212095a762e03f03eb
5
5
  SHA512:
6
- metadata.gz: a06027f1cf88b104618f9b1215f00cf987acd126ceb0be77fea5503bd07b7f9b770dd7084abe65021e4606c5a2f64b50bf4d52f67815bc39e99db88968bf11bd
7
- data.tar.gz: be7e36aa554586abe33168337955a7b477c7f12a60548a45172ba333400546ef1cbf4526d3190c821036fb650cf2a383a737f265d744b90c4f1df037323bdafe
6
+ metadata.gz: 1c4ace1e86a047b8cfe223ef507acae37f6e5a6a65107c253178d15fcb42bb3d7d50b194accb7fc91abee5f10b227346a4913282a193a023ffb03980acf657da
7
+ data.tar.gz: 750b479a0825302c6a8a6955d20b5ed15a1aa956e9fb19dc21f235bbc18b2b941355cdeb6058cee10787d1a62642357524f4a9ebc5f405f79545dd0e0af48d92
@@ -8,4 +8,4 @@ to keep the conversation linked together.
8
8
  ### Other Information
9
9
 
10
10
  If there's anything else that's important and relevant to your pull
11
- request, mention that information here.
11
+ request, mention that information here.
data/CHANGELOG.md CHANGED
@@ -1,4 +1,6 @@
1
1
  # Changelog
2
+ ## 1.1.1
3
+ Added templates, added generators, updated Readme, updated dependencies
2
4
  ## 1.0.1
3
5
  Bug fixes. test refactor. Added integration tests
4
6
  ## 1.0.0 (5-Sep-2023)
data/Gemfile CHANGED
@@ -1,8 +1,9 @@
1
1
  source 'http://rubygems.org'
2
2
  gemspec
3
- gem 'image_processing'
3
+
4
4
  group :test do
5
5
  gem 'database_cleaner-active_record'
6
+ gem 'image_processing'
6
7
  gem 'tzinfo-data', platforms: %i[mingw mswin x64_mingw jruby]
7
8
 
8
9
  gem 'database_cleaner'
data/README.md CHANGED
@@ -1,83 +1,77 @@
1
- # install
2
- ``` ruby
3
- gem 'html_to'
4
- ```
5
- ## dependencies
6
- ### gem dependecies
7
- - carrierwave
8
- - sidekiq
9
- ### system dependecies
1
+ [![codecov](https://codecov.io/gh/Vchekryzhov/html-to/graph/badge.svg?token=27NK3S64MS)](https://codecov.io/gh/Vchekryzhov/html-to)
2
+ [![build](https://github.com/vchekryzhov/html-to/actions/workflows/ruby.yml/badge.svg)](https://github.com/vchekryzhov/html-to/actions/workflows/ruby.yml/badge.svg)
3
+ [![Gem Version](https://img.shields.io/gem/v/html_to.svg)](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 🛠️ And active-storage is required
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
- # Get Started
22
- ### Prepare your model
23
- add string field to youe model, for attach uploader
24
-
25
- and add start to your model, what you want use
26
- ``` ruby
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
- ### creating template file
33
-
34
- you need create, on path ```@@share_template``` how example share/post will be ```app/views/share/post.html.erb```
35
-
36
- #### template file example:
37
- ``` html
38
- <!DOCTYPE html>
39
- <html lang="en">
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;
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
+ ```
56
46
 
57
- }
58
- .description{
59
- position: absolute;
60
- font-size: 48px;
61
- z-index: 1;
62
- }
47
+ to optimize attached images add `gem 'image_processing'`
63
48
 
64
- </style>
65
- </head>
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>
49
+ ## Basic usage
50
+ Add to your model 📖
75
51
 
52
+ ```ruby
53
+ include HtmlTo
54
+ html_to HtmlToSerializer
76
55
  ```
77
- In your template file you has access to your object via @obj
56
+ Now after save your model new image will generated and attached to ```meta_image``` in your model
78
57
 
79
- #### Assets, font, etc
80
- Headless browser will be start from Rails public path, all local assets should be access in the public folder.
58
+ ## Advanced usage
59
+ available options for customizations 🧰
60
+ ```ruby
61
+ html_to HtmlTo::DummySerializer, image_name: :my_image, template: :image
62
+ ```
63
+ | option | descriotions | default |
64
+ |--------------------|--------------------------------------------|------------|
65
+ | `image_name` | name of attachment | meta_image |
66
+ | `template` | HTML template | circle |
67
+ | `synchronous` | Run image generation job not in background | false |
68
+ | `skip_auto_update` | skip auto update after save | false |
69
+ | `width` | width of image | 1200 |
70
+ | `height` | height of image | 630 |
81
71
 
82
- # Runner
83
- gem use sidekiq worker and work at background. you need, to start your sidekiq.
72
+ ### template customization
73
+ there are two templates available `circle` and `image` for copy to your project:
74
+ ```bash
75
+ rails generate html_to:copy_template
76
+ ```
77
+ 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'
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
@@ -9,7 +9,7 @@ module HtmlTo
9
9
  end
10
10
 
11
11
  path = if /darwin/.match?(RbConfig::CONFIG['host_os'])
12
- "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
12
+ '/Applications/Google Chrome.app/Contents/MacOS/Google Chrome'
13
13
  elsif /linux/.match?(RbConfig::CONFIG['host_os'])
14
14
  release = %x(lsb_release -i -s)
15
15
  %x(which chromium-browser)
@@ -1,11 +1,11 @@
1
1
  require 'fileutils'
2
2
  require_relative 'chromium'
3
3
  require 'erb'
4
-
4
+ require 'shellwords'
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 = ERB.new(File.read(template)).result(binding)
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
- #{HtmlTo::Chromium.execute_path} \
28
- --headless=new \
29
+ #{HtmlTo::Chromium.execute_path.shellescape} \
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
 
@@ -1,6 +1,6 @@
1
1
  module HtmlTo
2
2
  class MetaImageGenerateJob < ::ActiveJob::Base
3
- queue_as :html_to
3
+ queue_as :default
4
4
  def perform(id, class_name, serializer, options)
5
5
  record = class_name.constantize.find(id)
6
6
  generator.call(record, serializer, options)
@@ -1,4 +1,4 @@
1
1
  module HtmlTo
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.1.2'.freeze
3
3
  public_constant :VERSION
4
4
  end
data/lib/html_to.rb CHANGED
@@ -9,7 +9,7 @@ module HtmlTo
9
9
  template width height image_name
10
10
  ].freeze
11
11
  DEFAULT_OPTIONS = {
12
- template: :white,
12
+ template: :circle,
13
13
  width: 1200,
14
14
  height: 630,
15
15
  image_name: :meta_image
@@ -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.0.1
4
+ version: 1.1.2
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-13 00:00:00.000000000 Z
11
+ date: 2023-09-18 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/PULL_REQUEST_TEMPLATE.md"
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/white.html.erb
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.5'
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.0.3.1
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>