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
@@ -545,6 +545,124 @@ func Gallery(appBag map[string]any, path, storageURL, prefix, title, downloadBas
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Redirect registers a redirect from → to on app.redirects.
|
||||
func Redirect(appBag map[string]any, from, to string, permanent bool) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
from, err := NormalizeRoutePath(from, out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
redirects := map[string]any{}
|
||||
if r, ok := out["redirects"].(map[string]any); ok {
|
||||
redirects = clone(r)
|
||||
}
|
||||
redirects[from] = map[string]any{
|
||||
"to": to,
|
||||
"permanent": permanent,
|
||||
}
|
||||
out["redirects"] = redirects
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// ErrorPage stores a custom page for HTTP 404 or 500.
|
||||
func ErrorPage(appBag map[string]any, status uint16, page any) (map[string]any, error) {
|
||||
if page == nil {
|
||||
return nil, fmt.Errorf("missing `page`")
|
||||
}
|
||||
out := clone(appBag)
|
||||
key := "page_404"
|
||||
if status == 500 {
|
||||
key = "page_500"
|
||||
}
|
||||
out[key] = page
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Sitemap registers a sitemap route bag on app.sitemap_routes.
|
||||
func Sitemap(appBag map[string]any, path, base string, table, loc string, limit int64, items any) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
if path == "" {
|
||||
path = "/sitemap.xml"
|
||||
}
|
||||
path, err := NormalizeRoutePath(path, out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if loc == "" {
|
||||
loc = "path"
|
||||
}
|
||||
if items == nil {
|
||||
items = []any{}
|
||||
}
|
||||
routes := map[string]any{}
|
||||
if r, ok := out["sitemap_routes"].(map[string]any); ok {
|
||||
routes = clone(r)
|
||||
}
|
||||
entry := map[string]any{
|
||||
"base": base,
|
||||
"loc": loc,
|
||||
"limit": float64(limit),
|
||||
"items": items,
|
||||
}
|
||||
if table != "" {
|
||||
entry["table"] = table
|
||||
}
|
||||
routes[path] = entry
|
||||
out["sitemap_routes"] = routes
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Robots sets app.robots_body from body or a default Allow-all robots.txt.
|
||||
func Robots(appBag map[string]any, body, sitemapURL string) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
text := strings.TrimSpace(body)
|
||||
if text == "" {
|
||||
text = defaultRobotsBody(sitemapURL)
|
||||
}
|
||||
out["robots_body"] = text
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func defaultRobotsBody(sitemapURL string) string {
|
||||
out := "User-agent: *\nAllow: /\n"
|
||||
if strings.TrimSpace(sitemapURL) != "" {
|
||||
out += "Sitemap: " + strings.TrimSpace(sitemapURL) + "\n"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// RouteRSS registers an RSS feed route on app.rss_routes.
|
||||
func RouteRSS(appBag map[string]any, path, table, order, title, link, description string, limit int64) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
path, err := NormalizeRoutePath(path, out)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if order == "" {
|
||||
order = "-created_at"
|
||||
}
|
||||
if title == "" {
|
||||
title = "Feed"
|
||||
}
|
||||
if link == "" {
|
||||
link = "/"
|
||||
}
|
||||
routes := map[string]any{}
|
||||
if r, ok := out["rss_routes"].(map[string]any); ok {
|
||||
routes = clone(r)
|
||||
}
|
||||
routes[path] = map[string]any{
|
||||
"table": table,
|
||||
"limit": float64(limit),
|
||||
"order": order,
|
||||
"title": title,
|
||||
"link": link,
|
||||
"description": description,
|
||||
}
|
||||
out["rss_routes"] = routes
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// Icons normalizes icons table → app.icons + app.site_head.
|
||||
func Icons(appBag map[string]any, table any) (map[string]any, error) {
|
||||
out := clone(appBag)
|
||||
|
||||
@@ -161,3 +161,61 @@ func TestGateCustomFields(t *testing.T) {
|
||||
t.Fatalf("%v", g1)
|
||||
}
|
||||
}
|
||||
|
||||
func TestRedirectStoresMap(t *testing.T) {
|
||||
a := app.New(map[string]any{"page": map[string]any{"title": "Home"}})
|
||||
out, err := app.Redirect(a, "/old", "/new", true)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rd, ok := out["redirects"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("redirects=%v", out["redirects"])
|
||||
}
|
||||
entry, ok := rd["/old"].(map[string]any)
|
||||
if !ok || entry["to"] != "/new" || entry["permanent"] != true {
|
||||
t.Fatalf("entry=%v", entry)
|
||||
}
|
||||
}
|
||||
|
||||
func TestErrorPage404(t *testing.T) {
|
||||
a := app.New(nil)
|
||||
p := map[string]any{"title": "Missing"}
|
||||
out, err := app.ErrorPage(a, 404, p)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
got, ok := out["page_404"].(map[string]any)
|
||||
if !ok || got["title"] != "Missing" {
|
||||
t.Fatalf("page_404=%v", out["page_404"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestRobotsDefaultBody(t *testing.T) {
|
||||
a := app.New(nil)
|
||||
out, err := app.Robots(a, "", "https://example.com/sitemap.xml")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
rb, ok := out["robots_body"].(string)
|
||||
if !ok || !strings.Contains(rb, "Sitemap: https://example.com/sitemap.xml") {
|
||||
t.Fatalf("robots_body=%v", out["robots_body"])
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitemapRouteBag(t *testing.T) {
|
||||
a := app.New(nil)
|
||||
items := []any{map[string]any{"loc": "/"}}
|
||||
out, err := app.Sitemap(a, "/sitemap.xml", "https://example.com", "", "path", 1000, items)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
routes, ok := out["sitemap_routes"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("sitemap_routes=%v", out["sitemap_routes"])
|
||||
}
|
||||
entry, ok := routes["/sitemap.xml"].(map[string]any)
|
||||
if !ok || entry["base"] != "https://example.com" {
|
||||
t.Fatalf("entry=%v", entry)
|
||||
}
|
||||
}
|
||||
@@ -22,6 +22,23 @@ func New(args map[string]any) map[string]any {
|
||||
return out
|
||||
}
|
||||
|
||||
// Paginate stamps page.paginate with offset/limit/path (Rust web_page_paginate).
|
||||
func Paginate(page map[string]any, offset, limit int64, path string) map[string]any {
|
||||
out := map[string]any{}
|
||||
for k, v := range page {
|
||||
out[k] = v
|
||||
}
|
||||
if path == "" {
|
||||
path = "/"
|
||||
}
|
||||
out["paginate"] = map[string]any{
|
||||
"offset": offset,
|
||||
"limit": limit,
|
||||
"path": path,
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func strOpt(args map[string]any, key, def string) string {
|
||||
if s, ok := strAny(args, key); ok {
|
||||
return s
|
||||
|
||||
@@ -27,3 +27,15 @@ func TestNewZhAliases(t *testing.T) {
|
||||
t.Fatalf("%v", p)
|
||||
}
|
||||
}
|
||||
|
||||
func TestPaginateBag(t *testing.T) {
|
||||
p := page.New(map[string]any{"title": "List"})
|
||||
out := page.Paginate(p, 0, 5, "/")
|
||||
pg, ok := out["paginate"].(map[string]any)
|
||||
if !ok {
|
||||
t.Fatalf("paginate=%v", out["paginate"])
|
||||
}
|
||||
if pg["offset"] != int64(0) || pg["limit"] != int64(5) || pg["path"] != "/" {
|
||||
t.Fatalf("%v", pg)
|
||||
}
|
||||
}
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
// Package sitemap builds sitemap.xml and robots.txt helpers (W7 / W-G7).
|
||||
package sitemap
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
)
|
||||
|
||||
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 xmlEscape(s string) string {
|
||||
r := strings.NewReplacer(
|
||||
"&", "&",
|
||||
"<", "<",
|
||||
">", ">",
|
||||
`"`, """,
|
||||
"'", "'",
|
||||
)
|
||||
return r.Replace(s)
|
||||
}
|
||||
|
||||
func rowsFromItems(items any) []map[string]any {
|
||||
switch t := items.(type) {
|
||||
case []any:
|
||||
out := make([]map[string]any, 0, len(t))
|
||||
for _, row := range t {
|
||||
switch r := row.(type) {
|
||||
case map[string]any:
|
||||
out = append(out, r)
|
||||
case string:
|
||||
out = append(out, map[string]any{"loc": r})
|
||||
default:
|
||||
out = append(out, map[string]any{"loc": cellStr(r)})
|
||||
}
|
||||
}
|
||||
return out
|
||||
case map[string]any:
|
||||
locs, ok := firstArray(t, "loc", "路径", "path", "url")
|
||||
if !ok {
|
||||
return nil
|
||||
}
|
||||
out := make([]map[string]any, 0, len(locs))
|
||||
for i, loc := range locs {
|
||||
obj := map[string]any{"loc": loc}
|
||||
for k, v := range t {
|
||||
if k == "loc" || k == "路径" || k == "path" || k == "url" {
|
||||
continue
|
||||
}
|
||||
if col, ok := v.([]any); ok && i < len(col) {
|
||||
obj[k] = col[i]
|
||||
}
|
||||
}
|
||||
out = append(out, obj)
|
||||
}
|
||||
return out
|
||||
default:
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func firstArray(m map[string]any, keys ...string) ([]any, bool) {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
if a, ok := v.([]any); ok {
|
||||
return a, true
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil, false
|
||||
}
|
||||
|
||||
// BuildSitemap builds a sitemap.xml document from rows.
|
||||
func BuildSitemap(base string, items any) string {
|
||||
base = strings.TrimRight(base, "/")
|
||||
rows := rowsFromItems(items)
|
||||
out := `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
`
|
||||
for _, obj := range rows {
|
||||
locRaw := cellStr(first(obj, "loc", "url", "路径", "path"))
|
||||
if locRaw == "" {
|
||||
continue
|
||||
}
|
||||
loc := locRaw
|
||||
if strings.HasPrefix(locRaw, "http://") || strings.HasPrefix(locRaw, "https://") {
|
||||
loc = locRaw
|
||||
} else if strings.HasPrefix(locRaw, "/") {
|
||||
loc = base + locRaw
|
||||
} else {
|
||||
loc = base + "/" + locRaw
|
||||
}
|
||||
out += " <url>\n"
|
||||
out += fmt.Sprintf(" <loc>%s</loc>\n", xmlEscape(loc))
|
||||
if lm := cellStr(first(obj, "lastmod", "更新")); lm != "" {
|
||||
out += fmt.Sprintf(" <lastmod>%s</lastmod>\n", xmlEscape(lm))
|
||||
}
|
||||
if cf := cellStr(first(obj, "changefreq", "频率")); cf != "" {
|
||||
out += fmt.Sprintf(" <changefreq>%s</changefreq>\n", xmlEscape(cf))
|
||||
}
|
||||
if pr := cellStr(first(obj, "priority", "优先级")); pr != "" {
|
||||
out += fmt.Sprintf(" <priority>%s</priority>\n", xmlEscape(pr))
|
||||
}
|
||||
out += " </url>\n"
|
||||
}
|
||||
out += "</urlset>\n"
|
||||
return out
|
||||
}
|
||||
|
||||
func first(m map[string]any, keys ...string) any {
|
||||
for _, k := range keys {
|
||||
if v, ok := m[k]; ok {
|
||||
return v
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// BuildRobots returns a default robots.txt body with optional Sitemap line.
|
||||
func BuildRobots(sitemapURL string) string {
|
||||
out := "User-agent: *\nAllow: /\n"
|
||||
if strings.TrimSpace(sitemapURL) != "" {
|
||||
out += "Sitemap: " + strings.TrimSpace(sitemapURL) + "\n"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// SitemapJSON is the ABI helper returning {"xml":"…"}.
|
||||
func SitemapJSON(base string, items any) map[string]any {
|
||||
return map[string]any{"xml": BuildSitemap(base, items)}
|
||||
}
|
||||
@@ -0,0 +1,40 @@
|
||||
package sitemap
|
||||
|
||||
import (
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestBuildSitemapRowTable(t *testing.T) {
|
||||
items := []any{
|
||||
map[string]any{"loc": "/", "更新": "2026-08-28"},
|
||||
map[string]any{"loc": "/about", "更新": "2026-08-28"},
|
||||
}
|
||||
xml := BuildSitemap("https://example.com", items)
|
||||
if !strings.Contains(xml, "https://example.com/") {
|
||||
t.Fatalf("missing home loc: %s", xml)
|
||||
}
|
||||
if !strings.Contains(xml, "https://example.com/about") {
|
||||
t.Fatalf("missing about loc: %s", xml)
|
||||
}
|
||||
if !strings.Contains(xml, "<lastmod>2026-08-28</lastmod>") {
|
||||
t.Fatalf("missing lastmod: %s", xml)
|
||||
}
|
||||
}
|
||||
|
||||
func TestBuildRobotsWithSitemap(t *testing.T) {
|
||||
body := BuildRobots("https://example.com/sitemap.xml")
|
||||
if !strings.Contains(body, "User-agent: *") {
|
||||
t.Fatalf("missing user-agent: %s", body)
|
||||
}
|
||||
if !strings.Contains(body, "Sitemap: https://example.com/sitemap.xml") {
|
||||
t.Fatalf("missing sitemap line: %s", body)
|
||||
}
|
||||
}
|
||||
|
||||
func TestSitemapJSONShape(t *testing.T) {
|
||||
j := SitemapJSON("https://x.com", []any{map[string]any{"loc": "/a"}})
|
||||
if j["xml"] == nil || j["xml"] == "" {
|
||||
t.Fatalf("expected xml: %v", j)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user