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,34 +136,49 @@ 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));
}
}
let re: Regex = Regex::new(r"(?ms)^\+\+\+\n(?P<toml>.*)\n\+\+\+\n").unwrap(); // 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();
match re.captures(&text) { match re.captures(&text) {
Some(caps) => { Some(caps) => {
let toml = caps.name("toml").unwrap(); let toml = caps.name("toml").unwrap();
match utils::toml_str_to_btreemap(&toml) { match utils::toml_str_to_btreemap(&toml) {
Ok(x) => {self.parse_from_btreemap(&x);}, Ok(x) => {self.parse_from_btreemap(&x);},
Err(e) => { Err(e) => {
error!("[*] Errors while parsing TOML: {:?}", e); error!("[*] Errors while parsing TOML: {:?}", e);
return Err(e); return Err(e);
}
}
}
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);
} }
} }
} }
None => {},
} }
} }