mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-28 21:41:35 +00:00
Rename suppress_whitespace
into whitespace
and update expected values to "suppress" and "preserve"
This commit is contained in:
parent
b16085acd6
commit
a64a727331
@ -1,7 +1,9 @@
|
|||||||
use crate::heritage::{Context, Heritage};
|
use crate::heritage::{Context, Heritage};
|
||||||
use crate::input::{Print, Source, TemplateInput};
|
use crate::input::{Print, Source, TemplateInput};
|
||||||
use crate::parser::{parse, Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws};
|
use crate::parser::{parse, Cond, CondTest, Expr, Loop, Node, Target, When, Whitespace, Ws};
|
||||||
use crate::{filters, get_template_source, read_config_file, CompileError, Config};
|
use crate::{
|
||||||
|
filters, get_template_source, read_config_file, CompileError, Config, WhitespaceHandling,
|
||||||
|
};
|
||||||
|
|
||||||
use proc_macro2::TokenStream;
|
use proc_macro2::TokenStream;
|
||||||
use quote::{quote, ToTokens};
|
use quote::{quote, ToTokens};
|
||||||
@ -66,7 +68,7 @@ fn build_template(ast: &syn::DeriveInput) -> Result<String, CompileError> {
|
|||||||
&contexts,
|
&contexts,
|
||||||
heritage.as_ref(),
|
heritage.as_ref(),
|
||||||
MapChain::new(),
|
MapChain::new(),
|
||||||
config.suppress_whitespace,
|
config.whitespace,
|
||||||
)
|
)
|
||||||
.build(&contexts[input.path.as_path()])?;
|
.build(&contexts[input.path.as_path()])?;
|
||||||
if input.print == Print::Code || input.print == Print::All {
|
if input.print == Print::Code || input.print == Print::All {
|
||||||
@ -251,8 +253,9 @@ struct Generator<'a, S: std::hash::BuildHasher> {
|
|||||||
buf_writable: Vec<Writable<'a>>,
|
buf_writable: Vec<Writable<'a>>,
|
||||||
// Counter for write! hash named arguments
|
// Counter for write! hash named arguments
|
||||||
named: usize,
|
named: usize,
|
||||||
// If set to `true`, the whitespace characters will be removed by default unless `+` is used.
|
// If set to `suppress`, the whitespace characters will be removed by default unless `+` is
|
||||||
suppress_whitespace: bool,
|
// used.
|
||||||
|
whitespace: WhitespaceHandling,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
||||||
@ -261,7 +264,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
|||||||
contexts: &'n HashMap<&'n Path, Context<'n>, S>,
|
contexts: &'n HashMap<&'n Path, Context<'n>, S>,
|
||||||
heritage: Option<&'n Heritage<'_>>,
|
heritage: Option<&'n Heritage<'_>>,
|
||||||
locals: MapChain<'n, &'n str, LocalMeta>,
|
locals: MapChain<'n, &'n str, LocalMeta>,
|
||||||
suppress_whitespace: bool,
|
whitespace: WhitespaceHandling,
|
||||||
) -> Generator<'n, S> {
|
) -> Generator<'n, S> {
|
||||||
Generator {
|
Generator {
|
||||||
input,
|
input,
|
||||||
@ -273,7 +276,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
|||||||
super_block: None,
|
super_block: None,
|
||||||
buf_writable: vec![],
|
buf_writable: vec![],
|
||||||
named: 0,
|
named: 0,
|
||||||
suppress_whitespace,
|
whitespace,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -284,7 +287,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
|||||||
self.contexts,
|
self.contexts,
|
||||||
self.heritage,
|
self.heritage,
|
||||||
locals,
|
locals,
|
||||||
self.suppress_whitespace,
|
self.whitespace,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1820,7 +1823,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
|||||||
match ws {
|
match ws {
|
||||||
Some(Whitespace::Trim) => true,
|
Some(Whitespace::Trim) => true,
|
||||||
Some(Whitespace::Preserve) => false,
|
Some(Whitespace::Preserve) => false,
|
||||||
None => self.suppress_whitespace,
|
None => self.whitespace == WhitespaceHandling::Suppress,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -1832,7 +1835,7 @@ impl<'a, S: std::hash::BuildHasher> Generator<'a, S> {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// If `suppress_whitespace` is enabled, we keep the whitespace characters only if there is
|
// If `whitespace` is set to `suppress`, we keep the whitespace characters only if there is
|
||||||
// a `+` character.
|
// a `+` character.
|
||||||
if !self.should_trim_ws(ws.0) {
|
if !self.should_trim_ws(ws.0) {
|
||||||
let val = self.next_ws.unwrap();
|
let val = self.next_ws.unwrap();
|
||||||
|
@ -118,7 +118,7 @@ struct Config<'a> {
|
|||||||
syntaxes: BTreeMap<String, Syntax<'a>>,
|
syntaxes: BTreeMap<String, Syntax<'a>>,
|
||||||
default_syntax: &'a str,
|
default_syntax: &'a str,
|
||||||
escapers: Vec<(HashSet<String>, String)>,
|
escapers: Vec<(HashSet<String>, String)>,
|
||||||
suppress_whitespace: bool,
|
whitespace: WhitespaceHandling,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Config<'_> {
|
impl Config<'_> {
|
||||||
@ -135,19 +135,23 @@ impl Config<'_> {
|
|||||||
RawConfig::from_toml_str(s)?
|
RawConfig::from_toml_str(s)?
|
||||||
};
|
};
|
||||||
|
|
||||||
let (dirs, default_syntax, suppress_whitespace) = match raw.general {
|
let (dirs, default_syntax, whitespace) = match raw.general {
|
||||||
Some(General {
|
Some(General {
|
||||||
dirs,
|
dirs,
|
||||||
default_syntax,
|
default_syntax,
|
||||||
suppress_whitespace,
|
whitespace,
|
||||||
}) => (
|
}) => (
|
||||||
dirs.map_or(default_dirs, |v| {
|
dirs.map_or(default_dirs, |v| {
|
||||||
v.into_iter().map(|dir| root.join(dir)).collect()
|
v.into_iter().map(|dir| root.join(dir)).collect()
|
||||||
}),
|
}),
|
||||||
default_syntax.unwrap_or(DEFAULT_SYNTAX_NAME),
|
default_syntax.unwrap_or(DEFAULT_SYNTAX_NAME),
|
||||||
suppress_whitespace.unwrap_or(false),
|
whitespace,
|
||||||
|
),
|
||||||
|
None => (
|
||||||
|
default_dirs,
|
||||||
|
DEFAULT_SYNTAX_NAME,
|
||||||
|
WhitespaceHandling::default(),
|
||||||
),
|
),
|
||||||
None => (default_dirs, DEFAULT_SYNTAX_NAME, false),
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if let Some(raw_syntaxes) = raw.syntax {
|
if let Some(raw_syntaxes) = raw.syntax {
|
||||||
@ -189,7 +193,7 @@ impl Config<'_> {
|
|||||||
syntaxes,
|
syntaxes,
|
||||||
default_syntax,
|
default_syntax,
|
||||||
escapers,
|
escapers,
|
||||||
suppress_whitespace,
|
whitespace,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -302,12 +306,29 @@ impl RawConfig<'_> {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Copy, PartialEq, Eq, Debug)]
|
||||||
|
#[cfg_attr(feature = "serde", derive(Deserialize))]
|
||||||
|
#[cfg_attr(feature = "serde", serde(field_identifier, rename_all = "lowercase"))]
|
||||||
|
pub(crate) enum WhitespaceHandling {
|
||||||
|
/// The default behaviour. It will leave the whitespace characters "as is".
|
||||||
|
Preserve,
|
||||||
|
/// It'll remove all the whitespace characters before and after the jinja block.
|
||||||
|
Suppress,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Default for WhitespaceHandling {
|
||||||
|
fn default() -> Self {
|
||||||
|
WhitespaceHandling::Preserve
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Deserialize))]
|
||||||
struct General<'a> {
|
struct General<'a> {
|
||||||
#[cfg_attr(feature = "serde", serde(borrow))]
|
#[cfg_attr(feature = "serde", serde(borrow))]
|
||||||
dirs: Option<Vec<&'a str>>,
|
dirs: Option<Vec<&'a str>>,
|
||||||
default_syntax: Option<&'a str>,
|
default_syntax: Option<&'a str>,
|
||||||
suppress_whitespace: Option<bool>,
|
#[cfg_attr(feature = "serde", serde(default))]
|
||||||
|
whitespace: WhitespaceHandling,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg_attr(feature = "serde", derive(Deserialize))]
|
#[cfg_attr(feature = "serde", derive(Deserialize))]
|
||||||
@ -659,14 +680,26 @@ mod tests {
|
|||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_suppress_whitespace_parsing() {
|
fn test_whitespace_parsing() {
|
||||||
let config = Config::new(
|
let config = Config::new(
|
||||||
r#"
|
r#"
|
||||||
[general]
|
[general]
|
||||||
suppress_whitespace = true
|
whitespace = "suppress"
|
||||||
"#,
|
"#,
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
assert!(config.suppress_whitespace);
|
assert_eq!(config.whitespace, WhitespaceHandling::Suppress);
|
||||||
|
|
||||||
|
let config = Config::new(r#""#).unwrap();
|
||||||
|
assert_eq!(config.whitespace, WhitespaceHandling::Preserve);
|
||||||
|
|
||||||
|
let config = Config::new(
|
||||||
|
r#"
|
||||||
|
[general]
|
||||||
|
whitespace = "preserve"
|
||||||
|
"#,
|
||||||
|
)
|
||||||
|
.unwrap();
|
||||||
|
assert_eq!(config.whitespace, WhitespaceHandling::Preserve);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,2 +1,2 @@
|
|||||||
[general]
|
[general]
|
||||||
suppress_whitespace = true
|
whitespace = "suppress"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user