liquidz 0.2.1
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 +7 -0
- data/LICENSE +21 -0
- data/README.md +101 -0
- data/ext/liquidz_ext/extconf.rb +10 -0
- data/ext/liquidz_ext/liquidz_ext.c +42 -0
- data/lib/liquidz_ext/liquid_patch.rb +36 -0
- data/lib/liquidz_ext/liquidz_ext.bundle +0 -0
- data/lib/liquidz_ext.rb +46 -0
- metadata +66 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA256:
|
|
3
|
+
metadata.gz: ef6c9c092df04414bd76a0c58bd8d07817cc9958d764b515e89eacad97061d7c
|
|
4
|
+
data.tar.gz: 46cc91dd3d1c98e4618898646b9b5849e2cf9da8e7d8be77cd2a74cae2534cc8
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: f101cfad36e446339aa15d13360c3ccef43f9ac156fff297f670830ec4ccff17509d7643f9452352f008e91c7933f88c3f4363c66a4674d16c24f2f0718353a4
|
|
7
|
+
data.tar.gz: c54ba1b2926f797af2bc5f4da43776b44a097173345a213630d7217561f335bea8adb7cc186db23c418f0a0db344c78f7bcccda31404c983230cf7553ac51cd7
|
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Pedro Piñera Buendía
|
|
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,101 @@
|
|
|
1
|
+
# Liquidz Ruby Binding
|
|
2
|
+
|
|
3
|
+
High-performance Liquid template engine for Ruby, powered by Zig.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
Add this line to your application's Gemfile:
|
|
8
|
+
|
|
9
|
+
```ruby
|
|
10
|
+
gem 'liquidz'
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
And then execute:
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
bundle install
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Or install it yourself:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
gem install liquidz
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
**Prerequisites:** You need to build the Zig library first:
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
zig build -Doptimize=ReleaseFast
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
Then build the gem extension:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
cd ext/liquidz_ext
|
|
35
|
+
ruby extconf.rb
|
|
36
|
+
make
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
```ruby
|
|
42
|
+
require 'liquidz_ext'
|
|
43
|
+
|
|
44
|
+
# Simple variable substitution
|
|
45
|
+
result = Liquidz.render("Hello, {{ name }}!", { name: "World" })
|
|
46
|
+
# => "Hello, World!"
|
|
47
|
+
|
|
48
|
+
# With loops
|
|
49
|
+
result = Liquidz.render("{% for item in items %}{{ item }} {% endfor %}", { items: ["a", "b", "c"] })
|
|
50
|
+
# => "a b c "
|
|
51
|
+
|
|
52
|
+
# With conditionals
|
|
53
|
+
result = Liquidz.render("{% if show %}visible{% endif %}", { show: true })
|
|
54
|
+
# => "visible"
|
|
55
|
+
|
|
56
|
+
# With filters
|
|
57
|
+
result = Liquidz.render("{{ name | upcase }}", { name: "hello" })
|
|
58
|
+
# => "HELLO"
|
|
59
|
+
|
|
60
|
+
# Using JSON string directly
|
|
61
|
+
result = Liquidz.render("{{ x }}", '{"x": 42}')
|
|
62
|
+
# => "42"
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## Drop-in Replacement for Liquid
|
|
66
|
+
|
|
67
|
+
Liquidz can act as a drop-in replacement for the standard Liquid gem:
|
|
68
|
+
|
|
69
|
+
```ruby
|
|
70
|
+
require 'liquidz_ext/liquid_patch'
|
|
71
|
+
|
|
72
|
+
# Now Liquid::Template.parse uses Liquidz under the hood
|
|
73
|
+
template = Liquid::Template.parse("Hello, {{ name }}!")
|
|
74
|
+
result = template.render({ "name" => "World" })
|
|
75
|
+
# => "Hello, World!"
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
## API
|
|
79
|
+
|
|
80
|
+
### `Liquidz.render(template, data = nil)`
|
|
81
|
+
|
|
82
|
+
Renders a Liquid template with the given data.
|
|
83
|
+
|
|
84
|
+
- `template` (String): The Liquid template string
|
|
85
|
+
- `data` (Hash, String, nil): The data to render with. Can be a Hash or a JSON string. Defaults to `nil`.
|
|
86
|
+
|
|
87
|
+
Returns the rendered template as a String.
|
|
88
|
+
|
|
89
|
+
Raises `RuntimeError` if rendering fails.
|
|
90
|
+
|
|
91
|
+
### `Liquidz.render_string(template, data = nil)`
|
|
92
|
+
|
|
93
|
+
Alias for `Liquidz.render`.
|
|
94
|
+
|
|
95
|
+
## Performance
|
|
96
|
+
|
|
97
|
+
Liquidz is approximately 3.3x faster than the standard Ruby Liquid gem for typical templates.
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
require "mkmf"
|
|
2
|
+
|
|
3
|
+
# Find the Zig-built library (bindings/ruby -> zig-out/lib)
|
|
4
|
+
project_root = File.expand_path("../../../..", __dir__)
|
|
5
|
+
lib_dir = File.join(project_root, "zig-out", "lib")
|
|
6
|
+
|
|
7
|
+
# Link against the static library directly
|
|
8
|
+
$LDFLAGS << " #{File.join(lib_dir, 'libliquidz_ffi.a')}"
|
|
9
|
+
|
|
10
|
+
create_makefile("liquidz_ext/liquidz_ext")
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
#include "ruby.h"
|
|
2
|
+
|
|
3
|
+
int liquidz_render_json(const char *template_ptr, size_t template_len,
|
|
4
|
+
const char *json_ptr, size_t json_len,
|
|
5
|
+
char **out_ptr, size_t *out_len);
|
|
6
|
+
void liquidz_free(char *ptr, size_t len);
|
|
7
|
+
|
|
8
|
+
static VALUE rb_liquidz_render(VALUE self, VALUE template_val, VALUE data_val) {
|
|
9
|
+
VALUE template_str = StringValue(template_val);
|
|
10
|
+
VALUE json_str = Qnil;
|
|
11
|
+
|
|
12
|
+
if (NIL_P(data_val)) {
|
|
13
|
+
json_str = rb_str_new("", 0);
|
|
14
|
+
} else if (RB_TYPE_P(data_val, T_STRING)) {
|
|
15
|
+
json_str = data_val;
|
|
16
|
+
} else {
|
|
17
|
+
json_str = rb_funcall(data_val, rb_intern("to_json"), 0);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const char *template_ptr = RSTRING_PTR(template_str);
|
|
21
|
+
size_t template_len = (size_t)RSTRING_LEN(template_str);
|
|
22
|
+
const char *json_ptr = RSTRING_PTR(json_str);
|
|
23
|
+
size_t json_len = (size_t)RSTRING_LEN(json_str);
|
|
24
|
+
|
|
25
|
+
char *out_ptr = NULL;
|
|
26
|
+
size_t out_len = 0;
|
|
27
|
+
|
|
28
|
+
int rc = liquidz_render_json(template_ptr, template_len, json_ptr, json_len,
|
|
29
|
+
&out_ptr, &out_len);
|
|
30
|
+
if (rc != 0) {
|
|
31
|
+
rb_raise(rb_eRuntimeError, "liquidz render failed");
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
VALUE result = rb_str_new(out_ptr, (long)out_len);
|
|
35
|
+
liquidz_free(out_ptr, out_len);
|
|
36
|
+
return result;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
void Init_liquidz_ext(void) {
|
|
40
|
+
VALUE mLiquidz = rb_define_module("Liquidz");
|
|
41
|
+
rb_define_singleton_method(mLiquidz, "render", rb_liquidz_render, 2);
|
|
42
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
require "liquid"
|
|
2
|
+
require "liquidz_ext"
|
|
3
|
+
|
|
4
|
+
module Liquidz
|
|
5
|
+
class Template
|
|
6
|
+
def initialize(source)
|
|
7
|
+
@source = source
|
|
8
|
+
end
|
|
9
|
+
|
|
10
|
+
def render(context = {}, **_opts)
|
|
11
|
+
Liquidz.render(@source, context)
|
|
12
|
+
end
|
|
13
|
+
|
|
14
|
+
def render!(*args, **kwargs)
|
|
15
|
+
render(*args, **kwargs)
|
|
16
|
+
end
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
def self.enable_liquid_patch!
|
|
20
|
+
return if @liquid_patch_enabled
|
|
21
|
+
|
|
22
|
+
Liquid::Template.singleton_class.class_eval do
|
|
23
|
+
unless method_defined?(:liquidz_orig_parse)
|
|
24
|
+
alias_method :liquidz_orig_parse, :parse
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
def parse(source, *args, **_kwargs)
|
|
28
|
+
Liquidz::Template.new(source)
|
|
29
|
+
end
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
@liquid_patch_enabled = true
|
|
33
|
+
end
|
|
34
|
+
end
|
|
35
|
+
|
|
36
|
+
Liquidz.enable_liquid_patch!
|
|
Binary file
|
data/lib/liquidz_ext.rb
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
require "json"
|
|
2
|
+
require "liquidz_ext/liquidz_ext"
|
|
3
|
+
|
|
4
|
+
# Liquidz - High-performance Liquid template engine powered by Zig
|
|
5
|
+
#
|
|
6
|
+
# @example Basic usage
|
|
7
|
+
# Liquidz.render("Hello, {{ name }}!", { name: "World" })
|
|
8
|
+
# # => "Hello, World!"
|
|
9
|
+
#
|
|
10
|
+
# @example With loops
|
|
11
|
+
# Liquidz.render("{% for item in items %}{{ item }} {% endfor %}", { items: ["a", "b", "c"] })
|
|
12
|
+
# # => "a b c "
|
|
13
|
+
#
|
|
14
|
+
# @example With filters
|
|
15
|
+
# Liquidz.render("{{ name | upcase }}", { name: "hello" })
|
|
16
|
+
# # => "HELLO"
|
|
17
|
+
#
|
|
18
|
+
module Liquidz
|
|
19
|
+
class Error < StandardError; end
|
|
20
|
+
class RenderError < Error; end
|
|
21
|
+
|
|
22
|
+
# Renders a Liquid template with the given data.
|
|
23
|
+
#
|
|
24
|
+
# @param template [String] The Liquid template string
|
|
25
|
+
# @param data [Hash, String, nil] The data to render with (Hash or JSON string)
|
|
26
|
+
# @return [String] The rendered template
|
|
27
|
+
# @raise [RenderError] If rendering fails
|
|
28
|
+
#
|
|
29
|
+
# @example
|
|
30
|
+
# Liquidz.render_string("Hello, {{ name }}!", { name: "World" })
|
|
31
|
+
# # => "Hello, World!"
|
|
32
|
+
#
|
|
33
|
+
def self.render_string(template, data = nil)
|
|
34
|
+
render(template, data)
|
|
35
|
+
rescue => e
|
|
36
|
+
raise RenderError, "Failed to render template: #{e.message}"
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
# Returns the version of the Liquidz library.
|
|
40
|
+
#
|
|
41
|
+
# @return [String] The version string
|
|
42
|
+
#
|
|
43
|
+
def self.version
|
|
44
|
+
"0.2.0"
|
|
45
|
+
end
|
|
46
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: liquidz
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.2.1
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Pedro Pinera Buendia
|
|
8
|
+
bindir: bin
|
|
9
|
+
cert_chain: []
|
|
10
|
+
date: 1980-01-02 00:00:00.000000000 Z
|
|
11
|
+
dependencies:
|
|
12
|
+
- !ruby/object:Gem::Dependency
|
|
13
|
+
name: json
|
|
14
|
+
requirement: !ruby/object:Gem::Requirement
|
|
15
|
+
requirements:
|
|
16
|
+
- - ">="
|
|
17
|
+
- !ruby/object:Gem::Version
|
|
18
|
+
version: '0'
|
|
19
|
+
type: :runtime
|
|
20
|
+
prerelease: false
|
|
21
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
22
|
+
requirements:
|
|
23
|
+
- - ">="
|
|
24
|
+
- !ruby/object:Gem::Version
|
|
25
|
+
version: '0'
|
|
26
|
+
description: A fast, drop-in replacement for the Liquid template engine, compiled
|
|
27
|
+
from Zig to native code for maximum performance.
|
|
28
|
+
email:
|
|
29
|
+
- pedro@ppinera.es
|
|
30
|
+
executables: []
|
|
31
|
+
extensions:
|
|
32
|
+
- ext/liquidz_ext/extconf.rb
|
|
33
|
+
extra_rdoc_files: []
|
|
34
|
+
files:
|
|
35
|
+
- LICENSE
|
|
36
|
+
- README.md
|
|
37
|
+
- ext/liquidz_ext/extconf.rb
|
|
38
|
+
- ext/liquidz_ext/liquidz_ext.c
|
|
39
|
+
- lib/liquidz_ext.rb
|
|
40
|
+
- lib/liquidz_ext/liquid_patch.rb
|
|
41
|
+
- lib/liquidz_ext/liquidz_ext.bundle
|
|
42
|
+
homepage: https://github.com/pepicrft/liquidz
|
|
43
|
+
licenses:
|
|
44
|
+
- MIT
|
|
45
|
+
metadata:
|
|
46
|
+
homepage_uri: https://github.com/pepicrft/liquidz
|
|
47
|
+
source_code_uri: https://github.com/pepicrft/liquidz
|
|
48
|
+
changelog_uri: https://github.com/pepicrft/liquidz/blob/main/CHANGELOG.md
|
|
49
|
+
rdoc_options: []
|
|
50
|
+
require_paths:
|
|
51
|
+
- lib
|
|
52
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
53
|
+
requirements:
|
|
54
|
+
- - ">="
|
|
55
|
+
- !ruby/object:Gem::Version
|
|
56
|
+
version: 2.7.0
|
|
57
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
58
|
+
requirements:
|
|
59
|
+
- - ">="
|
|
60
|
+
- !ruby/object:Gem::Version
|
|
61
|
+
version: '0'
|
|
62
|
+
requirements: []
|
|
63
|
+
rubygems_version: 3.6.9
|
|
64
|
+
specification_version: 4
|
|
65
|
+
summary: High-performance Liquid template engine powered by Zig
|
|
66
|
+
test_files: []
|