-
Notifications
You must be signed in to change notification settings - Fork 0
/
opml_document.go
391 lines (301 loc) · 9.95 KB
/
opml_document.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
// Copyright (c) VirtualTam
// SPDX-License-Identifier: MIT
package opml
import (
"encoding/json"
"encoding/xml"
"strconv"
"strings"
"time"
)
type (
OutlineType string
RSSVersion string
)
const (
Version1 string = "1.0"
Version1_1 string = "1.1"
Version2 string = "2.0"
OutlineTypeInclusion OutlineType = "include"
OutlineTypeLink OutlineType = "link"
OutlineTypeSubscription OutlineType = "rss"
OutlineTypeText OutlineType = "text"
RSSVersion1 RSSVersion = "RSS"
RSSVersion2 RSSVersion = "RSS2"
)
// A Document represents an OPML Document.
//
// See https://opml.org/spec2.opml for details on the OPML specification.
type Document struct {
XMLName xml.Name `xml:"opml" json:"-"`
Version string `xml:"version,attr" json:"version"`
Head Head `xml:"head" json:"head"`
Body Body `xml:"body" json:"body"`
}
// A Head contains the metadata for the OPML Document.
type Head struct {
// The title of the document.
Title string
// The date the document was created.
DateCreated time.Time
// The date indicating when the document was last modified.
DateModified time.Time
// The owner of the document.
OwnerName string
// The email address of the owner of the document.
OwnerEmail string
// A list of line numbers that are expanded.
// The line numbers in the list indicate which headlines to expand.
ExpansionState []int
// A number, saying which line of the outline is displayed on the top line of the window.
VertScrollState int
// The pixel location of the top edge of the window.
WindowTop int
// The pixel location of the left edge of the window.
WindowLeft int
// The pixel location of the bottom edge of the window.
WindowBottom int
// The pixel location of the right edge of the window.
WindowRight int
}
func (h *Head) MarshalJSON() ([]byte, error) {
mHead := newMarshalableHead(h)
return json.Marshal(mHead)
}
func (h *Head) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
mHead := newMarshalableHead(h)
return e.EncodeElement(mHead, start)
}
func (h *Head) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var mHead marshalableHead
if err := d.DecodeElement(&mHead, &start); err != nil {
return err
}
head, err := mHead.toHead()
if err != nil {
return err
}
*h = head
return nil
}
type marshalableHead struct {
Title string `xml:"title,omitempty" json:"title"`
DateCreatedStr string `xml:"dateCreated,omitempty" json:"date_created,omitempty"`
DateModifiedStr string `xml:"dateModified,omitempty" json:"date_modified,omitempty"`
OwnerName string `xml:"ownerName,omitempty" json:"owner_name,omitempty"`
OwnerEmail string `xml:"ownerEmail,omitempty" json:"owner_email,omitempty"`
ExpansionStatesStr string `xml:"expansionState,omitempty" json:"expansion_state,omitempty"`
VertScrollState int `xml:"vertScrollState,omitempty" json:"vert_scroll_state,omitempty"`
WindowTop int `xml:"windowTop,omitempty" json:"window_top,omitempty"`
WindowLeft int `xml:"windowLeft,omitempty" json:"window_left,omitempty"`
WindowBottom int `xml:"windowBottom,omitempty" json:"window_bottom,omitempty"`
WindowRight int `xml:"windowRight,omitempty" json:"window_right,omitempty"`
}
func newMarshalableHead(h *Head) marshalableHead {
mHead := marshalableHead{
Title: h.Title,
OwnerName: h.OwnerName,
OwnerEmail: h.OwnerEmail,
VertScrollState: h.VertScrollState,
WindowTop: h.WindowTop,
WindowLeft: h.WindowLeft,
WindowBottom: h.WindowBottom,
WindowRight: h.WindowRight,
}
if !h.DateCreated.IsZero() {
mHead.DateCreatedStr = encodeRFC1123Time(h.DateCreated)
}
if !h.DateModified.IsZero() {
mHead.DateModifiedStr = encodeRFC1123Time(h.DateModified)
}
if len(h.ExpansionState) > 0 {
var statesStr []string
for _, state := range h.ExpansionState {
statesStr = append(statesStr, strconv.Itoa(state))
}
mHead.ExpansionStatesStr = strings.Join(statesStr, ", ")
}
return mHead
}
func (mHead *marshalableHead) toHead() (Head, error) {
h := Head{
Title: mHead.Title,
OwnerName: mHead.OwnerName,
OwnerEmail: mHead.OwnerEmail,
VertScrollState: mHead.VertScrollState,
WindowTop: mHead.WindowTop,
WindowLeft: mHead.WindowLeft,
WindowBottom: mHead.WindowBottom,
WindowRight: mHead.WindowRight,
}
if mHead.DateCreatedStr != "" {
dateCreated, err := decodeTime(mHead.DateCreatedStr)
if err != nil {
return Head{}, err
}
h.DateCreated = dateCreated
}
if mHead.DateModifiedStr != "" {
dateModified, err := decodeTime(mHead.DateModifiedStr)
if err != nil {
return Head{}, err
}
h.DateModified = dateModified
}
if mHead.ExpansionStatesStr != "" {
var expansionStates []int
statesStr := strings.Split(mHead.ExpansionStatesStr, ",")
for _, stateStr := range statesStr {
stateStr = strings.TrimSpace(stateStr)
if stateStr == "" {
continue
}
state, err := strconv.Atoi(stateStr)
if err != nil {
return Head{}, err
}
expansionStates = append(expansionStates, state)
}
h.ExpansionState = expansionStates
}
return h, nil
}
// A Body contains one or more Outline elements.
type Body struct {
Outlines []Outline `xml:"outline" json:"outlines"`
}
// An Outline represents a text element, a subscription list item or a directory.
type Outline struct {
// The Text that is displayed when an outliner opens the OPML document.
Text string
// Special: The Type indicates how the attributes of the Outline are interpreted.
Type OutlineType
// Special: Indicates whether a breakpoint is set on this outline.
IsBreakpoint bool
// Special: Indicates whether the outline is commented.
//
// If an outline is commented, all subordinate outlines are considered to also be commented.
IsComment bool
// Special: A list of category strings.
Categories []string
// Special: The date when the outline node was created.
Created time.Time
// Inclusion: The HTTP address of the included link.
Url string
// Subscription: Version of RSS/Atom that is being supplied by the feed.
Version RSSVersion
// Subscription: Title is the top-level title from the feed.
Title string
// Subscription: Description is the top-level description element from the feed.
Description string
// Subscription: Language is the top-level language element for the feed.
Language string
// Subscription: HtmlUrl is the top-level link element from the feed.
HtmlUrl string
// Subscription: XmlUrl is the address of the feed.
XmlUrl string
// Directory: Subordinated outlines, arbitrarily structured.
Outlines []Outline
}
// IsDirectory returns whether this Outline is a directory and contains subordinated Outlines.
func (o *Outline) IsDirectory() bool {
return len(o.Outlines) > 0
}
// OutlineType returns the type of this Outline.
//
// If the Type is not set explicitly, it is inferred from the Outline's field values.
func (o *Outline) OutlineType() OutlineType {
if o.Type != "" {
return o.Type
}
return OutlineTypeText
}
func (o *Outline) MarshalJSON() ([]byte, error) {
mOutline := newMarshalableOutline(o)
return json.Marshal(mOutline)
}
func (o *Outline) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
mOutline := newMarshalableOutline(o)
return e.EncodeElement(mOutline, start)
}
func (o *Outline) UnmarshalXML(d *xml.Decoder, start xml.StartElement) error {
var mOutline marshalableOutline
if err := d.DecodeElement(&mOutline, &start); err != nil {
return err
}
outline, err := mOutline.toOutline()
if err != nil {
return err
}
*o = outline
return nil
}
type marshalableOutline struct {
Text string `xml:"text,attr" json:"text"`
CategoriesStr string `xml:"category,attr,omitempty" json:"categories,omitempty"`
CreatedStr string `xml:"created,attr,omitempty" json:"created,omitempty"`
Description string `xml:"description,attr,omitempty" json:"description,omitempty"`
HtmlUrl string `xml:"htmlUrl,attr,omitempty" json:"html_url,omitempty"`
IsBreakpoint bool `xml:"isBreakpoint,attr,omitempty" json:"is_breakpoint,omitempty"`
IsComment bool `xml:"isComment,attr,omitempty" json:"is_comment,omitempty"`
Language string `xml:"language,attr,omitempty" json:"language,omitempty"`
Title string `xml:"title,attr,omitempty" json:"title,omitempty"`
Type OutlineType `xml:"type,attr,omitempty" json:"type,omitempty"`
Url string `xml:"url,attr,omitempty" json:"url,omitempty"`
Version RSSVersion `xml:"version,attr,omitempty" json:"version,omitempty"`
XmlUrl string `xml:"xmlUrl,attr,omitempty" json:"xml_url,omitempty"`
Outlines []Outline `xml:"outline" json:"outlines,omitempty"`
}
func newMarshalableOutline(o *Outline) marshalableOutline {
mOutline := marshalableOutline{
Text: o.Text,
Type: o.Type,
CategoriesStr: strings.Join(o.Categories, ","),
Url: o.Url,
Version: o.Version,
Title: o.Title,
Description: o.Description,
HtmlUrl: o.HtmlUrl,
Language: o.Language,
XmlUrl: o.XmlUrl,
IsBreakpoint: o.IsBreakpoint,
IsComment: o.IsComment,
Outlines: o.Outlines,
}
if !o.Created.IsZero() {
mOutline.CreatedStr = encodeRFC1123Time(o.Created)
}
return mOutline
}
func (mo *marshalableOutline) toOutline() (Outline, error) {
outline := Outline{
// Text fields
Text: mo.Text,
Type: mo.Type,
// Special fields
IsBreakpoint: mo.IsBreakpoint,
IsComment: mo.IsComment,
// Inclusion fields
Url: mo.Url,
// Subscription fields
Version: mo.Version,
Title: mo.Title,
Description: mo.Description,
Language: mo.Language,
HtmlUrl: mo.HtmlUrl,
XmlUrl: mo.XmlUrl,
// Directory fields
Outlines: mo.Outlines,
}
if mo.CategoriesStr != "" {
outline.Categories = strings.Split(mo.CategoriesStr, ",")
}
if mo.CreatedStr != "" {
created, err := decodeTime(mo.CreatedStr)
if err != nil {
return Outline{}, err
}
outline.Created = created
}
return outline, nil
}