]> Sergey Matveev's repositories - path-extractor.git/blob - extractor.go
More matches (with _)
[path-extractor.git] / extractor.go
1 package pathextractor
2
3 import "regexp"
4 import "strings"
5 import "fmt"
6
7 type MatchOptions struct {
8         format string
9 }
10
11 func pathExtractor(input string) [][][]byte {
12         surroundRegex := "[@~\\-_a-zA-Z/.0-9]*"
13         r := regexp.MustCompile("(" + surroundRegex + "[\\./]" + surroundRegex + ")")
14         temp := [][][]byte{}
15         temp = r.FindAllSubmatch([]byte(input), -1)
16         return temp
17 }
18
19 func stripParens(input string) string {
20         r := regexp.MustCompile("^\\((.*)\\)$")
21         temp := [][]byte{}
22         temp = r.FindSubmatch([]byte(input))
23         if len(temp) <= 1 {
24                 return input
25         }
26         return string(temp[1])
27 }
28
29 func postProcess(input string) string {
30         input = stripParens(input)
31         return input
32 }
33
34 func GetAllMatches(input string, options MatchOptions) []string {
35         matches := [][][]byte{}
36         result := []string{}
37         s := string("")
38         // print(input)
39         matches = pathExtractor(input)
40         for _, match := range matches {
41                 s = string(match[1])
42                 if len(input) >= len(s+"(") && strings.Index(input, s+"(") != -1 {
43                         continue
44                 }
45
46                 if isEmail(s) || isDate(s) || isVersion(s) || isGitRange(s) || isGitInstruction(s) || endsWithInvalidString(s) || containsInvalidString(s) || len(s) <= 2 {
47                         continue
48                 }
49                 if isGitPath(s) {
50                         s = replaceGitPath(s)
51                 }
52                 s = postProcess(s)
53                 if options.format == "ackmate" {
54                         s = fmt.Sprint(s, ":45")
55                 }
56                 result = append(result, s)
57         }
58         return result
59 }