Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit multi-line string values as block scalars #136

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
84 changes: 70 additions & 14 deletions src/emitter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ impl<'a> YamlEmitter<'a> {
// write DocumentStart
writeln!(self.writer, "---")?;
self.level = -1;
self.emit_node(doc)
self.emit_node(false, doc)?;
writeln!(self.writer)?;
write!(self.writer, "...")?;
Ok(())
}

fn write_indent(&mut self) -> EmitResult {
Expand All @@ -156,13 +159,45 @@ impl<'a> YamlEmitter<'a> {
Ok(())
}

fn emit_node(&mut self, node: &Yaml) -> EmitResult {
fn emit_node(&mut self, is_val: bool, node: &Yaml) -> EmitResult {
match *node {
Yaml::Array(ref v) => self.emit_array(v),
Yaml::Hash(ref h) => self.emit_hash(h),
Yaml::String(ref v) => {
if need_quotes(v) {
escape_str(self.writer, v)?;
// For multi-line string values, use a block scalar.
if is_val && v.contains("\n") && is_valid_literal_block_scalar(v) {
write!(
self.writer,
"|{}{}",
// If the string ends in a newline, we need to have YAML preserve the
// newline characters using the "keep" chomp indicator.
if v.ends_with("\n") {
"+"
// Otherwise, it should strip them using the "strip" chomp indicator.
} else {
"-"
},
// Number of additional indent characters.
self.best_indent,
)?;
self.level += 1;
let mut lines = v.split("\n").peekable();
while let Some(line) = lines.next() {
// The last line is special: if it's blank, that means the string ends
// in a newline character and we used the "keep" chomp indicator above.
// In that case, we should suppress the last, empty line. Otherwise,
// print it normally.
if lines.peek().is_some() || !line.is_empty() {
writeln!(self.writer)?;
self.write_indent()?;
write!(self.writer, "{}", line)?;
}
}
self.level -= 1;
} else {
escape_str(self.writer, v)?;
}
} else {
write!(self.writer, "{}", v)?;
}
Expand Down Expand Up @@ -233,7 +268,7 @@ impl<'a> YamlEmitter<'a> {
write!(self.writer, ":")?;
self.emit_val(true, v)?;
} else {
self.emit_node(k)?;
self.emit_node(false, k)?;
write!(self.writer, ":")?;
self.emit_val(false, v)?;
}
Expand Down Expand Up @@ -273,7 +308,7 @@ impl<'a> YamlEmitter<'a> {
}
_ => {
write!(self.writer, " ")?;
self.emit_node(val)
self.emit_node(true, val)
}
}
}
Expand Down Expand Up @@ -340,6 +375,18 @@ fn need_quotes(string: &str) -> bool {
|| string.parse::<f64>().is_ok()
}

/// Check if the string can be expressed a valid literal block scalar.
/// The YAML spec supports all of the following in block literals except #xFEFF:
/// #x9 | #xA | [#x20-#x7E] /* 8 bit */
/// | #x85 | [#xA0-#xD7FF] | [#xE000-#xFFFD] /* 16 bit */
/// | [#x10000-#x10FFFF] /* 32 bit */
fn is_valid_literal_block_scalar(string: &str) -> bool {
string.chars().all(|character: char| match character {
'\t' | '\n' | '\x20'...'\x7e' | '\u{0085}' | '\u{00a0}'...'\u{d7ff}' => true,
_ => false,
})
}

#[cfg(test)]
mod test {
use super::*;
Expand All @@ -358,7 +405,7 @@ a3: [1, 2, 3]
a4:
- [a1, a2]
- 2
";
...";

let docs = YamlLoader::load_from_str(&s).unwrap();
let doc = &docs[0];
Expand Down Expand Up @@ -448,7 +495,8 @@ products:
"{}": empty hash key
x: test
y: avoid quoting here
z: string with spaces"#;
z: string with spaces
..."#;

let docs = YamlLoader::load_from_str(&s).unwrap();
let doc = &docs[0];
Expand All @@ -458,7 +506,9 @@ z: string with spaces"#;
emitter.dump(doc).unwrap();
}

assert_eq!(s, writer, "actual:\n\n{}\n", writer);
let docs2 = YamlLoader::load_from_str(&writer).unwrap();

assert_eq!(docs, docs2, "actual:\n\n{}\n", writer);
}

#[test]
Expand Down Expand Up @@ -506,7 +556,8 @@ null0: ~
- "OFF"
: false_bools
bool0: true
bool1: false"#;
bool1: false
..."#;

let docs = YamlLoader::load_from_str(&input).unwrap();
let doc = &docs[0];
Expand Down Expand Up @@ -543,7 +594,8 @@ a:
e:
- f
- g
- h: []"#
- h: []
..."#
} else {
r#"---
a:
Expand All @@ -554,7 +606,8 @@ e:
- f
- g
-
h: []"#
h: []
..."#
};

let docs = YamlLoader::load_from_str(&s).unwrap();
Expand All @@ -577,7 +630,8 @@ a:
- - c
- d
- - e
- f"#;
- f
..."#;

let docs = YamlLoader::load_from_str(&s).unwrap();
let doc = &docs[0];
Expand All @@ -601,7 +655,8 @@ a:
- d
- - e
- - f
- - e"#;
- - e
..."#;

let docs = YamlLoader::load_from_str(&s).unwrap();
let doc = &docs[0];
Expand All @@ -623,7 +678,8 @@ a:
b:
c:
d:
e: f"#;
e: f
..."#;

let docs = YamlLoader::load_from_str(&s).unwrap();
let doc = &docs[0];
Expand Down
12 changes: 12 additions & 0 deletions tests/test_round_trip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,15 @@ fn test_colon_in_string() {
let y = Yaml::String("x: %".to_owned());
test_round_trip(&y);
}

#[test]
fn test_newline() {
let y = Yaml::Array(vec![Yaml::String("\n".to_owned())]);
test_round_trip(&y);
}

#[test]
fn test_crlf() {
let y = Yaml::Array(vec![Yaml::String("\r\n".to_owned())]);
test_round_trip(&y);
}