aboutsummaryrefslogtreecommitdiffstats
path: root/atom/atom.go
blob: f0c49e8e89ff0986f26bfe1dac486686447d832d (plain) (blame)
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
// taken from https://git.sr.ht/~m15o/html-journal
// Copyright 2022 m15o
// Distributed under the terms of a BSD 3-Clause license, see LICENSE file.

package atom

import (
	"encoding/xml"
	"git.sr.ht/~m15o/htmlj"
	"time"
)

type Feed struct {
	XMLName  xml.Name `xml:"http://www.w3.org/2005/Atom feed"`
	Title    string   `xml:"title"`
	ID       string   `xml:"id"`
	Link     []Link   `xml:"link"`
	Updated  TimeStr  `xml:"updated"`
	Author   *Person  `xml:"author"`
	Icon     string   `xml:"icon,omitempty"`
	Logo     string   `xml:"logo,omitempty"`
	Subtitle string   `xml:"subtitle,omitempty"`
	Entry    []*Entry `xml:"entry"`
}

type Entry struct {
	Title     string  `xml:"title"`
	ID        string  `xml:"id"`
	Link      []Link  `xml:"link"`
	Published TimeStr `xml:"published"`
	Updated   TimeStr `xml:"updated"`
	Author    *Person `xml:"author"`
	Summary   *Text   `xml:"summary"`
	Content   *Text   `xml:"content"`
}

type Link struct {
	Rel      string `xml:"rel,attr,omitempty"`
	Href     string `xml:"href,attr"`
	Type     string `xml:"type,attr,omitempty"`
	HrefLang string `xml:"hreflang,attr,omitempty"`
	Title    string `xml:"title,attr,omitempty"`
	Length   uint   `xml:"length,attr,omitempty"`
}

type Person struct {
	Name     string `xml:"name"`
	URI      string `xml:"uri,omitempty"`
	Email    string `xml:"email,omitempty"`
	InnerXML string `xml:",innerxml"`
}

type Text struct {
	Type string `xml:"type,attr"`
	Body string `xml:",chardata"`
}

type TimeStr string

func Time(t time.Time) TimeStr {
	return TimeStr(t.Format("2006-01-02T15:04:05-07:00"))
}

func FeedFromJournal(u string, j *htmlj.Journal) *Feed {
	f := &Feed{
		Title: j.Title,
		ID:    u,
		Author: &Person{
			Name: j.Title,
			URI:  u,
		},
		Updated: Time(j.Updated),
		Link: []Link{
			{
				Rel:  "alternate",
				Href: u,
			},
		},
	}

	for i := 0; i < len(j.Entries); i++ {
		p := j.Entries[i]
		f.Entry = append(f.Entry, &Entry{
			Title:     p.Title,
			ID:        u + "#" + p.Title,
			Published: Time(p.Published),
			Updated:   Time(p.Published),
			Content: &Text{
				Type: "html",
				Body: p.Content,
			},
		})
	}

	return f
}