From 129f1bf3da188756c8c2ab6c894457e55987af5a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Tue, 26 Mar 2019 19:49:59 -0700 Subject: [PATCH] Add support for OPML v1 import --- reader/opml/opml.go | 12 +++---- reader/opml/parser_test.go | 71 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/reader/opml/opml.go b/reader/opml/opml.go index 27d0167e..76d61939 100644 --- a/reader/opml/opml.go +++ b/reader/opml/opml.go @@ -4,7 +4,9 @@ package opml // import "miniflux.app/reader/opml" -import "encoding/xml" +import ( + "encoding/xml" +) type opml struct { XMLName xml.Name `xml:"opml"` @@ -48,10 +50,6 @@ func (o *outline) GetSiteURL() string { return o.FeedURL } -func (o *outline) IsCategory() bool { - return o.Text != "" && o.SiteURL == "" && o.FeedURL == "" -} - func (o *outline) Append(subscriptions SubcriptionList, category string) SubcriptionList { if o.FeedURL != "" { subscriptions = append(subscriptions, &Subcription{ @@ -67,10 +65,10 @@ func (o *outline) Append(subscriptions SubcriptionList, category string) Subcrip func (o *opml) Transform() SubcriptionList { var subscriptions SubcriptionList - for _, outline := range o.Outlines { - if outline.IsCategory() { + if len(outline.Outlines) > 0 { for _, element := range outline.Outlines { + // outline.Text is only available in OPML v2. subscriptions = element.Append(subscriptions, outline.Text) } } else { diff --git a/reader/opml/parser_test.go b/reader/opml/parser_test.go index ef8e9f17..f6ce6346 100644 --- a/reader/opml/parser_test.go +++ b/reader/opml/parser_test.go @@ -122,6 +122,77 @@ func TestParseOpmlWithEmptyTitleAndEmptySiteURL(t *testing.T) { } } +func TestParseOpmlVersion1(t *testing.T) { + data := ` + + + mySubscriptions.opml + Wed, 13 Mar 2019 11:51:41 GMT + + + + + + + + + + + ` + + var expected SubcriptionList + expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) + expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""}) + + subscriptions, err := Parse(bytes.NewBufferString(data)) + if err != nil { + t.Error(err) + } + + if len(subscriptions) != 2 { + t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) + } + + for i := 0; i < len(subscriptions); i++ { + if !subscriptions[i].Equals(expected[i]) { + t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i]) + } + } +} + +func TestParseOpmlVersion1WithoutOuterOutline(t *testing.T) { + data := ` + + + mySubscriptions.opml + Wed, 13 Mar 2019 11:51:41 GMT + + + + + + + ` + + var expected SubcriptionList + expected = append(expected, &Subcription{Title: "Feed 1", FeedURL: "http://example.org/feed1/", SiteURL: "http://example.org/1", CategoryName: ""}) + expected = append(expected, &Subcription{Title: "Feed 2", FeedURL: "http://example.org/feed2/", SiteURL: "http://example.org/2", CategoryName: ""}) + + subscriptions, err := Parse(bytes.NewBufferString(data)) + if err != nil { + t.Error(err) + } + + if len(subscriptions) != 2 { + t.Errorf("Wrong number of subscriptions: %d instead of %d", len(subscriptions), 2) + } + + for i := 0; i < len(subscriptions); i++ { + if !subscriptions[i].Equals(expected[i]) { + t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], expected[i]) + } + } +} func TestParseInvalidXML(t *testing.T) { data := `garbage` _, err := Parse(bytes.NewBufferString(data))