Reimplement: Parser::divide_text()

This commit is contained in:
Matthias Beyer 2015-12-04 21:47:14 +01:00
parent 5c38e6752b
commit 2dfb76bd0d

View file

@ -139,31 +139,33 @@ impl<HP> Parser<HP> where
}
fn divide_text(&self, text: &String) -> Result<TextTpl, ParserError> {
let re = Regex::new(r"(?sm)^---$\n(.*)^---$\n(.*)").unwrap();
let re = Regex::new(r"(?sm)^---$(.*)^---$(.*)").unwrap();
debug!("Splitting: '{}'", text);
debug!(" regex = {:?}", re);
let captures = re.captures(&text[..]).unwrap_or({
debug!("Cannot capture from text");
return Err(ParserError::new("Cannot run regex on text",
text.clone(), 0,
"Cannot run regex on text to divide it into header and content."))
});
re.captures(text).map(|captures| {
if captures.len() != 2 {
if captures.len() != 3 {
debug!("Unexpected amount of captures");
return Err(ParserError::new("Unexpected Regex output",
text.clone(), 0,
"The regex to divide text into header and content had an unexpected output."))
}
let header = captures.at(0).map(|s| String::from(s));
let content = captures.at(1).map(|s| String::from(s));
let header = captures.at(1).map(|s| String::from(s));
let content = captures.at(2).map(|s| String::from(s));
debug!("Splitted, Header = '{:?}'", header);
debug!("Splitted, Data = '{:?}'", content);
debug!("Splitted, Header = '{:?}'", header.clone().unwrap_or("NONE".into()));
debug!("Splitted, Data = '{:?}'", content.clone().unwrap_or("NONE".into()));
Ok((header, content))
}).or_else(|| {
debug!("Cannot capture from text");
let e = ParserError::new("Cannot run regex on text",
text.clone(), 0,
"Cannot run regex on text to divide it into header and content.");
Some(Err(e))
}).unwrap()
}
}