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

Improve OPML import/export

This commit is contained in:
Frédéric Guillot 2017-11-20 14:35:11 -08:00
parent ace7524905
commit a76c2a8c22
15 changed files with 51 additions and 31 deletions

View file

@ -6,12 +6,13 @@ package opml
import (
"encoding/xml"
"fmt"
"io"
"github.com/miniflux/miniflux2/errors"
"golang.org/x/net/html/charset"
)
// Parse reads an OPML file and returns a SubcriptionList.
func Parse(data io.Reader) (SubcriptionList, error) {
opml := new(Opml)
decoder := xml.NewDecoder(data)
@ -19,7 +20,7 @@ func Parse(data io.Reader) (SubcriptionList, error) {
err := decoder.Decode(opml)
if err != nil {
return nil, fmt.Errorf("Unable to parse OPML file: %v\n", err)
return nil, errors.NewLocalizedError("Unable to parse OPML file: %v", err)
}
return opml.Transform(), nil

View file

@ -133,6 +133,6 @@ func TestParseInvalidXML(t *testing.T) {
_, err := Parse(bytes.NewBufferString(data))
if err == nil {
t.Error(err)
t.Error("Parse should generate an error")
}
}

View file

@ -11,6 +11,7 @@ import (
"log"
)
// Serialize returns a SubcriptionList in OPML format.
func Serialize(subscriptions SubcriptionList) string {
var b bytes.Buffer
writer := bufio.NewWriter(&b)
@ -34,7 +35,7 @@ func Serialize(subscriptions SubcriptionList) string {
}
encoder := xml.NewEncoder(writer)
encoder.Indent(" ", " ")
encoder.Indent(" ", " ")
if err := encoder.Encode(opml); err != nil {
log.Println(err)
return ""
@ -47,10 +48,6 @@ func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]Subcript
groups := make(map[string]SubcriptionList)
for _, subscription := range subscriptions {
// if subs, ok := groups[subscription.CategoryName]; !ok {
// groups[subscription.CategoryName] = SubcriptionList{}
// }
groups[subscription.CategoryName] = append(groups[subscription.CategoryName], subscription)
}

View file

@ -4,8 +4,11 @@
package opml
import "testing"
import "bytes"
import (
"bytes"
"fmt"
"testing"
)
func TestSerialize(t *testing.T) {
var subscriptions SubcriptionList
@ -14,6 +17,7 @@ func TestSerialize(t *testing.T) {
subscriptions = append(subscriptions, &Subcription{Title: "Feed 3", FeedURL: "http://example.org/feed/3", SiteURL: "http://example.org/3", CategoryName: "Category 2"})
output := Serialize(subscriptions)
fmt.Println(output)
feeds, err := Parse(bytes.NewBufferString(output))
if err != nil {
t.Error(err)
@ -23,9 +27,16 @@ func TestSerialize(t *testing.T) {
t.Errorf("Wrong number of subscriptions: %d instead of %d", len(feeds), 3)
}
for i := 0; i < len(feeds); i++ {
if !feeds[i].Equals(subscriptions[i]) {
t.Errorf(`Subscription are different: "%v" vs "%v"`, subscriptions[i], feeds[i])
found := false
for _, feed := range feeds {
if feed.Title == "Feed 1" && feed.CategoryName == "Category 1" &&
feed.FeedURL == "http://example.org/feed/1" && feed.SiteURL == "http://example.org/1" {
found = true
break
}
}
if !found {
t.Error("Serialized feed is incorrect")
}
}