Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Separate additional headers in sitemap definition #50

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module github.com/ikeikeikeike/go-sitemap-generator/v2
module github.com/aliworkshop/go-sitemap-generator/v2

go 1.9

Expand Down
27 changes: 26 additions & 1 deletion stm/builder_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,37 @@ func (b *BuilderFile) Content() []byte {

// XMLContent will return an XML of the sitemap built
func (b *BuilderFile) XMLContent() []byte {
c := bytes.Join(bytes.Fields(XMLHeader), []byte(" "))
c := bytes.Join(bytes.Fields(b.XMLHeader()), []byte(" "))
c = append(append(c, b.Content()...), XMLFooter...)

return c
}

// XMLHeader will return an XML of the sitemap additional headers
func (b *BuilderFile) XMLHeader() []byte {
xmlHeader := NewXmlHeaderGenerator()
if b.opts.image {
xmlHeader.WithImage()
}
if b.opts.video {
xmlHeader.WithVideo()
}
if b.opts.geo {
xmlHeader.WithGeo()
}
if b.opts.news {
xmlHeader.WithNews()
}
if b.opts.mobile {
xmlHeader.WithMobile()
}
if b.opts.pagemap {
xmlHeader.WithPageMap()
}

return xmlHeader.Generate()
}

// Write will write pooled bytes with header and footer to
// Location path for output sitemap file.
func (b *BuilderFile) Write() {
Expand Down
8 changes: 6 additions & 2 deletions stm/builder_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,18 @@ func (su *sitemapURL) XML() []byte {
lastmod := url.CreateElement("lastmod")
lastmod.SetText(time.Now().Format(time.RFC3339))
}
if _, ok := SetBuilderElementValue(url, su.data, "changefreq"); !ok {

/**
Google ignores <priority> and <changefreq> values
*/
/*if _, ok := SetBuilderElementValue(url, su.data, "changefreq"); !ok {
changefreq := url.CreateElement("changefreq")
changefreq.SetText("weekly")
}
if _, ok := SetBuilderElementValue(url, su.data, "priority"); !ok {
priority := url.CreateElement("priority")
priority.SetText("0.5")
}
}*/
SetBuilderElementValue(url, su.data, "expires")
SetBuilderElementValue(url, su.data, "mobile")
SetBuilderElementValue(url, su.data, "news")
Expand Down
72 changes: 57 additions & 15 deletions stm/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,63 @@ var (
)

var (
// XMLHeader exists for create sitemap xml as a specific sitemap document.
XMLHeader = []byte(`<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd"
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
xmlns:image="` + SchemaImage + `"
xmlns:video="` + SchemaVideo + `"
xmlns:geo="` + SchemaGeo + `"
xmlns:news="` + SchemaNews + `"
xmlns:mobile="` + SchemaMobile + `"
xmlns:pagemap="` + SchemaPagemap + `"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
>`)
XMLImageHeader = `xmlns:image="` + SchemaImage + `" `
XMLVideoHeader = `xmlns:video="` + SchemaVideo + `" `
XMLGeoHeader = `xmlns:geo="` + SchemaGeo + `" `
XMLNewsHeader = `xmlns:news="` + SchemaNews + `" `
XMLMobileHeader = `xmlns:mobile="` + SchemaMobile + `" `
XMLPageMapHeader = `xmlns:pagemap="` + SchemaPagemap + `" `
)

var (
// XMLFooter and XMLHeader will used from user together .
XMLFooter = []byte("</urlset>")
)

type HeaderGenerator struct {
xml string `json:"xml"`
}

func NewXmlHeaderGenerator() *HeaderGenerator {
return &HeaderGenerator{
xml: `<?xml version="1.0" encoding="UTF-8"?>
<urlset
xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" `,
}
}

func (h *HeaderGenerator) WithImage() *HeaderGenerator {
h.xml += XMLImageHeader
return h
}

func (h *HeaderGenerator) WithVideo() *HeaderGenerator {
h.xml += XMLVideoHeader
return h
}

func (h *HeaderGenerator) WithGeo() *HeaderGenerator {
h.xml += XMLGeoHeader
return h
}

func (h *HeaderGenerator) WithNews() *HeaderGenerator {
h.xml += XMLNewsHeader
return h
}

func (h *HeaderGenerator) WithMobile() *HeaderGenerator {
h.xml += XMLMobileHeader
return h
}

func (h *HeaderGenerator) WithPageMap() *HeaderGenerator {
h.xml += XMLPageMapHeader
return h
}

func (h *HeaderGenerator) Generate() []byte {
h.xml += `xmlns:xhtml="http://www.w3.org/1999/xhtml"
>`
return []byte(h.xml)
}
36 changes: 36 additions & 0 deletions stm/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ type Options struct {
adp Adapter
nmr *Namer
loc *Location
image bool
video bool
geo bool
news bool
mobile bool
pagemap bool
}

// SetDefaultHost sets that arg from Sitemap.Finalize method
Expand Down Expand Up @@ -76,6 +82,36 @@ func (opts *Options) SetAdapter(adp Adapter) {
opts.adp = adp
}

// SetImage inject image header in xml additional headers
func (opts *Options) SetImage(image bool) {
opts.image = image
}

// SetVideo inject video header in xml additional headers
func (opts *Options) SetVideo(video bool) {
opts.video = video
}

// SetGeo inject video header in xml additional headers
func (opts *Options) SetGeo(geo bool) {
opts.geo = geo
}

// SetNews inject news header in xml additional headers
func (opts *Options) SetNews(news bool) {
opts.news = news
}

// SetMobile inject mobile header in xml additional headers
func (opts *Options) SetMobile(mobile bool) {
opts.mobile = mobile
}

// SetPageMap inject pagemap header in xml additional headers
func (opts *Options) SetPageMap(pagemap bool) {
opts.pagemap = pagemap
}

// SitemapsHost sets that arg from Sitemap.SitemapsHost method
func (opts *Options) SitemapsHost() string {
if opts.sitemapsHost != "" {
Expand Down
30 changes: 30 additions & 0 deletions stm/sitemap.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,36 @@ func (sm *Sitemap) SetFilename(filename string) {
sm.opts.SetFilename(filename)
}

// SetImage option allows injecting image header in output file.
func (sm *Sitemap) SetImage(image bool) {
sm.opts.SetImage(image)
}

// SetVideo option allows injecting video header in output file.
func (sm *Sitemap) SetVideo(video bool) {
sm.opts.SetVideo(video)
}

// SetGeo option allows injecting geo header in output file.
func (sm *Sitemap) SetGeo(geo bool) {
sm.opts.SetGeo(geo)
}

// SetNews option allows injecting news header in output file.
func (sm *Sitemap) SetNews(news bool) {
sm.opts.SetNews(news)
}

// SetMobile option allows injecting mobile header in output file.
func (sm *Sitemap) SetMobile(mobile bool) {
sm.opts.SetMobile(mobile)
}

// SetPageMap option allows injecting pagemap header in output file.
func (sm *Sitemap) SetPageMap(pagemap bool) {
sm.opts.SetPageMap(pagemap)
}

// Create method must be that calls first this method in that before call to Add method on this struct.
func (sm *Sitemap) Create() *Sitemap {
sm.bldrs = NewBuilderIndexfile(sm.opts, sm.opts.IndexLocation())
Expand Down