You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
68 lines
1.2 KiB
Rust
68 lines
1.2 KiB
Rust
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(())
|
|
}
|