From 3105470ceea58f5e85c5b31f8e1704955d08da86 Mon Sep 17 00:00:00 2001 From: PoiScript Date: Sun, 20 Jan 2019 20:49:14 +0800 Subject: [PATCH] chore: abbreviate --- src/elements/mod.rs | 97 +++++++------- src/export/html.rs | 130 +++++++----------- src/export/mod.rs | 200 +++++++++++++--------------- src/parser.rs | 314 +++++++++++++++++++++----------------------- 4 files changed, 342 insertions(+), 399 deletions(-) diff --git a/src/elements/mod.rs b/src/elements/mod.rs index 3d1d4ed..c6f1031 100644 --- a/src/elements/mod.rs +++ b/src/elements/mod.rs @@ -15,10 +15,8 @@ pub use self::rule::Rule; #[cfg_attr(test, derive(PartialEq, Debug))] pub enum Element<'a> { Paragraph { - // end of the contents + cont_end: usize, end: usize, - // trailing space - trailing: usize, }, Keyword { key: &'a str, @@ -26,56 +24,56 @@ pub enum Element<'a> { }, FnDef { label: &'a str, - contents: &'a str, + cont: &'a str, }, - CenterBlock { + CtrBlock { args: Option<&'a str>, - contents_end: usize, + cont_end: usize, end: usize, }, - QuoteBlock { + QteBlock { args: Option<&'a str>, - contents_end: usize, + cont_end: usize, end: usize, }, - SpecialBlock { + SplBlock { args: Option<&'a str>, name: &'a str, - contents_end: usize, + cont_end: usize, end: usize, }, CommentBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, ExampleBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, ExportBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, SrcBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, VerseBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, DynBlock { args: Option<&'a str>, name: &'a str, - contents_end: usize, + cont_end: usize, end: usize, }, Rule, Comment(&'a str), List { ident: usize, - is_ordered: bool, - contents_end: usize, + ordered: bool, + cont_end: usize, end: usize, }, } @@ -94,17 +92,17 @@ impl<'a> Element<'a> { loop { // Unlike other element, footnote definition must starts at column 0 if bytes[pos] == b'[' { - if let Some((label, contents, off)) = FnDef::parse(&src[pos..]) { + if let Some((label, cont, off)) = FnDef::parse(&src[pos..]) { return if pos == start { - (off + 1, Some(Element::FnDef { label, contents }), None) + (off + 1, Some(Element::FnDef { label, cont }), None) } else { ( start, Some(Element::Paragraph { - end: pos - 1, - trailing: pos, + cont_end: pos - 1, + end: pos, }), - Some((Element::FnDef { label, contents }, off + 1)), + Some((Element::FnDef { label, cont }, off + 1)), ) }; } @@ -122,8 +120,8 @@ impl<'a> Element<'a> { ( start, Some(Element::Paragraph { - end, - trailing: pos - 1, + cont_end: end, + end: pos - 1, }), Some(($ele, $off)), ) @@ -136,12 +134,12 @@ impl<'a> Element<'a> { || bytes[pos] == b'*' || (bytes[pos] >= b'0' && bytes[pos] <= b'9') { - if let Some((ident, is_ordered, contents_end, end)) = List::parse(&src[end..]) { + if let Some((ident, ordered, cont_end, end)) = List::parse(&src[end..]) { ret!( Element::List { ident, - is_ordered, - contents_end, + ordered, + cont_end, end }, 0 @@ -150,7 +148,14 @@ impl<'a> Element<'a> { } if bytes[pos] == b'\n' { - return (start, Some(Element::Paragraph { end, trailing: pos }), None); + return ( + start, + Some(Element::Paragraph { + cont_end: end, + end: pos, + }), + None, + ); } // TODO: LaTeX environment @@ -165,66 +170,66 @@ impl<'a> Element<'a> { } if bytes[pos] == b'#' && bytes.get(pos + 1).filter(|&&b| b == b'+').is_some() { - if let Some((name, args, contents_beg, contents_end, end)) = + if let Some((name, args, contents_beg, cont_end, end)) = Block::parse(&src[pos..]) { match name.to_uppercase().as_str() { "COMMENT" => ret!( Element::CommentBlock { args, - contents: &src[pos + contents_beg + 1..pos + contents_end - 1], + cont: &src[pos + contents_beg + 1..pos + cont_end - 1], }, pos + end ), "EXAMPLE" => ret!( Element::ExampleBlock { args, - contents: &src[pos + contents_beg + 1..pos + contents_end - 1], + cont: &src[pos + contents_beg + 1..pos + cont_end - 1], }, pos + end ), "EXPORT" => ret!( Element::ExportBlock { args, - contents: &src[pos + contents_beg + 1..pos + contents_end - 1], + cont: &src[pos + contents_beg + 1..pos + cont_end - 1], }, pos + end ), "SRC" => ret!( Element::SrcBlock { args, - contents: &src[pos + contents_beg + 1..pos + contents_end - 1], + cont: &src[pos + contents_beg + 1..pos + cont_end - 1], }, pos + end ), "VERSE" => ret!( Element::VerseBlock { args, - contents: &src[pos + contents_beg + 1..pos + contents_end - 1], + cont: &src[pos + contents_beg + 1..pos + cont_end - 1], }, pos + end ), "CENTER" => ret!( - Element::CenterBlock { + Element::CtrBlock { args, - contents_end, + cont_end, end, }, pos + contents_beg ), "QUOTE" => ret!( - Element::QuoteBlock { + Element::QteBlock { args, - contents_end, + cont_end, end, }, pos + contents_beg ), _ => ret!( - Element::SpecialBlock { + Element::SplBlock { name, args, - contents_end, + cont_end, end, }, pos + contents_beg @@ -232,14 +237,14 @@ impl<'a> Element<'a> { }; } - if let Some((name, args, contents_beg, contents_end, end)) = + if let Some((name, args, contents_beg, cont_end, end)) = DynBlock::parse(&src[pos..]) { ret!( Element::DynBlock { name, args, - contents_end, + cont_end, end, }, pos + contents_beg @@ -268,8 +273,8 @@ impl<'a> Element<'a> { return ( start, Some(Element::Paragraph { - end: pos - 1, - trailing: pos, + cont_end: pos - 1, + end: pos, }), None, ); @@ -278,8 +283,8 @@ impl<'a> Element<'a> { return ( start, Some(Element::Paragraph { + cont_end: src.len(), end: src.len(), - trailing: src.len(), }), None, ); diff --git a/src/export/html.rs b/src/export/html.rs index 8805eb4..803e1e6 100644 --- a/src/export/html.rs +++ b/src/export/html.rs @@ -8,101 +8,74 @@ use std::io::{Result, Write}; pub struct HtmlHandler; impl Handler for HtmlHandler { - fn handle_start_headline(&mut self, w: &mut W, hdl: Headline) -> Result<()> { - write!( - w, - "{1}", - if hdl.level <= 6 { hdl.level } else { 6 }, - hdl.title - ) + fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<()> { + let level = if hdl.level <= 6 { hdl.level } else { 6 }; + write!(w, "{1}", level, hdl.title) } - fn handle_end_headline(&mut self, w: &mut W) -> Result<()> { + fn handle_headline_end(&mut self, w: &mut W) -> Result<()> { Ok(()) } - fn handle_start_section(&mut self, w: &mut W) -> Result<()> { + fn handle_section_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "
") } - fn handle_end_section(&mut self, w: &mut W) -> Result<()> { + fn handle_section_end(&mut self, w: &mut W) -> Result<()> { write!(w, "
") } - fn handle_start_paragraph(&mut self, w: &mut W) -> Result<()> { + fn handle_paragraph_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "

