Skip to content

Commit 0c7f034

Browse files
authored
Merge pull request #163 from hronro/main
Implement rm_newline Thanks @hronro !
2 parents 47e281c + 410ddef commit 0c7f034

File tree

11 files changed

+147
-17
lines changed

11 files changed

+147
-17
lines changed

sailfish-compiler/src/compiler.rs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,9 @@ impl Compiler {
6666
output: &Path,
6767
) -> Result<(), Error> {
6868
let analyzer = Analyzer::new();
69-
let optimizer = Optimizer::new().rm_whitespace(self.config.rm_whitespace);
69+
let optimizer = Optimizer::new()
70+
.rm_whitespace(self.config.rm_whitespace)
71+
.rm_newline(self.config.rm_newline);
7072

7173
let compile_file = |mut tsource: TranslatedSource,
7274
output: &Path|
@@ -116,7 +118,9 @@ impl Compiler {
116118
let parser = Parser::new().delimiter(self.config.delimiter);
117119
let translator = Translator::new().escape(self.config.escape);
118120
let resolver = Resolver::new().include_handler(include_handler);
119-
let optimizer = Optimizer::new().rm_whitespace(self.config.rm_whitespace);
121+
let optimizer = Optimizer::new()
122+
.rm_whitespace(self.config.rm_whitespace)
123+
.rm_newline(self.config.rm_newline);
120124

121125
let compile = || -> Result<String, Error> {
122126
let stream = parser.parse(input);

sailfish-compiler/src/config.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ pub struct Config {
55
pub delimiter: char,
66
pub escape: bool,
77
pub rm_whitespace: bool,
8+
pub rm_newline: bool,
89
pub template_dirs: Vec<PathBuf>,
910
#[doc(hidden)]
1011
pub cache_dir: PathBuf,
@@ -20,6 +21,7 @@ impl Default for Config {
2021
escape: true,
2122
cache_dir: Path::new(env!("OUT_DIR")).join("cache"),
2223
rm_whitespace: false,
24+
rm_newline: false,
2325
_non_exhaustive: (),
2426
}
2527
}
@@ -82,6 +84,10 @@ mod imp {
8284
if let Some(rm_whitespace) = optimizations.rm_whitespace {
8385
config.rm_whitespace = rm_whitespace;
8486
}
87+
88+
if let Some(rm_newline) = optimizations.rm_newline {
89+
config.rm_newline = rm_newline;
90+
}
8591
}
8692
}
8793

@@ -96,6 +102,7 @@ mod imp {
96102
#[serde(deny_unknown_fields)]
97103
struct Optimizations {
98104
rm_whitespace: Option<bool>,
105+
rm_newline: Option<bool>,
99106
}
100107

101108
#[derive(Deserialize, Debug)]

sailfish-compiler/src/optimizer.rs

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ use syn::{
99

1010
pub struct Optimizer {
1111
rm_whitespace: bool,
12+
rm_newline: bool,
1213
}
1314

1415
impl Optimizer {
1516
#[inline]
1617
pub fn new() -> Self {
1718
Self {
1819
rm_whitespace: false,
20+
rm_newline: false,
1921
}
2022
}
2123

@@ -25,17 +27,45 @@ impl Optimizer {
2527
self
2628
}
2729

30+
#[inline]
31+
pub fn rm_newline(mut self, new: bool) -> Self {
32+
self.rm_newline = new;
33+
self
34+
}
35+
2836
#[inline]
2937
pub fn optimize(&self, i: &mut Block) {
3038
OptmizerImpl {
3139
rm_whitespace: self.rm_whitespace,
40+
rm_newline: self.rm_newline,
3241
}
3342
.visit_block_mut(i);
3443
}
3544
}
3645

3746
struct OptmizerImpl {
3847
rm_whitespace: bool,
48+
rm_newline: bool,
49+
}
50+
51+
impl OptmizerImpl {
52+
fn apply_optimizations(&self, v: String) -> Option<TokenStream> {
53+
let mut optimized = v.to_string();
54+
55+
if self.rm_whitespace {
56+
optimized = remove_whitespace(&optimized);
57+
}
58+
if self.rm_newline {
59+
optimized = remove_newlines(&optimized);
60+
}
61+
62+
// Only return a token stream if the string was actually modified
63+
if optimized != v {
64+
Some(quote! { __sf_buf, #optimized })
65+
} else {
66+
None
67+
}
68+
}
3969
}
4070

4171
impl VisitMut for OptmizerImpl {
@@ -120,12 +150,8 @@ impl VisitMut for OptmizerImpl {
120150
}
121151

122152
fn visit_stmt_macro_mut(&mut self, i: &mut StmtMacro) {
123-
if self.rm_whitespace {
124-
if let Some(v) = get_rendertext_value(&i.mac) {
125-
let ts = match remove_whitespace(v) {
126-
Some(value) => value,
127-
None => return,
128-
};
153+
if let Some(v) = get_rendertext_value(&i.mac) {
154+
if let Some(ts) = self.apply_optimizations(v) {
129155
i.mac.tokens = ts;
130156
return;
131157
}
@@ -135,12 +161,8 @@ impl VisitMut for OptmizerImpl {
135161
}
136162

137163
fn visit_expr_macro_mut(&mut self, i: &mut ExprMacro) {
138-
if self.rm_whitespace {
139-
if let Some(v) = get_rendertext_value(&i.mac) {
140-
let ts = match remove_whitespace(v) {
141-
Some(value) => value,
142-
None => return,
143-
};
164+
if let Some(v) = get_rendertext_value(&i.mac) {
165+
if let Some(ts) = self.apply_optimizations(v) {
144166
i.mac.tokens = ts;
145167
return;
146168
}
@@ -150,15 +172,15 @@ impl VisitMut for OptmizerImpl {
150172
}
151173
}
152174

153-
fn remove_whitespace(v: String) -> Option<TokenStream> {
175+
fn remove_whitespace(v: &str) -> String {
154176
let mut buffer = String::new();
155177
let mut it = v.lines().peekable();
156178
if let Some(line) = it.next() {
157179
if it.peek().is_some() {
158180
buffer.push_str(line.trim_end());
159181
buffer.push('\n');
160182
} else {
161-
return None;
183+
return v.to_string();
162184
}
163185
}
164186
while let Some(line) = it.next() {
@@ -174,8 +196,11 @@ fn remove_whitespace(v: String) -> Option<TokenStream> {
174196
buffer.push_str(line.trim_start());
175197
}
176198
}
199+
buffer
200+
}
177201

178-
Some(quote! { __sf_buf, #buffer })
202+
fn remove_newlines(v: &str) -> String {
203+
v.replace(['\n', '\r'], "")
179204
}
180205

181206
fn get_rendertext_value(mac: &Macro) -> Option<String> {

sailfish-compiler/src/procmacro.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ struct DeriveTemplateOptions {
2424
delimiter: Option<LitChar>,
2525
escape: Option<LitBool>,
2626
rm_whitespace: Option<LitBool>,
27+
rm_newline: Option<LitBool>,
2728
}
2829

2930
impl DeriveTemplateOptions {
@@ -49,6 +50,8 @@ impl DeriveTemplateOptions {
4950
self.escape = Some(s.parse::<LitBool>()?);
5051
} else if key == "rm_whitespace" {
5152
self.rm_whitespace = Some(s.parse::<LitBool>()?);
53+
} else if key == "rm_newline" {
54+
self.rm_newline = Some(s.parse::<LitBool>()?);
5255
} else {
5356
return Err(syn::Error::new(
5457
key.span(),
@@ -81,6 +84,9 @@ fn merge_config_options(config: &mut Config, options: &DeriveTemplateOptions) {
8184
if let Some(ref rm_whitespace) = options.rm_whitespace {
8285
config.rm_whitespace = rm_whitespace.value;
8386
}
87+
if let Some(ref rm_newline) = options.rm_newline {
88+
config.rm_newline = rm_newline.value;
89+
}
8490
}
8591

8692
fn resolve_template_file(path: &str, template_dirs: &[PathBuf]) -> Option<PathBuf> {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div> <span>1</span> <span>2</span> <span>3</span></div><div> trailing spaces This line should be appeared on the same line as the previous line</div> <div>foo</div> <div>bar</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div>
2+
<span>1</span>
3+
<span>2</span>
4+
5+
<span>3</span>
6+
</div>
7+
<div>
8+
trailing spaces
9+
This line should be appeared on the same line as the previous line
10+
</div>
11+
<% for msg in self.messages { %>
12+
<div><%= msg %></div>
13+
<% } %>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div>
2+
<span>1</span>
3+
<span>2</span>
4+
5+
<span>3</span>
6+
</div>
7+
<div>
8+
trailing spaces
9+
This line should be appeared on the same line as the previous line
10+
</div>
11+
<% for msg in messages { %>
12+
<div><%= msg %></div>
13+
<% } %>
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
<div><span>1</span><span>2</span><span>3</span></div><div>trailing spacesThis line should be appeared on the same line as the previous line</div><div>foo</div><div>bar</div>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div>
2+
<span>1</span>
3+
<span>2</span>
4+
5+
<span>3</span>
6+
</div>
7+
<div>
8+
trailing spaces
9+
This line should be appeared on the same line as the previous line
10+
</div>
11+
<% for msg in self.messages { %>
12+
<div><%= msg %></div>
13+
<% } %>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<div>
2+
<span>1</span>
3+
<span>2</span>
4+
5+
<span>3</span>
6+
</div>
7+
<div>
8+
trailing spaces
9+
This line should be appeared on the same line as the previous line
10+
</div>
11+
<% for msg in messages { %>
12+
<div><%= msg %></div>
13+
<% } %>

0 commit comments

Comments
 (0)