diff --git a/.gitignore b/.gitignore index c740e7b..3c0deae 100644 --- a/.gitignore +++ b/.gitignore @@ -34,3 +34,5 @@ _testmain.go /public/ /requirements.txt /tmp + +.idea \ No newline at end of file diff --git a/go.mod b/go.mod index e06318e..1c3d387 100644 --- a/go.mod +++ b/go.mod @@ -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 ) diff --git a/go.sum b/go.sum index 1112acd..c731882 100644 --- a/go.sum +++ b/go.sum @@ -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= diff --git a/stm/builder_url.go b/stm/builder_url.go index f65dda3..51af5ee 100644 --- a/stm/builder_url.go +++ b/stm/builder_url.go @@ -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") } diff --git a/stm/builder_url_test.go b/stm/builder_url_test.go index fea5740..545240a 100644 --- a/stm/builder_url_test.go +++ b/stm/builder_url_test.go @@ -7,7 +7,7 @@ import ( "time" "github.com/beevik/etree" - "github.com/clbanning/mxj" + "github.com/clbanning/mxj/v2" ) func TestBlank(t *testing.T) { @@ -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() diff --git a/stm/options.go b/stm/options.go index ae2f0cb..a6e0f57 100644 --- a/stm/options.go +++ b/stm/options.go @@ -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 @@ -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 != "" { diff --git a/stm/sitemap_test.go b/stm/sitemap_test.go index bb90e99..5478e03 100644 --- a/stm/sitemap_test.go +++ b/stm/sitemap_test.go @@ -5,7 +5,7 @@ import ( "reflect" "testing" - "github.com/clbanning/mxj" + "github.com/clbanning/mxj/v2" ) func TestSitemapGenerator(t *testing.T) {