mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: generate_rss and generate_sitemap now use string builders.
This commit is contained in:
@@ -5,13 +5,10 @@ import "core:strings"
|
|||||||
import "core:time"
|
import "core:time"
|
||||||
|
|
||||||
generate_rss :: proc(pages: []Page, config: Site) -> string {
|
generate_rss :: proc(pages: []Page, config: Site) -> string {
|
||||||
parts: [dynamic]string
|
sb := strings.builder_make()
|
||||||
defer delete(parts)
|
|
||||||
|
|
||||||
append(
|
strings.write_string(&sb, fmt.aprintf(
|
||||||
&parts,
|
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||||
fmt.aprintf(
|
|
||||||
`<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
|
||||||
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
|
||||||
<channel>
|
<channel>
|
||||||
<title>%s</title>
|
<title>%s</title>
|
||||||
@@ -19,12 +16,11 @@ generate_rss :: proc(pages: []Page, config: Site) -> string {
|
|||||||
<description>%s</description>
|
<description>%s</description>
|
||||||
<language>en-us</language>
|
<language>en-us</language>
|
||||||
<atom:link href="%s/index.xml" rel="self" type="application/rss+xml"/>`,
|
<atom:link href="%s/index.xml" rel="self" type="application/rss+xml"/>`,
|
||||||
xml_escape(config.title),
|
xml_escape(config.title),
|
||||||
config.base_url,
|
config.base_url,
|
||||||
xml_escape(config.description),
|
xml_escape(config.description),
|
||||||
config.base_url,
|
config.base_url,
|
||||||
),
|
))
|
||||||
)
|
|
||||||
|
|
||||||
for page in pages {
|
for page in pages {
|
||||||
if page.type == .Home {
|
if page.type == .Home {
|
||||||
@@ -36,10 +32,8 @@ generate_rss :: proc(pages: []Page, config: Site) -> string {
|
|||||||
pub_date = format_rfc822(page.date)
|
pub_date = format_rfc822(page.date)
|
||||||
}
|
}
|
||||||
|
|
||||||
append(
|
strings.write_string(&sb, fmt.aprintf(
|
||||||
&parts,
|
`<item>
|
||||||
fmt.aprintf(
|
|
||||||
`<item>
|
|
||||||
<title>%s</title>
|
<title>%s</title>
|
||||||
<link>%s%s</link>
|
<link>%s%s</link>
|
||||||
<pubDate>%s</pubDate>
|
<pubDate>%s</pubDate>
|
||||||
@@ -47,42 +41,35 @@ generate_rss :: proc(pages: []Page, config: Site) -> string {
|
|||||||
<description>%s</description>
|
<description>%s</description>
|
||||||
</item>
|
</item>
|
||||||
`,
|
`,
|
||||||
xml_escape(page.title),
|
xml_escape(page.title),
|
||||||
config.base_url,
|
config.base_url,
|
||||||
page.permalink,
|
page.permalink,
|
||||||
pub_date,
|
pub_date,
|
||||||
config.base_url,
|
config.base_url,
|
||||||
page.permalink,
|
page.permalink,
|
||||||
xml_escape(page.body_html),
|
xml_escape(page.body_html),
|
||||||
),
|
))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
append(&parts, "</channel>\n</rss>")
|
strings.write_string(&sb, "</channel>\n</rss>")
|
||||||
|
return strings.to_string(sb)
|
||||||
return strings.join(parts[:], "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
generate_sitemap :: proc(pages: []Page, base_url: string) -> string {
|
generate_sitemap :: proc(pages: []Page, base_url: string) -> string {
|
||||||
parts: [dynamic]string
|
sb := strings.builder_make()
|
||||||
defer delete(parts)
|
|
||||||
|
|
||||||
append(
|
strings.write_string(&sb, `<?xml version="1.0" encoding="utf-8" standalone="yes"?>
|
||||||
&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">
|
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
|
||||||
`,
|
`)
|
||||||
)
|
|
||||||
|
|
||||||
for page in pages {
|
for page in pages {
|
||||||
lastmod := ""
|
lastmod := ""
|
||||||
if page.date != "" {
|
if page.date != "" {
|
||||||
lastmod = fmt.aprintf("<lastmod>%s</lastmod>", page.date)
|
lastmod = fmt.aprintf("<lastmod>%s</lastmod>", page.date)
|
||||||
}
|
}
|
||||||
append(
|
strings.write_string(&sb, fmt.aprintf(
|
||||||
&parts,
|
"<url><loc>%s%s</loc>%s</url>\n", base_url, page.permalink, lastmod,
|
||||||
fmt.aprintf("<url><loc>%s%s</loc>%s</url>\n", base_url, page.permalink, lastmod),
|
))
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Posts list page
|
// Posts list page
|
||||||
@@ -96,11 +83,12 @@ generate_sitemap :: proc(pages: []Page, base_url: string) -> string {
|
|||||||
if posts_lastmod != "" {
|
if posts_lastmod != "" {
|
||||||
posts_lm = fmt.aprintf("<lastmod>%s</lastmod>", 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))
|
strings.write_string(&sb, fmt.aprintf(
|
||||||
|
"<url><loc>%s/posts/</loc>%s</url>\n", base_url, posts_lm,
|
||||||
|
))
|
||||||
|
|
||||||
append(&parts, "</urlset>")
|
strings.write_string(&sb, "</urlset>")
|
||||||
|
return strings.to_string(sb)
|
||||||
return strings.join(parts[:], "")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: Leaks
|
// TODO: Leaks
|
||||||
|
|||||||
Reference in New Issue
Block a user