]> Sergey Matveev's repositories - linksexp.git/blob - opml.go
Unify copyright comment format
[linksexp.git] / opml.go
1 /*
2 linksexp -- Texinfo/XBEL/OPML/urls autogeneration from recfile bookmark
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
4
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
8
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with this program.  If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 package main
19
20 import (
21         "encoding/xml"
22         "os"
23         "strings"
24 )
25
26 type Outline struct {
27         Type     string `xml:"type,attr"`
28         Text     string `xml:"text,attr"`
29         XmlUrl   string `xml:"xmlUrl,attr"`
30         HtmlUrl  string `xml:"htmlUrl,attr"`
31         Category string `xml:"category,attr,omitempty"`
32 }
33
34 type OPML struct {
35         XMLName  xml.Name  `xml:"xbel"`
36         Version  string    `xml:"version,attr"`
37         Head     string    `xml:"head"`
38         Outlines []Outline `xml:"body>outline"`
39 }
40
41 func opml(data []map[string][]string) {
42         outlines := make([]Outline, 0, len(data))
43         for _, ent := range data {
44                 for _, feed := range ent["Feed"] {
45                         outlines = append(outlines, Outline{
46                                 Type:     "rss",
47                                 Text:     ent["Title"][0],
48                                 HtmlUrl:  ent["URL"][0],
49                                 XmlUrl:   feed,
50                                 Category: strings.Join(ent["Category"], ","),
51                         })
52                 }
53         }
54         x := OPML{Version: "2.0", Outlines: outlines}
55         out, err := xml.MarshalIndent(&x, "", "  ")
56         if err != nil {
57                 panic(err)
58         }
59         os.Stdout.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
60         os.Stdout.Write(out)
61 }