pub struct Converter { /* private fields */ }
Expand description
A builder for converting a string containing ANSI escape codes to HTML.
By default this will:
- Escape special HTML characters (
<>&'"
) prior to conversion. - Apply optimizations to minimize the number of generated HTML tags.
- Use hardcoded colors.
§Example
This skips HTML escaping and optimization, and sets a prefix for the CSS variables to customize 4-bit colors.
use ansi_to_html::Converter;
let converter = Converter::new()
.skip_escape(true)
.skip_optimize(true)
.four_bit_var_prefix(Some("custom-".to_owned()));
let bold = "\x1b[1m";
let red = "\x1b[31m";
let reset = "\x1b[0m";
let input = format!("<h1> <i></i> {bold}Hello {red}world!{reset} </h1>");
let converted = converter.convert(&input).unwrap();
assert_eq!(
converted,
// The `<h1>` and `</h1>` aren't escaped, useless `<i></i>` is kept, and
// `<span class='red'>` is used instead of `<span style='color:#a00'>`
"<h1> <i></i> <b>Hello <span style='color:var(--custom-red,#a00)'>world!</span></b> </h1>",
);
Implementations§
Source§impl Converter
impl Converter
Sourcepub fn skip_escape(self, skip: bool) -> Self
pub fn skip_escape(self, skip: bool) -> Self
Avoids escaping special HTML characters prior to conversion.
Sourcepub fn skip_optimize(self, skip: bool) -> Self
pub fn skip_optimize(self, skip: bool) -> Self
Skips removing some useless HTML tags.
Sourcepub fn four_bit_var_prefix(self, prefix: Option<String>) -> Self
pub fn four_bit_var_prefix(self, prefix: Option<String>) -> Self
Adds a custom prefix for the CSS variables used for all the 4-bit colors.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Converter
impl RefUnwindSafe for Converter
impl Send for Converter
impl Sync for Converter
impl Unpin for Converter
impl UnwindSafe for Converter
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more