-
Notifications
You must be signed in to change notification settings - Fork 4
/
meta.go
executable file
·115 lines (103 loc) · 2.96 KB
/
meta.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
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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package main
import (
"bytes"
"html/template"
"log"
"time"
"github.com/schollz/anonfiction/src/story"
"github.com/schollz/anonfiction/src/topic"
)
const rssTemplate = `
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="https://www.anonfiction.com/rss.xml" rel="self" type="application/rss+xml" />
<title>Stories Incognito</title>
<link>https://www.anonfiction.com</link>
<description></description>
<generator></generator>
<language>eng</language>
<managingEditor>editor@anonfiction.com (Stories Incognito Editors)</managingEditor>
<webMaster>web@anonfiction.com (Web Guru)</webMaster>
<copyright>Copyright 2017 Stories Incognito</copyright>
<lastBuildDate>{{ .Date.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</lastBuildDate>
{{ range .Stories }}<item>
<title>{{ .Topic }}</title>
<link>https://www.anonfiction.com/read/story/{{ .ID }}</link>
<pubDate>{{ .DatePublished.Format "Mon, 02 Jan 2006 15:04:05 -0700" }}</pubDate>
<guid>https://www.anonfiction.com/read/story/{{ .ID }}</guid>
<description>{{ .Description }}</description>
</item>{{ end }}
</channel>
</rss>
`
func RSS() string {
funcMap := template.FuncMap{
"slugify": slugify,
"unslugify": unslugify,
}
// Create a template, add the function map, and parse the text.
tmpl, err := template.New("rss").Funcs(funcMap).Parse(rssTemplate)
if err != nil {
log.Fatalf("parsing: %s", err)
}
type RSSData struct {
Stories []story.Story
Date time.Time
}
s, _ := story.ListPublished()
rss := RSSData{
Date: time.Now(),
Stories: s,
}
// Run the template to verify the output.
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, rss)
if err != nil {
log.Fatalf("execution: %s", err)
}
return tpl.String()
}
const siteMapTemplate = `<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{{ range .Topics }}<url>
<loc>https://www.anonfiction.com/read/topic/{{ .Name | slugify }}</loc>
<lastmod>{{ $.Date.Format "2006-01-02T15:04:05-07:00" }}</lastmod>
<changefreq>monthly</changefreq>
<priority>1</priority>
</url>{{ end }}
{{ range .Stories }}<url>
<loc>https://www.anonfiction.com/read/story/{{ .ID }}</loc>
<lastmod>{{ .DatePublished.Format "2006-01-02T15:04:05-07:00" }}</lastmod>
<changefreq>monthly</changefreq>
<priority>0.75</priority>
</url>{{ end }}
</urlset>`
func SiteMap() string {
funcMap := template.FuncMap{
"slugify": slugify,
"unslugify": unslugify,
}
// Create a template, add the function map, and parse the text.
tmpl, err := template.New("sitemap").Funcs(funcMap).Parse(siteMapTemplate)
if err != nil {
log.Fatalf("parsing: %s", err)
}
type Data struct {
Stories []story.Story
Topics []topic.Topic
Date time.Time
}
s, _ := story.ListPublished()
t, _ := topic.Load(TopicDB)
data := Data{
Date: time.Now(),
Stories: s,
Topics: t,
}
// Run the template to verify the output.
var tpl bytes.Buffer
err = tmpl.Execute(&tpl, data)
if err != nil {
log.Fatalf("execution: %s", err)
}
return tpl.String()
}