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
pub use self::digit::Digit;
pub use self::html::HTML;
mod digit {
pub trait Digit<T> {
fn digit(self, d: T) -> String;
}
impl Digit<i32> for i32 {
fn digit(self, d: i32) -> String {
let mut s = self.to_string();
let space = " ".repeat((d as usize) - s.len());
s.push_str(&space);
s
}
}
impl Digit<i32> for String {
fn digit(self, d: i32) -> String {
let mut s = self.clone();
let space = " ".repeat((d as usize) - self.len());
s.push_str(&space);
s
}
}
impl Digit<i32> for &'static str {
fn digit(self, d: i32) -> String {
let mut s = self.to_string();
let space = " ".repeat((d as usize) - self.len());
s.push_str(&space);
s
}
}
}
mod html {
use colored::Colorize;
pub enum Token {
Plain(String),
Bold(String),
Eof(String)
}
pub trait HTML {
fn ser(&self) -> Vec<Token>;
fn render(&self) -> String;
}
impl HTML for String {
fn ser(&self) -> Vec<Token> {
let mut tks = self.to_string();
tks = tks.replace(r#"<"#, "<");
tks = tks.replace(r#">"#, ">");
tks = tks.replace(r#"&"#, "&");
tks = tks.replace(r#"""#, "\"");
tks = tks.replace(r#" "#, " ");
let res: Vec<Token>;
{
let mut ptr = 0;
let mut output = vec![];
let mut bold = false;
for (i, e) in tks.chars().enumerate() {
match e {
'<' => {
match bold {
true => {
output.push(Token::Bold(tks[ptr..i].to_string()));
bold = false;
}
false => output.push(
Token::Plain(tks[ptr..i].to_string())
),
}
ptr = i;
},
'>' => {
match &tks[i-1..i] {
"-" => continue,
_ => match &tks[(ptr + 1)..i] {
"b" | "strong" => bold = true,
_ => {},
},
}
ptr = i + 1;
},
_ => {},
}
};
output.push(Token::Eof(tks[ptr..tks.len()].to_string()));
res = output;
}
res
}
fn render(&self) -> String {
let ts = self.ser();
let mut tks: Vec<String> = vec![];
for i in ts {
match i {
Token::Plain(s) => tks.push(s.normal().to_string()),
Token::Bold(s) => {
if s.contains("Example") {
let mut br = "-".repeat(50).dimmed().to_string();
br.push_str("\n\n");
tks.push(br);
} else if s.contains("Note") {
let mut br = "* ".repeat(25).dimmed().to_string();
br.push_str("\n\n");
tks.push(br);
}
tks.push(s.bold().to_string());
}
Token::Eof(s) => tks.push(s.normal().to_string()),
}
}
tks.join("")
}
}
}