htmltokit 0.1.0 → 0.2.0

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: c5d79e1fb1285b86c51d22235aab0874121b16523b39ad31ea2a67adcb0ce992
4
- data.tar.gz: 2acc136fc6182277b3b0f6d7ae3883614d18d4f8c70e30ae74cef3fcc705aaaa
3
+ metadata.gz: 3086f21fa4961f2d06f11ea07e623b97690e2d0bcbce8b56e9cc134d0fc4a9e0
4
+ data.tar.gz: 5a19c36209ac8a0a143224c71c0a88fa150b595b64c1c26cd2f045923b94f9bb
5
5
  SHA512:
6
- metadata.gz: 41b2f971115ffe4a230f53e0a64be1ce275a62a8650d43bf5d6248f5b228629c04932a18ce22351d2c8566f18a5f5beab0df151bb15155846e16f7eb26a7023d
7
- data.tar.gz: 0e3b09dd2b63ddd40cda3a812a4dd47f18fff29eef52c34d1196ce529770e5db2dfd8b2c80e0273bcd763488c498e1721ca2b7570f168b7e7862165975d08ebd
6
+ metadata.gz: 01c805f357b471d96d49a3f055acd53b5c75655cab36bed414dfc4d1457b1757615f5c8e202ce0b305ba49d46791e8b73edcb1bd727ca03b25260403e939bbab
7
+ data.tar.gz: 4d31b0324d3475525c8a34b6e36bb683f5757d4590e6ceed837cfbc081de4eb8382f58b21d89c8ca93e6d037378fb7cb2f11f5455864b96092acc6ccfff53d65
data/CHANGELOG.md CHANGED
@@ -1,5 +1,10 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.2.0] - 2022-12-31
4
+
5
+ - `HtmlToKit#to_image`
6
+ - `HtmlToKit#to_pdf`
7
+
3
8
  ## [0.1.0] - 2022-12-30
4
9
 
5
10
  - Initial release
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Htmltokit
2
2
 
