aboutsummaryrefslogtreecommitdiffstats
path: root/exporter/exporter.go
blob: 9172fec020e9bda541343e4de4357b40b63c2848 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
package exporter

import (
	"adammathes.com/neko/models/feed"
	"bytes"
	"encoding/json"
	"encoding/xml"
	"fmt"
	"html/template"
)

func ExportFeeds(format string) string {
	feeds, err := feed.All()
	if err != nil {
		panic(err)
	}

	s := ""
	switch format {
	case "text":
		for _, f := range feeds {
			s = s + fmt.Sprintf("%s\n", f.Url)
		}

	case "opml":
		s = s + fmt.Sprintf(`<opml version="2.0"><head><title>neko feeds</title></head><body>`)
		s = s + fmt.Sprintf("\n")
		for _, f := range feeds {
			b, _ := xml.Marshal(f)
			s = s + fmt.Sprintf("%s\n", string(b))
		}
		s = s + fmt.Sprintf(`</body></opml>`)

	case "json":
		js, _ := json.Marshal(feeds)
		s = fmt.Sprintf("%s\n", js)

	case "html":
		htmlTemplateString := `<html>
<head>
<title>feeds</title>
</head>
<body>
<ul>
{{ range . }}
<li><a href="{{.WebUrl}}">{{.Title}}</a> | <a href="{{.Url}}">xml</a></li>
{{ end }}
</ul>
</body>
</html>`
		var bts bytes.Buffer
		htmlTemplate, err := template.New("feeds").Parse(htmlTemplateString)
		err = htmlTemplate.Execute(&bts, feeds)
		if err != nil {
			panic(err)
		}
		s = bts.String()
	}

	return s
}