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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 286080c98560f491c524867c0986edd71e7e14bc912a50a2a72d627d4cb22e34
4
- data.tar.gz: 435a08c13f0211d694ca477d4ce8a7afba24c686d088a7b7831c0397177d127c
3
+ metadata.gz: 1e2af1a8848987b76777bf3aed2c0dfc3254e2486aab89e816e2751cb00ec10f
4
+ data.tar.gz: d4e71ff28a851c19ccc36403997cf2965e082f8267852f25a4efd704d4f96fa9
5
5
  SHA512:
6
- metadata.gz: d5ca027f7d92741dc71b872a5308a05228c9e7c7743ebcfd976175f3c4a5a0482d3831e0d3f664aa1a7870353e8d2d482c1e545b0fa558d802ca3131282323bd
7
- data.tar.gz: e4a9e936a5d96a1574c9af213912bed55de8e25af94775007397a70a7bdebc9f5b41519fc35668738c0929e9f50821b18f1c58d0897f84443334fcba15b4973c
6
+ metadata.gz: 3706e38ff13df4d57636bbb10bd58994d666454b5b2fbcd9dfbbdd5fe31a78ab13e7c768f56b8d188b8752a648eca6653452c7044eda88c7927ad89be61489c9
7
+ data.tar.gz: 33c0de522e5ff80866457b88263540532a561ebc7fd0740649d08f3861370824456f7cc98f4d94b5e07ff28f02cc58706c716ed564f6bac0586676e6c4495d93
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  ## [Unreleased]
2
2
 
3
+ ## [0.0.4] - 2025-04-22
4
+
5
+ - Allow `width`, `height` and `truncate` to be passed to the render function.
6
+
3
7
  ## [0.0.3] - 2025-04-22
4
8
 
5
9
  - Ensure that when render height is expanded to fit the content that we update
data/Cargo.lock CHANGED
@@ -1065,7 +1065,7 @@ checksum = "dfa686283ad6dd069f105e5ab091b04c62850d3e4cf5d67debad1933f55023df"
1065
1065
 
1066
1066
  [[package]]
1067
1067
  name = "himg"
1068
- version = "0.0.3"
1068
+ version = "0.0.4"
1069
1069
  dependencies = [
1070
1070
  "blitz-dom",
1071
1071
  "blitz-html",
data/README.md CHANGED
@@ -40,6 +40,10 @@ gem install himg
40
40
  png = Himg.render("<html bgcolor='blue'></html>")
41
41
  ```
42
42
 
43
+ ```ruby
44
+ png = Himg.render("<h1>Hi</h1>", width: 24, height: 24, truncate: false)
45
+ ```
46
+
43
47
  ### Rails
44
48
 
45
49
  Simply add a `show.himg.erb`!
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
@@ -1,7 +1,7 @@
1
1
  [package]
2
2
  name = "himg"
3
3
  description = "ruby bindings to expose a blitz html->png pipeline"
4
- version = "0.0.3"
4
+ version = "0.0.4"
5
5
  edition = "2024"
6
6
  authors = ["James Edwards-Jones <git@jamedjo.co.uk>"]
7
7
  license = "MIT"
@@ -30,6 +30,7 @@ async fn main() {
30
30
  height: 800,
31
31
  hidpi_scale: 1.0,
32
32
  },
33
+ truncate: false,
33
34
  color_scheme: ColorScheme::Light,
34
35
  allow_net_requests: true, //TODO: Implement using this
35
36
  };
@@ -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 computed_height = document.as_ref().root_element().final_layout.size.height;
68
- let render_height = (computed_height as u32).max(options.image_size.height).min(4000);
69
- let render_size = ImageSize {
70
- height: render_height,
71
- ..options.image_size
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 writer;
2
- pub mod image_size;
1
+ pub mod renderer;
3
2
  pub mod html_to_image;
4
- pub mod logger;
3
+ pub mod image_size;
5
4
  pub mod options;
5
+ pub mod writer;
6
+ pub mod logger;
6
7
 
7
- pub use html_to_image::html_to_image;
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
- // Determine output path, and open a file at that path.
43
- let mut output_buffer: Vec<u8> = Vec::new();
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
- // Encode buffer as PNG and write it to a file
46
- write_png(&mut output_buffer, &render_output.buffer, render_output.image_size.scaled_width(), render_output.image_size.scaled_height())?;
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
- Ok(output_buffer)
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<magnus::RString, magnus::Error> {
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("render", function!(render_blocking_rb, 1))?;
54
+ module.define_singleton_method("render_to_string", function!(render_blocking_rb, 2))?;
66
55
 
67
56
  Ok(())
68
57
  }
@@ -7,4 +7,5 @@ pub struct Options {
7
7
  pub image_size: ImageSize,
8
8
  pub color_scheme: ColorScheme,
9
9
  pub allow_net_requests: bool,
10
+ pub truncate: bool,
10
11
  }
@@ -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
+ }
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- himg (0.0.2)
4
+ himg (0.0.4)
5
5
  rb_sys (~> 0.9)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- himg (0.0.2)
4
+ himg (0.0.4)
5
5
  rb_sys (~> 0.9)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- himg (0.0.2)
4
+ himg (0.0.4)
5
5
  rb_sys (~> 0.9)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- himg (0.0.2)
4
+ himg (0.0.4)
5
5
  rb_sys (~> 0.9)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- himg (0.0.2)
4
+ himg (0.0.4)
5
5
  rb_sys (~> 0.9)
6
6
 
7
7
  GEM
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: ..
3
3
  specs:
4
- himg (0.0.2)
4
+ himg (0.0.4)
5
5
  rb_sys (~> 0.9)
6
6
 
7
7
  GEM
data/lib/himg/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Himg
4
- VERSION = "0.0.3"
4
+ VERSION = "0.0.4"
5
5
  end
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.3
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