-
-
Save katafrakt/0102b410805f7bb7202835ab997e2392 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
use orgize::export::{DefaultHtmlHandler, HtmlHandler}; | |
use orgize::{Element, Headline, Org}; | |
use std::convert::From; | |
use std::io::{Error as IOError, Write}; | |
use std::string::FromUtf8Error; | |
#[derive(Debug)] | |
pub enum MyError { | |
IO(IOError), | |
Heading, | |
Utf8(FromUtf8Error), | |
} | |
// From<std::io::Error> trait is required for custom error type | |
impl From<IOError> for MyError { | |
fn from(err: IOError) -> Self { | |
MyError::IO(err) | |
} | |
} | |
impl From<FromUtf8Error> for MyError { | |
fn from(err: FromUtf8Error) -> Self { | |
MyError::Utf8(err) | |
} | |
} | |
#[derive(Default)] | |
pub struct MyHtmlHandler(DefaultHtmlHandler); | |
impl HtmlHandler<MyError> for MyHtmlHandler { | |
fn start<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), MyError> { | |
match element { | |
Element::Title(title) => { | |
if title.level == 1 { | |
// do nothing | |
write!(w, "")?; | |
} else { | |
self.0.start(w, element)?; | |
} | |
} | |
Element::FnRef(footnote) => { | |
let uuid = uuid::Uuid::new_v4(); | |
match &footnote.definition { | |
Some(definition) => { | |
write!( | |
w, | |
"<label for=\"{0}\" class=\"sidenote-toggle sidenote-number\"></label><input type=\"checkbox\" id=\"{0}\" class=\"sidenote-toggle\" /><span class=\"sidenote\">{1}</span>", | |
uuid, | |
definition | |
)?; | |
} | |
_ => { | |
write!(w, "")?; | |
} | |
} | |
} | |
Element::InlineSrc(src) => { | |
write!(w, "<code class=\"highlighter-rouge\">{0}</code>", src.body)?; | |
} | |
_ => { | |
self.0.start(w, element)?; | |
} | |
} | |
Ok(()) | |
} | |
fn end<W: Write>(&mut self, mut w: W, element: &Element) -> Result<(), MyError> { | |
match element { | |
_ => { | |
self.0.end(w, element)?; | |
} | |
} | |
Ok(()) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment