Skip to content
Permalink
Browse files

Added comments and travis.yml

  • Loading branch information...
ikeikeikeike committed Nov 27, 2015
1 parent 67e243b commit 168167f6d6d043888b84bee2dcdc303f54403efa
Showing with 239 additions and 257 deletions.
  1. +14 −0 .travis.yml
  2. +65 −10 README.md
  3. +2 −0 stm/adapter.go
  4. +5 −0 stm/adapter_file.go
  5. +5 −3 stm/adapter_s3.go
  6. +5 −1 stm/builder.go
  7. +14 −22 stm/builder_file.go
  8. +7 −6 stm/builder_indexfile.go
  9. +3 −0 stm/builder_indexurl.go
  10. +10 −3 stm/builder_url.go
  11. +3 −3 stm/builder_url_test.go
  12. +24 −12 stm/consts.go
  13. +4 −185 stm/doc.go
  14. +21 −0 stm/location.go
  15. +11 −3 stm/namer.go
  16. +18 −0 stm/options.go
  17. +1 −0 stm/ping.go
  18. +19 −2 stm/sitemap.go
  19. +8 −7 stm/utils.go
@@ -0,0 +1,14 @@
language: go

go:
- 1.4
- 1.5
- tip

install:
- go get golang.org/x/tools/cmd/cover
- go list -f '{{range .Imports}}{{.}} {{end}}' ./... | xargs go get -d -v
- go list -f '{{range .TestImports}}{{.}} {{end}}' ./... | xargs go get -d -v

script:
- go test -v -cover ./...
@@ -1,6 +1,9 @@
###### Inspired by [sitemap_generator](http://github.com/kjvarga/sitemap_generator)

##### How do I generate sitemap in Golang? [![GoDoc](https://godoc.org/github.com/ikeikeikeike/go-sitemap-generator/stm?status.svg)](https://godoc.org/github.com/ikeikeikeike/go-sitemap-generator/stm)
##### How do I generate sitemap in Golang?

