feat(export): update html render
This commit is contained in:
parent
33f78ee207
commit
0a876e2f2b
10 changed files with 213 additions and 358 deletions
|
|
@ -1,72 +1,72 @@
|
|||
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::fs;
|
||||
use std::io::{Error as IOError, Write};
|
||||
use std::result::Result;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
struct CustomHtmlHandler;
|
||||
use orgize::export::*;
|
||||
use orgize::{Container, Org};
|
||||
use slugify::slugify;
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Error {
|
||||
enum MyError {
|
||||
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)
|
||||
// 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 Error {
|
||||
fn from(err: FromUtf8Error) -> Error {
|
||||
Error::Utf8(err)
|
||||
impl From<FromUtf8Error> for MyError {
|
||||
fn from(err: FromUtf8Error) -> Self {
|
||||
MyError::Utf8(err)
|
||||
}
|
||||
}
|
||||
|
||||
type Result = std::result::Result<(), Error>;
|
||||
struct CustomHtmlHandler;
|
||||
|
||||
impl<W: Write> HtmlHandler<W, Error> for CustomHtmlHandler {
|
||||
fn 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,
|
||||
)?)
|
||||
impl HtmlHandler<MyError> for CustomHtmlHandler {
|
||||
fn start<W: Write>(&mut self, mut w: W, container: Container<'_>) -> Result<(), MyError> {
|
||||
let mut default_handler = DefaultHtmlHandler;
|
||||
match container {
|
||||
Container::Headline(hdl) => {
|
||||
if hdl.level > 6 {
|
||||
return Err(MyError::Heading);
|
||||
} else {
|
||||
let slugify = slugify!(hdl.title);
|
||||
write!(
|
||||
w,
|
||||
"<h{0}><a id=\"{1}\" href=\"#{1}\">{2}</a></h{0}>",
|
||||
hdl.level, slugify, hdl.title,
|
||||
)?;
|
||||
}
|
||||
}
|
||||
_ => default_handler.start(w, container)?,
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result {
|
||||
fn main() -> Result<(), MyError> {
|
||||
let args: Vec<_> = args().collect();
|
||||
|
||||
if args.len() < 2 {
|
||||
println!("Usage: {} <org-file>", args[0]);
|
||||
eprintln!("Usage: {} <org-file>", args[0]);
|
||||
} else {
|
||||
let mut file = File::open(&args[1])?;
|
||||
let contents = String::from_utf8(fs::read(&args[1])?)?;
|
||||
let mut org = Org::new(&contents);
|
||||
let mut writer = Vec::new();
|
||||
|
||||
let mut contents = String::new();
|
||||
file.read_to_string(&mut contents)?;
|
||||
org.parse();
|
||||
org.html(&mut writer, CustomHtmlHandler)?;
|
||||
|
||||
let mut cursor = Cursor::new(Vec::new());
|
||||
|
||||
//let mut render = DefaultHtmlRender::new(cursor, &contents);
|
||||
// comment the following line and uncomment the line above to use the default handler
|
||||
let mut render = HtmlRender::new(CustomHtmlHandler, &mut cursor, &contents);
|
||||
|
||||
render.render()?;
|
||||
|
||||
println!("{}", String::from_utf8(cursor.into_inner())?);
|
||||
println!("{}", String::from_utf8(writer)?);
|
||||
}
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue