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