feat(headline): support custom keywords

This commit is contained in:
PoiScript 2019-02-15 11:32:52 +08:00
parent 88e1f8d62d
commit c7de340479
2 changed files with 159 additions and 113 deletions

View file

@ -200,6 +200,7 @@ pub struct Parser<'a> {
ele_buf: Option<(Element<'a>, usize)>,
obj_buf: Option<(Object<'a>, usize)>,
has_more_item: bool,
keywords: Option<&'a [&'a str]>,
}
impl<'a> Parser<'a> {
@ -212,6 +213,7 @@ impl<'a> Parser<'a> {
ele_buf: None,
obj_buf: None,
has_more_item: false,
keywords: None,
}
}
@ -225,6 +227,10 @@ impl<'a> Parser<'a> {
self.stack.len()
}
pub fn set_keywords(&mut self, keywords: &'a [&'a str]) {
self.keywords = Some(keywords)
}
fn next_sec_or_hdl(&mut self) -> Event<'a> {
let end = Headline::find_level(&self.text[self.off..], std::usize::MAX);
debug_assert!(end <= self.text[self.off..].len());
@ -239,7 +245,11 @@ impl<'a> Parser<'a> {
}
fn next_hdl(&mut self) -> Event<'a> {
let (hdl, off, end) = Headline::parse(&self.text[self.off..]);
let (hdl, off, end) = if let Some(keywords) = self.keywords {
Headline::parse_with_keywords(&self.text[self.off..], keywords)
} else {
Headline::parse(&self.text[self.off..])
};
debug_assert!(end <= self.text[self.off..].len());
self.stack.push(Container::Headline {
beg: self.off + off,