") } - fn handle_end_paragraph(&mut self, w: &mut W) -> Result<()> { + fn handle_paragraph_end(&mut self, w: &mut W) -> Result<()> { write!(w, "

") } - fn handle_start_center_block(&mut self, w: &mut W) -> Result<()> { - write!(w, "
") + fn handle_ctr_block_beg(&mut self, w: &mut W) -> Result<()> { + write!(w, r#"
"#) } - fn handle_end_center_block(&mut self, w: &mut W) -> Result<()> { + fn handle_ctr_block_end(&mut self, w: &mut W) -> Result<()> { write!(w, "
") } - fn handle_start_quote_block(&mut self, w: &mut W) -> Result<()> { + fn handle_qte_block_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "
") } - fn handle_end_quote_block(&mut self, w: &mut W) -> Result<()> { + fn handle_qte_block_end(&mut self, w: &mut W) -> Result<()> { write!(w, "
") } - fn handle_start_special_block( - &mut self, - w: &mut W, - name: &str, - args: Option<&str>, - ) -> Result<()> { + fn handle_spl_block_beg(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()> { write!(w, "
") } - fn handle_end_special_block(&mut self, w: &mut W) -> Result<()> { + fn handle_spl_block_end(&mut self, w: &mut W) -> Result<()> { write!(w, "
") } - fn handle_comment_block( - &mut self, - w: &mut W, - contents: &str, - args: Option<&str>, - ) -> Result<()> { + fn handle_comment_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> { Ok(()) } - fn handle_example_block( - &mut self, - w: &mut W, - contents: &str, - args: Option<&str>, - ) -> Result<()> { - write!(w, "
{}
", contents) + fn handle_example_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> { + write!(w, "
{}
", cont) } - fn handle_export_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) -> Result<()> { + fn handle_export_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> { Ok(()) } - fn handle_src_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) -> Result<()> { - write!(w, "
{}
", contents) + fn handle_src_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> { + write!(w, "
{}
", cont) } - fn handle_verse_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) -> Result<()> { + fn handle_verse_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> { Ok(()) } - fn handle_start_dyn_block(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()> { + fn handle_dyn_block_beg(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()> { Ok(()) } - fn handle_end_dyn_block(&mut self, w: &mut W) -> Result<()> { + fn handle_dyn_block_end(&mut self, w: &mut W) -> Result<()> { Ok(()) } - fn handle_start_list(&mut self, w: &mut W, is_ordered: bool) -> Result<()> { - if is_ordered { - write!(w, "
    ") - } else { - write!(w, "
      ") - } + fn handle_list_beg(&mut self, w: &mut W, ordered: bool) -> Result<()> { + write!(w, "{}", if ordered { "
        " } else { "
          " }) } - fn handle_end_list(&mut self, w: &mut W, is_ordered: bool) -> Result<()> { - if is_ordered { - write!(w, "
      ") - } else { - write!(w, "
    ") - } + fn handle_list_end(&mut self, w: &mut W, ordered: bool) -> Result<()> { + write!(w, "{}", if ordered { "
