test one smart default parser

This commit is contained in:
Bel LaPointe
2025-04-05 10:59:33 -06:00
parent e85fec9bbf
commit d40a1f8fd4
2 changed files with 131 additions and 23 deletions

56
main.go
View File

@@ -133,6 +133,11 @@ func Main(ctx context.Context) error {
)
}
const (
PatternGroupTitleHyphenSE = `^(\[[^\]]*\] )?(?P<title>.*?)( -)?[ \.](S(?P<season>[0-9]{2})E)?(?P<episode>[0-9]{2})[^0-9].*`
PatternTitleSE = `^(?P<title>.*) S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*`
)
func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fields, dry bool) error {
mvNLn := RealMvNLn
if dry {
@@ -141,10 +146,7 @@ func Run(ctx context.Context, outd, ind string, patterns []string, overrides Fie
return RunWith(ctx,
outd,
ind,
append(patterns,
`^\[[^\]]*\] (?P<title>.*) - (?P<episode>[0-9]+).*`,
`^(?P<title>.*) S(?P<season>[0-9]+)E(?P<episode>[0-9]+).*`,
),
append(patterns, PatternGroupTitleHyphenSE, PatternTitleSE),
overrides,
mvNLn,
)
@@ -171,28 +173,11 @@ func RunWith(ctx context.Context, outd, ind string, patterns []string, overrides
func one(ctx context.Context, outd, inf string, patterns []string, overrides Fields, mvNLn MvNLn) error {
f := path.Base(inf)
for _, pattern := range patterns {
re := regexp.MustCompile(pattern)
if !re.MatchString(f) {
found, match := Parse(f, pattern)
if !match {
continue
}
var found Fields
groupNames := re.SubexpNames()
groups := re.FindStringSubmatch(f)
for i := 1; i < len(groupNames); i++ {
v := groups[i]
switch groupNames[i] {
case "title":
found.Title = v
case "season":
found.Season = v
case "episode":
found.Episode = v
default:
return fmt.Errorf("unexpected capture group %q", groupNames[i])
}
}
for _, wr := range [][2]*string{
[2]*string{&found.Title, &overrides.Title},
[2]*string{&found.Season, &overrides.Season},
@@ -213,6 +198,31 @@ func one(ctx context.Context, outd, inf string, patterns []string, overrides Fie
return nil
}
func Parse(f string, pattern string) (Fields, bool) {
re := regexp.MustCompile(pattern)
if !re.MatchString(f) {
return Fields{}, false
}
var found Fields
groupNames := re.SubexpNames()
groups := re.FindStringSubmatch(f)
for i := 1; i < len(groupNames); i++ {
v := groups[i]
switch groupNames[i] {
case "title":
found.Title = v
case "season":
found.Season = v
case "episode":
found.Episode = v
default:
//return fmt.Errorf("unexpected capture group %q", groupNames[i])
}
}
return found, true
}
func foundOne(ctx context.Context, outd, inf string, fields Fields, mvNLn MvNLn) error {
tmpl, err := template.New(inf).Parse(outd)
if err != nil {