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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
//! Convert a string that can contain
//! [ANSI escape codes](https://en.wikipedia.org/wiki/ANSI_escape_code) to HTML.
//!
//! This crate currently supports SGR parameters (text style and colors).
//! The supported styles are:
//!
//! - bold
//! - italic
//! - underlined
//! - crossed out
//! - faint
//! - foreground and background colors: 3-bit, 4-bit, 8-bit, truecolor (24-bit)
//!
//! **Not** supported SGR parameters (note that most of these are niche features
//! and rarely supported by terminals):
//!
//! - slow/rapid blink
//! - reverse video
//! - conceal
//! - alternative fonts
//! - fraktur
//! - doubly underlined
//! - proportional spacing
//! - framed
//! - encircled
//! - overlined
//! - underline color (not in standard)
//! - ideogram attributes
//! - superscript, subscript (not in standard)
//! - bright foreground/background color (not in standard)
//!
//! All unsupported ANSI escape codes are stripped from the output.
//!
//! It should be easy to add support for more styles, if there's a straightforward HTML
//! representation. If you need a different style (e.g. doubly underlined), file an issue.
//!
//!
//! ## Example
//! ```
//! // \x1b[1m : bold   \x1b[31m : red
//! let input = "<h1> \x1b[1m Hello \x1b[31m world! </h1>";
//! let converted = ansi_to_html::convert_escaped(input).unwrap();
//! assert_eq!(
//!     converted.as_str(),
//!     "&lt;h1&gt; <b> Hello <span style='color:#a00'> world! &lt;/h1&gt;</span></b>"
//! );
//! ```
//!
//! ## Features
//!
//! Enable the `lazy-init` feature to initialize a few things lazily, which is faster if you're
//! converting many strings.
#![deny(unsafe_code)]

mod ansi;
mod color;
mod error;
mod esc;
mod html;

use ansi::{Ansi, AnsiIter};
use color::Color;

pub use error::Error;
pub use esc::Esc;

#[cfg(feature = "once_cell")]
use once_cell::sync::Lazy;
use regex::Regex;

/// Converts a string containing ANSI escape codes to HTML.
/// Special html characters (`<>&'"`) are escaped prior to the conversion.
///
/// This function attempts to minimize the number of generated HTML tags.
///
/// ## Example
///
/// ```
/// // \x1b[1m : bold   \x1b[31m : red
/// let input = "<h1> \x1b[1m Hello \x1b[31m world! </h1>";
/// let converted = ansi_to_html::convert_escaped(input).unwrap();
///
/// assert_eq!(
///     converted.as_str(),
///     "&lt;h1&gt; <b> Hello <span style='color:#a00'> world! &lt;/h1&gt;</span></b>"
/// );
/// ```
pub fn convert_escaped(ansi_string: &str) -> Result<String, Error> {
    let input = Esc(ansi_string).to_string();
    let html = html::ansi_to_html(&input, &ansi_regex())?;
    Ok(optimize(&html))
}

/// Converts a string containing ANSI escape codes to HTML.
///
/// If `escaped` is `true`, then special html characters (`<>&'"`) are escaped prior
/// to the conversion.
///
/// If `optimized` is `true`, this function attempts to minimize the number of
/// generated HTML tags. Set it to `false` if you want optimal performance.
///
/// ## Example
///
/// ```
/// // \x1b[1m : bold   \x1b[31m : red   \x1b[22m : bold off
/// let input = "\x1b[1m Hello \x1b[31m world \x1b[22m!";
/// let converted = ansi_to_html::convert_escaped(input).unwrap();
///
/// assert_eq!(
///     converted.as_str(),
///     "<b> Hello <span style='color:#a00'> world </span></b><span style='color:#a00'>!</span>"
/// );
/// ```
pub fn convert(input: &str, escaped: bool, optimized: bool) -> Result<String, Error> {
    let html = if escaped {
        let input = Esc(input).to_string();
        html::ansi_to_html(&input, &ansi_regex())?
    } else {
        html::ansi_to_html(input, &ansi_regex())?
    };

    let html = if optimized { optimize(&html) } else { html };
    Ok(html)
}

const ANSI_REGEX: &str = "\x1b(\\[[0-9;?]*[A-HJKSTfhilmnsu]|\\(B)";
const OPT_REGEX_1: &str = r"<span \w+='[^']*'></span>|<b></b>|<i></i>|<u></u>|<s></s>";
const OPT_REGEX_2: &str = "</b><b>|</i><i>|</u><u>|</s><s>";

#[cfg(not(feature = "once_cell"))]
fn ansi_regex() -> Regex {
    Regex::new(ANSI_REGEX).unwrap()
}

#[cfg(feature = "once_cell")]
fn ansi_regex() -> &'static Regex {
    static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(ANSI_REGEX).unwrap());
    &*REGEX
}

#[cfg(not(feature = "once_cell"))]
fn optimize(html: &str) -> String {
    let html = Regex::new(OPT_REGEX_1).unwrap().replace_all(html, "");
    let html = Regex::new(OPT_REGEX_2).unwrap().replace_all(&html, "");

    html.to_string()
}

#[cfg(feature = "once_cell")]
fn optimize(html: &str) -> String {
    static REGEXES: Lazy<(Regex, Regex)> = Lazy::new(|| {
        (
            Regex::new(OPT_REGEX_1).unwrap(),
            Regex::new(OPT_REGEX_2).unwrap(),
        )
    });
    let (regex1, regex2) = &*REGEXES;

    let html = regex1.replace_all(html, "");
    let html = regex2.replace_all(&html, "");

    html.to_string()
}