From b70de9cd7312d322af4169a02f884f10fd8e6965 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Kijewski?= Date: Sun, 15 Jun 2025 22:05:24 +0200 Subject: [PATCH] Unify splitting function, and finish comment sentences --- askama/src/filters/core.rs | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/askama/src/filters/core.rs b/askama/src/filters/core.rs index 35d10757..00c0f0c9 100644 --- a/askama/src/filters/core.rs +++ b/askama/src/filters/core.rs @@ -617,7 +617,7 @@ impl<'a> fmt::Write for WordCountWriter<'a> { } } -/// Replaces line breaks in plain text with appropriate HTML +/// Replaces line breaks in plain text with appropriate HTML. /// /// A single newline becomes an HTML line break `
` and a new line /// followed by a blank line becomes a paragraph break `

`. @@ -733,9 +733,7 @@ impl fmt::Write for NewlineCountingFormatter<'_, W> { if s.is_empty() { return Ok(()); } - - for line in s.split_inclusive('\n') { - let (has_eol, line) = strip_newline_suffix(line); + for (has_eol, line) in split_lines(s) { if !line.is_empty() { match replace(&mut self.counter, if has_eol { 1 } else { 0 }) { ..=0 => {} @@ -751,7 +749,7 @@ impl fmt::Write for NewlineCountingFormatter<'_, W> { } } -/// Converts all newlines in a piece of plain text to HTML line breaks +/// Converts all newlines in a piece of plain text to HTML line breaks. /// /// ``` /// # #[cfg(feature = "code-in-doc")] { @@ -802,8 +800,10 @@ impl FastWritable for Linebreaksbr { impl fmt::Write for LinebreaksbrFormatter<'_, W> { fn write_str(&mut self, s: &str) -> fmt::Result { - for line in s.split_inclusive('\n') { - let (has_eol, line) = strip_newline_suffix(line); + if s.is_empty() { + return Ok(()); + } + for (has_eol, line) in split_lines(s) { self.0.write_str(line)?; if has_eol { self.0.write_str("
")?; @@ -813,15 +813,16 @@ impl fmt::Write for LinebreaksbrFormatter<'_, W> { } } -/// Returns whether a newline suffix was stripped and the (maybe stripped) line. -fn strip_newline_suffix(line: &str) -> (bool, &str) { - if let Some(line) = line.strip_suffix("\r\n") { - (true, line) - } else if let Some(line) = line.strip_suffix('\n') { - (true, line) - } else { - (false, line) - } +/// Splits the input at `/\r?\n/g``; returns whether a newline suffix was stripped and the +/// (maybe stripped) line. +fn split_lines(s: &str) -> impl Iterator { + s.split_inclusive('\n').map(|line| { + if let Some(line) = line.strip_suffix('\n') { + (true, line.strip_suffix('\r').unwrap_or(line)) + } else { + (false, line) + } + }) } #[cfg(all(test, feature = "alloc"))]