-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfilter.go
37 lines (30 loc) · 944 Bytes
/
filter.go
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
package main
import (
"regexp"
)
type tagFilter struct {
Matching string `json:"matching"`
OutputAs string `json:"output_as"`
SortAs string `json:"sort_as"`
}
func (tf *tagFilter) processTag(t *Tag) {
regex := regexp.MustCompile(tf.Matching)
outputTemplate := tf.OutputAs
if outputTemplate == "" {
outputTemplate = "$0" // default
}
sortTemplate := tf.SortAs
if sortTemplate == "" {
sortTemplate = outputTemplate // default to same as output template
}
outputResult := []byte{}
sortResult := []byte{}
for _, submatches := range regex.FindAllStringSubmatchIndex(t.original, -1) {
// Apply the captured submatches to the template and append the output
// to the result.
outputResult = regex.ExpandString(outputResult, outputTemplate, t.original, submatches)
sortResult = regex.ExpandString(sortResult, sortTemplate, t.original, submatches)
}
t.outputAs = string(outputResult)
t.sortAs = string(sortResult)
}