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,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}
}

View File

@ -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",
}

View File

@ -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"
}

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")
}
}

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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 ""
}

View File

@ -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

View File

@ -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",
}))

View File

@ -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