1
0
Fork 0
mirror of https://github.com/miniflux/v2.git synced 2025-07-02 16:38:37 +00:00

When detecting the format, detect its version as well

There is no need to detect the format and then the version when both can be
done at the same time.

Add a benchmark as well, on large and small atom and rss files.
This commit is contained in:
jvoisin 2024-03-12 13:11:56 +01:00 committed by Frédéric Guillot
parent 688b73b7ae
commit 45d486b919
12 changed files with 3608 additions and 160 deletions

View file

@ -4,7 +4,6 @@
package atom // import "miniflux.app/v2/internal/reader/atom"
import (
"encoding/xml"
"fmt"
"io"
@ -17,14 +16,13 @@ type atomFeed interface {
}
// Parse returns a normalized feed struct from a Atom feed.
func Parse(baseURL string, r io.ReadSeeker) (*model.Feed, error) {
func Parse(baseURL string, r io.ReadSeeker, version string) (*model.Feed, error) {
var rawFeed atomFeed
if getAtomFeedVersion(r) == "0.3" {
if version == "0.3" {
rawFeed = new(atom03Feed)
} else {
rawFeed = new(atom10Feed)
}
r.Seek(0, io.SeekStart)
if err := xml_decoder.NewXMLDecoder(r).Decode(rawFeed); err != nil {
return nil, fmt.Errorf("atom: unable to parse feed: %w", err)
@ -32,25 +30,3 @@ func Parse(baseURL string, r io.ReadSeeker) (*model.Feed, error) {
return rawFeed.Transform(baseURL), nil
}
func getAtomFeedVersion(data io.ReadSeeker) string {
decoder := xml_decoder.NewXMLDecoder(data)
for {
token, _ := decoder.Token()
if token == nil {
break
}
if element, ok := token.(xml.StartElement); ok {
if element.Name.Local == "feed" {
for _, attr := range element.Attr {
if attr.Name.Local == "version" && attr.Value == "0.3" {
return "0.3"
}
}
return "1.0"
}
}
}
return "1.0"
}