From 1e8bc1c38499b9ba2a9f577a719d1bbf396e3676 Mon Sep 17 00:00:00 2001 From: Michael Bryan Date: Thu, 29 Jun 2017 20:05:24 +0800 Subject: [PATCH] Fleshed out the step_numbered() method some more --- src/loader/mod.rs | 11 +++++++++- src/loader/summary.rs | 47 ++++++++++++++++++++++++++++++++++++------- tests/loading.rs | 3 +-- 3 files changed, 51 insertions(+), 10 deletions(-) diff --git a/src/loader/mod.rs b/src/loader/mod.rs index 5e9b7891..f9322f8f 100644 --- a/src/loader/mod.rs +++ b/src/loader/mod.rs @@ -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 { + fn parse_summary(&self) -> Result { 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) } + } diff --git a/src/loader/summary.rs b/src/loader/summary.rs index 500cb06d..ef5586ae 100644 --- a/src/loader/summary.rs +++ b/src/loader/summary.rs @@ -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 { 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, 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) } } diff --git a/tests/loading.rs b/tests/loading.rs index 74b22b03..fed0cf04 100644 --- a/tests/loading.rs +++ b/tests/loading.rs @@ -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 {