package main
import "core:fmt"
import "core:strings"
import "core:time"
generate_rss :: proc(site: ^Site) -> string {
sb := strings.builder_make()
strings.write_string(&sb, fmt.aprintf(
`
%s
%s/
%s
en-us
`,
xml_escape(site.title),
site.base_url,
xml_escape(site.description),
site.base_url,
))
for page in site.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)
}
strings.write_string(&sb, fmt.aprintf(
`-
%s
%s%s
%s
%s%s
%s
`,
xml_escape(page.title),
site.base_url,
page.permalink,
pub_date,
site.base_url,
page.permalink,
xml_escape(page.body_html),
))
}
strings.write_string(&sb, "\n")
return strings.to_string(sb)
}
generate_sitemap :: proc(site: ^Site) -> string {
sb := strings.builder_make()
strings.write_string(&sb, `
`)
for page in site.pages {
lastmod := ""
if page.date != "" {
lastmod = fmt.aprintf("%s", page.date)
}
strings.write_string(&sb, fmt.aprintf(
"%s%s%s\n", site.base_url, page.permalink, lastmod,
))
}
// Posts list page
posts_lastmod := ""
for page in site.pages {
if page.type == .Post && page.date > posts_lastmod {
posts_lastmod = page.date
}
}
posts_lm := ""
if posts_lastmod != "" {
posts_lm = fmt.aprintf("%s", posts_lastmod)
}
strings.write_string(&sb, fmt.aprintf(
"%s/posts/%s\n", site.base_url, posts_lm,
))
strings.write_string(&sb, "")
return strings.to_string(sb)
}
// 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
}