feat(web): Go W-G7 RSS, sitemap, redirect, and SEO bags
Add paginate/meta wiring, RSS and sitemap builders, and app redirect/error/sitemap/robots route bags so content and W7 offline smokes pass on libweb.so. Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
1 parent
fe18c4d1d6
commit
23e4bc406e
11 files changed
+758
-1
No files matched your search
@@ -0,0 +1,83 @@
|
||||
// Package rss builds RSS 2.0 feeds (content-site W4 / W-G7).
|
||||
package rss
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func esc(s string) string {
|
||||
s = strings.ReplaceAll(s, "&", "&")
|
||||
s = strings.ReplaceAll(s, "<", "<")
|
||||
s = strings.ReplaceAll(s, ">", ">")
|
||||
s = strings.ReplaceAll(s, `"`, """)
|
||||
return s
|
||||
}
|
||||
|
||||
func cellStr(v any) string {
|
||||
switch t := v.(type) {
|
||||
case nil:
|
||||
return ""
|
||||
case string:
|
||||
return t
|
||||
case bool:
|
||||
return strconv.FormatBool(t)
|
||||
case float64:
|
||||
if t == float64(int64(t)) {
|
||||
return strconv.FormatInt(int64(t), 10)
|
||||
}
|
||||
return strconv.FormatFloat(t, 'f', -1, 64)
|
||||
default:
|
||||
return fmt.Sprint(t)
|
||||
}
|
||||
}
|
||||
|
||||
func pick(m map[string]any, keys ...string) any {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildRSS assembles RSS 2.0 XML from channel metadata and row maps.
|
||||
func BuildRSS(title, link, description string, items []any) string {
|
||||
body := fmt.Sprintf(`<?xml version="1.0" encoding="UTF-8"?>
|
||||
<rss version="2.0"><channel>
|
||||
<title>%s</title>
|
||||
<link>%s</link>
|
||||
<description>%s</description>
|
||||
`, esc(title), esc(link), esc(description))
|
||||
for _, row := range items {
|
||||
m, ok := row.(map[string]any)
|
||||
if !ok {
|
||||
continue
|
||||
}
|
||||
itemTitle := cellStr(pick(m, "title", "标题"))
|
||||
itemLink := cellStr(pick(m, "link", "href", "链接", "slug"))
|
||||
itemDesc := cellStr(pick(m, "description", "summary", "摘要", "body", "正文"))
|
||||
pubDate := cellStr(pick(m, "pubDate", "published", "created_at", "发布"))
|
||||
fullLink := link
|
||||
if itemLink != "" {
|
||||
if strings.HasPrefix(itemLink, "http") {
|
||||
fullLink = itemLink
|
||||
} else if itemLink[0] == '/' {
|
||||
fullLink = link + itemLink
|
||||
} else {
|
||||
fullLink = link + "/" + itemLink
|
||||
}
|
||||
}
|
||||
body += "<item>"
|
||||
body += fmt.Sprintf("<title>%s</title>", esc(itemTitle))
|
||||
body += fmt.Sprintf("<link>%s</link>", esc(fullLink))
|
||||
body += fmt.Sprintf("<description>%s</description>", esc(itemDesc))
|
||||
if pubDate != "" {
|
||||
body += fmt.Sprintf("<pubDate>%s</pubDate>", esc(pubDate))
|
||||
}
|
||||
body += "</item>"
|
||||
}
|
||||
body += "</channel></rss>"
|
||||
return body
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package rss
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildRSSContainsItem(t *testing.T) {
|
||||
items := []any{
|
||||
map[string]any{"title": "Hi", "slug": "/post/a", "summary": "text"},
|
||||
}
|
||||
xml := BuildRSS("Blog", "http://example.com", "Feed", items)
|
||||
if !strings.Contains(xml, "<rss") {
|
||||
t.Fatalf("missing rss root: %s", xml)
|
||||
}
|
||||
if !strings.Contains(xml, "<title>Hi</title>") {
|
||||
t.Fatalf("missing item title: %s", xml)
|
||||
}
|
||||
if !strings.Contains(xml, "http://example.com/post/a") {
|
||||
t.Fatalf("missing full link: %s", xml)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user