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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#![allow(dead_code)]
static DONT_BOLD: [char; 3] = [' ', '\'', '.'];
fn should_bold(c: char) -> bool {
for check in DONT_BOLD.iter() {
if c == *check {
return false;
}
}
true
}
#[derive(Debug, PartialEq, Eq, Clone)]
pub enum Marking {
ANSIBold,
MarkdownBold,
BBCodeBold,
HTMLBold,
Custom(bool, String, String)
}
impl Marking {
pub fn custom(dedupe: bool, prefix: String, suffix: String) -> Self {
Marking::Custom(dedupe, prefix, suffix)
}
fn vars(self) -> (bool, String, String) {
use self::Marking::*;
match self {
ANSIBold => (false, "\x1B[1m".to_string(), "\x1B[0m".to_string()),
MarkdownBold => (true, "**".to_string(), "**".to_string()),
BBCodeBold => (true, "[b]".to_string(), "[/b]".to_string()),
HTMLBold => (true, "<b>".to_string(), "</b>".to_string()),
Custom(a, b, c) => (a, b, c)
}
}
}
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum Pattern {
Left,
Right,
Cross
}
impl Pattern {
fn vars(self) -> (bool, bool) {
use self::Pattern::*;
match self {
Left => (true, false),
Right => (false, true),
Cross => (true, true)
}
}
}
pub fn boldline(input: String, marking: Marking, pattern: Pattern) -> Vec<String> {
let (leftwise, rightwise) = pattern.vars();
let both = leftwise && rightwise;
let (dedupe, prefix, suffix) = marking.vars();
let mut output: Vec<String> = vec![];
let mut was_next_to_last_iter = false;
for left in 0..input.len() {
let right = input.len()-left-1;
if (leftwise && !should_bold(input[left..left+1].as_bytes()[0] as char)) ||
(rightwise && !should_bold(input[right..right+1].as_bytes()[0] as char)) {
continue;
}
let mut next_to_eachother = false;
if left+1 == right || right+1 == left {
next_to_eachother = true;
} else {
was_next_to_last_iter = false;
}
if next_to_eachother && was_next_to_last_iter && pattern == Pattern::Cross {
continue;
} else if next_to_eachother {
was_next_to_last_iter = true;
}
let mut line = String::new();
for j in 0..input.len() {
let c = &input[j..j+1];
let leftchar = leftwise && left == j;
let rightchar = rightwise && right == j;
if leftchar || rightchar {
if both && next_to_eachother && dedupe {
if (left < right && rightchar) ||
(left > right && leftchar) {
line.push_str(c);
line.push_str(suffix.as_str());
} else {
line.push_str(prefix.as_str());
line.push_str(c);
}
} else {
line.push_str(prefix.as_str());
line.push_str(c);
line.push_str(suffix.as_str());
}
} else {
line.push_str(c);
}
}
output.push(line);
}
output
}