treehouse/src/html.rs

41 lines
986 B
Rust
Raw Normal View History

2023-08-20 12:15:48 +02:00
use std::fmt::{self, Display, Write};
2023-08-28 21:14:51 +02:00
pub mod breadcrumbs;
2024-11-27 19:02:30 +01:00
pub mod djot;
2024-03-10 23:23:50 +01:00
pub mod highlight;
pub mod navmap;
2023-08-18 17:04:12 +02:00
pub mod tree;
2023-08-20 12:15:48 +02:00
pub struct EscapeAttribute<'a>(pub &'a str);
2023-08-20 12:15:48 +02:00
impl Display for EscapeAttribute<'_> {
2023-08-20 12:15:48 +02:00
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(())
}
}
2023-08-20 15:05:59 +02:00
pub struct EscapeHtml<'a>(pub &'a str);
2023-08-20 15:05:59 +02:00
impl Display for EscapeHtml<'_> {
2023-08-20 15:05:59 +02:00
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(())
}
}