Skip to content
Permalink
Browse files

closes #11

Allows pretty formatting to output xml files.
  • Loading branch information...
ikeikeikeike committed Nov 24, 2016
1 parent 54f5ca7 commit 94ab5f84694fbdf256042170f40171abbe87ebcf
Showing with 45 additions and 25 deletions.
  1. +5 −4 stm/builder_file.go
  2. +4 −3 stm/builder_indexfile.go
  3. +6 −3 stm/builder_indexurl.go
  4. +6 −3 stm/builder_url.go
  5. +10 −10 stm/builder_url_test.go
  6. +7 −0 stm/options.go
  7. +7 −2 stm/sitemap.go
@@ -17,16 +17,17 @@ func (e *builderFileError) FullError() bool {
}

// NewBuilderFile returns the created the BuilderFile's pointer
func NewBuilderFile(loc *Location) *BuilderFile {
b := &BuilderFile{loc: loc}
func NewBuilderFile(opts *Options, loc *Location) *BuilderFile {
b := &BuilderFile{opts: opts, loc: loc}
b.clear()
return b
}

// BuilderFile provides implementation for the Builder interface.
type BuilderFile struct {
content []byte
opts *Options
loc *Location
content []byte
linkcnt int
newscnt int
}
@@ -37,7 +38,7 @@ func (b *BuilderFile) Add(url interface{}) BuilderError {
URL{"host": b.loc.opts.defaultHost},
)

smu, err := NewSitemapURL(u)
smu, err := NewSitemapURL(b.opts, u)
if err != nil {
log.Fatalf("[F] Sitemap: %s", err)
}
@@ -3,12 +3,13 @@ package stm
import "bytes"

// NewBuilderIndexfile returns the created the BuilderIndexfile's pointer
func NewBuilderIndexfile(loc *Location) *BuilderIndexfile {
return &BuilderIndexfile{loc: loc}
func NewBuilderIndexfile(opts *Options, loc *Location) *BuilderIndexfile {
return &BuilderIndexfile{opts: opts, loc: loc}
}

// BuilderIndexfile provides implementation for the Builder interface.
type BuilderIndexfile struct {
opts *Options
loc *Location
content []byte
linkcnt int
@@ -20,7 +21,7 @@ func (b *BuilderIndexfile) Add(link interface{}) BuilderError {
bldr := link.(*BuilderFile)
bldr.Write()

smu := NewSitemapIndexURL(URL{"loc": bldr.loc.URL()})
smu := NewSitemapIndexURL(b.opts, URL{"loc": bldr.loc.URL()})
b.content = append(b.content, smu.XML()...)

b.totalcnt += bldr.linkcnt
@@ -7,12 +7,13 @@ import (
)

// NewSitemapIndexURL and NewSitemapURL are almost the same behavior.
func NewSitemapIndexURL(url URL) SitemapURL {
return &sitemapIndexURL{data: url}
func NewSitemapIndexURL(opts *Options, url URL) SitemapURL {
return &sitemapIndexURL{opts: opts, data: url}
}

// sitemapIndexURL and sitemapURL are almost the same behavior.
type sitemapIndexURL struct {
opts *Options
data URL
}

@@ -28,8 +29,10 @@ func (su *sitemapIndexURL) XML() []byte {
lastmod.SetText(time.Now().Format(time.RFC3339))
}

if su.opts.pretty {
doc.Indent(2)
}
buf := poolBuffer.Get()
// doc.Indent(2)
doc.WriteTo(buf)

bytes := buf.Bytes()
@@ -36,14 +36,15 @@ var fieldnames = ToLowerString(structs.Names(&URLModel{}))

// NewSitemapURL returns the created the SitemapURL's pointer
// and it validates URL types error.
func NewSitemapURL(url URL) (SitemapURL, error) {
smu := &sitemapURL{data: url}
func NewSitemapURL(opts *Options, url URL) (SitemapURL, error) {
smu := &sitemapURL{opts: opts, data: url}
err := smu.validate()
return smu, err
}

// sitemapURL provides xml validator and xml builder.
type sitemapURL struct {
opts *Options
data URL
}

@@ -108,8 +109,10 @@ func (su *sitemapURL) XML() []byte {
SetBuilderElementValue(url, su.data, "image")
SetBuilderElementValue(url, su.data, "geo")

if su.opts.pretty {
doc.Indent(2)
}
buf := poolBuffer.Get()
// doc.Indent(2)
doc.WriteTo(buf)

bytes := buf.Bytes()
@@ -11,19 +11,19 @@ import (
)

func TestBlank(t *testing.T) {
if _, err := NewSitemapURL(URL{}); err == nil {
if _, err := NewSitemapURL(&Options{}, URL{}); err == nil {
t.Errorf(`Failed to validate blank arg ( URL{} ): %s`, err)
}
}

func TestItHasLocElement(t *testing.T) {
if _, err := NewSitemapURL(URL{}); err == nil {
if _, err := NewSitemapURL(&Options{}, URL{}); err == nil {
t.Errorf(`Failed to validate about must have loc attribute in URL type ( URL{} ): %s`, err)
}
}

func TestJustSetLocElement(t *testing.T) {
smu, err := NewSitemapURL(URL{"loc": "path", "host": "http://example.com"})
smu, err := NewSitemapURL(&Options{}, URL{"loc": "path", "host": "http://example.com"})

if err != nil {
t.Fatalf(`Fatal to validate! This is a critical error: %s`, err)
@@ -45,7 +45,7 @@ func TestJustSetLocElement(t *testing.T) {
}

func TestJustSetLocElementAndThenItNeedsCompleteValues(t *testing.T) {
smu, err := NewSitemapURL(URL{"loc": "path", "host": "http://example.com"})
smu, err := NewSitemapURL(&Options{}, URL{"loc": "path", "host": "http://example.com"})

if err != nil {
t.Fatalf(`Fatal to validate! This is a critical error: %s`, err)
@@ -93,7 +93,7 @@ func TestJustSetLocElementAndThenItNeedsCompleteValues(t *testing.T) {
}

func TestSetNilValue(t *testing.T) {
smu, err := NewSitemapURL(URL{"loc": "path", "priority": nil, "changefreq": nil, "lastmod": nil, "host": "http://example.com"})
smu, err := NewSitemapURL(&Options{}, URL{"loc": "path", "priority": nil, "changefreq": nil, "lastmod": nil, "host": "http://example.com"})

if err != nil {
t.Fatalf(`Fatal to validate! This is a critical error: %s`, err)
@@ -130,7 +130,7 @@ func TestSetNilValue(t *testing.T) {
}

func TestAutoGenerateSitemapHost(t *testing.T) {
smu, err := NewSitemapURL(URL{"loc": "path", "host": "http://example.com"})
smu, err := NewSitemapURL(&Options{}, URL{"loc": "path", "host": "http://example.com"})

if err != nil {
t.Fatalf(`Fatal to validate! This is a critical error: %s`, err)
@@ -425,14 +425,14 @@ func BenchmarkGenerateXML(b *testing.B) {
var data URL

data = URL{"loc": "/mobile", "mobile": true}
smu, _ = NewSitemapURL(data)
smu, _ = NewSitemapURL(&Options{}, data)
smu.XML()

data = URL{"loc": "/images", "image": []URL{
{"loc": "http://www.example.com/image.png", "title": "Image"},
{"loc": "http://www.example.com/image1.png", "title": "Image1"},
}}
smu, _ = NewSitemapURL(data)
smu, _ = NewSitemapURL(&Options{}, data)
smu.XML()

data = URL{"loc": "/videos", "video": URL{
@@ -443,7 +443,7 @@ func BenchmarkGenerateXML(b *testing.B) {
"category": "Category",
"tag": []string{"one", "two", "three"},
}}
smu, _ = NewSitemapURL(data)
smu, _ = NewSitemapURL(&Options{}, data)
smu.XML()

data = URL{"loc": "/news", "news": URL{
@@ -459,7 +459,7 @@ func BenchmarkGenerateXML(b *testing.B) {
"genres": "PressRelease",
}}

smu, _ = NewSitemapURL(data)
smu, _ = NewSitemapURL(&Options{}, data)
smu.XML()
}
}
@@ -11,6 +11,7 @@ func NewOptions() *Options {
filename: "sitemap",
verbose: true,
compress: true,
pretty: false,
adp: NewFileAdapter(),
}
}
@@ -24,6 +25,7 @@ type Options struct {
filename string
verbose bool
compress bool
pretty bool
adp Adapter
nmr *Namer
loc *Location
@@ -64,6 +66,11 @@ func (opts *Options) SetCompress(compress bool) {
opts.compress = compress
}

// SetPretty option sets pretty option to Options struct which allows pretty formatting to output files.
func (opts *Options) SetPretty(pretty bool) {
opts.pretty = pretty
}

// SetAdapter sets that arg from Sitemap.SetAdapter method
func (opts *Options) SetAdapter(adp Adapter) {
opts.adp = adp
@@ -61,21 +61,26 @@ func (sm *Sitemap) SetCompress(compress bool) {
sm.opts.SetCompress(compress)
}

// SetPretty option allows pretty formating to the output files.
func (sm *Sitemap) SetPretty(pretty bool) {
sm.opts.SetPretty(pretty)
}

// SetFilename can apply any name in this method if you wants to change output file name
func (sm *Sitemap) SetFilename(filename string) {
sm.opts.SetFilename(filename)
}

// 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.IndexLocation())
sm.bldrs = NewBuilderIndexfile(sm.opts, sm.opts.IndexLocation())
return sm
}

// Add Should call this after call to Create method on this struct.
func (sm *Sitemap) Add(url interface{}) *Sitemap {
if sm.bldr == nil {
sm.bldr = NewBuilderFile(sm.opts.Location())
sm.bldr = NewBuilderFile(sm.opts, sm.opts.Location())
}

err := sm.bldr.Add(url)

0 comments on commit 94ab5f8

Please sign in to comment.
You can’t perform that action at this time.