Skip to content

Commit 6b82f64

Browse files
committed
new quickstatic version with new filters like ternary and starts_with
1 parent caf65bf commit 6b82f64

File tree

4 files changed

+143
-2
lines changed

4 files changed

+143
-2
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "quickstatic"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
edition = "2021"
55
license = "MIT/Apache-2.0"
66
description = "First static site generator build for [Djot](https://djot.net). Optimized for the actual content and not the themes or bells and wistle of the Static site generator"

src/main.rs

+3
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,9 @@ fn build(root_dir: String) -> eyre::Result<()> {
313313
// or listing categories and tags.
314314
let builder = liquid::ParserBuilder::with_stdlib()
315315
.filter(crate::where_glob::WhereGlob)
316+
.filter(crate::where_glob::Ternary)
317+
.filter(crate::where_glob::StartsWith)
318+
.filter(crate::where_glob::Equals)
316319
.filter(liquid_lib::jekyll::Slugify)
317320
.filter(liquid_lib::jekyll::Push)
318321
.filter(liquid_lib::jekyll::Pop)

src/where_glob.rs

+138
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,141 @@ where
9999
{
100100
Error::with_msg("Invalid input").context("cause", cause)
101101
}
102+
103+
104+
//
105+
//
106+
//
107+
//
108+
109+
#[derive(Clone, ParseFilter, FilterReflection)]
110+
#[filter(
111+
name = "ternary",
112+
description = "Ternary Operator.",
113+
parameters(TernaryArgs),
114+
parsed(TernaryFilter)
115+
)]
116+
pub struct Ternary;
117+
118+
119+
120+
#[derive(Debug, FilterParameters)]
121+
struct TernaryArgs {
122+
#[parameter(description = "Value if condition is true", arg_type = "any")]
123+
true_value: Expression,
124+
125+
#[parameter(description = "Value if condition is false", arg_type = "any")]
126+
false_value: Expression,
127+
}
128+
129+
130+
#[derive(Debug, FromFilterParameters, Display_filter)]
131+
#[name = "ternary"]
132+
struct TernaryFilter {
133+
#[parameters]
134+
args: TernaryArgs,
135+
}
136+
137+
impl Filter for TernaryFilter {
138+
fn evaluate(&self, input: &dyn ValueView, runtime: &dyn Runtime) -> Result<Value, liquid_core::Error> {
139+
// Convert the input to a Value
140+
let input_value = input.to_value();
141+
142+
// Attempt to convert the Value into a boolean
143+
let condition = input_value.as_scalar().and_then(|s| s.to_bool()).unwrap_or(false);
144+
145+
let true_value = self.args.true_value.evaluate(runtime)?.into_owned();
146+
let false_value = self.args.false_value.evaluate(runtime)?.into_owned();
147+
148+
if condition {
149+
Ok(true_value)
150+
} else {
151+
Ok(false_value)
152+
}
153+
}
154+
}
155+
156+
157+
// StartsWith filter
158+
//
159+
160+
#[derive(Debug, Clone, ParseFilter, FilterReflection)]
161+
#[filter(
162+
name = "starts_with",
163+
description = "Checks if a string starts with a specified prefix.",
164+
parameters(StartsWithArgs),
165+
parsed(StartsWithFilter)
166+
)]
167+
pub struct StartsWith;
168+
169+
#[derive(Debug, FilterParameters)]
170+
struct StartsWithArgs {
171+
#[parameter(description = "The prefix to check for", arg_type = "str")]
172+
prefix: Expression,
173+
}
174+
175+
#[derive(Debug, FromFilterParameters, Display_filter)]
176+
#[name = "starts_with"]
177+
struct StartsWithFilter {
178+
#[parameters]
179+
args: StartsWithArgs,
180+
}
181+
182+
impl Filter for StartsWithFilter {
183+
fn evaluate(&self, input: &dyn ValueView, runtime: &dyn Runtime) -> Result<Value, liquid_core::Error> {
184+
// Attempt to convert the input to a scalar and then to a string
185+
let input_str = input.to_value().as_scalar().ok_or_else(|| liquid_core::Error::with_msg("Input is not a scalar value"))?.to_kstr().into_string();
186+
187+
// Evaluate the prefix argument and attempt to convert it to a string
188+
let prefix = self.args.prefix.evaluate(runtime)?.into_owned().as_scalar().ok_or_else(|| liquid_core::Error::with_msg("Prefix is not a scalar value"))?.to_kstr().into_string();
189+
190+
// Check if the input string starts with the prefix
191+
let result = input_str.starts_with(&prefix);
192+
193+
// Return the result as a Value
194+
Ok(Value::scalar(result))
195+
}
196+
}
197+
198+
199+
// Equals Filter
200+
//
201+
202+
#[derive(Clone, ParseFilter, FilterReflection)]
203+
#[filter(
204+
name = "equals",
205+
description = "Checks if a string equals a specified value.",
206+
parameters(EqualsArgs),
207+
parsed(EqualsFilter)
208+
)]
209+
pub struct Equals;
210+
211+
#[derive(Debug, FilterParameters)]
212+
struct EqualsArgs {
213+
#[parameter(description = "The value to compare against", arg_type = "any")]
214+
compare_value: Expression,
215+
}
216+
217+
#[derive(Debug, FromFilterParameters, Display_filter)]
218+
#[name = "equals"]
219+
pub struct EqualsFilter {
220+
#[parameters]
221+
args: EqualsArgs,
222+
}
223+
224+
225+
impl Filter for EqualsFilter {
226+
fn evaluate(&self, input: &dyn ValueView, runtime: &dyn Runtime) -> Result<Value, liquid_core::Error> {
227+
// Convert the input to a Value
228+
let input_value = input.to_value();
229+
230+
// Evaluate the compare_value argument and convert it to a Value
231+
let compare_value = self.args.compare_value.evaluate(runtime)?.into_owned();
232+
233+
// Check if the input value equals the compare_value
234+
let result = input_value == compare_value;
235+
236+
// Return the result as a Value
237+
Ok(Value::scalar(result))
238+
}
239+
}

0 commit comments

Comments
 (0)