about summary refs log tree commit diff stats
diff options
context:
space:
mode:
-rw-r--r--cmd/build/atom.go43
-rw-r--r--cmd/build/main.go (renamed from src/build.go)55
-rw-r--r--go.mod4
-rw-r--r--go.sum9
-rw-r--r--internal/config/config.go (renamed from src/config.go)7
5 files changed, 111 insertions, 7 deletions
diff --git a/cmd/build/atom.go b/cmd/build/atom.go
new file mode 100644
index 0000000..9116278
--- /dev/null
+++ b/cmd/build/atom.go
@@ -0,0 +1,43 @@
+package main
+
+import (
+	"encoding/xml"
+	"time"
+
+	. "alanpearce.eu/website/internal/config"
+)
+
+func makeTagURI(config Config, specific string) string {
+	return "tag:" + config.OriginalDomain + "," + config.DomainStartDate + ":" + specific
+}
+
+type Link struct {
+	XMLName xml.Name `xml:"link"`
+	Rel     string   `xml:"rel,attr"`
+	Type    string   `xml:"type,attr"`
+	Href    string   `xml:"href,attr"`
+}
+
+func makeLink(url string) Link {
+	return Link{
+		Rel:  "alternate",
+		Type: "text/html",
+		Href: url,
+	}
+}
+
+type FeedContent struct {
+	Content string `xml:",innerxml"`
+	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"`
+	Author  string      `xml:"author>name"`
+	Content FeedContent `xml:"content"`
+}
diff --git a/src/build.go b/cmd/build/main.go
index 3c2dfd5..48c9739 100644
--- a/src/build.go
+++ b/cmd/build/main.go
@@ -2,6 +2,7 @@ package main
 
 import (
 	"bytes"
+	"encoding/xml"
 	"fmt"
 	"log"
 	"os"
@@ -11,8 +12,11 @@ import (
 	"strings"
 	"time"
 
+	. "alanpearce.eu/website/internal/config"
+
 	"github.com/PuerkitoBio/goquery"
 	"github.com/adrg/frontmatter"
+	"github.com/antchfx/xmlquery"
 	mapset "github.com/deckarep/golang-set/v2"
 	"github.com/yuin/goldmark"
 	"github.com/yuin/goldmark/extension"
@@ -196,9 +200,51 @@ func renderListPage(tag string, config Config, posts []Post) string {
 	return html
 }
 
+func renderFeed(title string, config Config, posts []Post, specific string) string {
+	reader, err := os.Open("templates/feed.xml")
+	check(err)
+	doc, err := xmlquery.Parse(reader)
+	feed := doc.SelectElement("feed")
+	feed.SelectElement("title").FirstChild.Data = title
+	feed.SelectElement("link").SetAttr("href", config.BaseURL)
+	feed.SelectElement("id").FirstChild.Data = makeTagURI(config, specific)
+	datetime, err := posts[0].Date.MarshalText()
+	feed.SelectElement("updated").FirstChild.Data = string(datetime)
+	tpl := feed.SelectElement("entry")
+	xmlquery.RemoveFromTree(tpl)
+
+	for _, post := range posts {
+		text, err := xml.MarshalIndent(&FeedEntry{
+			Title:   post.Title,
+			Link:    makeLink(post.URL),
+			Id:      makeTagURI(config, post.Basename),
+			Updated: post.Date,
+			Summary: post.Description,
+			Author:  config.Title,
+			Content: FeedContent{
+				Content: post.Content,
+				Type:    "html",
+			},
+		}, "  ", "    ")
+		check(err)
+		entry, err := xmlquery.ParseWithOptions(strings.NewReader(string(text)), xmlquery.ParserOptions{
+			Decoder: &xmlquery.DecoderOptions{
+				Strict:    false,
+				AutoClose: xml.HTMLAutoClose,
+				Entity:    xml.HTMLEntity,
+			},
+		})
+		check(err)
+		xmlquery.AddChild(feed, entry.SelectElement("entry"))
+	}
+
+	return doc.OutputXML(true)
+}
+
 func main() {
-	config := getConfig()
-	err := os.MkdirAll("public/post", 0755)
+	err, config := GetConfig()
+	check(err)
+	err = os.MkdirAll("public/post", 0755)
 	check(err)
 	log.Print("Generating site...")
 	posts, tags := readPosts("content", "post", "public")
@@ -229,10 +275,12 @@ func main() {
 		// check(err)
 
 		// TODO: tag atom
+		// fmt.Printf("%+v\n", renderFeed(fmt.Sprintf("%s - %s", config.Title, tag), config, matchingPosts, tag))
 
-		fmt.Printf("%+v\n", renderListPage("", config, posts))
+		// fmt.Printf("%+v\n", renderListPage("", config, posts))
 
 		// TODO: atom
+		// fmt.Printf("%+v\n", renderFeed(config.Title, config, posts, "feed"))
 
 		// TODO: renderFeedStyles
 
@@ -242,4 +290,5 @@ func main() {
 	}
 
 	fmt.Printf("%+v\n", tags)
+	fmt.Println()
 }
diff --git a/go.mod b/go.mod
index da450e0..dbebe7b 100644
--- a/go.mod
+++ b/go.mod
@@ -7,10 +7,14 @@ require (
 	github.com/PuerkitoBio/goquery v1.9.1 // indirect
 	github.com/adrg/frontmatter v0.2.0 // indirect
 	github.com/andybalholm/cascadia v1.3.2 // indirect
+	github.com/antchfx/xmlquery v1.4.0 // indirect
+	github.com/antchfx/xpath v1.3.0 // indirect
 	github.com/deckarep/golang-set/v2 v2.6.0 // indirect
+	github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
 	github.com/yuin/goldmark v1.7.1 // indirect
 	go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect
 	golang.org/x/net v0.21.0 // indirect
+	golang.org/x/text v0.14.0 // indirect
 	gopkg.in/yaml.v2 v2.3.0 // indirect
 	gopkg.in/yaml.v3 v3.0.1 // indirect
 )
diff --git a/go.sum b/go.sum
index 0a9eadb..de84a45 100644
--- a/go.sum
+++ b/go.sum
@@ -7,8 +7,14 @@ github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd
 github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
 github.com/andybalholm/cascadia v1.3.2 h1:3Xi6Dw5lHF15JtdcmAHD3i1+T8plmv7BQ/nsViSLyss=
 github.com/andybalholm/cascadia v1.3.2/go.mod h1:7gtRlve5FxPPgIgX36uWBX58OdBsSS6lUvCFb+h7KvU=
+github.com/antchfx/xmlquery v1.4.0 h1:xg2HkfcRK2TeTbdb0m1jxCYnvsPaGY/oeZWTGqX/0hA=
+github.com/antchfx/xmlquery v1.4.0/go.mod h1:Ax2aeaeDjfIw3CwXKDQ0GkwZ6QlxoChlIBP+mGnDFjI=
+github.com/antchfx/xpath v1.3.0 h1:nTMlzGAK3IJ0bPpME2urTuFL76o4A96iYvoKFHRXJgc=
+github.com/antchfx/xpath v1.3.0/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
 github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
 github.com/deckarep/golang-set/v2 v2.6.0/go.mod h1:VAky9rY/yGXJOLEDv3OMci+7wtDpOF4IN+y82NBOac4=
+github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da h1:oI5xCqsCo564l8iNU+DwB5epxmsaqB+rhGL0m5jtYqE=
+github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da/go.mod h1:cIg4eruTrX1D+g88fzRXU5OdNfaM+9IcxsU14FzY7Hc=
 github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
 github.com/yuin/goldmark v1.7.1 h1:3bajkSilaCbjdKVsKdZjZCLBNPL9pYzrCakKaf4U49U=
 github.com/yuin/goldmark v1.7.1/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E=
@@ -22,6 +28,7 @@ golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLL
 golang.org/x/net v0.0.0-20210226172049-e18ecbb05110/go.mod h1:m0MpNAwzfU5UDzcl9v0D8zg8gWTRqZa9RBIspLL5mdg=
 golang.org/x/net v0.0.0-20220722155237-a158d28d115b/go.mod h1:XRhObCWvk6IyKnWLug+ECip1KBveYUHfp+8e9klMJ9c=
 golang.org/x/net v0.6.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
+golang.org/x/net v0.7.0/go.mod h1:2Tu9+aMcznHK/AK1HMvgo6xiTLG5rD5rZLDS+rp2Bjs=
 golang.org/x/net v0.9.0/go.mod h1:d48xBJpPfHeWQsugry2m+kC02ZBRGRgulfHnEXEuWns=
 golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
 golang.org/x/net v0.21.0/go.mod h1:bIjVDfnllIU7BJ2DNgfnXvpSvtn8VRwhlsaeUTyUS44=
@@ -44,6 +51,8 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
 golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
 golang.org/x/text v0.7.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
 golang.org/x/text v0.9.0/go.mod h1:e1OnstbJyHTd6l/uOt8jFFHp6TRDWZR/bV3emEE/zU8=
+golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
+golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
 golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
 golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
 golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
diff --git a/src/config.go b/internal/config/config.go
index bfa3052..aa46e6a 100644
--- a/src/config.go
+++ b/internal/config/config.go
@@ -1,4 +1,4 @@
-package main
+package config
 
 import (
 	"github.com/BurntSushi/toml"
@@ -30,9 +30,8 @@ type Config struct {
 	Menus map[string][]MenuItem
 }
 
-func getConfig() Config {
+func GetConfig() (error, Config) {
 	config := Config{}
 	_, err := toml.DecodeFile("config.toml", &config)
-	check(err)
-	return config
+	return err, config
 }