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
use ansi2::{theme::VsCodeTheme, Canvas};

pub fn to_html(s: &str) -> String {
    let canvas = Canvas::new(s);
    let mut s = String::new();
    s.push_str("<div class='ansi-main'>\n");
    for row in canvas.pixels.iter() {
        s.push_str("<div class='row'>");
        for c in row.iter() {
            let bold: &str = if c.bold { "bold" } else { "normal" };
            let fn_w = format!("font-weight: {bold};");
            let mut class = String::from("char");
            if c.bold {
                class.push_str(" char-bold")
            }

            s.push_str(&format!(
                "<div class='{class}' style='color: {};background: {}; {fn_w}; '>{}</div>",
                c.color.to_rgb(VsCodeTheme),
                c.bg_color.to_rgb(VsCodeTheme),
                html_escape::encode_text(&c.char.to_string())
            ))
        }
        s.push_str("</div>");
    }
    s.push_str("</div>\n");
    format!(
        r#"<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
.ansi-main{{
  display: flex;
  flex-direction: column;
}}

.row{{
  display: flex;
}}

.char{{
  margin: 0;
  padding: 0;
  font-family: Courier, monospace;
  white-space: pre;
}}

.char-bold{{
 font-weight: bold;
}}

  </style>
</head>
<body>
{s}
</body>
</html>
"#
    )
}