Skip to content

Instantly share code, notes, and snippets.

@katafrakt
Created September 14, 2024 09:44
Show Gist options
  • Save katafrakt/0102b410805f7bb7202835ab997e2392 to your computer and use it in GitHub Desktop.
Save katafrakt/0102b410805f7bb7202835ab997e2392 to your computer and use it in GitHub Desktop.
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