Fleshed out the step_numbered() method some more
This commit is contained in:
parent
42de0fb97a
commit
1e8bc1c384
|
@ -25,8 +25,16 @@ impl Loader {
|
|||
Loader { source_directory: source_directory.as_ref().to_path_buf() }
|
||||
}
|
||||
|
||||
/// Parse the summary file and use it to load a book from disk.
|
||||
pub fn load(&self) -> Result<()> {
|
||||
let summary = self.parse_summary()
|
||||
.chain_err(|| "Couldn't parse `SUMMARY.md`")?;
|
||||
|
||||
unimplemented!()
|
||||
}
|
||||
|
||||
/// Parse the `SUMMARY.md` file.
|
||||
pub fn parse_summary(&self) -> Result<Summary> {
|
||||
fn parse_summary(&self) -> Result<Summary> {
|
||||
let path = self.source_directory.join("SUMMARY.md");
|
||||
|
||||
let mut summary_content = String::new();
|
||||
|
@ -34,4 +42,5 @@ impl Loader {
|
|||
|
||||
summary::parse_summary(&summary_content)
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -225,7 +225,11 @@ impl<'a> SummaryParser<'a> {
|
|||
State::Begin => self.step_start(next_event)?,
|
||||
State::PrefixChapters => self.step_prefix(next_event)?,
|
||||
State::NumberedChapters(n) => self.step_numbered(next_event, n)?,
|
||||
_ => unimplemented!(),
|
||||
other => {
|
||||
trace!("[*] unimplemented state: {:?}", other);
|
||||
trace!("{:#?}", self.summary);
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
@ -280,6 +284,7 @@ impl<'a> SummaryParser<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Try to parse the title line.
|
||||
fn parse_title(&mut self) -> Option<String> {
|
||||
if let Some(Event::Start(Tag::Header(1))) = self.stream.next() {
|
||||
debug!("[*] Found a h1 in the SUMMARY");
|
||||
|
@ -332,19 +337,48 @@ impl<'a> SummaryParser<'a> {
|
|||
.chain_err(|| "List items should only contain links")?;
|
||||
|
||||
trace!("[*] Found a chapter: {:?}", it);
|
||||
self.push_numbered_section(SummaryItem::Link(it));
|
||||
Ok(())
|
||||
let section_number = self.push_numbered_section(SummaryItem::Link(it));
|
||||
trace!("[*] Section number was {}", section_number);
|
||||
}
|
||||
Event::Start(Tag::List(_)) => {
|
||||
match self.state {
|
||||
State::NumberedChapters(n) => {
|
||||
let new_nest = n + 1;
|
||||
self.state = State::NumberedChapters(new_nest);
|
||||
trace!("[*] Nesting level increased to {}", new_nest);
|
||||
}
|
||||
other => unreachable!(),
|
||||
}
|
||||
|
||||
}
|
||||
Event::End(Tag::List(_)) => {
|
||||
match self.state {
|
||||
State::NumberedChapters(n) => {
|
||||
if n == 0 {
|
||||
trace!("[*] Finished parsing the numbered chapters");
|
||||
self.state = State::SuffixChapters;
|
||||
} else {
|
||||
trace!("[*] Nesting level decreased to {}", n - 1);
|
||||
self.state = State::NumberedChapters(n - 1);
|
||||
}
|
||||
}
|
||||
other => unreachable!(),
|
||||
}
|
||||
}
|
||||
other => {
|
||||
trace!("[*] skipping unexpected token: {:?}", other);
|
||||
}
|
||||
other => unimplemented!()
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Push a new section at the end of the current nesting level.
|
||||
fn push_numbered_section(&mut self, item: SummaryItem) {
|
||||
fn push_numbered_section(&mut self, item: SummaryItem) -> SectionNumber {
|
||||
if let State::NumberedChapters(level) = self.state {
|
||||
push_item_at_nesting_level(&mut self.summary.numbered_chapters, item, level as usize)
|
||||
.chain_err(|| "The parser should always ensure we add the next item at the correct level")
|
||||
.unwrap();
|
||||
.unwrap()
|
||||
} else {
|
||||
// this method should only ever be called when parsing a numbered
|
||||
// section, therefore if we ever get here something has gone
|
||||
|
@ -381,7 +415,6 @@ fn push_item_at_nesting_level(links: &mut Vec<SummaryItem>, item: SummaryItem, l
|
|||
|
||||
let mut section_number = push_item_at_nesting_level(&mut last_link.nested_items, item, level - 1)?;
|
||||
section_number.insert(0, index as u32 + 1);
|
||||
println!("{:?}\t{:?}", section_number, last_link);
|
||||
Ok(section_number)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,8 +25,7 @@ const SUMMARY: &str = "
|
|||
";
|
||||
|
||||
#[test]
|
||||
#[ignore]
|
||||
fn load_summary_md() {
|
||||
fn parse_summary_md() {
|
||||
env_logger::init().unwrap();
|
||||
|
||||
let should_be = Summary {
|
||||
|
|
Loading…
Reference in New Issue