about summary refs log tree commit diff stats
path: root/internal/atom/atom.go
blob: f75d18adf7c9ec598daf82d722a5de23171bde9e (plain)
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
package atom

import (
	"bytes"
	"encoding/xml"
	"net/url"
	"time"

	"go.alanpearce.eu/website/internal/config"
)

func MakeTagURI(config *config.Config, specific string) string {
	return "tag:" + config.OriginalDomain + "," + config.DomainStartDate + ":" + specific
}

func LinkXSL(w *bytes.Buffer, url string) error {
	_, err := w.WriteString(`<?xml-stylesheet href="`)
	if err != nil {
		return err
	}
	err = xml.EscapeText(w, []byte(url))
	if err != nil {
		return err
	}
	_, err = w.WriteString(`" type="text/xsl"?>`)
	if err != nil {
		return err
	}

	return nil
}

type Link struct {
	XMLName xml.Name `xml:"link"`
	Rel     string   `xml:"rel,attr,omitempty"`
	Type    string   `xml:"type,attr,omitempty"`
	Href    string   `xml:"href,attr"`
}

func MakeLink(url *url.URL) Link {
	return Link{
		Rel:  "alternate",
		Type: "text/html",
		Href: url.String(),
	}
}

type FeedContent struct {
	Content string `xml:",chardata"`
	Type    string `xml:"type,attr"`
}

type FeedEntry struct {
	XMLName xml.Name    `xml:"entry"`
	Title   string      `xml:"title"`
	Link    Link        `xml:"link"`
	ID      string      `xml:"id"`
	Updated time.Time   `xml:"updated"`
	Summary string      `xml:"summary,omitempty"`
	Content FeedContent `xml:"content"`
	Author  string      `xml:"author>name"`
}

type Feed struct {
	XMLName xml.Name     `xml:"http://www.w3.org/2005/Atom feed"`
	Title   string       `xml:"title"`
	Link    Link         `xml:"link"`
	ID      string       `xml:"id"`
	Updated time.Time    `xml:"updated"`
	Entries []*FeedEntry `xml:"entry"`
}