only whitespace before chapter toml header

This commit is contained in:
Gambhiro 2017-01-21 19:01:55 +00:00
parent 0a5efcc704
commit 6213943420
1 changed files with 30 additions and 14 deletions

View File

@ -7,6 +7,7 @@ use std::ffi::OsStr;
use std::path::PathBuf; use std::path::PathBuf;
use std::fs::File; use std::fs::File;
use std::io::Read; use std::io::Read;
use std::str;
use std::collections::BTreeMap; use std::collections::BTreeMap;
use utils; use utils;
@ -135,20 +136,21 @@ impl Chapter {
} }
} }
let mut text = String::new();
match File::open(src_path) { match File::open(src_path) {
Err(e) => { return Err(format!("Read error: {:?}", e)); }, Err(e) => { return Err(format!("Read error: {:?}", e)); },
Ok(mut f) => { Ok(mut f) => {
let mut text = String::new();
match f.read_to_string(&mut text) { match f.read_to_string(&mut text) {
Ok(_) => {}, Ok(_) => {},
Err(e) => { Err(e) => {
return Err(format!("Error: {:#?}", e)); return Err(format!("Error: {:#?}", e));
}, },
} }
self.content = Some(utils::strip_toml_header(&text));
}
}
// it must only have whitespace before +++ to be a TOML header
match text.as_str().trim().find("+++") {
Some(n) => {
if n == 0 {
let re: Regex = Regex::new(r"(?ms)^\+\+\+\n(?P<toml>.*)\n\+\+\+\n").unwrap(); let re: Regex = Regex::new(r"(?ms)^\+\+\+\n(?P<toml>.*)\n\+\+\+\n").unwrap();
match re.captures(&text) { match re.captures(&text) {
@ -164,6 +166,20 @@ impl Chapter {
} }
None => {}, None => {},
} }
self.content = Some(utils::strip_toml_header(&text));
} else {
// no TOML header
self.content = Some(text);
}
},
None => {
// no TOML header
self.content = Some(text);
}
}
}
}
} }
Ok(self) Ok(self)