Skip to content

Commit

Permalink
don't escape colons
Browse files Browse the repository at this point in the history
  • Loading branch information
mutlusun committed May 2, 2022
1 parent da8aba8 commit dcc129f
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
2 changes: 1 addition & 1 deletion src/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ impl Chunk {
pub fn to_biblatex_string(&self, is_verbatim: bool) -> String {
let mut s = String::new();
for c in self.get().chars() {
if is_escapable(c, is_verbatim) {
if is_escapable(c, is_verbatim, false) {
s.push('\\');
}
s.push(c);
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1185,11 +1185,23 @@ mod tests {
fn test_verbatim_fields() {
let contents = fs::read_to_string("tests/libra.bib").unwrap();
let bibliography = Bibliography::parse(&contents).unwrap();

// Import an entry/field with escaped colons
let e = bibliography.get("dierksmeierJustHODLMoral2018").unwrap();
assert_eq!(e.doi().unwrap(), "10.1007/s41463-018-0036-z");
assert_eq!(
e.file().unwrap(),
"C:\\Users\\mhaug\\Zotero\\storage\\DTPR7TES\\Dierksmeier - 2018 - Just HODL On the Moral Claims of Bitcoin and Ripp.pdf"
);

// Import an entry/field with unescaped colons
let e = bibliography.get("LibraAssociationIndependent").unwrap();
assert_eq!(e.url().unwrap(), "https://libra.org/association/");

// Test export of entry (not escaping colons)
let e = bibliography.get("finextraFedGovernorChallenges2019").unwrap();
assert_eq!(e.to_biblatex_string(),
"@online{finextraFedGovernorChallenges2019,\nauthor = {FinExtra},\ndate = {2019-12-18},\nfile = {C:\\\\Users\\\\mhaug\\\\Zotero\\\\storage\\\\VY9LAKFE\\\\fed-governor-challenges-facebooks-libra-project.html},\ntitle = {Fed {Governor} Challenges {Facebook}'s {Libra} Project},\nurl = {https://www.finextra.com/newsarticle/34986/fed-governor-challenges-facebooks-libra-project},\nurldate = {2020-08-22},\n}"
)
}
}
11 changes: 8 additions & 3 deletions src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ impl<'s> ContentParser<'s> {
fn backslash(&mut self) -> Result<String, ParseError> {
self.eat_assert('\\');
match self.s.peek() {
Some(c) if is_escapable(c, self.verb_field) => {
Some(c) if is_escapable(c, self.verb_field, true) => {
self.s.eat();
Ok(c.to_string())
}
Expand Down Expand Up @@ -380,10 +380,15 @@ fn flatten(chunks: &mut Chunks) {
}

/// Characters that can be escaped.
pub fn is_escapable(c: char, verb: bool) -> bool {
///
/// In read mode (`read_char = true`), colons are also converted to an unescaped string to keep
/// compatiblity with Zotero. Zotero escapes colons when exporting verbatim fields. This crate
/// doesn't escape colons when exporting.
pub fn is_escapable(c: char, verb: bool, read_char: bool) -> bool {
match c {
'{' | '}' | '\\' | ':' => true,
'{' | '}' | '\\' => true,
'&' | '%' | '$' | '_' if !verb => true,
':' if read_char => true,
_ => false,
}
}
Expand Down

0 comments on commit dcc129f

Please sign in to comment.