-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopml.go
80 lines (64 loc) · 1.62 KB
/
opml.go
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
package main
import (
"encoding/xml"
"errors"
"io"
"time"
)
type entry struct {
Type string `xml:"type,attr"`
Text string `xml:"text,attr"`
Title string `xml:"title,attr"`
Desc string `xml:"description,attr"`
URL string `xml:"xmlUrl,attr"`
}
type opml struct {
Version string `xml:"version,attr"`
Title string `xml:"head>title"`
Pubdate opmltime `xml:"head>dateCreated"`
Spec string `xml:"head>docs"`
Entries []*entry `xml:"body>outline"`
}
func newOPML(title string) *opml {
return &opml{
Title: title,
Spec: "http://dev.opml.org/spec2.html",
Version: "2.0",
}
}
func (o *opml) writeTo(w io.Writer) error {
_, err := w.Write([]byte(xml.Header))
if err != nil {
return err
}
e := xml.NewEncoder(w)
e.Indent("", " ")
return e.Encode(o)
}
func (o *opml) readFrom(r io.Reader) error {
return xml.NewDecoder(r).Decode(o)
}
type opmltime time.Time
func (ot *opmltime) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var s string
if err := d.DecodeElement(&s, &start); err != nil {
return err
}
layouts := []string{
time.RFC1123Z, // "Mon, 02 Jan 2006 15:04:05 -0700"
time.RFC1123, // "Mon, 02 Jan 2006 15:04:05 MST"
time.RFC822Z, // "02 Jan 06 15:04 -0700"
time.RFC822, // "02 Jan 06 15:04 MST"
}
for _, l := range layouts {
if t, err := time.Parse(l, s); err == nil {
*ot = opmltime(t)
return nil
}
}
return errors.New("unsupported date format: " + s)
}
func (ot *opmltime) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
s := time.Time(*ot).Format(time.RFC1123Z) // "Mon, 02 Jan 2006 15:04:05 -0700"
return e.EncodeElement(s, start)
}