Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add wordwrap filter #651

Merged
merged 6 commits into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ All notable changes to MiniJinja are documented here.
- Added `sum` filter. #648
- Added `truncate` filter to `minijinja-contrib`. #647
- Added `wordcount` filter to `minijinja-contrib`. #649
- Added `wordwrap` filter to `minijinja-contrib`. #651

## 2.5.0

Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion minijinja-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ minijinja = { version = "=2.5.0", path = "../minijinja", features = [
"custom_syntax",
"loop_controls"
] }
minijinja-contrib = { version = "=2.5.0", optional = true, path = "../minijinja-contrib", features = ["pycompat", "datetime", "timezone", "rand"] }
minijinja-contrib = { version = "=2.5.0", optional = true, path = "../minijinja-contrib", features = ["pycompat", "datetime", "timezone", "rand", "unicode_wordwrap"] }
rustyline = { version = "14.0.0", optional = true }
serde = { version = "1.0.183", features = ["derive", "rc"] }
serde_json = "1.0.105"
Expand Down
3 changes: 3 additions & 0 deletions minijinja-contrib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,14 @@ pycompat = ["minijinja/builtins"]
datetime = ["time"]
timezone = ["time-tz"]
rand = ["dep:rand"]
wordwrap = ["textwrap"]
unicode_wordwrap = ["wordwrap", "textwrap/unicode-linebreak", "textwrap/unicode-width"]

[dependencies]
minijinja = { version = "2.5.0", path = "../minijinja", default-features = false }
rand = { version = "0.8.5", optional = true, default-features = false, features = ["std", "std_rng", "small_rng"] }
serde = "1.0.164"
textwrap = { version = "0.16.1", optional = true, default-features = false, features = ["smawk"] }
time = { version = "0.3.35", optional = true, features = ["serde", "formatting", "parsing"] }
time-tz = { version = "1.0.3", features = ["db"], optional = true }

Expand Down
61 changes: 61 additions & 0 deletions minijinja-contrib/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,3 +238,64 @@ pub fn wordcount(value: Value) -> Result<Value, Error> {

Ok(Value::from(count))
}

/// Wrap a string to the given width.
///
/// By default this filter is not unicode aware (feature = `wordwrap`) but when the unicode
/// feature is enabled (`unicode_wordwrap`) then it becomes so. It's implemented on top of
/// the `textwrap` crate.
///
/// **Keyword arguments:**
///
/// - `width`: Maximum length of wrapped lines (default: 79)
/// - `break_long_words`: If a word is longer than width, break it across lines (default: true)
/// - `break_on_hyphens`: If a word contains hyphens, it may be split across lines (default: true)
/// - `wrapstring`: String to join each wrapped line (default: newline)
#[cfg(feature = "wordwrap")]
#[cfg_attr(docsrs, doc(any(cfg(feature = "wordwrap"), cfg = "unicode_wordwrap")))]
pub fn wordwrap(value: Value, kwargs: Kwargs) -> Result<Value, Error> {
use textwrap::{wrap, Options as WrapOptions, WordSplitter};
let s = value.as_str().unwrap_or_default();

let width = kwargs.get::<Option<usize>>("width")?.unwrap_or(79);
let break_long_words = kwargs
.get::<Option<bool>>("break_long_words")?
.unwrap_or(true);
let break_on_hyphens = kwargs
.get::<Option<bool>>("break_on_hyphens")?
.unwrap_or(true);
let wrapstring = kwargs.get::<Option<&str>>("wrapstring")?.unwrap_or("\n");
kwargs.assert_all_used()?;

let mut options = WrapOptions::new(width).break_words(break_long_words);

if break_on_hyphens {
options = options.word_splitter(WordSplitter::HyphenSplitter);
}

// Handle empty/whitespace-only input
if s.trim().is_empty() {
return Ok(Value::from(""));
}

// Process paragraphs sequentially into final string
Ok(Value::from(s.lines().enumerate().fold(
String::new(),
|mut acc, (i, p)| {
if i > 0 {
acc.push_str(wrapstring);
}
if !p.trim().is_empty() {
// Wrap the paragraph and join with wrapstring
let wrapped = wrap(p, &options);
for (j, line) in wrapped.iter().enumerate() {
if j > 0 {
acc.push_str(wrapstring);
}
acc.push_str(line);
}
}
acc
},
)))
}
4 changes: 4 additions & 0 deletions minijinja-contrib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,10 @@ pub fn add_to_environment(env: &mut Environment) {
env.add_filter("filesizeformat", filters::filesizeformat);
env.add_filter("truncate", filters::truncate);
env.add_filter("wordcount", filters::wordcount);
#[cfg(feature = "wordwrap")]
{
env.add_filter("wordwrap", filters::wordwrap);
}
#[cfg(feature = "datetime")]
{
env.add_filter("datetimeformat", filters::datetimeformat);
Expand Down
81 changes: 81 additions & 0 deletions minijinja-contrib/tests/filters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,3 +267,84 @@ fn test_wordcount() {
"3"
);
}

#[test]
#[cfg(feature = "wordwrap")]
fn test_wordwrap() {
use minijinja_contrib::filters::wordwrap;

let mut env = minijinja::Environment::new();
env.add_filter("wordwrap", wordwrap);

// Test basic wrapping
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=20) }}",
context! {
text => "This is a long piece of text that should be wrapped at a specific width."
}
)
.unwrap(),
"This is a long piece\nof text that should\nbe wrapped at a\nspecific width."
);

// Test custom wrap string
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, wrapstring=' <br> ') }}",
context! {
text => "This is a test of custom wrap strings."
}
)
.unwrap(),
"This is <br> a test <br> of custom <br> wrap <br> strings."
);

// Test preserving newlines
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=20) }}",
context! {
text => "First paragraph.\n\nSecond paragraph."
}
)
.unwrap(),
"First paragraph.\n\nSecond paragraph."
);

// Test breaking long words
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, break_long_words=true) }}",
context! {
text => "ThisIsAVeryLongWordThatShouldBeBroken"
}
)
.unwrap(),
"ThisIsAVer\nyLongWordT\nhatShouldB\neBroken"
);

// Test not breaking long words
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, break_long_words=false) }}",
context! {
text => "ThisIsAVeryLongWordThatShouldBeBroken"
}
)
.unwrap(),
"ThisIsAVeryLongWordThatShouldBeBroken"
);

// Test breaking on hyphens
assert_eq!(
env.render_str(
"{{ text|wordwrap(width=10, break_on_hyphens=true) }}",
context! {
text => "This-is-a-hyphenated-word"
}
)
.unwrap(),
"This-is-a-\nhyphenated\n-word"
);
}