[![GoDoc](https://godoc.org/github.com/ikeikeikeike/go-sitemap-generator/stm?status.svg)](https://godoc.org/github.com/ikeikeikeike/go-sitemap-generator/stm) [![Build Status](https://travis-ci.org/ikeikeikeike/go-sitemap-generator.svg)](https://travis-ci.org/ikeikeikeike/go-sitemap-generator)


```go
package main
@@ -123,8 +126,8 @@ sm.Add(stm.URL{"loc": "/news", "news": stm.URL{

```go
sm.Add(stm.URL{"loc": "/images", "image": []stm.URL{
stm.URL{"loc": "http://www.example.com/image.png", "title": "Image"},
stm.URL{"loc": "http://www.example.com/image1.png", "title": "Image1"},
{"loc": "http://www.example.com/image.png", "title": "Image"},
{"loc": "http://www.example.com/image1.png", "title": "Image1"},
}})
```
@@ -150,25 +153,77 @@ sm.Add(stm.URL{"loc": "/geos", "geo": stm.URL{
}})
```

### PageMap Sitemaps
### Mobile Sitemaps

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

### Alternate Links
### Example


```go
```
package main
### Mobile Sitemaps
import (
"github.com/ikeikeikeike/go-sitemap-generator/stm"
)
```go
sm.Add(stm.URL{"loc": "mobiles", "mobile": true})
func main() {
sm := stm.NewSitemap()
sm.SetDefaultHost("http://yourhost.com")
sm.SetSitemapsHost("http://s3.amazonaws.com/sitemaps/")
sm.SetSitemapsPath("sitemaps/")
sm.SetFilename("anothername")
sm.SetCompress(true)
sm.SetVerbose(true)
sm.SetAdapter(&stm.S3Adapter{Region: "ap-northeast-1", Bucket: "your-bucket"})
sm.Create()
sm.Add(stm.URL{"loc": "/home", "changefreq": "dayly"})
sm.Add(stm.URL{"loc": "/abouts", "mobile": true})
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"},
}})
sm.Add(stm.URL{"loc": "/geos", "geo": stm.URL{
"format": "kml",
}})
sm.Finalize().PingSearchEngines("http://newengine.com/ping?url=%s")
}
```

### Documentation

- [API Reference](https://godoc.org/github.com/ikeikeikeike/go-sitemap-generator/stm)
- [sitemap_generator](http://github.com/kjvarga/sitemap_generator)

### How to testing

@@ -181,7 +236,7 @@ $ go get github.com/clbanning/mxj
Do testing

```console
$ (cd ./stm ; go test -v github.com/ikeikeikeike/go-sitemap-generator/stm...)
$ go test -v -cover ./...
```

#### Inspired by [sitemap_generator](http://github.com/kjvarga/sitemap_generator)
@@ -2,8 +2,10 @@ package stm

import "regexp"

// GzipPtn determines gzip file.
var GzipPtn = regexp.MustCompile(".gz$")

// Adapter provides interface for writes some kind of sitemap.
type Adapter interface {
Write(loc *Location, data []byte)
}
@@ -6,13 +6,16 @@ import (
"os"
)

// NewFileAdapter returns the created the FileAdapter's pointer
func NewFileAdapter() *FileAdapter {
adapter := &FileAdapter{}
return adapter
}

// FileAdapter provides implementation for the Adapter interface.
type FileAdapter struct{}

// Write will create sitemap xml file into the file systems.
func (adp *FileAdapter) Write(loc *Location, data []byte) {
dir := loc.Directory()
fi, err := os.Stat(dir)
@@ -37,12 +40,14 @@ func (adp *FileAdapter) Write(loc *Location, data []byte) {
}
}

// gzip will create sitemap file as a gzip.
func (adp *FileAdapter) gzip(file *os.File, data []byte) {
gz := gzip.NewWriter(file)
defer gz.Close()
gz.Write(data)
}

// plain will create uncompressed file.
func (adp *FileAdapter) plain(file *os.File, data []byte) {
file.Write(data)
defer file.Close()
@@ -12,12 +12,14 @@ import (
"github.com/aws/aws-sdk-go/service/s3/s3manager"
)

// S3Adapter provides implementation for the Adapter interface.
type S3Adapter struct {
Region string
Bucket string
ACL string
Region string
Bucket string
ACL string
}

// Write will create sitemap xml file into the s3.
func (adp *S3Adapter) Write(loc *Location, data []byte) {
var reader io.Reader = bytes.NewReader(data)

@@ -4,20 +4,23 @@ import (
"fmt"
)

// BuilderError provides interface for it can confirm the error in some difference.
type BuilderError interface {
error
FullError() bool
}

// Builder provides interface for adds some kind of url sitemap.
type Builder interface {
Content() []byte
Add(interface{}) BuilderError
Write()
run()
}

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

// URLJoinBy that's convenient.
func (u URL) URLJoinBy(key string, joins ...string) URL {
var values []string
for _, k := range joins {
@@ -28,6 +31,7 @@ func (u URL) URLJoinBy(key string, joins ...string) URL {
return u
}

// BungURLJoinBy that's convenient. Though, this is Bung method.
func (u *URL) BungURLJoinBy(key string, joins ...string) {
orig := *u

@@ -3,37 +3,35 @@ package stm
import (
"bytes"
"log"

)

// builderFileError is implementation for the BuilderError interface.
type builderFileError struct {
error
full bool
}

// FullError returns true if a sitemap xml had been limit size.
func (e *builderFileError) FullError() bool {
return e.full
}

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

// BuilderFile provides implementation for the Builder interface.
type BuilderFile struct {
content []byte
build chan sitemapURL
loc *Location
linkcnt int
newscnt int

urls []interface{} // XXX: For debug
}

// 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},
@@ -50,42 +48,36 @@ func (b *BuilderFile) Add(url interface{}) BuilderError {
return &builderFileError{error: err, full: true}
}

b.content = append(b.content, bytes...) // TODO: Sitemap xml have limit length
b.linkcnt += 1
// b.build <- smu; b.urls = append(b.urls, url) // XXX: For debug
b.content = append(b.content, bytes...)
b.linkcnt++
return nil
}

// isFileCanFit checks bytes to bigger than consts values.
func (b *BuilderFile) isFileCanFit(bytes []byte) bool {
r := len(append(b.content, bytes...)) < MaxSitemapFilesize
r = r && b.linkcnt < MaxSitemapLinks
return r && b.newscnt < MaxSitemapNews
}

// clear will initialize xml content.
func (b *BuilderFile) clear() {
// b.content = make([]byte, MaxSitemapLinks, MaxSitemapFilesize)
b.content = make([]byte, 0, MaxSitemapFilesize)
}

// Content will return pooled bytes on content attribute.
func (b *BuilderFile) Content() []byte {
return b.content
}

// Write will write pooled bytes with header and footer to
// Location path for output sitemap file.
func (b *BuilderFile) Write() {
b.loc.ReserveName()

c := bytes.Join(bytes.Fields(XMLHeader), []byte(" "))
c = append(append(c, b.Content()...), XMLFooter...)

b.loc.Write(c, b.linkcnt)
b.clear() // @xml_content = @xml_wrapper_start = @xml_wrapper_end = ''
}

func (b *BuilderFile) run() {
for {
select {
case smu := <-b.build:
b.content = append(b.content, smu.XML()...) // TODO: Sitemap xml have limit length
}
}
b.clear()
}
@@ -2,19 +2,20 @@ package stm

import "bytes"

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

// BuilderIndexfile provides implementation for the Builder interface.
type BuilderIndexfile struct {
loc *Location
content []byte
linkcnt int
totalcnt int
}

// Add method joins old bytes with creates bytes by it calls from Sitemap.Finalize method.
func (b *BuilderIndexfile) Add(link interface{}) BuilderError {
bldr := link.(*BuilderFile)
bldr.Write()
@@ -23,19 +24,19 @@ func (b *BuilderIndexfile) Add(link interface{}) BuilderError {
b.content = append(b.content, smu.XML()...)

b.totalcnt += bldr.linkcnt
b.linkcnt += 1
b.linkcnt++
return nil
}

// Content and BuilderFile.Content are almost the same behavior.
func (b *BuilderIndexfile) Content() []byte {
return b.content
}

// Write and Builderfile.Write are almost the same behavior.
func (b *BuilderIndexfile) Write() {
c := bytes.Join(bytes.Fields(IndexXMLHeader), []byte(" "))
c = append(append(c, b.Content()...), IndexXMLFooter...)

b.loc.Write(c, b.linkcnt)
}

func (b *BuilderIndexfile) run() {}
@@ -7,14 +7,17 @@ import (
"github.com/beevik/etree"
)

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

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

// XML and sitemapIndexURL.XML are almost the same behavior.
func (su *sitemapIndexURL) XML() []byte {
doc := etree.NewDocument()
sitemap := doc.CreateElement("sitemap")

0 comments on commit 168167f

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