]> Sergey Matveev's repositories - linksexp.git/blob - xbel.go
6db0db2593c5ade5a7693d365a7a1f7e8fbd4a83
[linksexp.git] / xbel.go
1 /*
2 linksexp -- Texinfo/XBEL/OPML 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 )
24
25 type Link struct {
26         Href string `xml:"href,attr"`
27 }
28
29 type Feed struct {
30         Owner string `xml:"owner,attr"`
31         Link  Link   `xml:"link"`
32 }
33
34 type Bookmark struct {
35         Href  string `xml:"href,attr"`
36         Title string `xml:"title"`
37         Feeds []Feed `xml:"info>metadata,omitempty"`
38 }
39
40 type Folder struct {
41         Title     string     `xml:"title"`
42         Bookmarks []Bookmark `xml:"bookmark"`
43 }
44
45 type XBEL struct {
46         XMLName xml.Name `xml:"xbel"`
47         Version string   `xml:"version,attr"`
48         Folders []Folder `xml:"folder"`
49 }
50
51 func xbel(cats []string, data map[string][]map[string][]string) {
52         folders := make([]Folder, 0, len(cats))
53         for _, cat := range cats {
54                 ents := data[cat]
55                 bs := make([]Bookmark, 0, len(ents))
56                 for _, ent := range ents {
57                         b := Bookmark{Href: ent["URL"][0], Title: ent["Title"][0]}
58                         for _, url := range ent["Feed"] {
59                                 b.Feeds = append(b.Feeds, Feed{Owner: "webfeed", Link: Link{url}})
60                         }
61                         bs = append(bs, b)
62                 }
63                 folders = append(folders, Folder{Title: cat, Bookmarks: bs})
64         }
65         x := XBEL{Version: "1.0", Folders: folders}
66         out, err := xml.MarshalIndent(&x, "", "  ")
67         if err != nil {
68                 panic(err)
69         }
70         os.Stdout.WriteString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n")
71         os.Stdout.WriteString("<!DOCTYPE xbel>\n")
72         os.Stdout.Write(out)
73 }