" } else { "" }) } - fn handle_start_list_item(&mut self, w: &mut W) -> Result<()> { + fn handle_list_beg_item(&mut self, w: &mut W) -> Result<()> { write!(w, "
  • ") } - fn handle_end_list_item(&mut self, w: &mut W) -> Result<()> { + fn handle_list_end_item(&mut self, w: &mut W) -> Result<()> { write!(w, "
  • ") } fn handle_aff_keywords(&mut self, w: &mut W) -> Result<()> { @@ -114,7 +87,7 @@ impl Handler for HtmlHandler { fn handle_clock(&mut self, w: &mut W) -> Result<()> { Ok(()) } - fn handle_comment(&mut self, w: &mut W, contents: &str) -> Result<()> { + fn handle_comment(&mut self, w: &mut W, cont: &str) -> Result<()> { Ok(()) } fn handle_table_start(&mut self, w: &mut W) -> Result<()> { @@ -129,7 +102,7 @@ impl Handler for HtmlHandler { fn handle_latex_env(&mut self, w: &mut W) -> Result<()> { Ok(()) } - fn handle_fn_def(&mut self, w: &mut W, label: &str, contents: &str) -> Result<()> { + fn handle_fn_def(&mut self, w: &mut W, label: &str, cont: &str) -> Result<()> { Ok(()) } fn handle_keyword(&mut self, w: &mut W, key: &str, value: &str) -> Result<()> { @@ -151,12 +124,11 @@ impl Handler for HtmlHandler { write!(w, "{}", inline_src.body) } fn handle_link(&mut self, w: &mut W, link: Link) -> Result<()> { - write!( - w, - "{}", - link.path, - link.desc.unwrap_or(link.path) - ) + if let Some(desc) = link.desc { + write!(w, r#"{}"#, link.path, desc) + } else { + write!(w, r#"{0}"#, link.path) + } } fn handle_macros(&mut self, w: &mut W, macros: Macros) -> Result<()> { Ok(()) @@ -174,37 +146,37 @@ impl Handler for HtmlHandler { fn handle_target(&mut self, w: &mut W, target: Target) -> Result<()> { Ok(()) } - fn handle_start_bold(&mut self, w: &mut W) -> Result<()> { + fn handle_bold_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_end_bold(&mut self, w: &mut W) -> Result<()> { + fn handle_bold_end(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_start_italic(&mut self, w: &mut W) -> Result<()> { + fn handle_italic_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_end_italic(&mut self, w: &mut W) -> Result<()> { + fn handle_italic_end(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_start_strike(&mut self, w: &mut W) -> Result<()> { + fn handle_strike_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_end_strike(&mut self, w: &mut W) -> Result<()> { + fn handle_strike_end(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_start_underline(&mut self, w: &mut W) -> Result<()> { + fn handle_underline_beg(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_end_underline(&mut self, w: &mut W) -> Result<()> { + fn handle_underline_end(&mut self, w: &mut W) -> Result<()> { write!(w, "") } - fn handle_verbatim(&mut self, w: &mut W, contents: &str) -> Result<()> { - write!(w, "{}", contents) + fn handle_verbatim(&mut self, w: &mut W, cont: &str) -> Result<()> { + write!(w, "{}", cont) } - fn handle_code(&mut self, w: &mut W, contents: &str) -> Result<()> { - write!(w, "{}", contents) + fn handle_code(&mut self, w: &mut W, cont: &str) -> Result<()> { + write!(w, "{}", cont) } - fn handle_text(&mut self, w: &mut W, contents: &str) -> Result<()> { - write!(w, "{}", contents.replace('\n', " ")) + fn handle_text(&mut self, w: &mut W, cont: &str) -> Result<()> { + write!(w, "{}", cont.replace('\n', " ")) } } diff --git a/src/export/mod.rs b/src/export/mod.rs index af8679f..f3c5984 100644 --- a/src/export/mod.rs +++ b/src/export/mod.rs @@ -8,45 +8,38 @@ use parser::Parser; use std::io::{Result, Write}; pub trait Handler { - fn handle_start_headline(&mut self, w: &mut W, hdl: Headline) -> Result<()>; - fn handle_end_headline(&mut self, w: &mut W) -> Result<()>; - fn handle_start_section(&mut self, w: &mut W) -> Result<()>; - fn handle_end_section(&mut self, w: &mut W) -> Result<()>; - fn handle_start_paragraph(&mut self, w: &mut W) -> Result<()>; - fn handle_end_paragraph(&mut self, w: &mut W) -> Result<()>; - fn handle_start_center_block(&mut self, w: &mut W) -> Result<()>; - fn handle_end_center_block(&mut self, w: &mut W) -> Result<()>; - fn handle_start_quote_block(&mut self, w: &mut W) -> Result<()>; - fn handle_end_quote_block(&mut self, w: &mut W) -> Result<()>; - fn handle_start_special_block( - &mut self, - w: &mut W, - name: &str, - args: Option<&str>, - ) -> Result<()>; - fn handle_end_special_block(&mut self, w: &mut W) -> Result<()>; - fn handle_comment_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) - -> Result<()>; - fn handle_example_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) - -> Result<()>; - fn handle_export_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) -> Result<()>; - fn handle_src_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) -> Result<()>; - fn handle_verse_block(&mut self, w: &mut W, contents: &str, args: Option<&str>) -> Result<()>; - fn handle_start_dyn_block(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()>; - fn handle_end_dyn_block(&mut self, w: &mut W) -> Result<()>; - fn handle_start_list(&mut self, w: &mut W, is_ordered: bool) -> Result<()>; - fn handle_end_list(&mut self, w: &mut W, is_ordered: bool) -> Result<()>; - fn handle_start_list_item(&mut self, w: &mut W) -> Result<()>; - fn handle_end_list_item(&mut self, w: &mut W) -> Result<()>; + fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<()>; + fn handle_headline_end(&mut self, w: &mut W) -> Result<()>; + fn handle_section_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_section_end(&mut self, w: &mut W) -> Result<()>; + fn handle_paragraph_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_paragraph_end(&mut self, w: &mut W) -> Result<()>; + fn handle_ctr_block_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_ctr_block_end(&mut self, w: &mut W) -> Result<()>; + fn handle_qte_block_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_qte_block_end(&mut self, w: &mut W) -> Result<()>; + fn handle_spl_block_beg(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()>; + fn handle_spl_block_end(&mut self, w: &mut W) -> Result<()>; + fn handle_comment_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()>; + fn handle_example_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()>; + fn handle_export_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()>; + fn handle_src_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()>; + fn handle_verse_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()>; + fn handle_dyn_block_beg(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()>; + fn handle_dyn_block_end(&mut self, w: &mut W) -> Result<()>; + fn handle_list_beg(&mut self, w: &mut W, ordered: bool) -> Result<()>; + fn handle_list_end(&mut self, w: &mut W, ordered: bool) -> Result<()>; + fn handle_list_beg_item(&mut self, w: &mut W) -> Result<()>; + fn handle_list_end_item(&mut self, w: &mut W) -> Result<()>; fn handle_aff_keywords(&mut self, w: &mut W) -> Result<()>; fn handle_call(&mut self, w: &mut W) -> Result<()>; fn handle_clock(&mut self, w: &mut W) -> Result<()>; - fn handle_comment(&mut self, w: &mut W, contents: &str) -> Result<()>; + fn handle_comment(&mut self, w: &mut W, cont: &str) -> Result<()>; fn handle_table_start(&mut self, w: &mut W) -> Result<()>; fn handle_table_end(&mut self, w: &mut W) -> Result<()>; fn handle_table_cell(&mut self, w: &mut W) -> Result<()>; fn handle_latex_env(&mut self, w: &mut W) -> Result<()>; - fn handle_fn_def(&mut self, w: &mut W, label: &str, contents: &str) -> Result<()>; + fn handle_fn_def(&mut self, w: &mut W, label: &str, cont: &str) -> Result<()>; fn handle_keyword(&mut self, w: &mut W, key: &str, value: &str) -> Result<()>; fn handle_rule(&mut self, w: &mut W) -> Result<()>; fn handle_cookie(&mut self, w: &mut W, cookie: Cookie) -> Result<()>; @@ -58,17 +51,17 @@ pub trait Handler { fn handle_radio_target(&mut self, w: &mut W, target: RadioTarget) -> Result<()>; fn handle_snippet(&mut self, w: &mut W, snippet: Snippet) -> Result<()>; fn handle_target(&mut self, w: &mut W, target: Target) -> Result<()>; - fn handle_start_bold(&mut self, w: &mut W) -> Result<()>; - fn handle_end_bold(&mut self, w: &mut W) -> Result<()>; - fn handle_start_italic(&mut self, w: &mut W) -> Result<()>; - fn handle_end_italic(&mut self, w: &mut W) -> Result<()>; - fn handle_start_strike(&mut self, w: &mut W) -> Result<()>; - fn handle_end_strike(&mut self, w: &mut W) -> Result<()>; - fn handle_start_underline(&mut self, w: &mut W) -> Result<()>; - fn handle_end_underline(&mut self, w: &mut W) -> Result<()>; - fn handle_verbatim(&mut self, w: &mut W, contents: &str) -> Result<()>; - fn handle_code(&mut self, w: &mut W, contents: &str) -> Result<()>; - fn handle_text(&mut self, w: &mut W, contents: &str) -> Result<()>; + fn handle_bold_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_bold_end(&mut self, w: &mut W) -> Result<()>; + fn handle_italic_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_italic_end(&mut self, w: &mut W) -> Result<()>; + fn handle_strike_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_strike_end(&mut self, w: &mut W) -> Result<()>; + fn handle_underline_beg(&mut self, w: &mut W) -> Result<()>; + fn handle_underline_end(&mut self, w: &mut W) -> Result<()>; + fn handle_verbatim(&mut self, w: &mut W, cont: &str) -> Result<()>; + fn handle_code(&mut self, w: &mut W, cont: &str) -> Result<()>; + fn handle_text(&mut self, w: &mut W, cont: &str) -> Result<()>; } pub struct Render<'a, W: Write, H: Handler> { @@ -94,75 +87,64 @@ impl<'a, W: Write, H: Handler> Render<'a, W, H> { use parser::Event::*; let w = &mut self.writer; + let h = &mut self.handler; for event in &mut self.parser { match event { - StartHeadline(hdl) => self.handler.handle_start_headline(w, hdl)?, - EndHeadline => self.handler.handle_end_headline(w)?, - StartSection => self.handler.handle_start_section(w)?, - EndSection => self.handler.handle_end_section(w)?, - StartParagraph => self.handler.handle_start_paragraph(w)?, - EndParagraph => self.handler.handle_end_paragraph(w)?, - StartCenterBlock => self.handler.handle_start_center_block(w)?, - EndCenterBlock => self.handler.handle_end_center_block(w)?, - StartQuoteBlock => self.handler.handle_start_quote_block(w)?, - EndQuoteBlock => self.handler.handle_end_quote_block(w)?, - StartSpecialBlock { name, args } => { - self.handler.handle_start_special_block(w, name, args)? - } - EndSpecialBlock => self.handler.handle_end_special_block(w)?, - CommentBlock { contents, args } => { - self.handler.handle_comment_block(w, contents, args)? - } - ExampleBlock { contents, args } => { - self.handler.handle_example_block(w, contents, args)? - } - ExportBlock { contents, args } => { - self.handler.handle_export_block(w, contents, args)? - } - SrcBlock { contents, args } => self.handler.handle_src_block(w, contents, args)?, - VerseBlock { contents, args } => { - self.handler.handle_verse_block(w, contents, args)? - } - StartDynBlock { name, args } => { - self.handler.handle_start_dyn_block(w, name, args)? - } - EndDynBlock => self.handler.handle_end_dyn_block(w)?, - StartList { is_ordered } => self.handler.handle_start_list(w, is_ordered)?, - EndList { is_ordered } => self.handler.handle_end_list(w, is_ordered)?, - StartListItem => self.handler.handle_start_list_item(w)?, - EndListItem => self.handler.handle_end_list_item(w)?, - AffKeywords => self.handler.handle_aff_keywords(w)?, - Call => self.handler.handle_call(w)?, - Clock => self.handler.handle_clock(w)?, - Comment(c) => self.handler.handle_comment(w, c)?, - TableStart => self.handler.handle_table_start(w)?, - TableEnd => self.handler.handle_table_end(w)?, - TableCell => self.handler.handle_table_cell(w)?, - LatexEnv => self.handler.handle_latex_env(w)?, - FnDef { label, contents } => self.handler.handle_fn_def(w, label, contents)?, - Keyword { key, value } => self.handler.handle_keyword(w, key, value)?, - Rule => self.handler.handle_rule(w)?, - Cookie(cookie) => self.handler.handle_cookie(w, cookie)?, - FnRef(fnref) => self.handler.handle_fn_ref(w, fnref)?, - InlineCall(inlinecall) => self.handler.handle_inline_call(w, inlinecall)?, - InlineSrc(inlinesrc) => self.handler.handle_inline_src(w, inlinesrc)?, - Link(link) => self.handler.handle_link(w, link)?, - Macros(macros) => self.handler.handle_macros(w, macros)?, - RadioTarget(radiotarget) => self.handler.handle_radio_target(w, radiotarget)?, - Snippet(snippet) => self.handler.handle_snippet(w, snippet)?, - Target(target) => self.handler.handle_target(w, target)?, - StartBold => self.handler.handle_start_bold(w)?, - EndBold => self.handler.handle_end_bold(w)?, - StartItalic => self.handler.handle_start_italic(w)?, - EndItalic => self.handler.handle_end_italic(w)?, - StartStrike => self.handler.handle_start_strike(w)?, - EndStrike => self.handler.handle_end_strike(w)?, - StartUnderline => self.handler.handle_start_underline(w)?, - EndUnderline => self.handler.handle_end_underline(w)?, - Verbatim(contents) => self.handler.handle_verbatim(w, contents)?, - Code(contents) => self.handler.handle_code(w, contents)?, - Text(contents) => self.handler.handle_text(w, contents)?, + HeadlineBeg(hdl) => h.handle_headline_beg(w, hdl)?, + HeadlineEnd => h.handle_headline_end(w)?, + SectionBeg => h.handle_section_beg(w)?, + SectionEnd => h.handle_section_end(w)?, + ParagraphBeg => h.handle_paragraph_beg(w)?, + ParagraphEnd => h.handle_paragraph_end(w)?, + CtrBlockBeg => h.handle_ctr_block_beg(w)?, + CtrBlockEnd => h.handle_ctr_block_end(w)?, + QteBlockBeg => h.handle_qte_block_beg(w)?, + QteBlockEnd => h.handle_qte_block_end(w)?, + SplBlockBeg { name, args } => h.handle_spl_block_beg(w, name, args)?, + SplBlockEnd => h.handle_spl_block_end(w)?, + CommentBlock { cont, args } => h.handle_comment_block(w, cont, args)?, + ExampleBlock { cont, args } => h.handle_example_block(w, cont, args)?, + ExportBlock { cont, args } => h.handle_export_block(w, cont, args)?, + SrcBlock { cont, args } => h.handle_src_block(w, cont, args)?, + VerseBlock { cont, args } => h.handle_verse_block(w, cont, args)?, + DynBlockBeg { name, args } => h.handle_dyn_block_beg(w, name, args)?, + DynBlockEnd => h.handle_dyn_block_end(w)?, + ListBeg { ordered } => h.handle_list_beg(w, ordered)?, + ListEnd { ordered } => h.handle_list_end(w, ordered)?, + ListItemBeg => h.handle_list_beg_item(w)?, + ListItemEnd => h.handle_list_end_item(w)?, + AffKeywords => h.handle_aff_keywords(w)?, + Call => h.handle_call(w)?, + Clock => h.handle_clock(w)?, + Comment(c) => h.handle_comment(w, c)?, + TableStart => h.handle_table_start(w)?, + TableEnd => h.handle_table_end(w)?, + TableCell => h.handle_table_cell(w)?, + LatexEnv => h.handle_latex_env(w)?, + FnDef { label, cont } => h.handle_fn_def(w, label, cont)?, + Keyword { key, value } => h.handle_keyword(w, key, value)?, + Rule => h.handle_rule(w)?, + Cookie(cookie) => h.handle_cookie(w, cookie)?, + FnRef(fnref) => h.handle_fn_ref(w, fnref)?, + InlineCall(inlinecall) => h.handle_inline_call(w, inlinecall)?, + InlineSrc(inlinesrc) => h.handle_inline_src(w, inlinesrc)?, + Link(link) => h.handle_link(w, link)?, + Macros(macros) => h.handle_macros(w, macros)?, + RadioTarget(radiotarget) => h.handle_radio_target(w, radiotarget)?, + Snippet(snippet) => h.handle_snippet(w, snippet)?, + Target(target) => h.handle_target(w, target)?, + BoldBeg => h.handle_bold_beg(w)?, + BoldEnd => h.handle_bold_end(w)?, + ItalicBeg => h.handle_italic_beg(w)?, + ItalicEnd => h.handle_italic_end(w)?, + StrikeBeg => h.handle_strike_beg(w)?, + StrikeEnd => h.handle_strike_end(w)?, + UnderlineBeg => h.handle_underline_beg(w)?, + UnderlineEnd => h.handle_underline_end(w)?, + Verbatim(cont) => h.handle_verbatim(w, cont)?, + Code(cont) => h.handle_code(w, cont)?, + Text(cont) => h.handle_text(w, cont)?, } } diff --git a/src/parser.rs b/src/parser.rs index 2f1e1a4..b356aca 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -14,30 +14,30 @@ pub enum Container { }, Paragraph { - end: usize, - trailing: usize, - }, - CenterBlock { - contents_end: usize, + cont_end: usize, end: usize, }, - QuoteBlock { - contents_end: usize, + CtrBlock { + cont_end: usize, end: usize, }, - SpecialBlock { - contents_end: usize, + QteBlock { + cont_end: usize, + end: usize, + }, + SplBlock { + cont_end: usize, end: usize, }, DynBlock { - contents_end: usize, + cont_end: usize, end: usize, }, List { ident: usize, - is_ordered: bool, - contents_end: usize, + ordered: bool, + cont_end: usize, end: usize, }, ListItem { @@ -61,59 +61,59 @@ pub enum Container { #[cfg_attr(test, derive(PartialEq))] #[derive(Debug)] pub enum Event<'a> { - StartHeadline(Headline<'a>), - EndHeadline, + HeadlineBeg(Headline<'a>), + HeadlineEnd, - StartSection, - EndSection, + SectionBeg, + SectionEnd, - StartParagraph, - EndParagraph, + ParagraphBeg, + ParagraphEnd, - StartCenterBlock, - EndCenterBlock, - StartQuoteBlock, - EndQuoteBlock, - StartSpecialBlock { + CtrBlockBeg, + CtrBlockEnd, + QteBlockBeg, + QteBlockEnd, + SplBlockBeg { name: &'a str, args: Option<&'a str>, }, - EndSpecialBlock, - StartDynBlock { + SplBlockEnd, + DynBlockBeg { name: &'a str, args: Option<&'a str>, }, - EndDynBlock, + DynBlockEnd, CommentBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, ExampleBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, ExportBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, SrcBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, VerseBlock { args: Option<&'a str>, - contents: &'a str, + cont: &'a str, }, - StartList { - is_ordered: bool, + ListBeg { + ordered: bool, }, - EndList { - is_ordered: bool, + ListEnd { + ordered: bool, }, - StartListItem, - EndListItem, + ListItemBeg, + ListItemEnd, AffKeywords, @@ -130,7 +130,7 @@ pub enum Event<'a> { LatexEnv, FnDef { label: &'a str, - contents: &'a str, + cont: &'a str, }, Keyword { key: &'a str, @@ -148,14 +148,14 @@ pub enum Event<'a> { Snippet(Snippet<'a>), Target(Target<'a>), - StartBold, - EndBold, - StartItalic, - EndItalic, - StartStrike, - EndStrike, - StartUnderline, - EndUnderline, + BoldBeg, + BoldEnd, + ItalicBeg, + ItalicEnd, + StrikeBeg, + StrikeEnd, + UnderlineBeg, + UnderlineEnd, Verbatim(&'a str), Code(&'a str), @@ -181,26 +181,26 @@ impl<'a> Parser<'a> { } } - fn start_section_or_headline(&mut self, tail: &'a str) -> Event<'a> { + fn start_sec_or_hdl(&mut self, tail: &'a str) -> Event<'a> { let end = Headline::find_level(tail, std::usize::MAX); if end != 0 { self.stack.push(Container::Section { end: self.off + end, }); - Event::StartSection + Event::SectionBeg } else { - self.start_headline(tail) + self.start_hdl(tail) } } - fn start_headline(&mut self, tail: &'a str) -> Event<'a> { + fn start_hdl(&mut self, tail: &'a str) -> Event<'a> { let (hdl, off, end) = Headline::parse(tail); self.stack.push(Container::Headline { beg: self.off + off, end: self.off + end, }); self.off += off; - Event::StartHeadline(hdl) + Event::HeadlineBeg(hdl) } fn next_ele(&mut self, end: usize) -> Event<'a> { @@ -216,43 +216,35 @@ impl<'a> Parser<'a> { if let Some(ele) = ele { match ele { - Element::Paragraph { end, trailing } => self.stack.push(Container::Paragraph { - end: end + self.off, - trailing: trailing + self.off, - }), - Element::QuoteBlock { - end, contents_end, .. - } => self.stack.push(Container::QuoteBlock { - contents_end: contents_end + self.off, + Element::Paragraph { cont_end, end } => self.stack.push(Container::Paragraph { + cont_end: cont_end + self.off, end: end + self.off, }), - Element::CenterBlock { - end, contents_end, .. - } => self.stack.push(Container::CenterBlock { - contents_end: contents_end + self.off, + Element::QteBlock { end, cont_end, .. } => self.stack.push(Container::QteBlock { + cont_end: cont_end + self.off, end: end + self.off, }), - Element::SpecialBlock { - end, contents_end, .. - } => self.stack.push(Container::SpecialBlock { - contents_end: contents_end + self.off, + Element::CtrBlock { end, cont_end, .. } => self.stack.push(Container::CtrBlock { + cont_end: cont_end + self.off, end: end + self.off, }), - Element::DynBlock { - end, contents_end, .. - } => self.stack.push(Container::DynBlock { - contents_end: contents_end + self.off, + Element::SplBlock { end, cont_end, .. } => self.stack.push(Container::SplBlock { + cont_end: cont_end + self.off, + end: end + self.off, + }), + Element::DynBlock { end, cont_end, .. } => self.stack.push(Container::DynBlock { + cont_end: cont_end + self.off, end: end + self.off, }), Element::List { ident, - is_ordered, - contents_end, + ordered, + cont_end, end, } => self.stack.push(Container::List { ident, - is_ordered, - contents_end: contents_end + self.off, + ordered, + cont_end: cont_end + self.off, end: end + self.off, }), _ => (), @@ -299,24 +291,24 @@ impl<'a> Parser<'a> { end: self.off + end, }); self.off += beg; - Event::StartListItem + Event::ListItemBeg } fn end(&mut self) -> Event<'a> { match self.stack.pop().unwrap() { - Container::Paragraph { .. } => Event::EndParagraph, - Container::Underline { .. } => Event::EndUnderline, - Container::Section { .. } => Event::EndSection, - Container::Strike { .. } => Event::EndStrike, - Container::Headline { .. } => Event::EndHeadline, - Container::Italic { .. } => Event::EndItalic, - Container::Bold { .. } => Event::EndBold, - Container::CenterBlock { .. } => Event::EndCenterBlock, - Container::QuoteBlock { .. } => Event::EndQuoteBlock, - Container::SpecialBlock { .. } => Event::EndSpecialBlock, - Container::DynBlock { .. } => Event::EndDynBlock, - Container::List { is_ordered, .. } => Event::EndList { is_ordered }, - Container::ListItem { .. } => Event::EndListItem, + Container::Bold { .. } => Event::BoldEnd, + 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, } } @@ -335,15 +327,15 @@ impl<'a> Parser<'a> { | Underline { end } => { assert!(self.off <= end); } - Paragraph { end, trailing } => { - // assert!(self.off <= trailing); + Paragraph { cont_end, end } => { assert!(self.off <= end); + assert!(self.off <= cont_end); } - CenterBlock { contents_end, end } - | QuoteBlock { contents_end, end } - | SpecialBlock { contents_end, end } - | DynBlock { contents_end, end } => { - assert!(self.off <= contents_end); + CtrBlock { cont_end, end } + | QteBlock { cont_end, end } + | SplBlock { cont_end, end } + | DynBlock { cont_end, end } => { + assert!(self.off <= cont_end); assert!(self.off <= end); } } @@ -362,7 +354,7 @@ impl<'a> Iterator for Parser<'a> { None } else { let tail = &self.text[self.off..]; - Some(self.start_section_or_headline(tail)) + Some(self.start_sec_or_hdl(tail)) } } else { let last = *self.stack.last_mut().unwrap(); @@ -373,37 +365,29 @@ impl<'a> Iterator for Parser<'a> { if self.off >= end { self.end() } else if self.off == beg { - self.start_section_or_headline(tail) + self.start_sec_or_hdl(tail) } else { - self.start_headline(tail) + self.start_hdl(tail) } } - Container::DynBlock { - contents_end, end, .. - } - | Container::CenterBlock { - contents_end, end, .. - } - | Container::QuoteBlock { - contents_end, end, .. - } - | Container::SpecialBlock { - contents_end, end, .. - } => { - if self.off >= contents_end { + Container::DynBlock { cont_end, end, .. } + | Container::CtrBlock { cont_end, end, .. } + | Container::QteBlock { cont_end, end, .. } + | Container::SplBlock { cont_end, end, .. } => { + if self.off >= cont_end { self.off = end; self.end() } else { - self.next_ele(contents_end) + self.next_ele(cont_end) } } Container::List { - contents_end, + cont_end, end, ident, .. } => { - if self.off >= contents_end { + if self.off >= cont_end { self.off = end; self.end() } else { @@ -425,12 +409,12 @@ impl<'a> Iterator for Parser<'a> { self.next_ele(end) } } - Container::Paragraph { end, trailing } => { - if self.off >= end { - self.off = trailing; + Container::Paragraph { cont_end, end } => { + if self.off >= cont_end { + self.off = end; self.end() } else { - self.next_obj(end) + self.next_obj(cont_end) } } Container::Bold { end } @@ -452,21 +436,21 @@ impl<'a> Iterator for Parser<'a> { impl<'a> From> for Event<'a> { fn from(obj: Object<'a>) -> Self { match obj { - Object::Bold { .. } => Event::StartBold, + Object::Bold { .. } => Event::BoldBeg, Object::Code(c) => Event::Code(c), Object::Cookie(c) => Event::Cookie(c), Object::FnRef(f) => Event::FnRef(f), Object::InlineCall(i) => Event::InlineCall(i), Object::InlineSrc(i) => Event::InlineSrc(i), - Object::Italic { .. } => Event::StartItalic, + Object::Italic { .. } => Event::ItalicBeg, Object::Link(l) => Event::Link(l), Object::Macros(m) => Event::Macros(m), Object::RadioTarget(r) => Event::RadioTarget(r), Object::Snippet(s) => Event::Snippet(s), - Object::Strike { .. } => Event::StartStrike, + Object::Strike { .. } => Event::StrikeBeg, Object::Target(t) => Event::Target(t), Object::Text(t) => Event::Text(t), - Object::Underline { .. } => Event::StartUnderline, + Object::Underline { .. } => Event::UnderlineBeg, Object::Verbatim(v) => Event::Verbatim(v), } } @@ -476,20 +460,20 @@ impl<'a> From> for Event<'a> { fn from(ele: Element<'a>) -> Self { match ele { Element::Comment(c) => Event::Comment(c), - Element::FnDef { label, contents } => Event::FnDef { label, contents }, + Element::CommentBlock { args, cont } => Event::CommentBlock { args, cont }, + Element::CtrBlock { .. } => Event::CtrBlockBeg, + Element::DynBlock { name, args, .. } => Event::DynBlockBeg { name, args }, + Element::ExampleBlock { args, cont } => Event::ExampleBlock { args, cont }, + Element::ExportBlock { args, cont } => Event::ExportBlock { args, cont }, + Element::FnDef { label, cont } => Event::FnDef { label, cont }, Element::Keyword { key, value } => Event::Keyword { key, value }, - Element::Paragraph { .. } => Event::StartParagraph, + Element::List { ordered, .. } => Event::ListBeg { ordered }, + Element::Paragraph { .. } => Event::ParagraphBeg, + Element::QteBlock { .. } => Event::QteBlockBeg, Element::Rule => Event::Rule, - Element::CenterBlock { .. } => Event::StartCenterBlock, - Element::QuoteBlock { .. } => Event::StartQuoteBlock, - Element::DynBlock { name, args, .. } => Event::StartDynBlock { name, args }, - Element::SpecialBlock { name, args, .. } => Event::StartSpecialBlock { name, args }, - Element::CommentBlock { args, contents } => Event::CommentBlock { args, contents }, - Element::ExampleBlock { args, contents } => Event::ExampleBlock { args, contents }, - Element::ExportBlock { args, contents } => Event::ExportBlock { args, contents }, - Element::SrcBlock { args, contents } => Event::SrcBlock { args, contents }, - Element::VerseBlock { args, contents } => Event::VerseBlock { args, contents }, - Element::List { is_ordered, .. } => Event::StartList { is_ordered }, + Element::SplBlock { name, args, .. } => Event::SplBlockBeg { name, args }, + Element::SrcBlock { args, cont } => Event::SrcBlock { args, cont }, + Element::VerseBlock { args, cont } => Event::VerseBlock { args, cont }, } } } @@ -499,40 +483,40 @@ fn parse() { use self::Event::*; let expected = vec![ - StartHeadline(Headline::new(1, None, None, "Title 1", None)), - StartSection, - StartParagraph, - StartBold, + HeadlineBeg(Headline::new(1, None, None, "Title 1", None)), + SectionBeg, + ParagraphBeg, + BoldBeg, Text("Section 1"), - EndBold, - EndParagraph, - EndSection, - StartHeadline(Headline::new(2, None, None, "Title 2", None)), - StartSection, - StartParagraph, - StartUnderline, + BoldEnd, + ParagraphEnd, + SectionEnd, + HeadlineBeg(Headline::new(2, None, None, "Title 2", None)), + SectionBeg, + ParagraphBeg, + UnderlineBeg, Text("Section 2"), - EndUnderline, - EndParagraph, - EndSection, - EndHeadline, - EndHeadline, - StartHeadline(Headline::new(1, None, None, "Title 3", None)), - StartSection, - StartParagraph, - StartItalic, + UnderlineEnd, + ParagraphEnd, + SectionEnd, + HeadlineEnd, + HeadlineEnd, + HeadlineBeg(Headline::new(1, None, None, "Title 3", None)), + SectionBeg, + ParagraphBeg, + ItalicBeg, Text("Section 3"), - EndItalic, - EndParagraph, - EndSection, - EndHeadline, - StartHeadline(Headline::new(1, None, None, "Title 4", None)), - StartSection, - StartParagraph, + ItalicEnd, + ParagraphEnd, + SectionEnd, + HeadlineEnd, + HeadlineBeg(Headline::new(1, None, None, "Title 4", None)), + SectionBeg, + ParagraphBeg, Verbatim("Section 4"), - EndParagraph, - EndSection, - EndHeadline, + ParagraphEnd, + SectionEnd, + HeadlineEnd, ]; assert_eq!(