Skip to content

Commit

Permalink
Treat newlines in values as spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
reknih committed Oct 29, 2023
1 parent 69c127c commit 0e5d55b
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
37 changes: 34 additions & 3 deletions src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,21 +101,33 @@ impl ChunksExt for [Spanned<Chunk>] {
fn format_sentence(&self) -> String {
let mut out = String::new();
let mut first = true;
let mut prev_was_whitespace = false;

for val in self {
match &val.v {
Chunk::Normal(s) => {
for c in s.chars() {
for mut c in s.chars() {
if c == '\n' || c == '\r' {
if prev_was_whitespace {
continue;
} else {
c = ' ';
}
}

if first {
out.extend(c.to_uppercase());
} else {
out.extend(c.to_lowercase());
}
first = false;
prev_was_whitespace = c.is_whitespace();
}
}
Chunk::Verbatim(s) => {
out.push_str(s);
prev_was_whitespace =
s.chars().last().map(char::is_whitespace).unwrap_or(false);
}
Chunk::Math(s) => {
out.push('$');
Expand All @@ -132,10 +144,29 @@ impl ChunksExt for [Spanned<Chunk>] {

fn format_verbatim(&self) -> String {
let mut out = String::new();
let mut prev_was_whitespace = false;

for val in self {
match &val.v {
Chunk::Normal(s) => out += s,
Chunk::Verbatim(s) => out += s,
Chunk::Normal(s) => {
for mut c in s.chars() {
if c == '\n' || c == '\r' {
if prev_was_whitespace {
continue;
} else {
c = ' ';
}
}

out.push(c);
prev_was_whitespace = c.is_whitespace();
}
}
Chunk::Verbatim(s) => {
out += s;
prev_was_whitespace =
s.chars().last().map(char::is_whitespace).unwrap_or(false);
}
Chunk::Math(s) => {
out.push('$');
out += s;
Expand Down
9 changes: 9 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1137,6 +1137,15 @@ mod tests {
}
}

#[test]
fn linebreak_field() {
let contents = r#"@book{key, title = {Hello
Martin}}"#;
let bibliography = Bibliography::parse(contents).unwrap();
let entry = bibliography.get("key").unwrap();
assert_eq!(entry.title().unwrap().format_verbatim(), "Hello Martin");
}

#[test]
fn test_verbatim_fields() {
let contents = fs::read_to_string("tests/libra.bib").unwrap();
Expand Down
8 changes: 8 additions & 0 deletions src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -433,4 +433,12 @@ mod tests {
assert_eq!(res[1], 4..6);
assert_eq!(res[2], 194..245);
}

#[test]
fn test_ranges_2228() {
let ranges = &[Spanned::zero(N("34,37--39"))];
let res = ranges.parse::<Vec<Range<u32>>>().unwrap();
assert_eq!(res[0], 34..34);
assert_eq!(res[1], 37..39);
}
}

0 comments on commit 0e5d55b

Please sign in to comment.