diff --git a/Cargo.toml b/Cargo.toml index 30ffac8..c096ae4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -6,3 +6,9 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +kor = { path = "../kor" } + +[features] +default = ["transform", "util"] +transform = [] +util = [] diff --git a/src/lib.rs b/src/lib.rs index ea86848..592273a 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1 +1,7 @@ pub mod parse; + +#[cfg(feature = "transform")] +pub mod transform; + +#[cfg(feature = "util")] +pub mod util; diff --git a/src/transform/html.rs b/src/transform/html.rs new file mode 100644 index 0000000..06bb25a --- /dev/null +++ b/src/transform/html.rs @@ -0,0 +1,67 @@ +use core::fmt::{Display, Write}; + +use crate::parse::{Block, ParagraphPiece, Spanned}; + +pub type Result = core::result::Result<(), core::fmt::Error>; + +pub trait HtmlizeExtensions { + type Extension; + + fn write(&self, ext: Self::Extension, writer: &mut W) -> Result + where + W: Write; +} + +fn paragraph_piece( + piece: ParagraphPiece, + writer: &mut W, + hex: &H, +) -> Result +where + S: Display, + W: Write, + H: HtmlizeExtensions, +{ + match piece { + ParagraphPiece::Text(t) => write!(writer, "{}", t.0), + ParagraphPiece::Extension(e) => hex.write(e, writer), + } +} + +fn block(block: Block, writer: &mut W, hex: &H) -> Result +where + S: Display, + W: Write, + H: HtmlizeExtensions, +{ + match block { + Block::Header(h) => { + writeln!(writer, "{}", h.level, h.content, h.level) + } + Block::Paragraph(p) => { + writeln!(writer, "

")?; + for p in p.pieces { + paragraph_piece(p.value, writer, hex)?; + } + + writeln!(writer, "

") + } + } +} + +pub fn write( + blocks: Vec>>, + writer: &mut W, + hex: &H, +) -> Result +where + S: Display, + W: Write, + H: HtmlizeExtensions, +{ + for b in blocks { + block(b.value, writer, hex)?; + } + + Ok(()) +} diff --git a/src/transform/mod.rs b/src/transform/mod.rs new file mode 100644 index 0000000..3d4613d --- /dev/null +++ b/src/transform/mod.rs @@ -0,0 +1 @@ +pub mod html;