/
sitemap.go
119 lines (99 loc) · 3.04 KB
/
sitemap.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package stm
import (
"log"
"runtime"
)
// NewSitemap returns the created the Sitemap's pointer
func NewSitemap(maxProc int) *Sitemap {
log.SetFlags(log.LstdFlags | log.Llongfile)
if maxProc < 1 || maxProc > runtime.NumCPU() {
maxProc = runtime.NumCPU()
}
log.Printf("Max processors %d\n", maxProc)
runtime.GOMAXPROCS(maxProc)
sm := &Sitemap{
opts: NewOptions(),
}
return sm
}
// Sitemap provides interface for create sitemap xml file and that has convenient interface.
// And also needs to use first this struct if it wants to use this package.
type Sitemap struct {
opts *Options
bldr Builder
bldrs Builder
}
// SetDefaultHost is your website's host name
func (sm *Sitemap) SetDefaultHost(host string) {
sm.opts.SetDefaultHost(host)
}
// SetSitemapsHost is the remote host where your sitemaps will be hosted
func (sm *Sitemap) SetSitemapsHost(host string) {
sm.opts.SetSitemapsHost(host)
}
// SetSitemapsPath sets this to a directory/path if you don't
// want to upload to the root of your `SitemapsHost`
func (sm *Sitemap) SetSitemapsPath(path string) {
sm.opts.SetSitemapsPath(path)
}
// SetPublicPath is the directory to write sitemaps to locally
func (sm *Sitemap) SetPublicPath(path string) {
sm.opts.SetPublicPath(path)
}
// SetAdapter can switch output file storage.
// We have S3Adapter and FileAdapter (default: FileAdapter)
func (sm *Sitemap) SetAdapter(adp Adapter) {
sm.opts.SetAdapter(adp)
}
// SetVerbose can switch verbose output to console.
func (sm *Sitemap) SetVerbose(verbose bool) {
sm.opts.SetVerbose(verbose)
}
// SetCompress can switch compress for the output file.
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, 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, sm.opts.Location())
}
err := sm.bldr.Add(url)
if err != nil {
if err.FullError() {
sm.Finalize()
return sm.Add(url)
}
}
return sm
}
// XMLContent returns the XML content of the sitemap
func (sm *Sitemap) XMLContent() []byte {
return sm.bldr.XMLContent()
}
// Finalize writes sitemap and index files if it had some
// specific condition in BuilderFile struct.
func (sm *Sitemap) Finalize() *Sitemap {
sm.bldrs.Add(sm.bldr)
sm.bldrs.Write()
sm.bldr = nil
return sm
}
// PingSearchEngines requests some ping server.
// It also has that includes PingSearchEngines function.
func (sm *Sitemap) PingSearchEngines(urls ...string) {
PingSearchEngines(sm.opts, urls...)
}