-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.go
86 lines (65 loc) · 2.5 KB
/
generator.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
package main
import (
"fmt"
"log"
"os"
"strings"
)
func generateBlogIndex(articles []Article, settings *Settings) {
var builder strings.Builder
blogs := GetBlogsSorted(articles)
builder.WriteString("<table>")
for _, blog := range blogs {
builder.WriteString(fmt.Sprintf("<tr><td>%s</td><td><a href='%s'>%s</a></td><td> </td></tr>\n", PubdateForBlogIndex(blog.PubDate), blog.Url, blog.Title))
}
builder.WriteString("</table>")
table := builder.String()
indexLocation := settings.WorkDir + "/index.html"
indexb, err := os.ReadFile(indexLocation)
if err != nil {
log.Fatal("Could not load index.html")
}
index := string(indexb)
index = strings.Replace(index, "<x-blog-index/>", table, 1)
err = os.WriteFile(indexLocation, []byte(index), 0644)
if err != nil {
log.Fatal("Could not save Blog Index")
}
}
func generateSitemap(articles []Article, settings *Settings) {
var builder strings.Builder
blogs := GetBlogsSorted(articles)
builder.WriteString(`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:xhtml="http://www.w3.org/1999/xhtml">`)
for _, blog := range blogs {
builder.WriteString(fmt.Sprintf("\n<url><loc>%s</loc><lastmod>%s</lastmod></url>", blog.Url, PubDateForSitemap(blog.PubDate)))
}
builder.WriteString("\n</urlset>")
sitemapLocation := settings.WorkDir + "/sitemap.xml"
err := os.WriteFile(sitemapLocation, []byte(string(builder.String())), 0644)
if err != nil {
log.Fatal("Could not save sitemap.xml")
}
}
func generateRSSFeed(articles []Article, settings *Settings) {
var builder strings.Builder
blogs := GetBlogsSorted(articles)
builder.WriteString(`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<title>NobenLog</title>
<link>https://noben.org/blog/</link>
<description>Recent content on NobenLog</description>
<generator>site-gen-go -- https://github.com/keyle/site-gen-go</generator>
<language>en-us</language>`)
for _, blog := range blogs {
builder.WriteString(fmt.Sprintf("<item><title>%s</title><link>%s</link><pubDate>%s</pubDate><guid>%s</guid><description><![CDATA[ %s ]]></description></item>\n", blog.Title, blog.Url, blog.PubDate, blog.Url, blog.Description))
}
builder.WriteString("</channel></rss>\n")
FeedLocation := settings.WorkDir + "/index.xml"
err := os.WriteFile(FeedLocation, []byte(string(builder.String())), 0644)
if err != nil {
log.Fatal("Could not save RSS feed")
}
}