2017-11-20 00:10:04 -05:00
|
|
|
// Copyright 2017 Frédéric Guillot. All rights reserved.
|
|
|
|
// Use of this source code is governed by the Apache 2.0
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
package opml // import "miniflux.app/reader/opml"
|
2017-11-20 00:10:04 -05:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bufio"
|
|
|
|
"bytes"
|
|
|
|
"encoding/xml"
|
2019-07-05 12:05:42 -04:00
|
|
|
"sort"
|
2017-12-15 21:55:57 -05:00
|
|
|
|
2018-08-25 00:51:50 -04:00
|
|
|
"miniflux.app/logger"
|
2017-11-20 00:10:04 -05:00
|
|
|
)
|
|
|
|
|
2017-11-20 17:35:11 -05:00
|
|
|
// Serialize returns a SubcriptionList in OPML format.
|
2017-11-20 00:10:04 -05:00
|
|
|
func Serialize(subscriptions SubcriptionList) string {
|
|
|
|
var b bytes.Buffer
|
|
|
|
writer := bufio.NewWriter(&b)
|
|
|
|
writer.WriteString(xml.Header)
|
|
|
|
|
2019-07-05 12:05:42 -04:00
|
|
|
feeds := normalizeFeeds(subscriptions)
|
2017-11-20 00:10:04 -05:00
|
|
|
encoder := xml.NewEncoder(writer)
|
2017-11-20 17:35:11 -05:00
|
|
|
encoder.Indent(" ", " ")
|
2017-11-20 22:11:06 -05:00
|
|
|
if err := encoder.Encode(feeds); err != nil {
|
2017-12-15 21:55:57 -05:00
|
|
|
logger.Error("[OPML:Serialize] %v", err)
|
2017-11-20 00:10:04 -05:00
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return b.String()
|
|
|
|
}
|
|
|
|
|
|
|
|
func groupSubscriptionsByFeed(subscriptions SubcriptionList) map[string]SubcriptionList {
|
|
|
|
groups := make(map[string]SubcriptionList)
|
|
|
|
|
|
|
|
for _, subscription := range subscriptions {
|
|
|
|
groups[subscription.CategoryName] = append(groups[subscription.CategoryName], subscription)
|
|
|
|
}
|
|
|
|
|
|
|
|
return groups
|
|
|
|
}
|
2019-07-05 12:05:42 -04:00
|
|
|
|
|
|
|
func normalizeFeeds(subscriptions SubcriptionList) *opml {
|
|
|
|
feeds := new(opml)
|
|
|
|
feeds.Version = "2.0"
|
|
|
|
|
|
|
|
groupedSubs := groupSubscriptionsByFeed(subscriptions)
|
|
|
|
var categories []string
|
|
|
|
for k := range groupedSubs {
|
|
|
|
categories = append(categories, k)
|
|
|
|
}
|
|
|
|
sort.Strings(categories)
|
|
|
|
|
|
|
|
for _, categoryName := range categories {
|
|
|
|
category := outline{Text: categoryName}
|
|
|
|
for _, subscription := range groupedSubs[categoryName] {
|
|
|
|
category.Outlines = append(category.Outlines, outline{
|
|
|
|
Title: subscription.Title,
|
|
|
|
Text: subscription.Title,
|
|
|
|
FeedURL: subscription.FeedURL,
|
|
|
|
SiteURL: subscription.SiteURL,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
feeds.Outlines = append(feeds.Outlines, category)
|
|
|
|
}
|
|
|
|
|
|
|
|
return feeds
|
|
|
|
}
|