File: classify.go

package info (click to toggle)
golang-forgejo-forgejo-reply 1.0.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 604 kB
  • sloc: makefile: 2
file content (93 lines) | stat: -rw-r--r-- 1,569 bytes parent folder | download
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
package reply

import (
	re2 "github.com/dlclark/regexp2"
)

const (
	delimiter = "d"
	embedded  = "b"
	empty     = "e"
	header    = "h"
	quote     = "q"
	signature = "s"
	text      = "t"
)

func classifyLine(line string) string {
	if isEmptyLine(line) {
		return empty
	}

	if isDelimiter(line) {
		return delimiter
	}

	if isSignature(line) {
		return signature
	}

	if isEmbeddedEmail(line) {
		return embedded
	}

	if isHeader(line) {
		return header
	}

	if isQuote(line) {
		return quote
	}

	return text
}

func isEmptyLine(line string) bool {
	ok, _ := re2.MustCompile(`^[[:blank:]]*$`, re2.RE2).MatchString(line)
	return ok
}

func isQuote(line string) bool {
	ok, _ := re2.MustCompile(`^[[:blank:]]*>`, re2.RE2).MatchString(line)
	return ok
}

func isDelimiter(line string) bool {
	ok, _ := re2.MustCompile(`^[[:blank:]]*([_,=+~#*ᐧ—]+|[\-]{4,}|[\-]+[[:blank:]])[[:blank:]]*$`, re2.RE2).MatchString(line)
	return ok
}

func isSignature(line string) bool {
	// remove any markdown links
	stripped, _ := re2.MustCompile(`\[([^\]]+)\]\([^\)]+\)`, re2.RE2).Replace(line, "$1", 0, -1)
	for _, r := range patterns["SIGNATURE_REGEXES"] {
		ok, _ := r.MatchString(stripped)
		if ok {
			return true
		}
	}

	return false
}

func isHeader(line string) bool {
	for _, r := range patterns["EMAIL_HEADER_REGEXES"] {
		ok, _ := r.MatchString(line)
		if ok {
			return true
		}
	}

	return false
}

func isEmbeddedEmail(line string) bool {
	for _, r := range patterns["EMBEDDED_REGEXES"] {
		ok, _ := r.MatchString(line)
		if ok {
			return true
		}
	}

	return false
}