From ecf0d7e67d0b6db5c728d6d1d9638be1ff6ffe02 Mon Sep 17 00:00:00 2001 From: PoiScript Date: Fri, 3 May 2019 02:18:56 +0800 Subject: [PATCH] feat(*): skip_container and event functions --- src/export/html.rs | 8 ++++++-- src/parser.rs | 22 ++++++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/export/html.rs b/src/export/html.rs index 6b7d1a2..4743eae 100644 --- a/src/export/html.rs +++ b/src/export/html.rs @@ -1,6 +1,6 @@ #![allow(unused_variables)] -use crate::{elements::*, headline::Headline, objects::*, Parser}; +use crate::{elements::*, headline::Headline, objects::*, Event, Parser}; use jetscii::bytes; use std::{ convert::From, @@ -29,6 +29,10 @@ pub trait HtmlHandler> { Ok(w.write_all(&bytes[pos..])?) } + fn event(&mut self, w: &mut W, event: Event) -> Result<(), E> { + handle_event!(event, self, w); + Ok(()) + } fn headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<(), E> { let level = if hdl.level <= 6 { hdl.level } else { 6 }; write!(w, "", level)?; @@ -277,7 +281,7 @@ impl<'a, W: Write, E: From, H: HtmlHandler> HtmlRender<'a, W, E, H> pub fn render(&mut self) -> Result<(), E> { for event in &mut self.parser { - handle_event!(event, &mut self.handler, self.writer); + self.handler.event(self.writer, event)?; } Ok(()) diff --git a/src/parser.rs b/src/parser.rs index 07123b0..ae6516a 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -197,6 +197,28 @@ impl<'a> Parser<'a> { self.text = text; } + /// skip the current container if exists and return its Event + pub fn skip_container(&mut self) -> Option> { + let (container, _, end) = self.stack.pop()?; + self.off = end; + Some(match container { + Container::Bold => Event::BoldEnd, + Container::Drawer => Event::DrawerEnd, + Container::CtrBlock => Event::CtrBlockEnd, + Container::DynBlock => Event::DynBlockEnd, + Container::Headline(_) => Event::HeadlineEnd, + Container::Italic => Event::ItalicEnd, + Container::List(_, ordered) => Event::ListEnd { ordered }, + Container::ListItem => Event::ListItemEnd, + Container::Paragraph => Event::ParagraphEnd, + Container::QteBlock => Event::QteBlockEnd, + Container::Section(_) => Event::SectionEnd, + Container::SplBlock => Event::SplBlockEnd, + Container::Strike => Event::StrikeEnd, + Container::Underline => Event::UnderlineEnd, + }) + } + fn next_section_or_headline(&mut self, text: &'a str) -> Event<'a> { let end = Headline::find_level(text, std::usize::MAX); if end != 0 {