htmltokit 0.1.0 → 0.2.0
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/CHANGELOG.md +5 -0
- data/README.md +16 -5
- data/lib/htmltokit/source.rb +39 -0
- data/lib/htmltokit/to_image.rb +57 -0
- data/lib/htmltokit/to_pdf.rb +62 -0
- data/lib/htmltokit/version.rb +2 -2
- data/lib/htmltokit.rb +17 -2
- metadata +33 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 3086f21fa4961f2d06f11ea07e623b97690e2d0bcbce8b56e9cc134d0fc4a9e0
|
4
|
+
data.tar.gz: 5a19c36209ac8a0a143224c71c0a88fa150b595b64c1c26cd2f045923b94f9bb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 01c805f357b471d96d49a3f055acd53b5c75655cab36bed414dfc4d1457b1757615f5c8e202ce0b305ba49d46791e8b73edcb1bd727ca03b25260403e939bbab
|
7
|
+
data.tar.gz: 4d31b0324d3475525c8a34b6e36bb683f5757d4590e6ceed837cfbc081de4eb8382f58b21d89c8ca93e6d037378fb7cb2f11f5455864b96092acc6ccfff53d65
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Htmltokit
|
2
2
|
|
3
|
-
|
3
|
+
A ruby gem converts HTML to PDF, JPEG and PNG with [ferrum](https://github.com/rubycdp/ferrum)
|
4
4
|
|
5
|
-
|
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
|
-
|
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/
|
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/
|
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
|
data/lib/htmltokit/version.rb
CHANGED
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
|
-
|
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.
|
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
|