Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
name = "mustache"
description = "Rust implementation of Mustache"
repository = "https://github.com/nickel-org/rust-mustache"
version = "0.6.2"
version = "0.6.3"
authors = ["erick.tryzelaar@gmail.com"]
license = "MIT/Apache-2.0"

Expand Down
27 changes: 22 additions & 5 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,15 @@ impl Template {
struct RenderContext<'a> {
template: &'a Template,
indent: String,
line_start: bool,
}

impl<'a> RenderContext<'a> {
fn new(template: &'a Template) -> RenderContext<'a> {
RenderContext {
template: template,
indent: "".to_string(),
line_start: true,
}
}

Expand Down Expand Up @@ -114,14 +116,29 @@ impl<'a> RenderContext<'a> {
}
}

fn write_tracking_newlines<W: Write>(&mut self, wr: &mut W, value: &str) {
wr.write_all(value.as_bytes()).unwrap();
self.line_start = match value.chars().last() {
None => self.line_start, // None == ""
Some('\n') => true,
_ => false
};
}

fn write_indent<W: Write>(&mut self, wr: &mut W) {
if self.line_start {
wr.write_all(self.indent.as_bytes()).unwrap();
}
}

fn render_text<W: Write>(
&mut self,
wr: &mut W,
value: &str
) {
// Indent the lines.
if self.indent.is_empty() {
wr.write_all(value.as_bytes()).unwrap();
self.write_tracking_newlines(wr, value);
} else {
let mut pos = 0;
let len = value.len();
Expand All @@ -142,10 +159,10 @@ impl<'a> RenderContext<'a> {
};

if line.as_bytes()[0] != b'\n' {
wr.write_all(self.indent.as_bytes()).unwrap();
self.write_indent(wr)
}

wr.write_all(line.as_bytes()).unwrap();
self.write_tracking_newlines(wr, line);
}
}
}
Expand Down Expand Up @@ -183,7 +200,7 @@ impl<'a> RenderContext<'a> {
match self.find(path, stack) {
None => { }
Some(mut value) => {
wr.write_all(self.indent.as_bytes()).unwrap();
self.write_indent(wr);

// Currently this doesn't allow Option<Option<Foo>>, which
// would be un-nameable in the view anyway, so I'm unsure if it's
Expand All @@ -198,7 +215,7 @@ impl<'a> RenderContext<'a> {

match *value {
StrVal(ref value) => {
wr.write_all(value.as_bytes()).unwrap();
self.write_tracking_newlines(wr, value);
}

// etags and utags use the default delimiter.
Expand Down