html_to 0.0.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: b3bedf49806d2f2f5319f238c5d1b63207a41b5ad2e38977e74516a3ddf9c7d8
4
+ data.tar.gz: a00e4081875eae7be5d02654cc983d95c599199aa26f77dd8888d22fdbdcca3f
5
+ SHA512:
6
+ metadata.gz: 8227785a68ec0d480b9aac58df8d15c6d4e6b39f5fad87ee38eced5bbd0fcbca14efd9306d2adc8727f90470b6c73470184f770909c8d070ccbe29c27e268087
7
+ data.tar.gz: 73742b8136ee0292521f81b7c441801d1e8d0f689d1f6ccb66eac606ee355e04903c6eea44c5372c4c2e96ffd4ff26d116135df4e278de1139cacba075016ee8
data/.gitignore ADDED
@@ -0,0 +1,49 @@
1
+ *.rbc
2
+ capybara-*.html
3
+ .rspec
4
+ /log
5
+ /tmp
6
+ /db/*.sqlite3
7
+ /db/*.sqlite3-journal
8
+ /public/system
9
+ /coverage/
10
+ /spec/tmp
11
+ *.orig
12
+ rerun.txt
13
+ pickle-email-*.html
14
+
15
+ # TODO Comment out this rule if you are OK with secrets being uploaded to the repo
16
+ config/initializers/secret_token.rb
17
+ config/master.key
18
+
19
+ # Only include if you have production secrets in this file, which is no longer a Rails default
20
+ # config/secrets.yml
21
+
22
+ # dotenv
23
+ # TODO Comment out this rule if environment variables can be committed
24
+ .env
25
+
26
+ ## Environment normalization:
27
+ /.bundle
28
+ /vendor/bundle
29
+
30
+ # these should all be checked in to normalize the environment:
31
+ # Gemfile.lock, .ruby-version, .ruby-gemset
32
+
33
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
34
+ .rvmrc
35
+
36
+ # if using bower-rails ignore default bower_components path bower.json files
37
+ /vendor/assets/bower_components
38
+ *.bowerrc
39
+ bower.json
40
+
41
+ # Ignore pow environment settings
42
+ .powenv
43
+
44
+ # Ignore Byebug command history file.
45
+ .byebug_history
46
+
47
+ # Ignore node_modules
48
+ node_modules/
49
+
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Viktor
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,35 @@
1
+ # html-to
2
+ # install
3
+ ``` ruby
4
+ gem 'html_to', git: 'git@github.com:Vchekryzhov/html-to.git'
5
+ ```
6
+ Ubuntu:
7
+ ``` bash
8
+ sudo apt install -y chromium-browser
9
+ ```
10
+
11
+ Debian:
12
+ ```bash
13
+ apt-get install chromium chromium-l10n
14
+ ```
15
+ # Get Started
16
+
17
+ 1)Создать html.erb файл со стилями в самом файле и путями до картинки на сервере.
18
+ > /app/view/share/post.html.erb
19
+ ``` html
20
+ <p> <%= obj.title %> </p>
21
+ <img src="obj.preview_image.path" >
22
+ <style >
23
+ .
24
+ .
25
+ .
26
+ </style>
27
+ ```
28
+ Html в headless браузере будет запускатся из папки /public все шрифты и дополнительные файлы нужно класть туда.
29
+
30
+ 2) В модели куда будут подключены шеры подключить модуль, указать путь до html и указать имя аплодера
31
+ ``` ruby
32
+ include HtmlTo
33
+ @@share_template = 'share/post'
34
+ @@share_uploader = 'meta_image'
35
+ ```
data/gemfile ADDED
@@ -0,0 +1,3 @@
1
+ # Gemfile
2
+ source 'https://rubygems.org'
3
+ ruby '2.3.3'
data/html_to.gemspec ADDED
@@ -0,0 +1,15 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+
3
+ Gem::Specification.new do |s|
4
+ s.name = 'html_to'
5
+ s.version = '0.0.2'
6
+ s.date = '2019-08-31'
7
+ s.summary = 'Html-To transforms html to other format'
8
+ s.description = 'Simple gem for transforms html page through chromium headless.'
9
+ s.author = 'Chekryzhov Viktor'
10
+ s.email = 'chekryzhov@charmerstudio.com'
11
+ s.homepage = 'https://github.com/Vchekryzhov/html-to'
12
+ s.license = 'MIT'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.add_dependency 'sidekiq', '~> 4'
15
+ end
data/lib/html_to.rb ADDED
@@ -0,0 +1,15 @@
1
+ class HtmlTo < Module
2
+
3
+ extend ActiveSupport::Concern
4
+ require 'html_to/html_headless.rb'
5
+ require 'html_to/sharing_image_generate.rb'
6
+ included do
7
+ after_commit :share_image_generate, unless: :skip_share_image_generate
8
+ attr_accessor :skip_share_image_generate
9
+ end
10
+
11
+ def share_image_generate
12
+ SharingImageGenerate.perform_async(id, self.class.to_s)
13
+ end
14
+
15
+ end
@@ -0,0 +1,54 @@
1
+ class HtmlTo::HtmlHeadless
2
+ require 'fileutils'
3
+
4
+ def to_image(obj, width=1200, height=630)
5
+
6
+ width = obj.class.class_variable_get(:@@html_to_width) if obj.class.class_variable_defined?(:@@html_to_width)
7
+ height = obj.class.class_variable_get(:@@html_to_height) if obj.class.class_variable_defined?(:@@html_to_height)
8
+
9
+ @template = File.read(Rails.root.join('app/views').join(obj.class.class_variable_get(:@@share_template)+".html.erb"))
10
+ html = ERB.new(@template.html_safe).result(binding)
11
+ File.open(html_file_path, 'w+') {|f| f.write(html) }
12
+ begin
13
+ cmd = "'#{chrome}'
14
+ --headless
15
+ --screenshot=#{screenshot_file_path}
16
+ --window-size=#{width},#{height}
17
+ --disable-gpu
18
+ --disable-features=NetworkService #{html_file_path}".gsub("\n",' ')
19
+ `#{cmd}`
20
+ if $?.success?
21
+ obj.skip_share_image_generate = true
22
+ obj.send("#{obj.class.class_variable_get(:@@share_uploader)}=", File.open(screenshot_file_path))
23
+ obj.save
24
+ else
25
+ raise "result = #{$?}; command = #{cmd}"
26
+ end
27
+ ensure
28
+ FileUtils.rm(html_file_path)
29
+ FileUtils.rm(screenshot_file_path)
30
+ end
31
+ end
32
+
33
+ def chrome
34
+ if RbConfig::CONFIG['host_os'] =~ /darwin/
35
+ "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
36
+ elsif RbConfig::CONFIG['host_os'] =~ /linux/
37
+ release = IO.popen("lsb_release -i -s").read
38
+ if release == "Debian\n"
39
+ 'chromium'
40
+ else
41
+ 'chromium-browser'
42
+ end
43
+ else
44
+ raise StandardError.new "host os don't detected"
45
+ end
46
+ end
47
+
48
+ def html_file_path
49
+ @path ||= Rails.public_path.join(SecureRandom.urlsafe_base64.downcase + ".html")
50
+ end
51
+ def screenshot_file_path
52
+ @screenshot_file_path ||= Rails.public_path.join(SecureRandom.urlsafe_base64.downcase + ".png")
53
+ end
54
+ end
@@ -0,0 +1,10 @@
1
+ class HtmlTo::SharingImageGenerate
2
+ require 'erb'
3
+ require 'sidekiq'
4
+ include Sidekiq::Worker
5
+ sidekiq_options queue: 'default'
6
+ def perform(id, class_name)
7
+ @obj = class_name.constantize.find(id)
8
+ HtmlTo::HtmlHeadless.new.to_image(@obj)
9
+ end
10
+ end
metadata ADDED
@@ -0,0 +1,64 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: html_to
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Chekryzhov Viktor
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2019-08-31 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: sidekiq
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4'
27
+ description: Simple gem for transforms html page through chromium headless.
28
+ email: chekryzhov@charmerstudio.com
29
+ executables: []
30
+ extensions: []
31
+ extra_rdoc_files: []
32
+ files:
33
+ - ".gitignore"
34
+ - LICENSE
35
+ - README.md
36
+ - gemfile
37
+ - html_to.gemspec
38
+ - lib/html_to.rb
39
+ - lib/html_to/html_headless.rb
40
+ - lib/html_to/sharing_image_generate.rb
41
+ homepage: https://github.com/Vchekryzhov/html-to
42
+ licenses:
43
+ - MIT
44
+ metadata: {}
45
+ post_install_message:
46
+ rdoc_options: []
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ required_rubygems_version: !ruby/object:Gem::Requirement
55
+ requirements:
56
+ - - ">="
57
+ - !ruby/object:Gem::Version
58
+ version: '0'
59
+ requirements: []
60
+ rubygems_version: 3.1.2
61
+ signing_key:
62
+ specification_version: 4
63
+ summary: Html-To transforms html to other format
64
+ test_files: []