|
| 1 | +use std::{ |
| 2 | + cell::RefCell, |
| 3 | + fmt::{self, Write}, |
| 4 | +}; |
| 5 | + |
| 6 | +#[derive(Debug)] |
| 7 | +pub(crate) struct HtmlWriter { |
| 8 | + inner: RefCell<String>, |
| 9 | +} |
| 10 | + |
| 11 | +impl fmt::Write for HtmlWriter { |
| 12 | + #[inline] |
| 13 | + fn write_fmt(&mut self, args: fmt::Arguments<'_>) -> fmt::Result { |
| 14 | + self.inner.get_mut().write_fmt(args) |
| 15 | + } |
| 16 | + |
| 17 | + #[inline] |
| 18 | + fn write_char(&mut self, c: char) -> fmt::Result { |
| 19 | + self.inner.get_mut().write_char(c) |
| 20 | + } |
| 21 | + |
| 22 | + #[inline] |
| 23 | + fn write_str(&mut self, s: &str) -> fmt::Result { |
| 24 | + self.inner.get_mut().write_str(s) |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +impl From<HtmlWriter> for String { |
| 29 | + #[inline] |
| 30 | + fn from(html: HtmlWriter) -> Self { |
| 31 | + html.into_inner() |
| 32 | + } |
| 33 | +} |
| 34 | + |
| 35 | +impl HtmlWriter { |
| 36 | + pub fn new() -> Self { |
| 37 | + Self { inner: RefCell::new(String::new()) } |
| 38 | + } |
| 39 | + |
| 40 | + pub fn with_capacity(capacity: usize) -> Self { |
| 41 | + Self { inner: RefCell::new(String::with_capacity(capacity)) } |
| 42 | + } |
| 43 | + |
| 44 | + pub fn writeln<S: AsRef<str>>(&self, line: S) -> fmt::Result { |
| 45 | + writeln!(self.inner.borrow_mut(), "{}", line.as_ref()) |
| 46 | + } |
| 47 | + |
| 48 | + pub fn into_inner(self) -> String { |
| 49 | + self.inner.into_inner() |
| 50 | + } |
| 51 | + |
| 52 | + pub fn html<F>(&self, tag: &'static str, attrs: &str, inner: F) -> fmt::Result |
| 53 | + where |
| 54 | + F: FnOnce(&Self) -> fmt::Result, |
| 55 | + { |
| 56 | + // Allocate space for the HTML being printed |
| 57 | + let write_amt_guess = { |
| 58 | + // opening tag. 2 extra for '<' and '>' |
| 59 | + 2 + tag.len() + attrs.len() + |
| 60 | + // approximate inner content length |
| 61 | + 256 + |
| 62 | + // closing tag. 3 extra for '</' and '>' |
| 63 | + 3 + tag.len() |
| 64 | + }; |
| 65 | + let mut s = self.inner.borrow_mut(); |
| 66 | + s.reserve(write_amt_guess); |
| 67 | + |
| 68 | + // Write the opening tag |
| 69 | + write!(s, "<{tag}")?; |
| 70 | + if attrs.is_empty() { |
| 71 | + writeln!(s, ">")?; |
| 72 | + } else { |
| 73 | + writeln!(s, " {attrs}>")?; |
| 74 | + } |
| 75 | + |
| 76 | + // Callback produces the inner content |
| 77 | + drop(s); |
| 78 | + inner(self)?; |
| 79 | + |
| 80 | + // Write the closing tag |
| 81 | + writeln!(self.inner.borrow_mut(), "</{tag}>")?; |
| 82 | + |
| 83 | + Ok(()) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +macro_rules! make_tag { |
| 88 | + ($name:ident) => { |
| 89 | + impl HtmlWriter { |
| 90 | + #[inline] |
| 91 | + pub fn $name<F>(&self, attrs: &str, inner: F) -> fmt::Result |
| 92 | + where |
| 93 | + F: FnOnce(&Self) -> fmt::Result, |
| 94 | + { |
| 95 | + self.html(stringify!($name), attrs, inner) |
| 96 | + } |
| 97 | + } |
| 98 | + }; |
| 99 | +} |
| 100 | + |
| 101 | +make_tag!(div); |
| 102 | +make_tag!(span); |
| 103 | + |
| 104 | +#[cfg(test)] |
| 105 | +mod test { |
| 106 | + use super::*; |
| 107 | + |
| 108 | + #[test] |
| 109 | + fn test_div() { |
| 110 | + let html = HtmlWriter::new(); |
| 111 | + html.div("", |html| html.writeln("Hello, world!")).unwrap(); |
| 112 | + |
| 113 | + assert_eq!( |
| 114 | + html.into_inner().as_str(), |
| 115 | + "<div> |
| 116 | +Hello, world! |
| 117 | +</div> |
| 118 | +" |
| 119 | + ); |
| 120 | + } |
| 121 | +} |
0 commit comments