Add HTML transforming code
parent
18dc2f62b1
commit
7ecd5b8ee5
@ -1 +1,7 @@
|
|||||||
pub mod parse;
|
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…
Reference in New Issue