feat(*): skip_container and event functions

This commit is contained in:
PoiScript 2019-05-03 02:18:56 +08:00
parent 9357ac6278
commit ecf0d7e67d
2 changed files with 28 additions and 2 deletions

View file

@ -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<W: Write, E: From<Error>> {
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, "<h{}>", level)?;
@ -277,7 +281,7 @@ impl<'a, W: Write, E: From<Error>, H: HtmlHandler<W, E>> 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(())

View file

@ -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<Event<'a>> {
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 {