-
Notifications
You must be signed in to change notification settings - Fork 1
/
namespace_psc.go
70 lines (61 loc) · 1.75 KB
/
namespace_psc.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
package types
import (
"encoding/xml"
"fmt"
"net/url"
"time"
)
// NamespacePSC is the namespace for Podlove Simple Chapters.
const NamespacePSC string = "http://podlove.org/simple-chapters"
// PSCChapters is the root element for Podlove Simple Chapters.
type PSCChapters struct {
XMLName xml.Name `xml:"psc:chapters"`
Version string `xml:"version,attr"`
Chapters []PSCChapter
}
// PSCChapter is a single chapter in Podlove Simple Chapters.
type PSCChapter struct {
Start time.Duration
Title string
Href *url.URL
Image *url.URL
}
func (encoded PSCChapter) MarshalXML(e *xml.Encoder, start xml.StartElement) error {
// Do default except for attributes, which we will marshal ourselves.
start.Name.Local = "psc:chapter"
start.Attr = append(start.Attr, xml.Attr{
Name: xml.Name{Local: "start"},
Value: formatChapterStart(encoded.Start),
})
start.Attr = append(start.Attr, xml.Attr{
Name: xml.Name{Local: "title"},
Value: encoded.Title,
})
if encoded.Href != nil {
start.Attr = append(start.Attr, xml.Attr{
Name: xml.Name{Local: "href"},
Value: encoded.Href.String(),
})
}
if encoded.Image != nil {
start.Attr = append(start.Attr, xml.Attr{
Name: xml.Name{Local: "image"},
Value: encoded.Image.String(),
})
}
return e.EncodeElement(struct{}{}, start)
}
func formatChapterStart(start time.Duration) string {
hours := int(start.Hours())
minutes := int(start.Minutes()) - hours*60
seconds := int(start.Seconds()) - minutes*60 - hours*3600
milliseconds := int(start.Milliseconds()) - seconds*1000 - minutes*60000 - hours*3600000
str := fmt.Sprintf("%02d:%02d", minutes, seconds)
if hours > 0 {
str = fmt.Sprintf("%02d:%s", hours, str)
}
if milliseconds > 0 {
str = fmt.Sprintf("%s.%03d", str, milliseconds)
}
return str
}