3
- Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/htmltokit`. To experiment with that code, run `bin/console` for an interactive prompt.
3
+ A ruby gem converts HTML to PDF, JPEG and PNG with [ferrum](https://github.com/rubycdp/ferrum)
4
4
 
5
- TODO: Delete this and the text above, and describe your gem
5
+ ⚠️ It's still in the very early stage.
6
6
 
7
7
  ## Installation
8
8
 
@@ -14,9 +14,20 @@ If bundler is not being used to manage dependencies, install the gem by executin
14
14
 
15
15
  $ gem install htmltokit
16
16
 
17
+ Install [Chrome](https://www.google.com/chrome/) or [Chromium](https://www.chromium.org/getting-involved/download-chromium)
18
+
17
19
  ## Usage
18
20
 
19
- TODO: Write usage instructions here
21
+ ```ruby
22
+ html_to_kit = HtmlToKit.new('https://example.com')
23
+ html_to_kit = HtmlToKit.new('<html><body><h1>Hello World!</h1></body></html>')
24
+ html_to_kit = HtmlToKit.new(a_html_file)
25
+
26
+ html_to_kit.to_image('example.png')
27
+ html_to_kit.to_image('example.jpg')
28
+
29
+ html_to_kit.to_pdf
30
+ ```
20
31
 
21
32
  ## Development
22
33
 
@@ -26,7 +37,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
26
37
 
27
38
  ## Contributing
28
39
 
29
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/htmltokit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/htmltokit/blob/main/CODE_OF_CONDUCT.md).
40
+ Bug reports and pull requests are welcome on GitHub at https://github.com/htmltokit/htmltokit. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/htmltokit/htmltokit/blob/main/CODE_OF_CONDUCT.md).
30
41
 
31
42
  ## License
32
43
 
@@ -34,4 +45,4 @@ The gem is available as open source under the terms of the [MIT License](https:/
34
45
 
35
46
  ## Code of Conduct
36
47
 
37
- Everyone interacting in the Htmltokit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/htmltokit/blob/main/CODE_OF_CONDUCT.md).
48
+ Everyone interacting in the Htmltokit project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/htmltokit/htmltokit/blob/main/CODE_OF_CONDUCT.md).
@@ -0,0 +1,39 @@
1
+ class HtmlToKit
2
+ class Source
3
+
4
+ class InvalidURL < StandardError; end
5
+ class InvalidHTML < StandardError; end
6
+
7
+ def initialize(url_file_or_html)
8
+ @value = url_file_or_html
9
+ end
10
+
11
+ def url?
12
+ @is_url ||= @value.is_a?(String) && @value.match?(/\Ahttp/)
13
+ end
14
+
15
+ def file?
16
+ @is_file ||= @value.is_a?(File) || @value.is_a?(Tempfile)
17
+ end
18
+
19
+ def html?
20
+ @is_html ||= !(url? || file?)
21
+ end
22
+
23
+ def url
24
+ raise InvalidURL unless url?
25
+ @value
26
+ end
27
+
28
+ def html
29
+ raise InvalidHTML if url?
30
+
31
+ if html?
32
+ @value
33
+ else
34
+ @value.read
35
+ end
36
+ end
37
+
38
+ end
39
+ end
@@ -0,0 +1,57 @@
1
+ class HtmlToKit
2
+ class ToImage < ActiveMethod::Base
3
+
4
+ argument :path, default: nil
5
+
6
+ def call
7
+ open_page
8
+ make_screenshot
9
+ close_browser
10
+ end
11
+
12
+ private
13
+
14
+ def open_page
15
+ if source.url?
16
+ visit source.url
17
+ else
18
+ render source.html
19
+ end
20
+ end
21
+
22
+ def visit(url)
23
+ browser.go_to(url)
24
+ end
25
+
26
+ def render(html)
27
+ browser.network.intercept
28
+ html_intercepted = false
29
+ browser.on(:request) do |request|
30
+ if html_intercepted
31
+ request.continue
32
+ else
33
+ html_intercepted = true
34
+ request.respond(body: html)
35
+ end
36
+ end
37
+ visit("http://example.com")
38
+ end
39
+
40
+ def make_screenshot
41
+ browser.screenshot(path: path || "example.png")
42
+ end
43
+
44
+ def close_browser
45
+ browser.quit
46
+ end
47
+
48
+ def browser
49
+ @browser ||= Ferrum::Browser.new
50
+ end
51
+
52
+ def source
53
+ html_to_kit.source
54
+ end
55
+
56
+ end
57
+ end
@@ -0,0 +1,62 @@
1
+ class HtmlToKit
2
+ class ToPDF < ActiveMethod::Base
3
+
4
+ argument :path, default: nil
5
+ argument :opts, default: {}
6
+
7
+ def call
8
+ open_page
9
+ save_pdf_on_disk
10
+ close_browser
11
+ end
12
+
13
+ private
14
+
15
+ def open_page
16
+ if source.url?
17
+ visit source.url
18
+ else
19
+ render source.html
20
+ end
21
+ end
22
+
23
+ def visit(url)
24
+ browser.go_to(url)
25
+ end
26
+
27
+ def render(html)
28
+ browser.network.intercept
29
+ html_intercepted = false
30
+ browser.on(:request) do |request|
31
+ if html_intercepted
32
+ request.continue
33
+ else
34
+ html_intercepted = true
35
+ request.respond(body: html)
36
+ end
37
+ end
38
+ visit("http://example.com")
39
+ end
40
+
41
+ def save_pdf_on_disk
42
+ browser.pdf(
43
+ path: path || "example.pdf",
44
+ format: opts[:format] || :A4,
45
+ landscape: opts[:landscape] || false
46
+ )
47
+ end
48
+
49
+ def close_browser
50
+ browser.quit
51
+ end
52
+
53
+ def browser
54
+ @browser ||= Ferrum::Browser.new
55
+ end
56
+
57
+ def source
58
+ html_to_kit.source
59
+ end
60
+
61
+ end
62
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- module Htmltokit
4
- VERSION = "0.1.0"
3
+ class HtmlToKit
4
+ VERSION = "0.2.0"
5
5
  end
data/lib/htmltokit.rb CHANGED
@@ -1,8 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'ferrum'
4
+ require 'active_method'
3
5
  require_relative "htmltokit/version"
6
+ require_relative "htmltokit/source"
7
+ require_relative "htmltokit/to_image"
8
+ require_relative "htmltokit/to_pdf"
9
+
10
+ class HtmlToKit
11
+ include ActiveMethod
4
12
 
5
- module Htmltokit
6
13
  class Error < StandardError; end
7
- # Your code goes here...
14
+
15
+ attr_reader :source
16
+
17
+ def initialize(url_file_or_html)
18
+ @source = Source.new(url_file_or_html)
19
+ end
20
+
21
+ active_method :to_image
22
+ active_method :to_pdf, ToPDF
8
23
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: htmltokit
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Hopper Gee
@@ -9,7 +9,35 @@ autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
11
  date: 2022-12-30 00:00:00.000000000 Z
12
- dependencies: []
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: ferrum
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '0.13'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '0.13'
27
+ - !ruby/object:Gem::Dependency
28
+ name: active_method
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
13
41
  description: Conver HTML to PDF/PNG/JPEG
14
42
  email:
15
43
  - hopper.gee@hey.com
@@ -24,6 +52,9 @@ files:
24
52
  - README.md
25
53
  - Rakefile
26
54
  - lib/htmltokit.rb
55
+ - lib/htmltokit/source.rb
56
+ - lib/htmltokit/to_image.rb
57
+ - lib/htmltokit/to_pdf.rb
27
58
  - lib/htmltokit/version.rb
28
59
  - sig/htmltokit.rbs
29
60
  homepage: https://github.com/htmltokit/htmltokit