From a76c2a8c22a44bbaada4434ff5ecfaaf54de7d44 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fr=C3=A9d=C3=A9ric=20Guillot?= Date: Mon, 20 Nov 2017 14:35:11 -0800 Subject: [PATCH] Improve OPML import/export --- errors/errors.go | 5 +++++ locale/translations.go | 7 ++++--- locale/translations/fr_FR.json | 3 ++- reader/opml/parser.go | 5 +++-- reader/opml/parser_test.go | 2 +- reader/opml/serializer.go | 7 ++----- reader/opml/serializer_test.go | 21 ++++++++++++++++----- server/static/bin.go | 2 +- server/static/css.go | 2 +- server/static/js.go | 2 +- server/template/common.go | 2 +- server/template/template.go | 15 +++++++++------ server/template/views.go | 2 +- server/ui/controller/opml.go | 5 +++-- sql/sql.go | 2 +- 15 files changed, 51 insertions(+), 31 deletions(-) diff --git a/errors/errors.go b/errors/errors.go index e40b3c23..a99a3ab5 100644 --- a/errors/errors.go +++ b/errors/errors.go @@ -6,22 +6,27 @@ package errors import ( "fmt" + "github.com/miniflux/miniflux2/locale" ) +// LocalizedError represents an error than could be translated to another language. type LocalizedError struct { message string args []interface{} } +// Error returns untranslated error message. func (l LocalizedError) Error() string { return fmt.Sprintf(l.message, l.args...) } +// Localize returns the translated error message. func (l LocalizedError) Localize(translation *locale.Language) string { return translation.Get(l.message, l.args...) } +// NewLocalizedError returns a new LocalizedError. func NewLocalizedError(message string, args ...interface{}) LocalizedError { return LocalizedError{message: message, args: args} } diff --git a/locale/translations.go b/locale/translations.go index 7b9bf15f..abf35601 100644 --- a/locale/translations.go +++ b/locale/translations.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.735395751 -0800 PST m=+0.047479249 +// 2017-11-20 14:18:33.283078515 -0800 PST m=+0.039164593 package locale @@ -125,12 +125,13 @@ var Translations = map[string]string{ "Never": "Jamais", "Unable to execute request: %v": "Impossible d'exécuter cette requête: %v", "Last Parsing Error": "Dernière erreur d'analyse", - "There is a problem with this feed": "Il y a un problème avec cet abonnement" + "There is a problem with this feed": "Il y a un problème avec cet abonnement", + "Unable to parse OPML file: %v": "Impossible de lire le fichier OPML : %v" } `, } var TranslationsChecksums = map[string]string{ "en_US": "6fe95384260941e8a5a3c695a655a932e0a8a6a572c1e45cb2b1ae8baa01b897", - "fr_FR": "1f75e5a4b581755f7f84687126bc5b96aaf0109a2f83a72a8770c2ad3ddb7ba3", + "fr_FR": "8b38f8e33f70c463560b49e7f408509304e4d8f61ae78ca26e33bbba28a9ec76", } diff --git a/locale/translations/fr_FR.json b/locale/translations/fr_FR.json index 7699bb70..863596cc 100644 --- a/locale/translations/fr_FR.json +++ b/locale/translations/fr_FR.json @@ -109,5 +109,6 @@ "Never": "Jamais", "Unable to execute request: %v": "Impossible d'exécuter cette requête: %v", "Last Parsing Error": "Dernière erreur d'analyse", - "There is a problem with this feed": "Il y a un problème avec cet abonnement" + "There is a problem with this feed": "Il y a un problème avec cet abonnement", + "Unable to parse OPML file: %v": "Impossible de lire le fichier OPML : %v" } diff --git a/reader/opml/parser.go b/reader/opml/parser.go index 5d8babd4..22706413 100644 --- a/reader/opml/parser.go +++ b/reader/opml/parser.go @@ -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 diff --git a/reader/opml/parser_test.go b/reader/opml/parser_test.go index 02543dfc..95a832b3 100644 --- a/reader/opml/parser_test.go +++ b/reader/opml/parser_test.go @@ -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") } } diff --git a/reader/opml/serializer.go b/reader/opml/serializer.go index 20c7046a..3ca859af 100644 --- a/reader/opml/serializer.go +++ b/reader/opml/serializer.go @@ -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) } diff --git a/reader/opml/serializer_test.go b/reader/opml/serializer_test.go index b1ef2a63..dd431d9d 100644 --- a/reader/opml/serializer_test.go +++ b/reader/opml/serializer_test.go @@ -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") + } } diff --git a/server/static/bin.go b/server/static/bin.go index 32ac5b65..e9cba0f1 100644 --- a/server/static/bin.go +++ b/server/static/bin.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.696612572 -0800 PST m=+0.008696070 +// 2017-11-20 14:18:33.249018092 -0800 PST m=+0.005104170 package static diff --git a/server/static/css.go b/server/static/css.go index b862d6ae..105de9a8 100644 --- a/server/static/css.go +++ b/server/static/css.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.70263347 -0800 PST m=+0.014716968 +// 2017-11-20 14:18:33.25118969 -0800 PST m=+0.007275768 package static diff --git a/server/static/js.go b/server/static/js.go index cf47d473..ceea2893 100644 --- a/server/static/js.go +++ b/server/static/js.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.707892515 -0800 PST m=+0.019976013 +// 2017-11-20 14:18:33.255571671 -0800 PST m=+0.011657749 package static diff --git a/server/template/common.go b/server/template/common.go index 4e41d023..80bdd394 100644 --- a/server/template/common.go +++ b/server/template/common.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.731951822 -0800 PST m=+0.044035320 +// 2017-11-20 14:18:33.28176883 -0800 PST m=+0.037854908 package template diff --git a/server/template/template.go b/server/template/template.go index 086cdc5c..7e2bf217 100644 --- a/server/template/template.go +++ b/server/template/template.go @@ -6,11 +6,6 @@ package template import ( "bytes" - "github.com/miniflux/miniflux2/errors" - "github.com/miniflux/miniflux2/locale" - "github.com/miniflux/miniflux2/server/route" - "github.com/miniflux/miniflux2/server/template/helper" - "github.com/miniflux/miniflux2/server/ui/filter" "html/template" "io" "log" @@ -18,6 +13,12 @@ import ( "strings" "time" + "github.com/miniflux/miniflux2/errors" + "github.com/miniflux/miniflux2/locale" + "github.com/miniflux/miniflux2/server/route" + "github.com/miniflux/miniflux2/server/template/helper" + "github.com/miniflux/miniflux2/server/ui/filter" + "github.com/gorilla/mux" ) @@ -61,11 +62,13 @@ func (t *TemplateEngine) ParseAll() { }, "t": func(key interface{}, args ...interface{}) string { switch key.(type) { - case string, error: + case string: return t.currentLocale.Get(key.(string), args...) case errors.LocalizedError: err := key.(errors.LocalizedError) return err.Localize(t.currentLocale) + case error: + return key.(error).Error() default: return "" } diff --git a/server/template/views.go b/server/template/views.go index f5ce314a..c7376f03 100644 --- a/server/template/views.go +++ b/server/template/views.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.712375345 -0800 PST m=+0.024458843 +// 2017-11-20 14:18:33.257809595 -0800 PST m=+0.013895673 package template diff --git a/server/ui/controller/opml.go b/server/ui/controller/opml.go index 45d34f8e..9d4d2afc 100644 --- a/server/ui/controller/opml.go +++ b/server/ui/controller/opml.go @@ -5,8 +5,9 @@ package controller import ( - "github.com/miniflux/miniflux2/server/core" "log" + + "github.com/miniflux/miniflux2/server/core" ) func (c *Controller) Export(ctx *core.Context, request *core.Request, response *core.Response) { @@ -52,7 +53,7 @@ func (c *Controller) UploadOPML(ctx *core.Context, request *core.Request, respon } response.Html().Render("import", args.Merge(tplParams{ - "errorMessage": impErr.Error(), + "errorMessage": impErr, "menu": "feeds", })) diff --git a/sql/sql.go b/sql/sql.go index f4bb1233..95a0e2e0 100644 --- a/sql/sql.go +++ b/sql/sql.go @@ -1,5 +1,5 @@ // Code generated by go generate; DO NOT EDIT. -// 2017-11-20 13:44:53.690484632 -0800 PST m=+0.002568130 +// 2017-11-20 14:18:33.247054842 -0800 PST m=+0.003140920 package sql