Struct Converter

Source
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

Source

pub fn new() -> Self

Creates a new set of default options.

Source

pub fn skip_escape(self, skip: bool) -> Self

Avoids escaping special HTML characters prior to conversion.

Source

pub fn skip_optimize(self, skip: bool) -> Self

Skips removing some useless HTML tags.

Source

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.

Source

pub fn convert(&self, input: &str) -> Result<String, Error>

Converts a string containing ANSI escape codes to HTML.

Trait Implementations§

Source§

impl Clone for Converter

Source§

fn clone(&self) -> Converter

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Converter

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Converter

Source§

fn default() -> Converter

Returns the “default value” for a type. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.