Move to common trait for any exports
This commit is contained in:
parent
9d7852c4f9
commit
00b2c8f3c0
|
@ -5,7 +5,7 @@ use std::io::{Error as IOError, Write};
|
|||
use std::result::Result;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use orgize::export::{DefaultHtmlHandler, HtmlHandler};
|
||||
use orgize::export::{DefaultHtmlHandler, ExportHandler};
|
||||
use orgize::{Element, Org};
|
||||
use slugify::slugify;
|
||||
|
||||
|
@ -32,7 +32,7 @@ impl From<FromUtf8Error> for MyError {
|
|||
#[derive(Default)]
|
||||
struct MyHtmlHandler(DefaultHtmlHandler);
|
||||
|
||||
impl HtmlHandler<MyError> for MyHtmlHandler {
|
||||
impl ExportHandler<MyError> for MyHtmlHandler {
|
||||
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), MyError> {
|
||||
if let Element::Title(title) = element {
|
||||
if title.level > 6 {
|
||||
|
@ -72,7 +72,7 @@ fn main() -> Result<(), MyError> {
|
|||
|
||||
let mut writer = Vec::new();
|
||||
let mut handler = MyHtmlHandler::default();
|
||||
Org::parse(&contents).write_html_custom(&mut writer, &mut handler)?;
|
||||
Org::parse(&contents).write(&mut writer, &mut handler)?;
|
||||
|
||||
println!("{}", String::from_utf8(writer)?);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@ use std::io::{Error, Result as IOResult, Write};
|
|||
use jetscii::{bytes, BytesConst};
|
||||
|
||||
use crate::elements::{Element, Table, TableCell, TableRow, Timestamp};
|
||||
use crate::export::write_datetime;
|
||||
use crate::export::{write_datetime, ExportHandler};
|
||||
|
||||
/// A wrapper for escaping sensitive characters in html.
|
||||
///
|
||||
|
@ -49,16 +49,11 @@ impl<S: AsRef<str>> fmt::Display for HtmlEscape<S> {
|
|||
}
|
||||
}
|
||||
|
||||
pub trait HtmlHandler<E: From<Error>>: Default {
|
||||
fn start<W: Write>(&mut self, w: W, element: &Element) -> Result<(), E>;
|
||||
fn end<W: Write>(&mut self, w: W, element: &Element) -> Result<(), E>;
|
||||
}
|
||||
|
||||
/// Default Html Handler
|
||||
#[derive(Default)]
|
||||
pub struct DefaultHtmlHandler;
|
||||
|
||||
impl HtmlHandler<Error> for DefaultHtmlHandler {
|
||||
impl ExportHandler<Error> for DefaultHtmlHandler {
|
||||
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> IOResult<()> {
|
||||
match element {
|
||||
// container elements
|
||||
|
@ -304,7 +299,7 @@ mod syntect_handler {
|
|||
///
|
||||
/// }
|
||||
/// ```
|
||||
pub struct SyntectHtmlHandler<E: From<Error>, H: HtmlHandler<E>> {
|
||||
pub struct SyntectHtmlHandler<E: From<Error>, H: ExportHandler<E>> {
|
||||
/// syntax set, default is `SyntaxSet::load_defaults_newlines()`
|
||||
pub syntax_set: SyntaxSet,
|
||||
/// theme set, default is `ThemeSet::load_defaults()`
|
||||
|
@ -319,7 +314,7 @@ mod syntect_handler {
|
|||
pub error_type: PhantomData<E>,
|
||||
}
|
||||
|
||||
impl<E: From<Error>, H: HtmlHandler<E>> SyntectHtmlHandler<E, H> {
|
||||
impl<E: From<Error>, H: ExportHandler<E>> SyntectHtmlHandler<E, H> {
|
||||
pub fn new(inner: H) -> Self {
|
||||
SyntectHtmlHandler {
|
||||
inner,
|
||||
|
@ -339,7 +334,7 @@ mod syntect_handler {
|
|||
}
|
||||
}
|
||||
|
||||
impl<E: From<Error>, H: HtmlHandler<E>> Default for SyntectHtmlHandler<E, H> {
|
||||
impl<E: From<Error>, H: ExportHandler<E>> Default for SyntectHtmlHandler<E, H> {
|
||||
fn default() -> Self {
|
||||
SyntectHtmlHandler {
|
||||
syntax_set: SyntaxSet::load_defaults_newlines(),
|
||||
|
@ -352,7 +347,7 @@ mod syntect_handler {
|
|||
}
|
||||
}
|
||||
|
||||
impl<E: From<Error>, H: HtmlHandler<E>> HtmlHandler<E> for SyntectHtmlHandler<E, H> {
|
||||
impl<E: From<Error>, H: ExportHandler<E>> ExportHandler<E> for SyntectHtmlHandler<E, H> {
|
||||
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> {
|
||||
match element {
|
||||
Element::InlineSrc(inline_src) => write!(
|
||||
|
|
|
@ -5,12 +5,18 @@ mod org;
|
|||
|
||||
#[cfg(feature = "syntect")]
|
||||
pub use html::SyntectHtmlHandler;
|
||||
pub use html::{DefaultHtmlHandler, HtmlEscape, HtmlHandler};
|
||||
pub use org::{DefaultOrgHandler, OrgHandler};
|
||||
pub use html::{DefaultHtmlHandler, HtmlEscape};
|
||||
pub use org::{DefaultOrgHandler};
|
||||
|
||||
use std::io::{Error, Write};
|
||||
|
||||
use crate::elements::Datetime;
|
||||
use crate::elements::{Datetime, Element};
|
||||
|
||||
pub trait ExportHandler<E: From<Error>>: Default {
|
||||
fn start<W: Write>(&mut self, writer: W, element: &Element) -> Result<(), E>;
|
||||
fn end<W: Write>(&mut self, writer: W, element: &Element) -> Result<(), E>;
|
||||
}
|
||||
|
||||
|
||||
pub(crate) fn write_datetime<W: Write>(
|
||||
mut w: W,
|
||||
|
|
|
@ -1,17 +1,12 @@
|
|||
use std::io::{Error, Result as IOResult, Write};
|
||||
|
||||
use crate::elements::{Clock, Element, Table, Timestamp};
|
||||
use crate::export::write_datetime;
|
||||
|
||||
pub trait OrgHandler<E: From<Error>>: Default {
|
||||
fn start<W: Write>(&mut self, w: W, element: &Element) -> Result<(), E>;
|
||||
fn end<W: Write>(&mut self, w: W, element: &Element) -> Result<(), E>;
|
||||
}
|
||||
use crate::export::{write_datetime, ExportHandler};
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct DefaultOrgHandler;
|
||||
|
||||
impl OrgHandler<Error> for DefaultOrgHandler {
|
||||
impl ExportHandler<Error> for DefaultOrgHandler {
|
||||
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> IOResult<()> {
|
||||
match element {
|
||||
// container elements
|
||||
|
|
16
src/lib.rs
16
src/lib.rs
|
@ -74,13 +74,13 @@
|
|||
//! );
|
||||
//! ```
|
||||
//!
|
||||
//! # Render html with custom `HtmlHandler`
|
||||
//! # Render html with custom `ExportHandler`
|
||||
//!
|
||||
//! To customize html rendering, simply implementing [`HtmlHandler`] trait and passing
|
||||
//! it to the [`Org::write_html_custom`] function.
|
||||
//! To customize html rendering, simply implementing [`ExportHandler`] trait and passing
|
||||
//! it to the [`Org::write`] function.
|
||||
//!
|
||||
//! [`HtmlHandler`]: export/trait.HtmlHandler.html
|
||||
//! [`Org::write_html_custom`]: struct.Org.html#method.write_html_custom
|
||||
//! [`ExportHandler`]: export/trait.ExportHandler.html
|
||||
//! [`Org::write`]: struct.Org.html#method.write
|
||||
//!
|
||||
//! The following code demonstrates how to add a id for every headline and return
|
||||
//! own error type while rendering.
|
||||
|
@ -90,7 +90,7 @@
|
|||
//! use std::io::{Error as IOError, Write};
|
||||
//! use std::string::FromUtf8Error;
|
||||
//!
|
||||
//! use orgize::export::{DefaultHtmlHandler, HtmlHandler};
|
||||
//! use orgize::export::{DefaultHtmlHandler, ExportHandler};
|
||||
//! use orgize::{Element, Org};
|
||||
//! use slugify::slugify;
|
||||
//!
|
||||
|
@ -117,7 +117,7 @@
|
|||
//! #[derive(Default)]
|
||||
//! struct MyHtmlHandler(DefaultHtmlHandler);
|
||||
//!
|
||||
//! impl HtmlHandler<MyError> for MyHtmlHandler {
|
||||
//! impl ExportHandler<MyError> for MyHtmlHandler {
|
||||
//! fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), MyError> {
|
||||
//! if let Element::Title(title) = element {
|
||||
//! if title.level > 6 {
|
||||
|
@ -150,7 +150,7 @@
|
|||
//! fn main() -> Result<(), MyError> {
|
||||
//! let mut writer = Vec::new();
|
||||
//! let mut handler = MyHtmlHandler::default();
|
||||
//! Org::parse("* title\n*section*").write_html_custom(&mut writer, &mut handler)?;
|
||||
//! Org::parse("* title\n*section*").write(&mut writer, &mut handler)?;
|
||||
//!
|
||||
//! assert_eq!(
|
||||
//! String::from_utf8(writer)?,
|
||||
|
|
43
src/org.rs
43
src/org.rs
|
@ -5,7 +5,7 @@ use std::ops::{Index, IndexMut};
|
|||
use crate::{
|
||||
config::{ParseConfig, DEFAULT_CONFIG},
|
||||
elements::{Element, Keyword},
|
||||
export::{DefaultHtmlHandler, DefaultOrgHandler, HtmlHandler, OrgHandler},
|
||||
export::{DefaultHtmlHandler, DefaultOrgHandler, ExportHandler},
|
||||
parsers::{blank_lines_count, parse_container, Container, OwnedArena},
|
||||
};
|
||||
|
||||
|
@ -109,20 +109,12 @@ impl<'a> Org<'a> {
|
|||
})
|
||||
}
|
||||
|
||||
/// Writes an `Org` struct as html format.
|
||||
pub fn write_html<W>(&self, writer: W) -> Result<(), Error>
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
self.write_html_custom(writer, &mut DefaultHtmlHandler)
|
||||
}
|
||||
|
||||
/// Writes an `Org` struct as html format with custom `HtmlHandler`.
|
||||
pub fn write_html_custom<W, H, E>(&self, mut writer: W, handler: &mut H) -> Result<(), E>
|
||||
/// Writes the document using the given {ExportHandler}
|
||||
pub fn write<W, X, E>(&self, mut writer: W, handler: &mut X) -> Result<(), E>
|
||||
where
|
||||
W: Write,
|
||||
E: From<Error>,
|
||||
H: HtmlHandler<E>,
|
||||
X: ExportHandler<E>,
|
||||
{
|
||||
for event in self.iter() {
|
||||
match event {
|
||||
|
@ -134,29 +126,20 @@ impl<'a> Org<'a> {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Writes an `Org` struct as html format.
|
||||
pub fn write_html<W>(&self, writer: W) -> Result<(), Error>
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
self.write(writer, &mut DefaultHtmlHandler)
|
||||
}
|
||||
|
||||
/// Writes an `Org` struct as org format.
|
||||
pub fn write_org<W>(&self, writer: W) -> Result<(), Error>
|
||||
where
|
||||
W: Write,
|
||||
{
|
||||
self.write_org_custom(writer, &mut DefaultOrgHandler)
|
||||
}
|
||||
|
||||
/// Writes an `Org` struct as org format with custom `OrgHandler`.
|
||||
pub fn write_org_custom<W, H, E>(&self, mut writer: W, handler: &mut H) -> Result<(), E>
|
||||
where
|
||||
W: Write,
|
||||
E: From<Error>,
|
||||
H: OrgHandler<E>,
|
||||
{
|
||||
for event in self.iter() {
|
||||
match event {
|
||||
Event::Start(element) => handler.start(&mut writer, element)?,
|
||||
Event::End(element) => handler.end(&mut writer, element)?,
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
self.write(writer, &mut DefaultOrgHandler)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue