From d7c84aff746ec8d9e5134786c148b2ee923ba06e Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Fri, 18 Nov 2022 17:01:31 +0100 Subject: [PATCH 01/13] add feature indent --- minijinja/src/filters.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index 47c362c8..b3dba077 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -752,6 +752,35 @@ mod builtins { }) } + /// indents Value with spaces or tabs + /// + /// This filter is only available if the `indent` feature is enabled. The resulting + /// characters. The optional parameter to the filter can be set to `true` to enable + /// indenting with \t. + /// This filter is useful, if you want to template yaml-files + /// + /// ```jinja + /// example: + /// config: + /// {{ global_config|indent(2,true) }}; #indent with 2 Tabs + /// {{ glabal_config|indent(4) }} #indent with 4 + /// ``` + #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins", feature = "indent"))))] + #[cfg(feature = "indent")] + pub fn indent(value: String, spaces: usize, tabs: Option) -> String { + let mut output: String = String::new(); + if tabs.unwrap_or(false) { + for line in value.split('\n') { + output.push_str(format!("{}{}\n", String::from("\t").repeat(spaces), line).as_str()); + } + } else { + for line in value.split('\n') { + output.push_str(format!("{}{}\n", String::from(" ").repeat(spaces), line).as_str()); + } + } + output + } + /// URL encodes a value. /// /// If given a map it encodes the parameters into a query set, otherwise it From 20c38e0faa0354bc0ca4076094f8484ee92bd575 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Fri, 18 Nov 2022 17:03:37 +0100 Subject: [PATCH 02/13] fixed docstrings --- minijinja/src/filters.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index b3dba077..af8be6d2 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -754,8 +754,8 @@ mod builtins { /// indents Value with spaces or tabs /// - /// This filter is only available if the `indent` feature is enabled. The resulting - /// characters. The optional parameter to the filter can be set to `true` to enable + /// This filter is only available if the `indent` feature is enabled. + /// The optional parameter to the filter can be set to `true` to enable /// indenting with \t. /// This filter is useful, if you want to template yaml-files /// From c9b17a1cfd4970f504e48301db10579886b24e54 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Fri, 18 Nov 2022 17:27:32 +0100 Subject: [PATCH 03/13] edited docstring --- minijinja/src/filters.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index af8be6d2..8c10eca6 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -756,7 +756,7 @@ mod builtins { /// /// This filter is only available if the `indent` feature is enabled. /// The optional parameter to the filter can be set to `true` to enable - /// indenting with \t. + /// indenting with \t (tabs). /// This filter is useful, if you want to template yaml-files /// /// ```jinja @@ -781,6 +781,13 @@ mod builtins { output } + #[test] + #[cfg(feature="indent")] + fn test_indent_with_spaces() { + let teststring = String::from("test\ntest1\ntest2\n"); + assert_eq!(indent(teststring, 2), String::from(" test\n test1\n test2\n")); + } + /// URL encodes a value. /// /// If given a map it encodes the parameters into a query set, otherwise it From 11b32b65a5bcc3e5614562f640e8389df562b98a Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Sun, 20 Nov 2022 14:26:10 +0100 Subject: [PATCH 04/13] matching jinja2s indent filter --- minijinja/src/filters.rs | 74 +++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 16 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index 8c10eca6..5ec62dd4 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -752,40 +752,82 @@ mod builtins { }) } - /// indents Value with spaces or tabs + /// indents Value with spaces /// - /// This filter is only available if the `indent` feature is enabled. - /// The optional parameter to the filter can be set to `true` to enable - /// indenting with \t (tabs). + /// This filter is only available if the `indent` feature is enabled. + /// The first optional parameter to the filter can be set to `true` to + /// indent the first line + /// the second optional parameter to the filter can be set to `true` + /// to indent blank lines /// This filter is useful, if you want to template yaml-files /// /// ```jinja /// example: /// config: - /// {{ global_config|indent(2,true) }}; #indent with 2 Tabs - /// {{ glabal_config|indent(4) }} #indent with 4 + /// {{ global_config|indent(2,true) }} #indent whole Value with two spaces + /// {{ global_conifg|indent(2,false) }} #indent /// ``` #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins", feature = "indent"))))] #[cfg(feature = "indent")] - pub fn indent(value: String, spaces: usize, tabs: Option) -> String { + pub fn indent( + value: String, + width: usize, + indent_first_line: Option, + indent_blank_lines: Option, + ) -> String { let mut output: String = String::new(); - if tabs.unwrap_or(false) { - for line in value.split('\n') { - output.push_str(format!("{}{}\n", String::from("\t").repeat(spaces), line).as_str()); + for (i, line) in value.split('\n').enumerate() { + if line == "" { + if indent_blank_lines.unwrap_or(false) { + output.push_str(String::from(" ").repeat(width).as_str()); + } + output.push_str("\n"); + continue; } - } else { - for line in value.split('\n') { - output.push_str(format!("{}{}\n", String::from(" ").repeat(spaces), line).as_str()); + if i == 0 && indent_first_line.unwrap_or(false) == false { + output.push_str(format!("{}\n", line).as_str()); + continue; } + output.push_str(format!("{}{}\n", String::from(" ").repeat(width), line).as_str()); } output } #[test] - #[cfg(feature="indent")] - fn test_indent_with_spaces() { + #[cfg(feature = "indent")] + fn test_indent() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2), + String::from("test\n test1\n\n test2\n") + ); + } + #[test] + #[cfg(feature = "indent")] + fn test_indent_with_indented_first_line() { + let teststring = String::from("test\ntest1\ntest2\n"); + assert_eq!( + indent(teststring, 2, true), + String::from(" test\n test1\n test2\n") + ); + } + #[test] + #[cfg(feature = "indent")] + fn test_indent_with_indented_blank_line() { + let teststring = String::from("test\ntest1\ntest2\n"); + assert_eq!( + indent(teststring, 2, true), + String::from("test\n test1\n \n test2\n") + ); + } + #[test] + #[cfg(feature = "indent")] + fn test_indent_with_all_indented() { let teststring = String::from("test\ntest1\ntest2\n"); - assert_eq!(indent(teststring, 2), String::from(" test\n test1\n test2\n")); + assert_eq!( + indent(teststring, 2, true), + String::from(" test\n test1\n \n test2\n") + ); } /// URL encodes a value. From bcc7f44c1fe5efb9274b0d9a9d24b773baf61627 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Sun, 20 Nov 2022 14:38:53 +0100 Subject: [PATCH 05/13] edited docstring --- minijinja/src/filters.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index 5ec62dd4..a39749e6 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -756,16 +756,17 @@ mod builtins { /// /// This filter is only available if the `indent` feature is enabled. /// The first optional parameter to the filter can be set to `true` to - /// indent the first line + /// indent the first line. The parameter defaults to false. /// the second optional parameter to the filter can be set to `true` - /// to indent blank lines + /// to indent blank lines. The parameter defaults to false. /// This filter is useful, if you want to template yaml-files /// /// ```jinja /// example: /// config: + /// {{ global_conifg|indent(2) }} #does not indent first line /// {{ global_config|indent(2,true) }} #indent whole Value with two spaces - /// {{ global_conifg|indent(2,false) }} #indent + /// {{ global_config|indent(2,true,true)}} #indents whole multiline value /// ``` #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins", feature = "indent"))))] #[cfg(feature = "indent")] From 9b686c644c45d5718adb6ee37e86008f360d5f10 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Sun, 20 Nov 2022 22:32:38 +0100 Subject: [PATCH 06/13] removed feature, matching jinja2 removed feature, because it sould be builtin for jinja2 --- minijinja/Cargo.toml | 2 +- minijinja/src/filters.rs | 65 ++++++++++++++++++++++++---------------- 2 files changed, 40 insertions(+), 27 deletions(-) diff --git a/minijinja/Cargo.toml b/minijinja/Cargo.toml index 45284e74..e8c0f672 100644 --- a/minijinja/Cargo.toml +++ b/minijinja/Cargo.toml @@ -13,7 +13,7 @@ rust-version = "1.61" exclude = ["tests"] [package.metadata.docs.rs] -features = ["source", "json", "urlencode"] +features = ["source", "json", "urlencode", "indent"] rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "doc-header.html"] [features] diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index a39749e6..c526d265 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -249,6 +249,7 @@ pub fn escape(state: &State, v: Value) -> Result { mod builtins { use super::*; + use crate::Environment; use crate::error::ErrorKind; use crate::value::{ValueKind, ValueRepr}; use std::borrow::Cow; @@ -768,66 +769,78 @@ mod builtins { /// {{ global_config|indent(2,true) }} #indent whole Value with two spaces /// {{ global_config|indent(2,true,true)}} #indents whole multiline value /// ``` - #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins", feature = "indent"))))] - #[cfg(feature = "indent")] + #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins"))))] pub fn indent( value: String, width: usize, indent_first_line: Option, indent_blank_lines: Option, ) -> String { + + fn strip_trailing_newline(input: String) -> String { + String::from(input + .strip_suffix("\r\n") + .or_else(||input.strip_suffix('\n')) + .unwrap_or(input.as_str())) + } + let mut output: String = String::new(); - for (i, line) in value.split('\n').enumerate() { - if line == "" { + let mut iterator = value.split('\n').peekable(); + if !indent_first_line.unwrap_or(false) { + output.push_str(iterator.next().unwrap()); + output.push('\n'); + } + while let Some(line) = &iterator.next() { + if iterator.peek().is_none() { + break // because split creates a empty String after the last new line + } + if line.is_empty() { if indent_blank_lines.unwrap_or(false) { output.push_str(String::from(" ").repeat(width).as_str()); } - output.push_str("\n"); - continue; - } - if i == 0 && indent_first_line.unwrap_or(false) == false { - output.push_str(format!("{}\n", line).as_str()); - continue; + } else { + output.push_str(format!("{}{}", String::from(" ").repeat(width), line).as_str()); } - output.push_str(format!("{}{}\n", String::from(" ").repeat(width), line).as_str()); + output.push('\n'); } + output = strip_trailing_newline(output); // strip last newline output } #[test] - #[cfg(feature = "indent")] + #[cfg(feature = "builtins")] fn test_indent() { let teststring = String::from("test\ntest1\n\ntest2\n"); assert_eq!( - indent(teststring, 2), - String::from("test\n test1\n\n test2\n") + indent(teststring, 2, None, None), + String::from("test\n test1\n\n test2") ); } #[test] - #[cfg(feature = "indent")] + #[cfg(feature = "builtins")] fn test_indent_with_indented_first_line() { - let teststring = String::from("test\ntest1\ntest2\n"); + let teststring = String::from("test\ntest1\n\ntest2\n"); assert_eq!( - indent(teststring, 2, true), - String::from(" test\n test1\n test2\n") + indent(teststring, 2, Some(true), None), + String::from(" test\n test1\n\n test2") ); } #[test] - #[cfg(feature = "indent")] + #[cfg(feature = "builtins")] fn test_indent_with_indented_blank_line() { - let teststring = String::from("test\ntest1\ntest2\n"); + let teststring = String::from("test\ntest1\n\ntest2\n"); assert_eq!( - indent(teststring, 2, true), - String::from("test\n test1\n \n test2\n") + indent(teststring, 2, None, Some(true)), + String::from("test\n test1\n \n test2") ); } #[test] - #[cfg(feature = "indent")] + #[cfg(feature = "builtins")] fn test_indent_with_all_indented() { - let teststring = String::from("test\ntest1\ntest2\n"); + let teststring = String::from("test\ntest1\n\ntest2\n"); assert_eq!( - indent(teststring, 2, true), - String::from(" test\n test1\n \n test2\n") + indent(teststring, 2, Some(true), Some(true)), + String::from(" test\n test1\n \n test2") ); } From 7c288e9d6ec35137395cf99ab98866220d74eea9 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Sun, 20 Nov 2022 22:40:00 +0100 Subject: [PATCH 07/13] fixed typos and docstrings --- minijinja/src/filters.rs | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index c526d265..53e749af 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -249,7 +249,6 @@ pub fn escape(state: &State, v: Value) -> Result { mod builtins { use super::*; - use crate::Environment; use crate::error::ErrorKind; use crate::value::{ValueKind, ValueRepr}; use std::borrow::Cow; @@ -755,7 +754,6 @@ mod builtins { /// indents Value with spaces /// - /// This filter is only available if the `indent` feature is enabled. /// The first optional parameter to the filter can be set to `true` to /// indent the first line. The parameter defaults to false. /// the second optional parameter to the filter can be set to `true` @@ -767,7 +765,7 @@ mod builtins { /// config: /// {{ global_conifg|indent(2) }} #does not indent first line /// {{ global_config|indent(2,true) }} #indent whole Value with two spaces - /// {{ global_config|indent(2,true,true)}} #indents whole multiline value + /// {{ global_config|indent(2,true,true)}} #indent whole Value and all Blank Lines value /// ``` #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins"))))] pub fn indent( From bbb722e627ef5188b995a4cefc5150acc71d9056 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Sun, 20 Nov 2022 22:41:07 +0100 Subject: [PATCH 08/13] cleanup of experiments --- minijinja/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minijinja/Cargo.toml b/minijinja/Cargo.toml index e8c0f672..45284e74 100644 --- a/minijinja/Cargo.toml +++ b/minijinja/Cargo.toml @@ -13,7 +13,7 @@ rust-version = "1.61" exclude = ["tests"] [package.metadata.docs.rs] -features = ["source", "json", "urlencode", "indent"] +features = ["source", "json", "urlencode"] rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "doc-header.html"] [features] From 114f32acba5be17c7993197159c7ed43bd6f6308 Mon Sep 17 00:00:00 2001 From: Michael Michel Date: Mon, 21 Nov 2022 18:12:40 +0100 Subject: [PATCH 09/13] simplify by stripping newline from input before applying filter --- minijinja/src/filters.rs | 30 +++++++++++++++++++++++++----- 1 file changed, 25 insertions(+), 5 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index 53e749af..8af2ca39 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -782,16 +782,18 @@ mod builtins { .unwrap_or(input.as_str())) } + let value = strip_trailing_newline(value); + let mut output: String = String::new(); - let mut iterator = value.split('\n').peekable(); + let mut iterator = value.split('\n'); if !indent_first_line.unwrap_or(false) { output.push_str(iterator.next().unwrap()); output.push('\n'); } - while let Some(line) = &iterator.next() { - if iterator.peek().is_none() { - break // because split creates a empty String after the last new line - } + for line in iterator { + // if iterator.peek().is_none() { + // break // because split creates a empty String after the last new line + // } if line.is_empty() { if indent_blank_lines.unwrap_or(false) { output.push_str(String::from(" ").repeat(width).as_str()); @@ -805,6 +807,24 @@ mod builtins { output } + #[test] + #[cfg(feature = "builtins")] + fn test_indent_one_empty_line() { + let teststring = String::from("\n"); + assert_eq!( + indent(teststring, 2, None, None), + String::from("") + ); + } + #[test] + #[cfg(feature = "builtins")] + fn test_indent_one_line() { + let teststring = String::from("test\n"); + assert_eq!( + indent(teststring, 2, None, None), + String::from("test") + ); + } #[test] #[cfg(feature = "builtins")] fn test_indent() { From 840b6d87524dc58f2434a9c0006d63efab64144c Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 27 Nov 2022 19:54:04 +0100 Subject: [PATCH 10/13] rustfmt --- minijinja/src/filters.rs | 21 ++++++++------------- 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index 96da9d84..a8e950b2 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -776,12 +776,13 @@ mod builtins { indent_first_line: Option, indent_blank_lines: Option, ) -> String { - fn strip_trailing_newline(input: String) -> String { - String::from(input - .strip_suffix("\r\n") - .or_else(||input.strip_suffix('\n')) - .unwrap_or(input.as_str())) + String::from( + input + .strip_suffix("\r\n") + .or_else(|| input.strip_suffix('\n')) + .unwrap_or(input.as_str()), + ) } let value = strip_trailing_newline(value); @@ -813,19 +814,13 @@ mod builtins { #[cfg(feature = "builtins")] fn test_indent_one_empty_line() { let teststring = String::from("\n"); - assert_eq!( - indent(teststring, 2, None, None), - String::from("") - ); + assert_eq!(indent(teststring, 2, None, None), String::from("")); } #[test] #[cfg(feature = "builtins")] fn test_indent_one_line() { let teststring = String::from("test\n"); - assert_eq!( - indent(teststring, 2, None, None), - String::from("test") - ); + assert_eq!(indent(teststring, 2, None, None), String::from("test")); } #[test] #[cfg(feature = "builtins")] From e4d3d6561d2c65f612baad59743c2357c28e1f82 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 27 Nov 2022 19:55:38 +0100 Subject: [PATCH 11/13] Move filter tests --- minijinja/src/filters.rs | 103 ++++++++++++++++++++------------------- 1 file changed, 54 insertions(+), 49 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index a8e950b2..8f371d85 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -810,55 +810,6 @@ mod builtins { output } - #[test] - #[cfg(feature = "builtins")] - fn test_indent_one_empty_line() { - let teststring = String::from("\n"); - assert_eq!(indent(teststring, 2, None, None), String::from("")); - } - #[test] - #[cfg(feature = "builtins")] - fn test_indent_one_line() { - let teststring = String::from("test\n"); - assert_eq!(indent(teststring, 2, None, None), String::from("test")); - } - #[test] - #[cfg(feature = "builtins")] - fn test_indent() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, None, None), - String::from("test\n test1\n\n test2") - ); - } - #[test] - #[cfg(feature = "builtins")] - fn test_indent_with_indented_first_line() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, Some(true), None), - String::from(" test\n test1\n\n test2") - ); - } - #[test] - #[cfg(feature = "builtins")] - fn test_indent_with_indented_blank_line() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, None, Some(true)), - String::from("test\n test1\n \n test2") - ); - } - #[test] - #[cfg(feature = "builtins")] - fn test_indent_with_all_indented() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, Some(true), Some(true)), - String::from(" test\n test1\n \n test2") - ); - } - /// URL encodes a value. /// /// If given a map it encodes the parameters into a query set, otherwise it @@ -1042,6 +993,60 @@ mod builtins { ); }); } + + #[test] + #[cfg(feature = "builtins")] + fn test_indent_one_empty_line() { + let teststring = String::from("\n"); + assert_eq!(indent(teststring, 2, None, None), String::from("")); + } + + #[test] + #[cfg(feature = "builtins")] + fn test_indent_one_line() { + let teststring = String::from("test\n"); + assert_eq!(indent(teststring, 2, None, None), String::from("test")); + } + + #[test] + #[cfg(feature = "builtins")] + fn test_indent() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, None, None), + String::from("test\n test1\n\n test2") + ); + } + + #[test] + #[cfg(feature = "builtins")] + fn test_indent_with_indented_first_line() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, Some(true), None), + String::from(" test\n test1\n\n test2") + ); + } + + #[test] + #[cfg(feature = "builtins")] + fn test_indent_with_indented_blank_line() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, None, Some(true)), + String::from("test\n test1\n \n test2") + ); + } + + #[test] + #[cfg(feature = "builtins")] + fn test_indent_with_all_indented() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, Some(true), Some(true)), + String::from(" test\n test1\n \n test2") + ); + } } #[cfg(feature = "builtins")] From cf34ba2121ecb5f68c5c33351c50060be3bcf2b0 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 27 Nov 2022 20:02:04 +0100 Subject: [PATCH 12/13] Optimize indent code --- minijinja/src/filters.rs | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index 8f371d85..f56bcd0d 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -771,21 +771,18 @@ mod builtins { /// ``` #[cfg_attr(docsrs, doc(cfg(all(feature = "builtins"))))] pub fn indent( - value: String, + mut value: String, width: usize, indent_first_line: Option, indent_blank_lines: Option, ) -> String { - fn strip_trailing_newline(input: String) -> String { - String::from( - input - .strip_suffix("\r\n") - .or_else(|| input.strip_suffix('\n')) - .unwrap_or(input.as_str()), - ) + fn strip_trailing_newline(input: &mut String) { + if let Some(stripped) = input.strip_suffix(&['\r', '\n'][..]) { + input.truncate(stripped.len()); + } } - let value = strip_trailing_newline(value); + strip_trailing_newline(&mut value); let mut output: String = String::new(); let mut iterator = value.split('\n'); @@ -794,19 +791,16 @@ mod builtins { output.push('\n'); } for line in iterator { - // if iterator.peek().is_none() { - // break // because split creates a empty String after the last new line - // } if line.is_empty() { if indent_blank_lines.unwrap_or(false) { - output.push_str(String::from(" ").repeat(width).as_str()); + output.push_str(&" ".repeat(width)); } } else { - output.push_str(format!("{}{}", String::from(" ").repeat(width), line).as_str()); + output.push_str(format!("{}{}", " ".repeat(width), line).as_str()); } output.push('\n'); } - output = strip_trailing_newline(output); // strip last newline + strip_trailing_newline(&mut output); output } From f4675a1ed2508b3900063619dc731101bfcb82e3 Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sun, 27 Nov 2022 20:08:49 +0100 Subject: [PATCH 13/13] Isolate filter tests --- minijinja/src/filters.rs | 54 --------------------------------- minijinja/tests/test_filters.rs | 50 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 54 deletions(-) create mode 100644 minijinja/tests/test_filters.rs diff --git a/minijinja/src/filters.rs b/minijinja/src/filters.rs index f56bcd0d..28aab7a4 100644 --- a/minijinja/src/filters.rs +++ b/minijinja/src/filters.rs @@ -987,60 +987,6 @@ mod builtins { ); }); } - - #[test] - #[cfg(feature = "builtins")] - fn test_indent_one_empty_line() { - let teststring = String::from("\n"); - assert_eq!(indent(teststring, 2, None, None), String::from("")); - } - - #[test] - #[cfg(feature = "builtins")] - fn test_indent_one_line() { - let teststring = String::from("test\n"); - assert_eq!(indent(teststring, 2, None, None), String::from("test")); - } - - #[test] - #[cfg(feature = "builtins")] - fn test_indent() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, None, None), - String::from("test\n test1\n\n test2") - ); - } - - #[test] - #[cfg(feature = "builtins")] - fn test_indent_with_indented_first_line() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, Some(true), None), - String::from(" test\n test1\n\n test2") - ); - } - - #[test] - #[cfg(feature = "builtins")] - fn test_indent_with_indented_blank_line() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, None, Some(true)), - String::from("test\n test1\n \n test2") - ); - } - - #[test] - #[cfg(feature = "builtins")] - fn test_indent_with_all_indented() { - let teststring = String::from("test\ntest1\n\ntest2\n"); - assert_eq!( - indent(teststring, 2, Some(true), Some(true)), - String::from(" test\n test1\n \n test2") - ); - } } #[cfg(feature = "builtins")] diff --git a/minijinja/tests/test_filters.rs b/minijinja/tests/test_filters.rs new file mode 100644 index 00000000..9bddd492 --- /dev/null +++ b/minijinja/tests/test_filters.rs @@ -0,0 +1,50 @@ +#![cfg(feature = "builtins")] +use minijinja::filters::indent; + +#[test] +fn test_indent_one_empty_line() { + let teststring = String::from("\n"); + assert_eq!(indent(teststring, 2, None, None), String::from("")); +} + +#[test] +fn test_indent_one_line() { + let teststring = String::from("test\n"); + assert_eq!(indent(teststring, 2, None, None), String::from("test")); +} + +#[test] +fn test_indent() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, None, None), + String::from("test\n test1\n\n test2") + ); +} + +#[test] +fn test_indent_with_indented_first_line() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, Some(true), None), + String::from(" test\n test1\n\n test2") + ); +} + +#[test] +fn test_indent_with_indented_blank_line() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, None, Some(true)), + String::from("test\n test1\n \n test2") + ); +} + +#[test] +fn test_indent_with_all_indented() { + let teststring = String::from("test\ntest1\n\ntest2\n"); + assert_eq!( + indent(teststring, 2, Some(true), Some(true)), + String::from(" test\n test1\n \n test2") + ); +}