himg 0.0.3 → 0.0.4
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 +4 -0
- data/Cargo.lock +1 -1
- data/README.md +4 -0
- data/Rakefile +1 -1
- data/ext/himg/Cargo.toml +1 -1
- data/ext/himg/examples/file.rs +1 -0
- data/ext/himg/src/html_to_image.rs +9 -5
- data/ext/himg/src/lib.rs +34 -45
- data/ext/himg/src/options.rs +1 -0
- data/ext/himg/src/renderer.rs +28 -0
- data/gemfiles/plain_ruby.gemfile.lock +1 -1
- data/gemfiles/rails_6.gemfile.lock +1 -1
- data/gemfiles/rails_7_0.gemfile.lock +1 -1
- data/gemfiles/rails_7_1.gemfile.lock +1 -1
- data/gemfiles/rails_7_2.gemfile.lock +1 -1
- data/gemfiles/rails_8.gemfile.lock +1 -1
- data/lib/himg/version.rb +1 -1
- data/lib/himg.rb +4 -0
- metadata +2 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1e2af1a8848987b76777bf3aed2c0dfc3254e2486aab89e816e2751cb00ec10f
|
4
|
+
data.tar.gz: d4e71ff28a851c19ccc36403997cf2965e082f8267852f25a4efd704d4f96fa9
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3706e38ff13df4d57636bbb10bd58994d666454b5b2fbcd9dfbbdd5fe31a78ab13e7c768f56b8d188b8752a648eca6653452c7044eda88c7927ad89be61489c9
|
7
|
+
data.tar.gz: 33c0de522e5ff80866457b88263540532a561ebc7fd0740649d08f3861370824456f7cc98f4d94b5e07ff28f02cc58706c716ed564f6bac0586676e6c4495d93
|
data/CHANGELOG.md
CHANGED
data/Cargo.lock
CHANGED
data/README.md
CHANGED
data/Rakefile
CHANGED
@@ -20,7 +20,7 @@ RbSys::ExtensionTask.new("himg", GEMSPEC) do |ext|
|
|
20
20
|
end
|
21
21
|
|
22
22
|
require "bump/tasks"
|
23
|
-
Bump.replace_in_default = %w[ext/himg/Cargo.toml]
|
23
|
+
Bump.replace_in_default = %w[Cargo.lock ext/himg/Cargo.toml]
|
24
24
|
Bump.changelog = :editor
|
25
25
|
|
26
26
|
task default: %i[compile rubocop]
|
data/ext/himg/Cargo.toml
CHANGED
data/ext/himg/examples/file.rs
CHANGED
@@ -64,11 +64,15 @@ pub async fn html_to_image(
|
|
64
64
|
logger.log("Resolved styles and layout");
|
65
65
|
|
66
66
|
// Determine height to render
|
67
|
-
let
|
68
|
-
|
69
|
-
|
70
|
-
height
|
71
|
-
|
67
|
+
let render_size = if options.truncate {
|
68
|
+
options.image_size
|
69
|
+
} else {
|
70
|
+
let computed_height = document.as_ref().root_element().final_layout.size.height;
|
71
|
+
let render_height = (computed_height as u32).max(options.image_size.height).min(10_000);
|
72
|
+
ImageSize {
|
73
|
+
height: render_height,
|
74
|
+
..options.image_size
|
75
|
+
}
|
72
76
|
};
|
73
77
|
logger.log("Calculated render dimensions from document");
|
74
78
|
|
data/ext/himg/src/lib.rs
CHANGED
@@ -1,57 +1,46 @@
|
|
1
|
-
pub mod
|
2
|
-
pub mod image_size;
|
1
|
+
pub mod renderer;
|
3
2
|
pub mod html_to_image;
|
4
|
-
pub mod
|
3
|
+
pub mod image_size;
|
5
4
|
pub mod options;
|
5
|
+
pub mod writer;
|
6
|
+
pub mod logger;
|
6
7
|
|
7
|
-
pub use
|
8
|
-
pub use image_size::ImageSize;
|
9
|
-
pub use options::Options;
|
10
|
-
pub use writer::write_png;
|
11
|
-
pub use logger::{Logger, TimedLogger};
|
12
|
-
|
13
|
-
use blitz_traits::{ColorScheme};
|
14
|
-
use magnus::{function, prelude::*, ExceptionClass, Error, Ruby};
|
15
|
-
|
16
|
-
pub fn render_blocking(html: String) -> Result<Vec<u8>, std::io::Error> {
|
17
|
-
let runtime = tokio::runtime::Runtime::new()?;
|
18
|
-
|
19
|
-
runtime.block_on(render(html))
|
20
|
-
}
|
21
|
-
|
22
|
-
// render_to_bytes, render_to_string, render_to_file, render_to_io
|
23
|
-
pub async fn render(html: String) -> Result<Vec<u8>, std::io::Error> {
|
24
|
-
let mut logger = TimedLogger::init();
|
25
|
-
|
26
|
-
// Configure viewport dimensions
|
27
|
-
let options = Options {
|
28
|
-
image_size: ImageSize {
|
29
|
-
width: 720, //TODO: pass this in
|
30
|
-
height: 405, //TODO: decide if this will be fixed or dynamic from the document
|
31
|
-
hidpi_scale: 1.0,
|
32
|
-
},
|
33
|
-
color_scheme: ColorScheme::Light,
|
34
|
-
allow_net_requests: true, //TODO: Implement using this
|
35
|
-
};
|
36
|
-
|
37
|
-
// Render to Image
|
38
|
-
//let base_url = format!("file://{}", path_string.clone());
|
39
|
-
let base_url = None;
|
40
|
-
let render_output = html_to_image(&html, base_url, options, &mut logger).await;
|
8
|
+
pub use renderer::render_blocking;
|
41
9
|
|
42
|
-
|
43
|
-
|
10
|
+
use crate::image_size::ImageSize;
|
11
|
+
use crate::options::Options;
|
12
|
+
use blitz_traits::ColorScheme;
|
13
|
+
use magnus::{function, prelude::*, ExceptionClass, Error, Ruby, RString, RHash};
|
44
14
|
|
45
|
-
|
46
|
-
|
15
|
+
impl Options {
|
16
|
+
fn get_option<V: magnus::TryConvert + magnus::IntoValue>(optional_hash: Option<RHash>, key: &str, default: V) -> Result<V, Error> {
|
17
|
+
match optional_hash {
|
18
|
+
Some(hash) => hash.lookup2::<&str, V, V>(key, default),
|
19
|
+
None => Ok(default),
|
20
|
+
}
|
21
|
+
}
|
47
22
|
|
48
|
-
|
23
|
+
pub fn from_ruby(hash: Option<RHash>) -> Result<Self, Error> {
|
24
|
+
let options = Options {
|
25
|
+
image_size: ImageSize {
|
26
|
+
width: Self::get_option(hash, "width", 720)?,
|
27
|
+
height: Self::get_option(hash, "height", 405)?,
|
28
|
+
hidpi_scale: 1.0,
|
29
|
+
},
|
30
|
+
truncate: Self::get_option(hash, "truncate", true)?,
|
31
|
+
color_scheme: ColorScheme::Light,
|
32
|
+
allow_net_requests: true, //TODO: Implement using this
|
33
|
+
};
|
34
|
+
|
35
|
+
Ok(options)
|
36
|
+
}
|
49
37
|
}
|
50
38
|
|
51
|
-
pub fn render_blocking_rb(ruby: &Ruby, html: String) -> Result<
|
39
|
+
pub fn render_blocking_rb(ruby: &Ruby, html: String, options: Option<RHash>) -> Result<RString, Error> {
|
40
|
+
let options = Options::from_ruby(options)?;
|
52
41
|
let exception_class = ExceptionClass::from_value(magnus::eval("Himg::Error").unwrap()).unwrap();
|
53
42
|
|
54
|
-
match render_blocking(html) {
|
43
|
+
match render_blocking(html, options) {
|
55
44
|
Ok(data) => Ok(ruby.str_from_slice(&data)),
|
56
45
|
Err(e) => Err(Error::new(exception_class, format!("{}", e))),
|
57
46
|
}
|
@@ -62,7 +51,7 @@ fn init(ruby: &Ruby) -> Result<(), Error> {
|
|
62
51
|
let module = ruby.define_module("Himg")?;
|
63
52
|
|
64
53
|
//TODO: Allow optional base_url for resolving linked resources (stylesheets, images, fonts, etc)
|
65
|
-
module.define_singleton_method("
|
54
|
+
module.define_singleton_method("render_to_string", function!(render_blocking_rb, 2))?;
|
66
55
|
|
67
56
|
Ok(())
|
68
57
|
}
|
data/ext/himg/src/options.rs
CHANGED
@@ -0,0 +1,28 @@
|
|
1
|
+
use crate::html_to_image::html_to_image;
|
2
|
+
use crate::options::Options;
|
3
|
+
use crate::writer::write_png;
|
4
|
+
use crate::logger::{TimedLogger};
|
5
|
+
|
6
|
+
pub fn render_blocking(html: String, options: Options) -> Result<Vec<u8>, std::io::Error> {
|
7
|
+
let runtime = tokio::runtime::Runtime::new()?;
|
8
|
+
|
9
|
+
runtime.block_on(render(html, options))
|
10
|
+
}
|
11
|
+
|
12
|
+
// render_to_bytes, render_to_string, render_to_file, render_to_io
|
13
|
+
pub async fn render(html: String, options: Options) -> Result<Vec<u8>, std::io::Error> {
|
14
|
+
let mut logger = TimedLogger::init();
|
15
|
+
|
16
|
+
// Render to Image
|
17
|
+
//let base_url = format!("file://{}", path_string.clone());
|
18
|
+
let base_url = None;
|
19
|
+
let render_output = html_to_image(&html, base_url, options, &mut logger).await;
|
20
|
+
|
21
|
+
// Determine output path, and open a file at that path.
|
22
|
+
let mut output_buffer: Vec<u8> = Vec::new();
|
23
|
+
|
24
|
+
// Encode buffer as PNG and write it to a file
|
25
|
+
write_png(&mut output_buffer, &render_output.buffer, render_output.image_size.scaled_width(), render_output.image_size.scaled_height())?;
|
26
|
+
|
27
|
+
Ok(output_buffer)
|
28
|
+
}
|
data/lib/himg/version.rb
CHANGED
data/lib/himg.rb
CHANGED
@@ -17,4 +17,8 @@ end
|
|
17
17
|
# Converts HTML to an Image for a minimal subset of HTML and CSS
|
18
18
|
module Himg
|
19
19
|
class Error < StandardError; end
|
20
|
+
|
21
|
+
def self.render(html, width: 720, height: 405, truncate: true)
|
22
|
+
render_to_string(html, "width" => width, "height" => height, "truncate" => truncate)
|
23
|
+
end
|
20
24
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: himg
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.4
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- James Edwards-Jones
|
@@ -180,6 +180,7 @@ files:
|
|
180
180
|
- ext/himg/src/lib.rs
|
181
181
|
- ext/himg/src/logger.rs
|
182
182
|
- ext/himg/src/options.rs
|
183
|
+
- ext/himg/src/renderer.rs
|
183
184
|
- ext/himg/src/writer.rs
|
184
185
|
- gemfiles/plain_ruby.gemfile
|
185
186
|
- gemfiles/plain_ruby.gemfile.lock
|