mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
feaat: Simplified the template loading code.
This commit is contained in:
@@ -7,8 +7,10 @@ import "core:time"
|
||||
generate_rss :: proc(site: ^Site) -> string {
|
||||
sb := strings.builder_make()
|
||||
|
||||
strings.write_string(&sb, fmt.aprintf(
|
||||
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||
strings.write_string(
|
||||
&sb,
|
||||
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>
|
||||
@@ -16,14 +18,15 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
<description>%s</description>
|
||||
<language>en-us</language>
|
||||
<atom:link href="%s/index.xml" rel="self" type="application/rss+xml"/>`,
|
||||
xml_escape(site.title),
|
||||
site.base_url,
|
||||
xml_escape(site.description),
|
||||
site.base_url,
|
||||
))
|
||||
xml_escape(site.title),
|
||||
site.base_url,
|
||||
xml_escape(site.description),
|
||||
site.base_url,
|
||||
),
|
||||
)
|
||||
|
||||
for page in site.pages {
|
||||
if page.type == .Home {
|
||||
if page.section == "" && page._is_index {
|
||||
continue
|
||||
}
|
||||
|
||||
@@ -32,8 +35,10 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
pub_date = format_rfc822(page.date)
|
||||
}
|
||||
|
||||
strings.write_string(&sb, fmt.aprintf(
|
||||
`<item>
|
||||
strings.write_string(
|
||||
&sb,
|
||||
fmt.aprintf(
|
||||
`<item>
|
||||
<title>%s</title>
|
||||
<link>%s%s</link>
|
||||
<pubDate>%s</pubDate>
|
||||
@@ -41,14 +46,15 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
<description>%s</description>
|
||||
</item>
|
||||
`,
|
||||
xml_escape(page.title),
|
||||
site.base_url,
|
||||
page.permalink,
|
||||
pub_date,
|
||||
site.base_url,
|
||||
page.permalink,
|
||||
xml_escape(page.body_html),
|
||||
))
|
||||
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, "</channel>\n</rss>")
|
||||
@@ -58,34 +64,59 @@ generate_rss :: proc(site: ^Site) -> string {
|
||||
generate_sitemap :: proc(site: ^Site) -> string {
|
||||
sb := strings.builder_make()
|
||||
|
||||
strings.write_string(&sb, `<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||
strings.write_string(
|
||||
&sb,
|
||||
`<?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 site.pages {
|
||||
lastmod := ""
|
||||
if page.date != "" {
|
||||
lastmod = fmt.aprintf("<lastmod>%s</lastmod>", page.date)
|
||||
}
|
||||
strings.write_string(&sb, fmt.aprintf(
|
||||
"<url><loc>%s%s</loc>%s</url>\n", site.base_url, page.permalink, lastmod,
|
||||
))
|
||||
strings.write_string(
|
||||
&sb,
|
||||
fmt.aprintf("<url><loc>%s%s</loc>%s</url>\n", site.base_url, page.permalink, lastmod),
|
||||
)
|
||||
}
|
||||
|
||||
// Posts list page
|
||||
posts_lastmod := ""
|
||||
// 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.type == .Post && page.date > posts_lastmod {
|
||||
posts_lastmod = page.date
|
||||
if page.section != "" && !page._is_index {
|
||||
sections[page.section] = true
|
||||
}
|
||||
}
|
||||
posts_lm := ""
|
||||
if posts_lastmod != "" {
|
||||
posts_lm = fmt.aprintf("<lastmod>%s</lastmod>", posts_lastmod)
|
||||
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
|
||||
}
|
||||
}
|
||||
lm := ""
|
||||
if section_lastmod != "" {
|
||||
lm = fmt.aprintf("<lastmod>%s</lastmod>", section_lastmod)
|
||||
}
|
||||
strings.write_string(
|
||||
&sb,
|
||||
fmt.aprintf("<url><loc>%s/%s/</loc>%s</url>\n", site.base_url, section, lm),
|
||||
)
|
||||
}
|
||||
strings.write_string(&sb, fmt.aprintf(
|
||||
"<url><loc>%s/posts/</loc>%s</url>\n", site.base_url, posts_lm,
|
||||
))
|
||||
|
||||
strings.write_string(&sb, "</urlset>")
|
||||
return strings.to_string(sb)
|
||||
|
||||
Reference in New Issue
Block a user