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::result::Result;
|
||||||
use std::string::FromUtf8Error;
|
use std::string::FromUtf8Error;
|
||||||
|
|
||||||
use orgize::export::{DefaultHtmlHandler, HtmlHandler};
|
use orgize::export::{DefaultHtmlHandler, ExportHandler};
|
||||||
use orgize::{Element, Org};
|
use orgize::{Element, Org};
|
||||||
use slugify::slugify;
|
use slugify::slugify;
|
||||||
|
|
||||||
|
@ -32,7 +32,7 @@ impl From<FromUtf8Error> for MyError {
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct MyHtmlHandler(DefaultHtmlHandler);
|
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> {
|
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), MyError> {
|
||||||
if let Element::Title(title) = element {
|
if let Element::Title(title) = element {
|
||||||
if title.level > 6 {
|
if title.level > 6 {
|
||||||
|
@ -72,7 +72,7 @@ fn main() -> Result<(), MyError> {
|
||||||
|
|
||||||
let mut writer = Vec::new();
|
let mut writer = Vec::new();
|
||||||
let mut handler = MyHtmlHandler::default();
|
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)?);
|
println!("{}", String::from_utf8(writer)?);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use std::io::{Error, Result as IOResult, Write};
|
||||||
use jetscii::{bytes, BytesConst};
|
use jetscii::{bytes, BytesConst};
|
||||||
|
|
||||||
use crate::elements::{Element, Table, TableCell, TableRow, Timestamp};
|
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.
|
/// 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
|
/// Default Html Handler
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DefaultHtmlHandler;
|
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<()> {
|
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> IOResult<()> {
|
||||||
match element {
|
match element {
|
||||||
// container elements
|
// 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()`
|
/// syntax set, default is `SyntaxSet::load_defaults_newlines()`
|
||||||
pub syntax_set: SyntaxSet,
|
pub syntax_set: SyntaxSet,
|
||||||
/// theme set, default is `ThemeSet::load_defaults()`
|
/// theme set, default is `ThemeSet::load_defaults()`
|
||||||
|
@ -319,7 +314,7 @@ mod syntect_handler {
|
||||||
pub error_type: PhantomData<E>,
|
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 {
|
pub fn new(inner: H) -> Self {
|
||||||
SyntectHtmlHandler {
|
SyntectHtmlHandler {
|
||||||
inner,
|
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 {
|
fn default() -> Self {
|
||||||
SyntectHtmlHandler {
|
SyntectHtmlHandler {
|
||||||
syntax_set: SyntaxSet::load_defaults_newlines(),
|
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> {
|
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), E> {
|
||||||
match element {
|
match element {
|
||||||
Element::InlineSrc(inline_src) => write!(
|
Element::InlineSrc(inline_src) => write!(
|
||||||
|
|
|
@ -5,12 +5,18 @@ mod org;
|
||||||
|
|
||||||
#[cfg(feature = "syntect")]
|
#[cfg(feature = "syntect")]
|
||||||
pub use html::SyntectHtmlHandler;
|
pub use html::SyntectHtmlHandler;
|
||||||
pub use html::{DefaultHtmlHandler, HtmlEscape, HtmlHandler};
|
pub use html::{DefaultHtmlHandler, HtmlEscape};
|
||||||
pub use org::{DefaultOrgHandler, OrgHandler};
|
pub use org::{DefaultOrgHandler};
|
||||||
|
|
||||||
use std::io::{Error, Write};
|
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>(
|
pub(crate) fn write_datetime<W: Write>(
|
||||||
mut w: W,
|
mut w: W,
|
||||||
|
|
|
@ -1,17 +1,12 @@
|
||||||
use std::io::{Error, Result as IOResult, Write};
|
use std::io::{Error, Result as IOResult, Write};
|
||||||
|
|
||||||
use crate::elements::{Clock, Element, Table, Timestamp};
|
use crate::elements::{Clock, Element, Table, Timestamp};
|
||||||
use crate::export::write_datetime;
|
use crate::export::{write_datetime, ExportHandler};
|
||||||
|
|
||||||
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>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
pub struct DefaultOrgHandler;
|
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<()> {
|
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> IOResult<()> {
|
||||||
match element {
|
match element {
|
||||||
// container elements
|
// 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
|
//! To customize html rendering, simply implementing [`ExportHandler`] trait and passing
|
||||||
//! it to the [`Org::write_html_custom`] function.
|
//! it to the [`Org::write`] function.
|
||||||
//!
|
//!
|
||||||
//! [`HtmlHandler`]: export/trait.HtmlHandler.html
|
//! [`ExportHandler`]: export/trait.ExportHandler.html
|
||||||
//! [`Org::write_html_custom`]: struct.Org.html#method.write_html_custom
|
//! [`Org::write`]: struct.Org.html#method.write
|
||||||
//!
|
//!
|
||||||
//! The following code demonstrates how to add a id for every headline and return
|
//! The following code demonstrates how to add a id for every headline and return
|
||||||
//! own error type while rendering.
|
//! own error type while rendering.
|
||||||
|
@ -90,7 +90,7 @@
|
||||||
//! use std::io::{Error as IOError, Write};
|
//! use std::io::{Error as IOError, Write};
|
||||||
//! use std::string::FromUtf8Error;
|
//! use std::string::FromUtf8Error;
|
||||||
//!
|
//!
|
||||||
//! use orgize::export::{DefaultHtmlHandler, HtmlHandler};
|
//! use orgize::export::{DefaultHtmlHandler, ExportHandler};
|
||||||
//! use orgize::{Element, Org};
|
//! use orgize::{Element, Org};
|
||||||
//! use slugify::slugify;
|
//! use slugify::slugify;
|
||||||
//!
|
//!
|
||||||
|
@ -117,7 +117,7 @@
|
||||||
//! #[derive(Default)]
|
//! #[derive(Default)]
|
||||||
//! struct MyHtmlHandler(DefaultHtmlHandler);
|
//! 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> {
|
//! fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), MyError> {
|
||||||
//! if let Element::Title(title) = element {
|
//! if let Element::Title(title) = element {
|
||||||
//! if title.level > 6 {
|
//! if title.level > 6 {
|
||||||
|
@ -150,7 +150,7 @@
|
||||||
//! fn main() -> Result<(), MyError> {
|
//! fn main() -> Result<(), MyError> {
|
||||||
//! let mut writer = Vec::new();
|
//! let mut writer = Vec::new();
|
||||||
//! let mut handler = MyHtmlHandler::default();
|
//! 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!(
|
//! assert_eq!(
|
||||||
//! String::from_utf8(writer)?,
|
//! String::from_utf8(writer)?,
|
||||||
|
|
43
src/org.rs
43
src/org.rs
|
@ -5,7 +5,7 @@ use std::ops::{Index, IndexMut};
|
||||||
use crate::{
|
use crate::{
|
||||||
config::{ParseConfig, DEFAULT_CONFIG},
|
config::{ParseConfig, DEFAULT_CONFIG},
|
||||||
elements::{Element, Keyword},
|
elements::{Element, Keyword},
|
||||||
export::{DefaultHtmlHandler, DefaultOrgHandler, HtmlHandler, OrgHandler},
|
export::{DefaultHtmlHandler, DefaultOrgHandler, ExportHandler},
|
||||||
parsers::{blank_lines_count, parse_container, Container, OwnedArena},
|
parsers::{blank_lines_count, parse_container, Container, OwnedArena},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -109,20 +109,12 @@ impl<'a> Org<'a> {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Writes an `Org` struct as html format.
|
/// Writes the document using the given {ExportHandler}
|
||||||
pub fn write_html<W>(&self, writer: W) -> Result<(), Error>
|
pub fn write<W, X, E>(&self, mut writer: W, handler: &mut X) -> Result<(), E>
|
||||||
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>
|
|
||||||
where
|
where
|
||||||
W: Write,
|
W: Write,
|
||||||
E: From<Error>,
|
E: From<Error>,
|
||||||
H: HtmlHandler<E>,
|
X: ExportHandler<E>,
|
||||||
{
|
{
|
||||||
for event in self.iter() {
|
for event in self.iter() {
|
||||||
match event {
|
match event {
|
||||||
|
@ -134,29 +126,20 @@ impl<'a> Org<'a> {
|
||||||
Ok(())
|
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.
|
/// Writes an `Org` struct as org format.
|
||||||
pub fn write_org<W>(&self, writer: W) -> Result<(), Error>
|
pub fn write_org<W>(&self, writer: W) -> Result<(), Error>
|
||||||
where
|
where
|
||||||
W: Write,
|
W: Write,
|
||||||
{
|
{
|
||||||
self.write_org_custom(writer, &mut DefaultOrgHandler)
|
self.write(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(())
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue