feat(export): handler with custom error type
This commit is contained in:
parent
355ea8b46e
commit
0b22db1f0f
4 changed files with 256 additions and 237 deletions
|
|
@ -1,26 +1,53 @@
|
|||
use std::env::args;
|
||||
use std::fs::File;
|
||||
use std::io::{Cursor, Read, Result, Write};
|
||||
|
||||
use orgize::export::*;
|
||||
use orgize::headline::Headline;
|
||||
use slugify::slugify;
|
||||
use std::convert::From;
|
||||
use std::env::args;
|
||||
use std::fs::File;
|
||||
use std::io::{Cursor, Error as IOError, Read, Write};
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
struct CustomHtmlHandler;
|
||||
|
||||
impl<W: Write> HtmlHandler<W> for CustomHtmlHandler {
|
||||
fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<()> {
|
||||
write!(
|
||||
w,
|
||||
r##"<h{0}><a class="anchor" href="#{1}">{2}</a></h{0}>"##,
|
||||
if hdl.level <= 6 { hdl.level } else { 6 },
|
||||
slugify!(hdl.title),
|
||||
hdl.title,
|
||||
)
|
||||
#[derive(Debug)]
|
||||
enum Error {
|
||||
IO(IOError),
|
||||
Heading,
|
||||
Utf8(FromUtf8Error),
|
||||
}
|
||||
|
||||
// From<std::io::Error> trait is required
|
||||
impl From<IOError> for Error {
|
||||
fn from(err: IOError) -> Error {
|
||||
Error::IO(err)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<()> {
|
||||
impl From<FromUtf8Error> for Error {
|
||||
fn from(err: FromUtf8Error) -> Error {
|
||||
Error::Utf8(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Result = std::result::Result<(), Error>;
|
||||
|
||||
impl<W: Write> HtmlHandler<W, Error> for CustomHtmlHandler {
|
||||
fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result {
|
||||
if hdl.level > 6 {
|
||||
Err(Error::Heading)
|
||||
} else {
|
||||
Ok(write!(
|
||||
w,
|
||||
r##"<h{0}><a class="anchor" href="#{1}">{2}</a></h{0}>"##,
|
||||
hdl.level,
|
||||
slugify!(hdl.title),
|
||||
hdl.title,
|
||||
)?)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result {
|
||||
let args: Vec<_> = args().collect();
|
||||
|
||||
if args.len() < 2 {
|
||||
|
|
@ -39,10 +66,7 @@ fn main() -> Result<()> {
|
|||
|
||||
render.render()?;
|
||||
|
||||
println!(
|
||||
"{}",
|
||||
String::from_utf8(cursor.into_inner()).expect("invalid utf-8")
|
||||
);
|
||||
println!("{}", String::from_utf8(cursor.into_inner())?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -1,103 +1,104 @@
|
|||
#![allow(unused_variables)]
|
||||
|
||||
use std::fmt;
|
||||
use std::io::{Result, Write};
|
||||
|
||||
use jetscii::ascii_chars;
|
||||
|
||||
use crate::elements::Key;
|
||||
use crate::headline::Headline;
|
||||
use crate::objects::Cookie;
|
||||
use crate::parser::Parser;
|
||||
use jetscii::ascii_chars;
|
||||
use std::convert::From;
|
||||
use std::fmt;
|
||||
use std::io::{Error, Write};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub trait HtmlHandler<W: Write> {
|
||||
fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<()> {
|
||||
pub trait HtmlHandler<W: Write, E: From<Error>> {
|
||||
fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<(), E> {
|
||||
let level = if hdl.level <= 6 { hdl.level } else { 6 };
|
||||
write!(w, "<h{0}>{1}</h{0}>", level, Escape(hdl.title))
|
||||
Ok(write!(w, "<h{0}>{1}</h{0}>", level, Escape(hdl.title))?)
|
||||
}
|
||||
fn handle_headline_end(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_headline_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_section_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<section>")
|
||||
fn handle_section_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<section>")?)
|
||||
}
|
||||
fn handle_section_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</section>")
|
||||
fn handle_section_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</section>")?)
|
||||
}
|
||||
fn handle_paragraph_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<p>")
|
||||
fn handle_paragraph_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<p>")?)
|
||||
}
|
||||
fn handle_paragraph_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</p>")
|
||||
fn handle_paragraph_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</p>")?)
|
||||
}
|
||||
fn handle_ctr_block_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, r#"<div style="text-align: center">"#)
|
||||
fn handle_ctr_block_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, r#"<div style="text-align: center">"#)?)
|
||||
}
|
||||
fn handle_ctr_block_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</div>")
|
||||
fn handle_ctr_block_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</div>")?)
|
||||
}
|
||||
fn handle_qte_block_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<blockquote>")
|
||||
fn handle_qte_block_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<blockquote>")?)
|
||||
}
|
||||
fn handle_qte_block_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</blockquote>")
|
||||
fn handle_qte_block_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</blockquote>")?)
|
||||
}
|
||||
fn handle_spl_block_beg(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()> {
|
||||
write!(w, "<div>")
|
||||
fn handle_spl_block_beg(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<(), E> {
|
||||
Ok(write!(w, "<div>")?)
|
||||
}
|
||||
fn handle_spl_block_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</div>")
|
||||
fn handle_spl_block_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</div>")?)
|
||||
}
|
||||
fn handle_comment_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> {
|
||||
fn handle_comment_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_example_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> {
|
||||
write!(w, "<pre><code>{}</code></pre>", Escape(cont))
|
||||
fn handle_example_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<(), E> {
|
||||
Ok(write!(w, "<pre><code>{}</code></pre>", Escape(cont))?)
|
||||
}
|
||||
fn handle_export_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<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_src_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<()> {
|
||||
write!(w, "<pre><code>{}</code></pre>", Escape(cont))
|
||||
fn handle_src_block(&mut self, w: &mut W, cont: &str, args: Option<&str>) -> Result<(), E> {
|
||||
Ok(write!(w, "<pre><code>{}</code></pre>", Escape(cont))?)
|
||||
}
|
||||
fn handle_verse_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<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_dyn_block_beg(&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<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_dyn_block_end(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_dyn_block_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_list_beg(&mut self, w: &mut W, ordered: bool) -> Result<()> {
|
||||
fn handle_list_beg(&mut self, w: &mut W, ordered: bool) -> Result<(), E> {
|
||||
if ordered {
|
||||
write!(w, "<ol>")
|
||||
Ok(write!(w, "<ol>")?)
|
||||
} else {
|
||||
write!(w, "<ul>")
|
||||
Ok(write!(w, "<ul>")?)
|
||||
}
|
||||
}
|
||||
fn handle_list_end(&mut self, w: &mut W, ordered: bool) -> Result<()> {
|
||||
fn handle_list_end(&mut self, w: &mut W, ordered: bool) -> Result<(), E> {
|
||||
if ordered {
|
||||
write!(w, "</ol>")
|
||||
Ok(write!(w, "</ol>")?)
|
||||
} else {
|
||||
write!(w, "</ul>")
|
||||
Ok(write!(w, "</ul>")?)
|
||||
}
|
||||
}
|
||||
fn handle_list_beg_item(&mut self, w: &mut W, bullet: &str) -> Result<()> {
|
||||
write!(w, "<li>")
|
||||
fn handle_list_beg_item(&mut self, w: &mut W, bullet: &str) -> Result<(), E> {
|
||||
Ok(write!(w, "<li>")?)
|
||||
}
|
||||
fn handle_list_end_item(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</li>")
|
||||
fn handle_list_end_item(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</li>")?)
|
||||
}
|
||||
fn handle_call(&mut self, w: &mut W, value: &str) -> Result<()> {
|
||||
fn handle_call(&mut self, w: &mut W, value: &str) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_clock(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_clock(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_comment(&mut self, w: &mut W, cont: &str) -> Result<()> {
|
||||
fn handle_comment(&mut self, w: &mut W, cont: &str) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_fixed_width(&mut self, w: &mut W, cont: &str) -> Result<()> {
|
||||
fn handle_fixed_width(&mut self, w: &mut W, cont: &str) -> Result<(), E> {
|
||||
for line in cont.lines() {
|
||||
// remove leading colon
|
||||
write!(w, "<pre>{}</pre>", Escape(&line[1..]))?;
|
||||
|
|
@ -105,31 +106,36 @@ pub trait HtmlHandler<W: Write> {
|
|||
|
||||
Ok(())
|
||||
}
|
||||
fn handle_table_start(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_table_start(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_table_end(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_table_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_table_cell(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_table_cell(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_latex_env(&mut self, w: &mut W) -> Result<()> {
|
||||
fn handle_latex_env(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_fn_def(&mut self, w: &mut W, label: &str, cont: &str) -> Result<()> {
|
||||
fn handle_fn_def(&mut self, w: &mut W, label: &str, cont: &str) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_keyword(&mut self, w: &mut W, key: Key<'_>, value: &str) -> Result<()> {
|
||||
fn handle_keyword(&mut self, w: &mut W, key: Key<'_>, value: &str) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_rule(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<hr>")
|
||||
fn handle_rule(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<hr>")?)
|
||||
}
|
||||
fn handle_cookie(&mut self, w: &mut W, cookie: Cookie) -> Result<()> {
|
||||
fn handle_cookie(&mut self, w: &mut W, cookie: Cookie) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_fn_ref(&mut self, w: &mut W, label: Option<&str>, def: Option<&str>) -> Result<()> {
|
||||
fn handle_fn_ref(
|
||||
&mut self,
|
||||
w: &mut W,
|
||||
label: Option<&str>,
|
||||
def: Option<&str>,
|
||||
) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_inline_call(
|
||||
|
|
@ -139,7 +145,7 @@ pub trait HtmlHandler<W: Write> {
|
|||
args: &str,
|
||||
inside_header: Option<&str>,
|
||||
end_header: Option<&str>,
|
||||
) -> Result<()> {
|
||||
) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_inline_src(
|
||||
|
|
@ -148,64 +154,105 @@ pub trait HtmlHandler<W: Write> {
|
|||
lang: &str,
|
||||
option: Option<&str>,
|
||||
body: &str,
|
||||
) -> Result<()> {
|
||||
write!(w, "<code>{}</code>", Escape(body))
|
||||
) -> Result<(), E> {
|
||||
Ok(write!(w, "<code>{}</code>", Escape(body))?)
|
||||
}
|
||||
fn handle_link(&mut self, w: &mut W, path: &str, desc: Option<&str>) -> Result<()> {
|
||||
fn handle_link(&mut self, w: &mut W, path: &str, desc: Option<&str>) -> Result<(), E> {
|
||||
if let Some(desc) = desc {
|
||||
write!(w, r#"<a href="{}">{}</a>"#, Escape(path), Escape(desc))
|
||||
Ok(write!(
|
||||
w,
|
||||
r#"<a href="{}">{}</a>"#,
|
||||
Escape(path),
|
||||
Escape(desc)
|
||||
)?)
|
||||
} else {
|
||||
write!(w, r#"<a href="{0}">{0}</a>"#, Escape(path))
|
||||
Ok(write!(w, r#"<a href="{0}">{0}</a>"#, Escape(path))?)
|
||||
}
|
||||
}
|
||||
fn handle_macros(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<()> {
|
||||
fn handle_macros(&mut self, w: &mut W, name: &str, args: Option<&str>) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_radio_target(&mut self, w: &mut W, target: &str) -> Result<()> {
|
||||
fn handle_radio_target(&mut self, w: &mut W, target: &str) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_snippet(&mut self, w: &mut W, name: &str, value: &str) -> Result<()> {
|
||||
fn handle_snippet(&mut self, w: &mut W, name: &str, value: &str) -> Result<(), E> {
|
||||
if name.eq_ignore_ascii_case("HTML") {
|
||||
write!(w, "{}", value)
|
||||
Ok(write!(w, "{}", value)?)
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
fn handle_target(&mut self, w: &mut W, target: &str) -> Result<()> {
|
||||
fn handle_target(&mut self, w: &mut W, target: &str) -> Result<(), E> {
|
||||
Ok(())
|
||||
}
|
||||
fn handle_bold_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<b>")
|
||||
fn handle_bold_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<b>")?)
|
||||
}
|
||||
fn handle_bold_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</b>")
|
||||
fn handle_bold_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</b>")?)
|
||||
}
|
||||
fn handle_italic_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<i>")
|
||||
fn handle_italic_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<i>")?)
|
||||
}
|
||||
fn handle_italic_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</i>")
|
||||
fn handle_italic_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</i>")?)
|
||||
}
|
||||
fn handle_strike_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<s>")
|
||||
fn handle_strike_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<s>")?)
|
||||
}
|
||||
fn handle_strike_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</s>")
|
||||
fn handle_strike_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</s>")?)
|
||||
}
|
||||
fn handle_underline_beg(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "<u>")
|
||||
fn handle_underline_beg(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "<u>")?)
|
||||
}
|
||||
fn handle_underline_end(&mut self, w: &mut W) -> Result<()> {
|
||||
write!(w, "</u>")
|
||||
fn handle_underline_end(&mut self, w: &mut W) -> Result<(), E> {
|
||||
Ok(write!(w, "</u>")?)
|
||||
}
|
||||
fn handle_verbatim(&mut self, w: &mut W, cont: &str) -> Result<()> {
|
||||
write!(w, "<code>{}</code>", Escape(cont))
|
||||
fn handle_verbatim(&mut self, w: &mut W, cont: &str) -> Result<(), E> {
|
||||
Ok(write!(w, "<code>{}</code>", Escape(cont))?)
|
||||
}
|
||||
fn handle_code(&mut self, w: &mut W, cont: &str) -> Result<()> {
|
||||
write!(w, "<code>{}</code>", Escape(cont))
|
||||
fn handle_code(&mut self, w: &mut W, cont: &str) -> Result<(), E> {
|
||||
Ok(write!(w, "<code>{}</code>", Escape(cont))?)
|
||||
}
|
||||
fn handle_text(&mut self, w: &mut W, cont: &str) -> Result<()> {
|
||||
write!(w, "{}", Escape(cont))
|
||||
fn handle_text(&mut self, w: &mut W, cont: &str) -> Result<(), E> {
|
||||
Ok(write!(w, "{}", Escape(cont))?)
|
||||
}
|
||||
}
|
||||
|
||||
pub struct DefaultHtmlHandler;
|
||||
|
||||
impl<W: Write> HtmlHandler<W, Error> for DefaultHtmlHandler {}
|
||||
|
||||
pub struct HtmlRender<'a, W: Write, E: From<Error>, H: HtmlHandler<W, E>> {
|
||||
pub parser: Parser<'a>,
|
||||
pub writer: &'a mut W,
|
||||
handler: H,
|
||||
error_type: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<'a, W: Write> HtmlRender<'a, W, Error, DefaultHtmlHandler> {
|
||||
pub fn default(writer: &'a mut W, text: &'a str) -> Self {
|
||||
HtmlRender::new(DefaultHtmlHandler, writer, text)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, W: Write, E: From<Error>, H: HtmlHandler<W, E>> HtmlRender<'a, W, E, H> {
|
||||
pub fn new(handler: H, writer: &'a mut W, text: &'a str) -> Self {
|
||||
HtmlRender {
|
||||
parser: Parser::new(text),
|
||||
handler,
|
||||
writer,
|
||||
error_type: PhantomData,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self) -> Result<(), E> {
|
||||
for event in &mut self.parser {
|
||||
handle_event!(event, &mut self.handler, &mut self.writer);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,126 +1,74 @@
|
|||
mod html;
|
||||
#[macro_use]
|
||||
macro_rules! handle_event {
|
||||
($event:expr, $handler:expr, $writer:expr) => {
|
||||
use crate::parser::Event::*;
|
||||
|
||||
pub use html::*;
|
||||
|
||||
use crate::parser::Parser;
|
||||
use std::io::{Result, Write};
|
||||
|
||||
macro_rules! create_render {
|
||||
($handler:ident, $default_handler:ident, $render:ident, $default_render:ident) => {
|
||||
struct $default_handler;
|
||||
|
||||
impl<W: Write> $handler<W> for $default_handler {}
|
||||
|
||||
pub struct $default_render<'a, W: Write>($render<'a, W, $default_handler>);
|
||||
|
||||
impl<'a, W: Write> $default_render<'a, W> {
|
||||
#[inline]
|
||||
pub fn new(writer: &'a mut W, text: &'a str) -> Self {
|
||||
$default_render($render::new($default_handler, writer, text))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn render(&mut self) -> Result<()> {
|
||||
self.0.render()
|
||||
}
|
||||
}
|
||||
|
||||
pub struct $render<'a, W: Write, H: $handler<W>> {
|
||||
pub parser: Parser<'a>,
|
||||
handler: H,
|
||||
writer: &'a mut W,
|
||||
}
|
||||
|
||||
impl<'a, W: Write, H: $handler<W>> $render<'a, W, H> {
|
||||
pub fn new(handler: H, writer: &'a mut W, text: &'a str) -> Self {
|
||||
$render {
|
||||
parser: Parser::new(text),
|
||||
handler,
|
||||
writer,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn render(&mut self) -> Result<()> {
|
||||
use crate::parser::Event::*;
|
||||
|
||||
let w = &mut self.writer;
|
||||
let h = &mut self.handler;
|
||||
|
||||
for event in &mut self.parser {
|
||||
match event {
|
||||
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 { bullet } => h.handle_list_beg_item(w, bullet)?,
|
||||
ListItemEnd => h.handle_list_end_item(w)?,
|
||||
Call { value } => h.handle_call(w, value)?,
|
||||
Clock => h.handle_clock(w)?,
|
||||
Comment(c) => h.handle_comment(w, c)?,
|
||||
FixedWidth(f) => h.handle_fixed_width(w, f)?,
|
||||
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 { label, def } => h.handle_fn_ref(w, label, def)?,
|
||||
InlineSrc { lang, option, body } => {
|
||||
h.handle_inline_src(w, lang, option, body)?
|
||||
}
|
||||
InlineCall {
|
||||
name,
|
||||
args,
|
||||
inside_header,
|
||||
end_header,
|
||||
} => h.handle_inline_call(w, name, args, inside_header, end_header)?,
|
||||
Link { path, desc } => h.handle_link(w, path, desc)?,
|
||||
Macros { name, args } => h.handle_macros(w, name, args)?,
|
||||
RadioTarget { target } => h.handle_radio_target(w, target)?,
|
||||
Snippet { name, value } => h.handle_snippet(w, name, value)?,
|
||||
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)?,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
match $event {
|
||||
HeadlineBeg(hdl) => $handler.handle_headline_beg($writer, hdl)?,
|
||||
HeadlineEnd => $handler.handle_headline_end($writer)?,
|
||||
SectionBeg => $handler.handle_section_beg($writer)?,
|
||||
SectionEnd => $handler.handle_section_end($writer)?,
|
||||
ParagraphBeg => $handler.handle_paragraph_beg($writer)?,
|
||||
ParagraphEnd => $handler.handle_paragraph_end($writer)?,
|
||||
CtrBlockBeg => $handler.handle_ctr_block_beg($writer)?,
|
||||
CtrBlockEnd => $handler.handle_ctr_block_end($writer)?,
|
||||
QteBlockBeg => $handler.handle_qte_block_beg($writer)?,
|
||||
QteBlockEnd => $handler.handle_qte_block_end($writer)?,
|
||||
SplBlockBeg { name, args } => $handler.handle_spl_block_beg($writer, name, args)?,
|
||||
SplBlockEnd => $handler.handle_spl_block_end($writer)?,
|
||||
CommentBlock { cont, args } => $handler.handle_comment_block($writer, cont, args)?,
|
||||
ExampleBlock { cont, args } => $handler.handle_example_block($writer, cont, args)?,
|
||||
ExportBlock { cont, args } => $handler.handle_export_block($writer, cont, args)?,
|
||||
SrcBlock { cont, args } => $handler.handle_src_block($writer, cont, args)?,
|
||||
VerseBlock { cont, args } => $handler.handle_verse_block($writer, cont, args)?,
|
||||
DynBlockBeg { name, args } => $handler.handle_dyn_block_beg($writer, name, args)?,
|
||||
DynBlockEnd => $handler.handle_dyn_block_end($writer)?,
|
||||
ListBeg { ordered } => $handler.handle_list_beg($writer, ordered)?,
|
||||
ListEnd { ordered } => $handler.handle_list_end($writer, ordered)?,
|
||||
ListItemBeg { bullet } => $handler.handle_list_beg_item($writer, bullet)?,
|
||||
ListItemEnd => $handler.handle_list_end_item($writer)?,
|
||||
Call { value } => $handler.handle_call($writer, value)?,
|
||||
Clock => $handler.handle_clock($writer)?,
|
||||
Comment(c) => $handler.handle_comment($writer, c)?,
|
||||
FixedWidth(f) => $handler.handle_fixed_width($writer, f)?,
|
||||
TableStart => $handler.handle_table_start($writer)?,
|
||||
TableEnd => $handler.handle_table_end($writer)?,
|
||||
TableCell => $handler.handle_table_cell($writer)?,
|
||||
LatexEnv => $handler.handle_latex_env($writer)?,
|
||||
FnDef { label, cont } => $handler.handle_fn_def($writer, label, cont)?,
|
||||
Keyword { key, value } => $handler.handle_keyword($writer, key, value)?,
|
||||
Rule => $handler.handle_rule($writer)?,
|
||||
Cookie(cookie) => $handler.handle_cookie($writer, cookie)?,
|
||||
FnRef { label, def } => $handler.handle_fn_ref($writer, label, def)?,
|
||||
InlineSrc { lang, option, body } => {
|
||||
$handler.handle_inline_src($writer, lang, option, body)?
|
||||
}
|
||||
InlineCall {
|
||||
name,
|
||||
args,
|
||||
inside_header,
|
||||
end_header,
|
||||
} => $handler.handle_inline_call($writer, name, args, inside_header, end_header)?,
|
||||
Link { path, desc } => $handler.handle_link($writer, path, desc)?,
|
||||
Macros { name, args } => $handler.handle_macros($writer, name, args)?,
|
||||
RadioTarget { target } => $handler.handle_radio_target($writer, target)?,
|
||||
Snippet { name, value } => $handler.handle_snippet($writer, name, value)?,
|
||||
Target { target } => $handler.handle_target($writer, target)?,
|
||||
BoldBeg => $handler.handle_bold_beg($writer)?,
|
||||
BoldEnd => $handler.handle_bold_end($writer)?,
|
||||
ItalicBeg => $handler.handle_italic_beg($writer)?,
|
||||
ItalicEnd => $handler.handle_italic_end($writer)?,
|
||||
StrikeBeg => $handler.handle_strike_beg($writer)?,
|
||||
StrikeEnd => $handler.handle_strike_end($writer)?,
|
||||
UnderlineBeg => $handler.handle_underline_beg($writer)?,
|
||||
UnderlineEnd => $handler.handle_underline_end($writer)?,
|
||||
Verbatim(cont) => $handler.handle_verbatim($writer, cont)?,
|
||||
Code(cont) => $handler.handle_code($writer, cont)?,
|
||||
Text(cont) => $handler.handle_text($writer, cont)?,
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
create_render!(
|
||||
HtmlHandler,
|
||||
DefaultHtmlHandller,
|
||||
HtmlRender,
|
||||
DefaultHtmlRender
|
||||
);
|
||||
mod html;
|
||||
|
||||
pub use html::*;
|
||||
|
|
|
|||
|
|
@ -24,7 +24,7 @@
|
|||
//! Alternatively, you can use the built-in render directly:
|
||||
//!
|
||||
//! ```rust
|
||||
//! use orgize::export::DefaultHtmlRender;
|
||||
//! use orgize::export::HtmlRender;
|
||||
//! use std::io::Cursor;
|
||||
//!
|
||||
//! let contents = r"* Title 1
|
||||
|
|
@ -37,7 +37,7 @@
|
|||
//! =Section 4=";
|
||||
//!
|
||||
//! let mut cursor = Cursor::new(Vec::new());
|
||||
//! let mut render = DefaultHtmlRender::new(&mut cursor, &contents);
|
||||
//! let mut render = HtmlRender::default(&mut cursor, &contents);
|
||||
//!
|
||||
//! render
|
||||
//! .render()
|
||||
|
|
@ -50,7 +50,7 @@
|
|||
//! add an anchor to every headline.
|
||||
//!
|
||||
//! ```rust
|
||||
//! use std::io::{Cursor, Result, Write};
|
||||
//! use std::io::{Cursor, Error, Result, Write};
|
||||
//!
|
||||
//! use orgize::export::*;
|
||||
//! use orgize::headline::Headline;
|
||||
|
|
@ -58,7 +58,7 @@
|
|||
//!
|
||||
//! struct CustomHtmlHandler;
|
||||
//!
|
||||
//! impl<W: Write> HtmlHandler<W> for CustomHtmlHandler {
|
||||
//! impl<W: Write> HtmlHandler<W, Error> for CustomHtmlHandler {
|
||||
//! fn handle_headline_beg(&mut self, w: &mut W, hdl: Headline) -> Result<()> {
|
||||
//! write!(
|
||||
//! w,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue