-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
124 lines (99 loc) · 3.24 KB
/
main.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
// The parser for [the xml that exported by Evernote (.enex file)](https://help.evernote.com/hc/en-us/articles/209005557).
package enex
import (
"encoding/base64"
"encoding/xml"
"fmt"
"io"
"net/url"
"time"
)
type EncodedData struct {
data []byte
}
func (ed *EncodedData) String() string {
return fmt.Sprintf("{EncodedData length=%d}", len(ed.data))
}
func (ed *EncodedData) UnmarshalText(text []byte) error {
if decoded, err := base64.StdEncoding.DecodeString(string(text)); err != nil {
return err
} else {
ed.data = decoded
return nil
}
}
func (ed EncodedData) MarshalText() ([]byte, error) {
s := base64.StdEncoding.EncodeToString(ed.data)
return []byte(s), nil
}
type Recognition struct {
XML string `xml:",cdata" json:",omitempty"`
}
type Resource struct {
Data EncodedData `xml:"data"`
Type string `xml:"mime"`
Name string `xml:"resource-attributes>file-name"`
Attachment string `xml:"resource-attributes>attachment,omitempty" json:",omitempty"`
Width int `xml:"width,omitempty" json:",omitempty"`
Height int `xml:"height,omitempty" json:",omitempty"`
Recognition Recognition `xml:"recognition,omitempty"`
}
type Content struct {
XML string `xml:",cdata"`
}
type DateTime time.Time
func (dt *DateTime) UnmarshalText(text []byte) (err error) {
var t time.Time
for _, f := range []string{"2006-01-02T15:04:05Z", "20060102T150405Z"} {
t, err = time.Parse(f, string(text))
if err == nil {
break
}
}
*dt = DateTime(t)
return
}
func (dt DateTime) MarshalText() ([]byte, error) {
s := time.Time(dt).Format("20060102T150405Z")
return []byte(s), nil
}
func (dt DateTime) String() string {
return time.Time(dt).Format("2006-01-02T15:04:05Z")
}
type Note struct {
Title string `xml:"title"`
Content Content `xml:"content"`
CreatedAt DateTime `xml:"created"`
UpdatedAt DateTime `xml:"updated,omitempty" json:",omitempty"`
Tags []string `xml:"tag"`
Author string `xml:"note-attributes>author"`
ReceivedAt DateTime `xml:"note-attributes>subject-date,omitempty" json:",omitempty"`
Source string `xml:"note-attributes>source,omitempty" json:",omitempty"`
SourceURL *url.URL `xml:"note-attributes>source-url,omitempty" json:",omitempty"`
Resources []Resource `xml:"resource,omitempty" json:",omitempty"`
}
type EvernoteExportedXML struct {
Notes []Note `xml:"note"`
ExportedAt DateTime `xml:"export-date,attr"`
ExportedBy string `xml:"application,attr"`
Version string `xml:"version,attr"`
}
/*
Parse .enex file from bytes into EvernoteExportedXML.
`data` is an xml data.
NOTE: This function is just wrapper of xml.Unmarshal. Please directly use xml package if you want customize behavior.
*/
func Parse(data []byte) (EvernoteExportedXML, error) {
var parsed EvernoteExportedXML
return parsed, xml.Unmarshal(data, &parsed)
}
/*
Parse .enex file from io.Reader into EvernoteExportedXML.
`r` is a reader to read .enex file.
NOTE: This function is just wrapper of xml.Decoder.Decode. Please directly use xml package if you want customize behavior.
*/
func ParseFromReader(r io.Reader) (EvernoteExportedXML, error) {
decoder := xml.NewDecoder(r)
var parsed EvernoteExportedXML
return parsed, decoder.Decode(&parsed)
}