-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathmod.rs
158 lines (139 loc) · 4.82 KB
/
mod.rs
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
use regex::Regex;
use crate::{Ansi, AnsiIter, Color, Error};
mod minifier;
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
enum Style {
Bold,
Faint,
Italic,
Underline,
CrossedOut,
ForegroundColor(Color),
BackgroundColor(Color),
}
impl Style {
fn apply(&self, buf: &mut String) {
let s;
buf.push_str(match self {
Style::Bold => "<b>",
Style::Faint => "<span style='opacity:0.67'>",
Style::Italic => "<i>",
Style::Underline => "<u>",
Style::CrossedOut => "<s>",
Style::ForegroundColor(c) => {
s = format!("<span style='color:{}'>", c);
&s
}
Style::BackgroundColor(c) => {
s = format!("<span style='background:{}'>", c);
&s
}
});
}
fn clear(&self, buf: &mut String) {
buf.push_str(match self {
Style::Bold => "</b>",
Style::Faint => "</span>",
Style::Italic => "</i>",
Style::Underline => "</u>",
Style::CrossedOut => "</s>",
Style::ForegroundColor(_) => "</span>",
Style::BackgroundColor(_) => "</span>",
})
}
}
/// Convert ANSI sequences to html. This does NOT escape html characters such as `<` and `&`.
pub fn ansi_to_html(mut input: &str, ansi_regex: &Regex) -> Result<String, Error> {
let mut minifier = minifier::Minifier::default();
loop {
match ansi_regex.find(input) {
Some(m) => {
if m.start() > 0 {
let (before, after) = input.split_at(m.start());
minifier.push_str(before);
input = after;
}
let len = m.range().len();
input = &input[len..];
if !m.as_str().ends_with('m') {
continue;
}
if len == 3 {
minifier.clear_styles();
continue;
}
let nums = &m.as_str()[2..len - 1];
let nums = nums.split(';').map(|n| n.parse::<u8>());
for ansi in AnsiIter::new(nums) {
minifier.push_ansi_code(ansi?);
}
}
None => {
minifier.push_str(input);
break;
}
}
}
minifier.push_ansi_code(Ansi::Reset); // make sure all tags are closed
Ok(minifier.into_html())
}
#[derive(Debug, Default)]
struct AnsiConverter {
styles: Vec<Style>,
styles_to_apply: Vec<Style>,
result: String,
}
impl AnsiConverter {
fn consume_ansi_code(&mut self, ansi: Ansi) {
match ansi {
Ansi::Noop => {}
Ansi::Reset => self.clear_style(|_| true),
Ansi::Bold => self.set_style(Style::Bold),
Ansi::Faint => self.set_style(Style::Faint),
Ansi::Italic => self.set_style(Style::Italic),
Ansi::Underline => self.set_style(Style::Underline),
Ansi::CrossedOut => self.set_style(Style::CrossedOut),
Ansi::BoldOff => self.clear_style(|&s| s == Style::Bold),
Ansi::BoldAndFaintOff => self.clear_style(|&s| s == Style::Bold || s == Style::Faint),
Ansi::ItalicOff => self.clear_style(|&s| s == Style::Italic),
Ansi::UnderlineOff => self.clear_style(|&s| s == Style::Underline),
Ansi::CrossedOutOff => self.clear_style(|&s| s == Style::CrossedOut),
Ansi::ForgroundColor(c) => self.set_style(Style::ForegroundColor(c)),
Ansi::DefaultForegroundColor => {
self.clear_style(|&s| matches!(s, Style::ForegroundColor(_)))
}
Ansi::BackgroundColor(c) => self.set_style(Style::BackgroundColor(c)),
Ansi::DefaultBackgroundColor => {
self.clear_style(|&s| matches!(s, Style::BackgroundColor(_)))
}
}
}
fn set_style(&mut self, s: Style) {
if !self.styles.contains(&s) {
s.apply(&mut self.result);
self.styles.push(s);
}
}
fn clear_style(&mut self, cond: impl Fn(&Style) -> bool) {
if let Some((i, _)) = self.styles.iter().enumerate().find(|&(_, s)| cond(s)) {
while self.styles.len() > i {
let style = self.styles.pop().unwrap();
style.clear(&mut self.result);
if !cond(&style) {
self.styles_to_apply.push(style);
}
}
}
for &style in &self.styles_to_apply {
style.apply(&mut self.result);
self.styles.push(style);
}
self.styles_to_apply.clear();
}
fn push_str(&mut self, s: &str) {
self.result.push_str(s);
}
fn result(self) -> String {
self.result
}
}