mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
139 lines
2.8 KiB
Odin
139 lines
2.8 KiB
Odin
package main
|
|
|
|
import "core:fmt"
|
|
import "core:strings"
|
|
import "core:time"
|
|
|
|
generate_rss :: proc(pages: []Page, config: Site) -> string {
|
|
parts: [dynamic]string
|
|
defer delete(parts)
|
|
|
|
append(
|
|
&parts,
|
|
fmt.aprintf(
|
|
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
|
<channel>
|
|
<title>%s</title>
|
|
<link>%s/</link>
|
|
<description>%s</description>
|
|
<language>en-us</language>
|
|
<atom:link href="%s/index.xml" rel="self" type="application/rss+xml"/>`,
|
|
xml_escape(config.title),
|
|
config.base_url,
|
|
xml_escape(config.description),
|
|
config.base_url,
|
|
),
|
|
)
|
|
|
|
for page in pages {
|
|
if page.type == .Home {
|
|
continue
|
|
}
|
|
|
|
pub_date := "Mon, 01 Jan 0001 00:00:00 +0000"
|
|
if page.date != "" {
|
|
pub_date = format_rfc822(page.date)
|
|
}
|
|
|
|
append(
|
|
&parts,
|
|
fmt.aprintf(
|
|
`<item>
|
|
<title>%s</title>
|
|
<link>%s%s</link>
|
|
<pubDate>%s</pubDate>
|
|
<guid>%s%s</guid>
|
|
<description>%s</description>
|
|
</item>
|
|
`,
|
|
xml_escape(page.title),
|
|
config.base_url,
|
|
page.permalink,
|
|
pub_date,
|
|
config.base_url,
|
|
page.permalink,
|
|
xml_escape(page.body_html),
|
|
),
|
|
)
|
|
}
|
|
|
|
append(&parts, "</channel>\n</rss>")
|
|
|
|
return strings.join(parts[:], "")
|
|
}
|
|
|
|
generate_sitemap :: proc(pages: []Page, base_url: string) -> string {
|
|
parts: [dynamic]string
|
|
defer delete(parts)
|
|
|
|
append(
|
|
&parts,
|
|
`<?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 page in pages {
|
|
lastmod := ""
|
|
if page.date != "" {
|
|
lastmod = fmt.aprintf("<lastmod>%s</lastmod>", page.date)
|
|
}
|
|
append(
|
|
&parts,
|
|
fmt.aprintf("<url><loc>%s%s</loc>%s</url>\n", base_url, page.permalink, lastmod),
|
|
)
|
|
}
|
|
|
|
// Posts list page
|
|
posts_lastmod := ""
|
|
for page in pages {
|
|
if page.type == .Post && page.date > posts_lastmod {
|
|
posts_lastmod = page.date
|
|
}
|
|
}
|
|
posts_lm := ""
|
|
if posts_lastmod != "" {
|
|
posts_lm = fmt.aprintf("<lastmod>%s</lastmod>", posts_lastmod)
|
|
}
|
|
append(&parts, fmt.aprintf("<url><loc>%s/posts/</loc>%s</url>\n", base_url, posts_lm))
|
|
|
|
append(&parts, "</urlset>")
|
|
|
|
return strings.join(parts[:], "")
|
|
}
|
|
|
|
// TODO: Leaks
|
|
format_rfc822 :: proc(iso: string) -> string {
|
|
if len(iso) < 19 {
|
|
// TODO: should indicate error somehow
|
|
return iso
|
|
}
|
|
|
|
date, offset, _ := time.iso8601_to_time_and_offset(iso)
|
|
|
|
weekday := fmt.tprintf("%s", time.weekday(date))
|
|
month := fmt.tprintf("%s", time.month(date))
|
|
buf: [8]byte
|
|
t := time.to_string_hms(date, buf[:])
|
|
|
|
return fmt.aprintf(
|
|
"%s, %02d %s %d %s %3d%2d",
|
|
weekday[:3],
|
|
time.day(date),
|
|
month[:3],
|
|
time.year(date),
|
|
t,
|
|
offset / 60,
|
|
offset % 60,
|
|
)
|
|
}
|
|
|
|
xml_escape :: proc(s: string) -> string {
|
|
r, _ := strings.replace_all(s, "&", "&")
|
|
r, _ = strings.replace_all(r, "<", "<")
|
|
r, _ = strings.replace_all(r, ">", ">")
|
|
return r
|
|
}
|
|
|