1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
use alloc::string::String;
use stylish_core::Arguments;
use crate::Html;
#[cfg(feature = "macros")]
/// Create a [`String`] with inline ANSI escape codes styling its formatted
/// data.
///
/// ```rust
/// assert_eq!(
/// stylish::html::format!("Hello {:(fg=red)}", "Ferris"),
/// "Hello <span style=color:red>Ferris</span>",
/// );
/// ```
#[macro_export]
macro_rules! format {
($($arg:tt)*) => {{
let res = $crate::format($crate::𓀄::format_args!($($arg)*));
res
}}
}
/// Render the given attributed [`Arguments`] into a [`String`] by converting
/// the attributes into HTML elements.
///
/// ```rust
/// assert_eq!(
/// stylish::html::format(stylish::format_args!(
/// "Hello {:(fg=red)}",
/// "Ferris"
/// )),
/// "Hello <span style=color:red>Ferris</span>",
/// );
/// ```
pub fn format(args: Arguments<'_>) -> String {
let mut html = Html::new(String::new());
html.write_fmt(args)
.expect("a formatting trait implementation returned an error");
html.finish().expect("String cannot fail")
}