atom/README.md

133 lines
2.9 KiB
Markdown
Raw Permalink Normal View History

2024-10-17 20:47:37 +02:00
# atom
2024-10-12 09:36:40 +02:00
2024-10-18 18:00:38 +02:00
An extensible Atom feed generator library that aims to be very close to RFC4287.
It diligently checks for compliance with the standard and provides functions for
2024-10-20 12:57:24 +02:00
easy creation, extension and deletion of elements.
2024-10-18 06:04:05 +02:00
## Installation
To install the latest version of the module, use the following command:
2024-10-18 18:00:38 +02:00
```sh
2024-10-18 06:04:05 +02:00
go get git.streifling.com/jason/atom@latest
```
## Usage
2024-10-20 12:57:24 +02:00
This library provides convenient functions to safely create, extend and delete
elements and attributes of Atom feeds. It also provides checks for all
constructs' adherence to RFC4287.
2024-10-18 06:04:05 +02:00
2024-10-18 18:00:38 +02:00
```go
2024-10-18 06:04:05 +02:00
package main
import (
"fmt"
"log"
"git.streifling.com/jason/atom"
)
func main() {
2024-10-19 23:44:36 +02:00
feed := atom.NewFeed("Example Feed")
if err := feed.Check(); err != nil {
2024-10-18 06:04:05 +02:00
log.Fatalln(err)
}
author := atom.NewPerson("John Doe")
author.Email = "john.doe@example.com"
2024-10-19 23:44:36 +02:00
if err := author.Check(); err != nil {
2024-10-18 06:04:05 +02:00
log.Fatalln(err)
}
2024-10-19 23:44:36 +02:00
feed.AddAuthor(author)
2024-10-18 06:04:05 +02:00
2024-10-19 23:44:36 +02:00
entry := atom.NewEntry("First Entry")
entry.Content = atom.NewContent(atom.InlineText, "text", "This is the content of the first entry.")
if err := entry.Check(); err != nil {
2024-10-18 06:04:05 +02:00
log.Fatalln(err)
}
2024-10-19 23:44:36 +02:00
feed.AddEntry(entry)
2024-10-18 06:04:05 +02:00
feedString, err := feed.ToXML("utf-8")
if err != nil {
log.Fatalln(err)
}
fmt.Println(feedString)
}
```
2024-10-18 18:00:38 +02:00
It is also possible to use this library in a way similar to what other libraries
2024-10-19 23:44:36 +02:00
provide.
2024-10-18 06:04:05 +02:00
2024-10-18 18:00:38 +02:00
```go
2024-10-18 06:04:05 +02:00
package main
import (
"fmt"
"log"
"time"
"git.streifling.com/jason/atom"
)
func main() {
now := time.Now()
feed := &atom.Feed{
Title: &atom.PlainText{
Type: "text",
Text: "Example Feed",
},
ID: &atom.ID{URI: "urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6"},
Updated: &atom.Date{DateTime: atom.DateTime(now)},
Authors: []*atom.Person{
{
Name: "John Doe",
Email: "john.doe@example.com",
},
},
Entries: []*atom.Entry{
{
Title: &atom.PlainText{
Type: "text",
Text: "First Entry",
},
ID: &atom.ID{URI: "urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a"},
Updated: &atom.Date{DateTime: atom.DateTime(now)},
Content: &atom.InlineTextContent{
Type: "text",
Text: "This is the content of the first entry.",
},
},
},
}
feedString, err := feed.ToXML("utf-8")
if err != nil {
log.Fatalln(err)
}
fmt.Println(feedString)
}
```
2024-10-19 23:44:36 +02:00
The output of both ways of using it is an RFC4287 compliant Atom feed.
2024-10-18 06:04:05 +02:00
2024-10-18 18:00:38 +02:00
```xml
2024-10-18 06:04:05 +02:00
<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<author>
<name>John Doe</name>
<email>john.doe@example.com</email>
</author>
<id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
<title type="text">Example Feed</title>
<updated>2024-10-18T05:49:08+02:00</updated>
<entry>
<content type="text">This is the content of the first entry.</content>
<id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
<title type="text">First Entry</title>
<updated>2006-01-02T15:04:05+07:00</updated>
</entry>
</feed>
```