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