1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-06-27 16:36:00 +00:00

refactor(processor): extract some functions into an utils.go file

This commit is contained in:
jvoisin 2025-06-18 22:27:18 +02:00 committed by Frédéric Guillot
parent 46b159ac58
commit fe4b00b9f8
4 changed files with 85 additions and 59 deletions

View file

@ -0,0 +1,82 @@
// SPDX-FileCopyrightText: Copyright The Miniflux Authors. All rights reserved.
// SPDX-License-Identifier: Apache-2.0
package processor // import "miniflux.app/v2/internal/reader/processor"
import (
"errors"
"fmt"
"regexp"
"strconv"
"time"
"github.com/tdewolff/minify/v2"
"github.com/tdewolff/minify/v2/html"
)
// TODO: use something less horrible than a regex to parse ISO 8601 durations.
var (
iso8601Regex = regexp.MustCompile(`^P((?P<year>\d+)Y)?((?P<month>\d+)M)?((?P<week>\d+)W)?((?P<day>\d+)D)?(T((?P<hour>\d+)H)?((?P<minute>\d+)M)?((?P<second>\d+)S)?)?$`)
)
func parseISO8601(from string) (time.Duration, error) {
var match []string
var d time.Duration
if iso8601Regex.MatchString(from) {
match = iso8601Regex.FindStringSubmatch(from)
} else {
return 0, errors.New("youtube: could not parse duration string")
}
for i, name := range iso8601Regex.SubexpNames() {
part := match[i]
if i == 0 || name == "" || part == "" {
continue
}
val, err := strconv.ParseInt(part, 10, 64)
if err != nil {
return 0, err
}
switch name {
case "hour":
d += time.Duration(val) * time.Hour
case "minute":
d += time.Duration(val) * time.Minute
case "second":
d += time.Duration(val) * time.Second
default:
return 0, fmt.Errorf("youtube: unknown field %s", name)
}
}
return d, nil
}
func minifyContent(content string) string {
m := minify.New()
// Options required to avoid breaking the HTML content.
m.Add("text/html", &html.Minifier{
KeepEndTags: true,
KeepQuotes: true,
})
if minifiedHTML, err := m.String("text/html", content); err == nil {
content = minifiedHTML
}
return content
}
func containsRegexPattern(pattern string, entries []string) bool {
for _, entry := range entries {
if matched, _ := regexp.MatchString(pattern, entry); matched {
return true
}
}
return false
}