Skip to content
Permalink
Browse files

Merge branch 'kosto1992-master' into v2

  • Loading branch information...
ikeikeikeike committed Oct 28, 2018
2 parents e033eb3 + 9381398 commit c68941e30f7a3292afef82ed2f8800bd47fde3d1
Showing with 331 additions and 243 deletions.
  1. +105 −72 README.md
  2. +34 −10 stm/builder.go
  3. +1 −1 stm/builder_file.go
  4. +1 −1 stm/builder_indexfile.go
  5. +4 −4 stm/builder_test.go
  6. +12 −3 stm/builder_url.go
  7. +73 −73 stm/builder_url_test.go
  8. +6 −2 stm/sitemap.go
  9. +4 −4 stm/sitemap_test.go
  10. +86 −68 stm/utils.go
  11. +5 −5 stm/utils_test.go
177 README.md
@@ -11,15 +11,15 @@ import (
func main() {
sm := stm.NewSitemap()
sm := stm.NewSitemap(1)
// Create method must be called first before adding entries to
// the sitemap.
sm.Create()
sm.Add(stm.URL{"loc": "home", "changefreq": "always", "mobile": true})
sm.Add(stm.URL{"loc": "readme"})
sm.Add(stm.URL{"loc": "aboutme", "priority": 0.1})
sm.Add(stm.URL{{"loc", "home"}, {"changefreq", "always"}, {"mobile", true}})
sm.Add(stm.URL{{"loc", "readme"}})
sm.Add(stm.URL{{"loc", "aboutme"}, {"priority", 0.1}})
sm.Finalize().PingSearchEngines()
}
@@ -53,13 +53,24 @@ Current Features or To-Do

## Getting Started

### Setting concurrency
To disable concurrency, set number of CPUs to 1.
```go
sm := stm.NewSitemap(1)
```

If you want to set max CPUs that are available, set number of CPUs <= 0.
```go
sm := stm.NewSitemap(0)
```

### Preventing Output

To disable all non-essential output you can set `sm.SetVerbose` to `false`.
To disable output inline use the following:

```go
sm := stm.NewSitemap()
sm := stm.NewSitemap(1)
sm.SetVerbose(false)
```

@@ -113,7 +124,7 @@ import (
)
func main() {
sm := stm.NewSitemap()
sm := stm.NewSitemap(1)
sm.SetDefaultHost("http://example.com")
sm.SetSitemapsPath("sitemap-generator") // default: public
sm.SetSitemapsHost("http://s3.amazonaws.com/sitemap-generator/")
@@ -126,9 +137,9 @@ func main() {
sm.Create()
sm.Add(stm.URL{"loc": "home", "changefreq": "always", "mobile": true})
sm.Add(stm.URL{"loc": "readme"})
sm.Add(stm.URL{"loc": "aboutme", "priority": 0.1})
sm.Add(stm.URL{{"loc", "home"}, {"changefreq", "always"}, {"mobile", true}})
sm.Add(stm.URL{{"loc", "readme"}})
sm.Add(stm.URL{{"loc", "aboutme"}, {"priority", 0.1}})
sm.Finalize().PingSearchEngines()
}
@@ -137,45 +148,55 @@ func main() {
### News sitemaps

```go
sm.Add(stm.URL{"loc": "/news", "news": stm.URL{
"publication": stm.URL{
"name": "Example",
"language": "en",
sm.Add(stm.URL{
{"loc", "/news"},
{"news", stm.URL{
{"publication", stm.URL{
{"name", "Example"},
{"language", "en"},
},
"title": "My Article",
"keywords": "my article, articles about myself",
"stock_tickers": "SAO:PETR3",
"publication_date": "2011-08-22",
"access": "Subscription",
"genres": "PressRelease",
}})
},
{"title", "My Article"},
{"keywords", "my article, articles about myself"},
{"stock_tickers", "SAO:PETR3"},
{"publication_date", "2011-08-22"},
{"access", "Subscription"},
{"genres", "PressRelease"},
},},})
```

Look at [Creating a Google News Sitemap](https://support.google.com/news/publisher/answer/74288) as required.

### Video sitemaps

```go
sm.Add(stm.URL{"loc": "/videos", "video": stm.URL{
"thumbnail_loc": "http://www.example.com/video1_thumbnail.png",
"title": "Title",
"description": "Description",
"content_loc": "http://www.example.com/cool_video.mpg",
"category": "Category",
"tag": []string{"one", "two", "three"},
"player_loc": stm.Attrs{"https://example.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}},
}})
sm.Add(stm.URL{
{"loc", "/videos"},
{"video", stm.URL{
{"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"},
{"title", "Title"},
{"description", "Description"},
{"content_loc", "http://www.example.com/cool_video.mpg"},
{"category", "Category"},
{"tag", []string{"one", "two", "three"}},
{"player_loc", stm.Attrs{"https://example.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}},},
},
},
})
```

Look at [Video sitemaps](https://support.google.com/webmasters/answer/80471) as required.

### Image sitemaps

```go
sm.Add(stm.URL{"loc": "/images", "image": []stm.URL{
{"loc": "http://www.example.com/image.png", "title": "Image"},
{"loc": "http://www.example.com/image1.png", "title": "Image1"},
}})
sm.Add(stm.URL{
{"loc", "/images"},
{"image", []stm.URL{
{{"loc", "http://www.example.com/image.png"}, {"title", "Image"}},
{{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}},
},},
})
```

@@ -184,9 +205,12 @@ Look at [Image sitemaps](https://support.google.com/webmasters/answer/178636) as
### Geo sitemaps

```go
sm.Add(stm.URL{"loc": "/geos", "geo": stm.URL{
"format": "kml",
}})
sm.Add(stm.URL{
{"loc", "/geos"},
{"geo", stm.URL{
{"format", "kml"},
},},
})
```

Couldn't find Geo sitemaps example, although it's similar to:
@@ -203,7 +227,7 @@ Couldn't find Geo sitemaps example, although it's similar to:
### Mobile sitemaps

```go
sm.Add(stm.URL{"loc": "mobiles", "mobile": true})
sm.Add(stm.URL{{"loc", "mobiles"}, {"mobile", true}})
```

Look at [Feature phone sitemaps](https://support.google.com/webmasters/answer/6082207) as required.
@@ -219,7 +243,7 @@ import (
)
func main() {
sm := stm.NewSitemap()
sm := stm.NewSitemap(0)
sm.SetDefaultHost("http://yourhost.com")
sm.SetSitemapsHost("http://s3.amazonaws.com/sitemaps/")
sm.SetSitemapsPath("sitemaps/")
@@ -230,41 +254,50 @@ func main() {
sm.Create()
sm.Add(stm.URL{"loc": "/home", "changefreq": "daily"})
sm.Add(stm.URL{{"loc", "/home"}, {"changefreq", "daily"}})
sm.Add(stm.URL{"loc": "/abouts", "mobile": true})
sm.Add(stm.URL{{"loc", "/abouts"}, {"mobile", true}})
sm.Add(stm.URL{"loc": "/news", "news": stm.URL{
"publication": stm.URL{
"name": "Example",
"language": "en",
sm.Add(stm.URL{{"loc", "/news"},
{"news", stm.URL{
{"publication", stm.URL{
{"name", "Example"},
{"language", "en"},
},
},
"title": "My Article",
"keywords": "my article, articles about myself",
"stock_tickers": "SAO:PETR3",
"publication_date": "2011-08-22",
"access": "Subscription",
"genres": "PressRelease",
}})
sm.Add(stm.URL{"loc": "/images", "image": []stm.URL{
{"loc": "http://www.example.com/image.png", "title": "Image"},
{"loc": "http://www.example.com/image1.png", "title": "Image1"},
}})
sm.Add(stm.URL{"loc": "/videos", "video": stm.URL{
"thumbnail_loc": "http://www.example.com/video1_thumbnail.png",
"title": "Title",
"description": "Description",
"content_loc": "http://www.example.com/cool_video.mpg",
"category": "Category",
"tag": []string{"one", "two", "three"},
"player_loc": stm.Attrs{"https://example.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}},
}})
sm.Add(stm.URL{"loc": "/geos", "geo": stm.URL{
"format": "kml",
}})
{"title", "My Article"},
{"keywords", "my article, articles about myself"},
{"stock_tickers", "SAO:PETR3"},
{"publication_date", "2011-08-22"},
{"access", "Subscription"},
{"genres", "PressRelease"},
},},
})
sm.Add(stm.URL{{"loc", "/images"},
{"image", []stm.URL{
{{"loc", "http://www.example.com/image.png"}, {"title", "Image"}},
{{"loc", "http://www.example.com/image1.png"}, {"title", "Image1"}},
},},
})
sm.Add(stm.URL{{"loc", "/videos"},
{"video", stm.URL{
{"thumbnail_loc", "http://www.example.com/video1_thumbnail.png"},
{"title", "Title"},
{"description", "Description"},
{"content_loc", "http://www.example.com/cool_video.mpg"},
{"category", "Category"},
{"tag", []string{"one", "two", "three"}},
{"player_loc", stm.Attrs{"https://example.com/p/flash/moogaloop/6.2.9/moogaloop.swf?clip_id=26", map[string]string{"allow_embed": "Yes", "autoplay": "autoplay=1"}}},
},},
})
sm.Add(stm.URL{{"loc", "/geos"},
{"geo", stm.URL{
{"format", "kml"},
},},
})
sm.Finalize().PingSearchEngines("http://newengine.com/ping?url=%s")
}
@@ -284,11 +317,11 @@ import (
)
func buildSitemap() *stm.Sitemap {
sm := stm.NewSitemap()
sm := stm.NewSitemap(1)
sm.SetDefaultHost("http://example.com")
sm.Create()
sm.Add(stm.URL{"loc": "/", "changefreq": "daily"})
sm.Add(stm.URL{{"loc", "/"}, {"changefreq", "daily"}})
// Note: Do not call `sm.Finalize()` because it flushes
// the underlying data structure from memory to disk.
@@ -1,8 +1,6 @@
package stm

import (
"fmt"
)
import "fmt"

var poolBuffer = NewBufferPool()

@@ -32,16 +30,29 @@ type Attrs []interface{}
type Attr map[string]string

// URL User should use this typedef in main func.
type URL map[string]interface{}
type URL [][]interface{}

// URLJoinBy that's convenient.
func (u URL) URLJoinBy(key string, joins ...string) URL {
var values []string
for _, k := range joins {
values = append(values, fmt.Sprint(u[k]))
var vals interface{}
for _, v := range u {
if v[0] == k {
vals = v[1]
break
}
}
values = append(values, fmt.Sprint(vals))
}

u[key] = URLJoin("", values...)
var index int
var v []interface{}
for index, v = range u {
if v[0] == key {
break
}
}
u[index][1] = URLJoin("", values...)
return u
}

@@ -51,10 +62,23 @@ func (u *URL) BungURLJoinBy(key string, joins ...string) {

var values []string
for _, k := range joins {
values = append(values, fmt.Sprint(orig[k]))
var vals interface{}
for _, v := range *u {
if v[0] == k {
vals = v[1]
break
}
}
values = append(values, fmt.Sprint(vals))
}

orig[key] = URLJoin("", values...)
var index int
var v []interface{}
for index, v = range *u {
if v[0] == key {
break
}
}
orig[index][1] = URLJoin("", values...)
*u = orig
}

@@ -35,7 +35,7 @@ type BuilderFile struct {
// Add method joins old bytes with creates bytes by it calls from Sitemap.Add method.
func (b *BuilderFile) Add(url interface{}) BuilderError {
u := MergeMap(url.(URL),
URL{"host": b.loc.opts.defaultHost},
URL{{"host", b.loc.opts.defaultHost}},
)

b.linkcnt++
@@ -21,7 +21,7 @@ func (b *BuilderIndexfile) Add(link interface{}) BuilderError {
bldr := link.(*BuilderFile)
bldr.Write()

smu := NewSitemapIndexURL(b.opts, 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
@@ -6,17 +6,17 @@ import (
)

func TestURLType(t *testing.T) {
url := URL{"loc": "1", "host": "http://example.com"}
expect := URL{"loc": "http://example.com/1", "host": "http://example.com"}
url := URL{{"loc", "1"}, {"host", "http://example.com"}}
expect := URL{{"loc", "http://example.com/1"}, {"host", "http://example.com"}}

url = url.URLJoinBy("loc", "host", "loc")

if !reflect.DeepEqual(url, expect) {
t.Fatalf("Failed to join url in URL type: deferrent URL %v and %v", url, expect)
}

url = URL{"loc": "1", "host": "http://example.com", "mobile": true}
expect = URL{"loc": "http://example.com/1/true", "host": "http://example.com", "mobile": true}
url = URL{{"loc", "1"}, {"host", "http://example.com"}, {"mobile", true}}
expect = URL{{"loc", "http://example.com/1/true"}, {"host", "http://example.com"}, {"mobile", true}}

url.BungURLJoinBy("loc", "host", "loc", "mobile")

0 comments on commit c68941e

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