package main
import "core:fmt"
import "core:strings"
import "core:time"
generate_rss :: proc(site: ^Site) -> string {
sb := strings.builder_make()
fmt.sbprintf(
&sb,
`
%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.section == "" && page._is_index {
continue
}
pub_date := "Mon, 01 Jan 0001 00:00:00 +0000"
if page.date != "" {
pub_date = format_rfc822(page.date)
}
fmt.sbprintf(
&sb,
`-
%s
%s
%s
%s
%s
`,
xml_escape(page.title),
page.url,
pub_date,
page.url,
xml_escape(page.content),
)
}
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 {
fmt.sbprintf(&sb, "%s", page.url)
if page.date != "" {
fmt.sbprintf(&sb, "%s", page.date)
}
fmt.sbprintf(&sb, "\n")
}
// Section index pages (for sections without an index in content)
sections := make(map[string]bool)
defer delete(sections)
for page in site.pages {
if page.section != "" && !page._is_index {
sections[page.section] = true
}
}
for section in sections {
has_index := false
for page in site.pages {
if page.section == section && page._is_index {
has_index = true
break
}
}
if has_index {
continue
}
section_lastmod := ""
for page in site.pages {
if page.section == section && !page._is_index && page.date > section_lastmod {
section_lastmod = page.date
}
}
fmt.sbprintf(&sb, "%s/%s/", site.base_url, section)
if section_lastmod != "" {
fmt.sbprintf(&sb, "%s", section_lastmod)
}
fmt.sbprintf(&sb, "\n")
}
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
}