package sitemap
import (
"context"
"fmt"
neturl "net/url"
"path"
"strings"
)
type EncodeToXmlInterface interface {
EncodeToXml(ctx context.Context) string
}
func (s SiteMapBuilder) EncodeToXml(ctx context.Context) string {
b := strings.Builder{}
b.WriteString(``)
b.WriteString(``)
var hostWithScheme string
if h, ok := ctx.Value(hostWithSchemeKey).(string); ok {
hostWithScheme = h
}
var urls = make([]URL, len(s.urls))
copy(urls, s.urls)
for _, contextfunc := range s.contextFuncs {
urls = append(urls, contextfunc(ctx)...)
}
for _, model := range s.models {
urls = append(urls, model.Sitemap(ctx)...)
}
for _, url := range urls {
u, err := neturl.Parse(url.Loc)
if err != nil {
continue
}
if u.Host == "" {
url.Loc = path.Join(hostWithScheme, url.Loc)
}
b.WriteString(``)
b.WriteString(fmt.Sprintf(`%s`, url.Loc))
if url.LastMod != "" {
b.WriteString(fmt.Sprintf(`%s`, url.LastMod))
}
if url.Changefreq != "" {
b.WriteString(fmt.Sprintf(`%s`, url.Changefreq))
}
if url.Priority != 0.0 {
b.WriteString(fmt.Sprintf(`%f`, url.Priority))
}
b.WriteString(``)
}
b.WriteString(``)
return b.String()
}
func (s SiteMapIndexBuilder) EncodeToXml(ctx context.Context) string {
b := strings.Builder{}
b.WriteString(``)
b.WriteString(``)
for _, site := range s.siteMaps {
b.WriteString(``)
b.WriteString(fmt.Sprintf(`%s`, site.ToUrl(ctx)))
b.WriteString(``)
}
b.WriteString(``)
return b.String()
}