From b9490c4b5d370760825a3cd14b97f1163e22e6f5 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= `.
-/// Paragraph tags only wrap content; empty paragraphs are removed.
-/// No ` Foo\nBar Baz
"))
}
-/// Replaces only paragraph breaks in plain text with appropriate HTML
-///
-/// A new line followed by a blank line becomes a paragraph break `
` tags are added.
-///
-/// ```
-/// # #[cfg(feature = "code-in-doc")] {
-/// # use askama::Template;
-/// /// ```jinja
-/// /// {{ lines|paragraphbreaks }}
-/// /// ```
-/// #[derive(Template)]
-/// #[template(ext = "html", in_doc = true)]
-/// struct Example<'a> {
-/// lines: &'a str,
-/// }
-///
-/// assert_eq!(
-/// Example { lines: "Foo\nBar\n\nBaz" }.to_string(),
-/// "(S);
-
-impl {
- #[inline]
- fn fmt(&self, dest: &mut fmt::Formatter<'_>) -> fmt::Result {
- let mut buffer;
- flush_paragraphbreaks(dest, try_to_str!(self.0 => buffer))
- }
-}
-
-impl {
- #[inline]
- fn write_into
").replace("
", ""); - write!(dest, "{linebroken}
") -} - /// Converts to lowercase /// /// ``` @@ -609,24 +550,6 @@ mod tests { ); } - #[test] - fn test_paragraphbreaks() { - assert_eq!( - paragraphbreaks("Foo\nBar Baz").unwrap().to_string(), - "Foo\nBar Baz
" - ); - assert_eq!( - paragraphbreaks("Foo\nBar\n\nBaz").unwrap().to_string(), - "Foo\nBar
Baz
" - ); - assert_eq!( - paragraphbreaks("Foo\n\n\n\n\nBar\n\nBaz") - .unwrap() - .to_string(), - "Foo
\nBar
Baz
" - ); - } - #[test] fn test_lower() { assert_eq!(lower("Foo").unwrap().to_string(), "foo"); diff --git a/askama/src/filters/core.rs b/askama/src/filters/core.rs index 9b17aa09..35d01371 100644 --- a/askama/src/filters/core.rs +++ b/askama/src/filters/core.rs @@ -641,41 +641,94 @@ impl<'a> fmt::Write for WordCountWriter<'a> { /// # } /// ``` #[inline] -pub fn linebreaks`.
+/// Paragraph tags only wrap content; empty paragraphs are removed.
+/// No `
` tags are added.
+///
+/// ```
+/// # #[cfg(feature = "code-in-doc")] {
+/// # use askama::Template;
+/// /// ```jinja
+/// /// {{ lines|paragraphbreaks }}
+/// /// ```
+/// #[derive(Template)]
+/// #[template(ext = "html", in_doc = true)]
+/// struct Example<'a> {
+/// lines: &'a str,
+/// }
+///
+/// assert_eq!(
+/// Example { lines: "Foo\nBar\n\nBaz" }.to_string(),
+/// "
Foo\nBar
Baz
" +/// ); +/// # } +/// ``` +#[inline] +pub fn paragraphbreaks")?; - let mut formatter = LinebreakFormatter { dest, counter: -1 }; - write!(formatter, "{}", self.0)?; - formatter.dest.write_str("
") +pub struct NewlineCounting")?; + inner(&mut formatter)?; + formatter.dest.write_str("
")?; + Ok(()) } } -impl")?; - let mut formatter = LinebreakFormatter { dest, counter: -1 }; - self.0.write_into(&mut formatter, values)?; - dest.write_str("
")?; - Ok(()) + self.run(dest, |f| self.source.write_into(f, values)) } } -struct LinebreakFormatter<'a, W: ?Sized> { +struct NewlineCountingFormatter<'a, W: ?Sized> { dest: &'a mut W, counter: isize, + one: &'static str, } -impl")?, } self.dest.write_str(line)?; @@ -827,4 +880,22 @@ mod tests { "
Foo
Bar
Baz
" ); } + + #[test] + fn test_paragraphbreaks() { + assert_eq!( + paragraphbreaks("Foo\nBar Baz").unwrap().to_string(), + "Foo\nBar Baz
" + ); + assert_eq!( + paragraphbreaks("Foo\nBar\n\nBaz").unwrap().to_string(), + "Foo\nBar
Baz
" + ); + assert_eq!( + paragraphbreaks("Foo\n\n\n\n\nBar\n\nBaz") + .unwrap() + .to_string(), + "Foo
Bar
Baz
" + ); + } } diff --git a/askama/src/filters/mod.rs b/askama/src/filters/mod.rs index 1c5a515c..af07b62c 100644 --- a/askama/src/filters/mod.rs +++ b/askama/src/filters/mod.rs @@ -25,11 +25,12 @@ mod urlencode; #[cfg(feature = "alloc")] pub use self::alloc::{ - capitalize, fmt, format, linebreaksbr, lower, lowercase, paragraphbreaks, title, titlecase, - trim, upper, uppercase, + capitalize, fmt, format, linebreaksbr, lower, lowercase, title, titlecase, trim, upper, + uppercase, }; pub use self::core::{ - PluralizeCount, center, join, linebreaks, pluralize, reject, reject_with, truncate, wordcount, + PluralizeCount, center, join, linebreaks, paragraphbreaks, pluralize, reject, reject_with, + truncate, wordcount, }; pub use self::escape::{ AutoEscape, AutoEscaper, Escaper, Html, HtmlSafe, HtmlSafeOutput, MaybeSafe, Safe, Text, diff --git a/askama_derive/src/generator/filter.rs b/askama_derive/src/generator/filter.rs index 59b70327..04abc80c 100644 --- a/askama_derive/src/generator/filter.rs +++ b/askama_derive/src/generator/filter.rs @@ -323,9 +323,8 @@ impl<'a> Generator<'a, '_> { ctx: &Context<'_>, buf: &mut Buffer, args: &[WithSpan<'a, Expr<'a>>], - node: Span<'_>, + _node: Span<'_>, ) -> Result-enabled by- ```jinja {{ text_to_break | paragraphbreaks }} ``` diff --git a/testing/tests/filter_block.rs b/testing/tests/filter_block.rs index c7797d9f..ba0e8927 100644 --- a/testing/tests/filter_block.rs +++ b/testing/tests/filter_block.rs @@ -302,11 +302,9 @@ fn filter_nested_filter_blocks() { }; assert_eq!( template.render().unwrap(), - r"[ -<P>HELLO &</P><P>GOODBYE! -</P> -<P>HELLO &</P><P>GOODBYE! -</P>] + "[ +<P>HELLO &</P><P>GOODBYE!</P> +<P>HELLO &</P><P>GOODBYE!</P>] 2" ); }"alloc"
-enabled by"default"
-