diff options
author | Aaron Power <theaaronepower@gmail.com> | 2018-06-29 11:36:45 +0100 |
---|---|---|
committer | Dirkjan Ochtman <dirkjan@ochtman.nl> | 2018-06-29 13:25:12 +0200 |
commit | 8228ec4c058f5ec21afd0ae2b62b86e922f9befd (patch) | |
tree | 61229d7fd85e9728309e2bd1d8b5b07adf838e1a | |
parent | 970e3ecfbdf9e4e4c4faacdb87efa656e812398a (diff) | |
download | askama-8228ec4c058f5ec21afd0ae2b62b86e922f9befd.tar.gz askama-8228ec4c058f5ec21afd0ae2b62b86e922f9befd.tar.bz2 askama-8228ec4c058f5ec21afd0ae2b62b86e922f9befd.zip |
Added linebreaks and linebreaksbr filters
Diffstat (limited to '')
-rw-r--r-- | askama_shared/src/filters/mod.rs | 33 |
1 files changed, 32 insertions, 1 deletions
diff --git a/askama_shared/src/filters/mod.rs b/askama_shared/src/filters/mod.rs index 65215e2..f0a6090 100644 --- a/askama_shared/src/filters/mod.rs +++ b/askama_shared/src/filters/mod.rs @@ -18,11 +18,13 @@ use escaping::{self, MarkupDisplay}; // Askama or should refer to a local `filters` module. It should contain all the // filters shipped with Askama, even the optional ones (since optional inclusion // in the const vector based on features seems impossible right now). -pub const BUILT_IN_FILTERS: [&str; 12] = [ +pub const BUILT_IN_FILTERS: [&str; 14] = [ "e", "escape", "format", "join", + "linebreaks", + "linebreaksbr", "lower", "lowercase", "safe", @@ -69,6 +71,22 @@ where /// the Askama code generator. pub fn format() {} +/// Replaces line breaks in plain text with appropriate HTML; a single newline +/// becomes an HTML line break `<br>` and a new line followed by a blank line +/// becomes a paragraph break `<p>`. +pub fn linebreaks(s: &fmt::Display) -> Result<String> { + let s = format!("{}", s); + let linebroken = s.replace("\n\n", "</p><p>").replace("\n", "<br/>"); + + Ok(format!("<p>{}</p>", linebroken)) +} + +/// Converts all newlines in a piece of plain text to HTML line breaks. +pub fn linebreaksbr(s: &fmt::Display) -> Result<String> { + let s = format!("{}", s); + Ok(s.replace("\n", "<br/>")) +} + /// Converts to lowercase. pub fn lower(s: &fmt::Display) -> Result<String> { let s = format!("{}", s); @@ -134,6 +152,19 @@ where #[cfg(test)] mod tests { use super::*; + + #[test] + fn test_linebreaks() { + assert_eq!(linebreaks(&"Foo\nBar Baz").unwrap(), "<p>Foo<br/>Bar Baz</p>"); + assert_eq!(linebreaks(&"Foo\nBar\n\nBaz").unwrap(), "<p>Foo<br/>Bar</p><p>Baz</p>"); + } + + #[test] + fn test_linebreaksbr() { + assert_eq!(linebreaksbr(&"Foo\nBar").unwrap(), "Foo<br/>Bar"); + assert_eq!(linebreaksbr(&"Foo\nBar\n\nBaz").unwrap(), "Foo<br/>Bar<br/><br/>Baz"); + } + #[test] fn test_lower() { assert_eq!(lower(&"Foo").unwrap(), "foo"); |