treehouse/src/html.rs

40 lines
986 B
Rust

use std::fmt::{self, Display, Write};
pub mod breadcrumbs;
pub mod djot;
pub mod highlight;
pub mod navmap;
pub mod tree;
pub struct EscapeAttribute<'a>(pub &'a str);
impl Display for EscapeAttribute<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for c in self.0.chars() {
if c == '"' {
f.write_str("&quot;")?;
} else {
f.write_char(c)?;
}
}
Ok(())
}
}
pub struct EscapeHtml<'a>(pub &'a str);
impl Display for EscapeHtml<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for c in self.0.chars() {
match c {
'<' => f.write_str("&lt;")?,
'>' => f.write_str("&gt;")?,
'&' => f.write_str("&amp;")?,
'\'' => f.write_str("&apos;")?,
'"' => f.write_str("&quot;")?,
_ => f.write_char(c)?,
}
}
Ok(())
}
}