| 1 |
package reply |
| 2 |
|
| 3 |
import ( |
| 4 |
"io" |
| 5 |
"io/ioutil" |
| 6 |
"strings" |
| 7 |
"time" |
| 8 |
|
| 9 |
"github.com/dlclark/regexp2" |
| 10 |
) |
| 11 |
|
| 12 |
func init() { |
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
regexp2.DefaultMatchTimeout = 1 * time.Second |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
func FromReader(reader io.Reader) (string, error) { |
| 21 |
bytes, err := ioutil.ReadAll(reader) |
| 22 |
if err != nil { |
| 23 |
return "", err |
| 24 |
} |
| 25 |
|
| 26 |
return FromText(string(bytes)), nil |
| 27 |
} |
| 28 |
|
| 29 |
|
| 30 |
func FromText(text string) string { |
| 31 |
if strings.TrimSpace(text) == "" { |
| 32 |
return text |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
text = cleanup(text) |
| 37 |
|
| 38 |
|
| 39 |
lines := strings.Split(text, "\n") |
| 40 |
patternBuilder := strings.Builder{} |
| 41 |
|
| 42 |
for _, line := range lines { |
| 43 |
patternBuilder.WriteString(classifyLine(line)) |
| 44 |
} |
| 45 |
|
| 46 |
pattern := patternBuilder.String() |
| 47 |
|
| 48 |
|
| 49 |
{ |
| 50 |
match, err := regexp2.MustCompile(`d`, regexp2.RE2).FindStringMatch(pattern) |
| 51 |
if err != nil { |
| 52 |
return "" |
| 53 |
} |
| 54 |
if match != nil { |
| 55 |
index := match.Index |
| 56 |
pattern = sliceString(pattern, 0, index-1) |
| 57 |
lines = sliceArray(lines, 0, index-1) |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
|
| 62 |
for { |
| 63 |
match, err := regexp2.MustCompile(`s`, regexp2.RE2).FindStringMatch(pattern) |
| 64 |
if err != nil { |
| 65 |
return "" |
| 66 |
} |
| 67 |
if match != nil { |
| 68 |
index := match.Index |
| 69 |
pattern = stringSliceBang(pattern, index) |
| 70 |
lines = sliceSliceBang(lines, index) |
| 71 |
} else { |
| 72 |
break |
| 73 |
} |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
{ |
| 78 |
match, err := regexp2.MustCompile(`^(b[^t]+)*b[bqeh]+t[et]*$`, regexp2.RE2).FindStringMatch(pattern) |
| 79 |
if err != nil { |
| 80 |
return "" |
| 81 |
} |
| 82 |
if match != nil { |
| 83 |
submatch, err := regexp2.MustCompile(`t[et]*$`, regexp2.RE2).FindStringMatch(pattern) |
| 84 |
if err != nil { |
| 85 |
return "" |
| 86 |
} |
| 87 |
|
| 88 |
index := submatch.Index |
| 89 |
pattern = "" |
| 90 |
lines = sliceArray(lines, index, len(lines)-1) |
| 91 |
} |
| 92 |
} |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
{ |
| 97 |
match, err := regexp2.MustCompile(`te*b[^q]*$`, regexp2.RE2).FindStringMatch(pattern) |
| 98 |
if err != nil { |
| 99 |
return "" |
| 100 |
} |
| 101 |
if match != nil { |
| 102 |
index := match.Index |
| 103 |
pattern = sliceString(pattern, 0, index) |
| 104 |
lines = sliceArray(lines, 0, index) |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
{ |
| 111 |
match, err := regexp2.MustCompile(`te*b[eqbh]*([te]*)$`, regexp2.RE2).FindStringMatch(pattern) |
| 112 |
if err != nil { |
| 113 |
return "" |
| 114 |
} |
| 115 |
if match != nil && strings.Count(match.GroupByNumber(1).String(), "t") < 7 { |
| 116 |
submatch, err := regexp2.MustCompile(`te*b[eqbh]*[te]*$`, regexp2.RE2).FindStringMatch(pattern) |
| 117 |
if err != nil { |
| 118 |
return "" |
| 119 |
} |
| 120 |
index := submatch.Index |
| 121 |
pattern = sliceString(pattern, 0, index) |
| 122 |
lines = sliceArray(lines, 0, index) |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
{ |
| 129 |
match, err := regexp2.MustCompile(`te*[qbe]+$`, regexp2.RE2).FindStringMatch(pattern) |
| 130 |
if err != nil { |
| 131 |
return "" |
| 132 |
} |
| 133 |
if match != nil { |
| 134 |
index := match.Index |
| 135 |
pattern = sliceString(pattern, 0, index) |
| 136 |
lines = sliceArray(lines, 0, index) |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
|
| 141 |
for { |
| 142 |
match, err := regexp2.MustCompile(`b`, regexp2.RE2).FindStringMatch(pattern) |
| 143 |
if err != nil { |
| 144 |
return "" |
| 145 |
} |
| 146 |
if match != nil { |
| 147 |
index := match.Index |
| 148 |
pattern = stringSliceBang(pattern, index) |
| 149 |
lines = sliceSliceBang(lines, index) |
| 150 |
} else { |
| 151 |
break |
| 152 |
} |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
{ |
| 157 |
match, err := regexp2.MustCompile(`h+[hte]+h+e`, regexp2.RE2).FindStringMatch(pattern) |
| 158 |
if err != nil { |
| 159 |
return "" |
| 160 |
} |
| 161 |
if match != nil { |
| 162 |
index := match.Index |
| 163 |
for i := 0; i < match.Length; i++ { |
| 164 |
c := []rune(header)[0] |
| 165 |
pattern = stringReplaceChar(pattern, c, index+i) |
| 166 |
} |
| 167 |
} |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
{ |
| 173 |
match, err := regexp2.MustCompile(`t[eq]*h{3,}`, regexp2.RE2).FindStringMatch(pattern) |
| 174 |
if err != nil { |
| 175 |
return "" |
| 176 |
} |
| 177 |
if match != nil { |
| 178 |
index := match.Index |
| 179 |
pattern = sliceString(pattern, 0, index) |
| 180 |
lines = sliceArray(lines, 0, index) |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
for { |
| 186 |
match, err := regexp2.MustCompile(`h`, regexp2.RE2).FindStringMatch(pattern) |
| 187 |
if err != nil { |
| 188 |
return "" |
| 189 |
} |
| 190 |
if match != nil { |
| 191 |
index := match.Index |
| 192 |
pattern = stringSliceBang(pattern, index) |
| 193 |
lines = sliceSliceBang(lines, index) |
| 194 |
} else { |
| 195 |
break |
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
{ |
| 201 |
match1, err := regexp2.MustCompile(`t`, regexp2.RE2).FindStringMatch(pattern) |
| 202 |
if err != nil { |
| 203 |
return "" |
| 204 |
} |
| 205 |
match2, err := regexp2.MustCompile(`[eq]+$`, regexp2.RE2).FindStringMatch(pattern) |
| 206 |
if err != nil { |
| 207 |
return "" |
| 208 |
} |
| 209 |
if match1 != nil && match2 != nil { |
| 210 |
index := match2.Index |
| 211 |
pattern = sliceString(pattern, 0, index-1) |
| 212 |
lines = sliceArray(lines, 0, index-1) |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
return strings.Join(lines, "\n") |
| 217 |
} |
| 218 |
|
| 219 |
func cleanup(text string) string { |
| 220 |
|
| 221 |
replacer := strings.NewReplacer( |
| 222 |
"\r\n", "\n", |
| 223 |
"\r", "\n", |
| 224 |
) |
| 225 |
|
| 226 |
text = replacer.Replace(text) |
| 227 |
|
| 228 |
|
| 229 |
for _, r := range patterns["REMOVE_PGP_MARKERS_REGEX"] { |
| 230 |
text, _ = r.Replace(text, "", 0, -1) |
| 231 |
} |
| 232 |
|
| 233 |
|
| 234 |
for _, r := range patterns["REMOVE_UNSUBSCRIBE_REGEX"] { |
| 235 |
text, _ = r.Replace(text, "", 0, -1) |
| 236 |
} |
| 237 |
|
| 238 |
|
| 239 |
for _, r := range patterns["REMOVE_ALIAS_REGEX"] { |
| 240 |
text, _ = r.Replace(text, "", 0, -1) |
| 241 |
} |
| 242 |
|
| 243 |
|
| 244 |
for _, r := range patterns["CHANGE_ENCLOSED_QUOTE_ONE_REGEX"] { |
| 245 |
text, _ = r.ReplaceFunc(text, func(m regexp2.Match) string { |
| 246 |
newText, _ := regexp2.MustCompile(`^`, regexp2.RE2).Replace(m.GroupByNumber(2).String(), "> ", 0, -1) |
| 247 |
return newText |
| 248 |
}, 0, -1) |
| 249 |
} |
| 250 |
|
| 251 |
for _, r := range patterns["CHANGE_ENCLOSED_QUOTE_TWO_REGEX"] { |
| 252 |
text, _ = r.ReplaceFunc(text, func(m regexp2.Match) string { |
| 253 |
newText, _ := regexp2.MustCompile(`^`, regexp2.RE2).Replace(m.GroupByNumber(1).String(), "> ", 0, -1) |
| 254 |
return newText |
| 255 |
}, 0, -1) |
| 256 |
} |
| 257 |
|
| 258 |
|
| 259 |
for _, r := range patterns["FIX_QUOTES_FORMAT_REGEX"] { |
| 260 |
text, _ = r.ReplaceFunc(text, func(m regexp2.Match) string { |
| 261 |
newText, _ := regexp2.MustCompile(`([[:alpha:]]+>|\|)`, regexp2.RE2).Replace(m.GroupByNumber(1).String(), ">", 0, -1) |
| 262 |
return newText |
| 263 |
}, 0, -1) |
| 264 |
} |
| 265 |
|
| 266 |
|
| 267 |
for _, regex := range patterns["FIX_EMBEDDED_REGEX"] { |
| 268 |
text, _ = regex.ReplaceFunc(text, func(m regexp2.Match) string { |
| 269 |
if strings.Count(m.String(), "\n") > 4 { |
| 270 |
return m.String() |
| 271 |
} |
| 272 |
newText, _ := regexp2.MustCompile(`\n+[[:space:]]*`, regexp2.RE2).Replace(m.String(), " ", 0, -1) |
| 273 |
return newText |
| 274 |
}, 0, -1) |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
return strings.TrimSpace(text) |
| 279 |
} |