Add HTML transforming code

main
idylls 2 years ago
parent 18dc2f62b1
commit 7ecd5b8ee5
Signed by: idylls
GPG Key ID: 8A7167CBC2CC9F0F

@ -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 = []

@ -1 +1,7 @@
pub mod parse;
#[cfg(feature = "transform")]
pub mod transform;
#[cfg(feature = "util")]
pub mod util;

@ -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<W>(&self, ext: Self::Extension, writer: &mut W) -> Result
where
W: Write;
}
fn paragraph_piece<S, E, W, H>(
piece: ParagraphPiece<S, E>,
writer: &mut W,
hex: &H,
) -> Result
where
S: Display,
W: Write,
H: HtmlizeExtensions<Extension = E>,
{
match piece {
ParagraphPiece::Text(t) => write!(writer, "{}", t.0),
ParagraphPiece::Extension(e) => hex.write(e, writer),
}
}
fn block<S, E, W, H>(block: Block<S, E>, writer: &mut W, hex: &H) -> Result
where
S: Display,
W: Write,
H: HtmlizeExtensions<Extension = E>,
{
match block {
Block::Header(h) => {
writeln!(writer, "<h{}>{}</h{}>", h.level, h.content, h.level)
}
Block::Paragraph(p) => {
writeln!(writer, "<p>")?;
for p in p.pieces {
paragraph_piece(p.value, writer, hex)?;
}
writeln!(writer, "</p>")
}
}
}
pub fn write<S, E, W, H>(
blocks: Vec<Spanned<Block<S, E>>>,
writer: &mut W,
hex: &H,
) -> Result
where
S: Display,
W: Write,
H: HtmlizeExtensions<Extension = E>,
{
for b in blocks {
block(b.value, writer, hex)?;
}
Ok(())
}

@ -0,0 +1 @@
pub mod html;
Loading…
Cancel
Save