Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
  • Loading branch information
porozhnyy committed Jan 30, 2023
1 parent c473e35 commit 3f9fe6e
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 29 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ _testmain.go
/public/
/requirements.txt
/tmp

.idea
4 changes: 2 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
module github.com/ikeikeikeike/go-sitemap-generator/v2
module github.com/porozhnyy/go-sitemap-generator

go 1.9

require (
github.com/beevik/etree v1.1.0
github.com/clbanning/mxj v1.8.3
github.com/clbanning/mxj/v2 v2.5.7
github.com/fatih/structs v1.1.0
)
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
github.com/beevik/etree v1.1.0 h1:T0xke/WvNtMoCqgzPhkX2r4rjY3GDZFi+FjpRZY2Jbs=
github.com/beevik/etree v1.1.0/go.mod h1:r8Aw8JqVegEf0w2fDnATrX9VpkMcyFeM0FhwO62wh+A=
github.com/clbanning/mxj v1.8.3 h1:2r/KCJi52w2MRz+K+UMa/1d7DdCjnLqYJfnbr7dYNWI=
github.com/clbanning/mxj v1.8.3/go.mod h1:BVjHeAH+rl9rs6f+QIpeRl0tfu10SXn1pUSa5PVGJng=
github.com/clbanning/mxj/v2 v2.5.7 h1:7q5lvUpaPF/WOkqgIDiwjBJaznaLCCBd78pi8ZyAnE0=
github.com/clbanning/mxj/v2 v2.5.7/go.mod h1:hNiWqW14h+kc+MdF9C6/YoRfjEJoR3ou6tn/Qo+ve2s=
github.com/fatih/structs v1.1.0 h1:Q7juDM0QtcnhCpeyLGQKyg4TOIghuNXrkL32pHAUMxo=
github.com/fatih/structs v1.1.0/go.mod h1:9NiDSp5zOcgEDl+j00MP/WkGVPOlPRLejGD8Ga6PJ7M=
6 changes: 3 additions & 3 deletions stm/builder_url.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,15 @@ func (su *sitemapURL) XML() []byte {
url := doc.CreateElement("url")

SetBuilderElementValue(url, su.data.URLJoinBy("loc", "host", "loc"), "loc")
if _, ok := SetBuilderElementValue(url, su.data, "lastmod"); !ok {
if _, ok := SetBuilderElementValue(url, su.data, "lastmod"); !ok && !su.opts.omitDefaultLastMod {
lastmod := url.CreateElement("lastmod")
lastmod.SetText(time.Now().Format(time.RFC3339))
}
if _, ok := SetBuilderElementValue(url, su.data, "changefreq"); !ok {
if _, ok := SetBuilderElementValue(url, su.data, "changefreq"); !ok && !su.opts.omitDefaultChangeFreq {
changefreq := url.CreateElement("changefreq")
changefreq.SetText("weekly")
}
if _, ok := SetBuilderElementValue(url, su.data, "priority"); !ok {
if _, ok := SetBuilderElementValue(url, su.data, "priority"); !ok && !su.opts.omitDefaultPriority {
priority := url.CreateElement("priority")
priority.SetText("0.5")
}
Expand Down
44 changes: 43 additions & 1 deletion stm/builder_url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (
"time"

"github.com/beevik/etree"
"github.com/clbanning/mxj"
"github.com/clbanning/mxj/v2"
)

func TestBlank(t *testing.T) {
Expand Down Expand Up @@ -448,6 +448,48 @@ func TestAttrWithoutTypedef(t *testing.T) {
}
}

func TestOmitDefaults(t *testing.T) {
opts := Options{}
opts.SetOmitDefaultLastMod(true)
opts.SetOmitDefaultPriority(true)
opts.SetOmitDefaultChangeFreq(true)

smu, err := NewSitemapURL(&opts, URL{{"loc", "path"}, {"host", "http://example.com"}})

if err != nil {
t.Fatalf(`Fatal to validate! This is a critical error: %v`, err)
}

doc := etree.NewDocument()
doc.ReadFromBytes(smu.XML())

var elm *etree.Element
url := doc.SelectElement("url")

elm = url.SelectElement("loc")
if elm == nil {
t.Errorf(`Failed to generate xml that loc element is blank: %v`, elm)
}
if elm != nil && elm.Text() != "http://example.com/path" {
t.Errorf(`Failed to generate xml thats deferrent value in loc element: %v`, elm.Text())
}

elm = url.SelectElement("priority")
if elm != nil {
t.Errorf(`Failed to generate xml that omits the default priority element: %v`, elm)
}

elm = url.SelectElement("changefreq")
if elm != nil {
t.Errorf(`Failed to generate xml that omits the default changefreq element: %v`, elm)
}

elm = url.SelectElement("lastmod")
if elm != nil {
t.Errorf(`Failed to generate xml that omits the default lastmod element: %v`, elm)
}
}

func BenchmarkGenerateXML(b *testing.B) {

b.ReportAllocs()
Expand Down
60 changes: 40 additions & 20 deletions stm/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,31 +4,37 @@ package stm
func NewOptions() *Options {
// Default values
return &Options{
defaultHost: "http://www.example.com",
sitemapsHost: "", // http://s3.amazonaws.com/sitemap-generator/,
publicPath: "public/",
sitemapsPath: "sitemaps/",
filename: "sitemap",
verbose: true,
compress: true,
pretty: false,
adp: NewFileAdapter(),
defaultHost: "http://www.example.com",
sitemapsHost: "", // http://s3.amazonaws.com/sitemap-generator/,
publicPath: "public/",
sitemapsPath: "sitemaps/",
filename: "sitemap",
verbose: true,
compress: true,
pretty: false,
adp: NewFileAdapter(),
omitDefaultLastMod: true,
omitDefaultChangeFreq: true,
omitDefaultPriority: true,
}
}

// Options exists for the Sitemap struct.
type Options struct {
defaultHost string
sitemapsHost string
publicPath string
sitemapsPath string
filename string
verbose bool
compress bool
pretty bool
adp Adapter
nmr *Namer
loc *Location
defaultHost string
sitemapsHost string
publicPath string
sitemapsPath string
filename string
verbose bool
compress bool
pretty bool
adp Adapter
nmr *Namer
loc *Location
omitDefaultLastMod bool
omitDefaultChangeFreq bool
omitDefaultPriority bool
}

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

func (opts *Options) SetOmitDefaultLastMod(omit bool) {
opts.omitDefaultLastMod = omit
}

// SetOmitDefaultChangeFreq controls whether to output a changefreq XML entity when none is provided in the URL builder
func (opts *Options) SetOmitDefaultChangeFreq(omit bool) {
opts.omitDefaultChangeFreq = omit
}

// SetOmitDefaultPriority controls whether to output a Priority XML entity when none is provided in the URL builder
func (opts *Options) SetOmitDefaultPriority(omit bool) {
opts.omitDefaultPriority = omit
}

// SitemapsHost sets that arg from Sitemap.SitemapsHost method
func (opts *Options) SitemapsHost() string {
if opts.sitemapsHost != "" {
Expand Down
2 changes: 1 addition & 1 deletion stm/sitemap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"reflect"
"testing"

"github.com/clbanning/mxj"
"github.com/clbanning/mxj/v2"
)

func TestSitemapGenerator(t *testing.T) {
Expand Down

0 comments on commit 3f9fe6e

Please sign in to comment.