Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
102 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package sitemap | ||
|
||
import ( | ||
"encoding/xml" | ||
"github.com/snabb/diagio" | ||
"io" | ||
) | ||
|
||
// SitemapIndex is like Sitemap except the elements are named differently | ||
// (and ChangeFreq and Priority may not be used). | ||
// New instances must be created with NewSitemapIndex() in order to set the | ||
// xmlns attribute correctly. | ||
type SitemapIndex struct { | ||
XMLName xml.Name `xml:"sitemapindex"` | ||
Xmlns string `xml:"xmlns,attr"` | ||
|
||
URLs []*URL `xml:"sitemap"` | ||
} | ||
|
||
// New returns new SitemapIndex. | ||
func NewSitemapIndex() *SitemapIndex { | ||
return &SitemapIndex{ | ||
Xmlns: "http://www.sitemaps.org/schemas/sitemap/0.9", | ||
URLs: make([]*URL, 0), | ||
} | ||
} | ||
|
||
// Add adds an URL to a SitemapIndex. | ||
func (s *SitemapIndex) Add(u *URL) { | ||
s.URLs = append(s.URLs, u) | ||
} | ||
|
||
// WriteTo writes XML encoded sitemap index to given io.Writer. | ||
// Implements io.WriterTo. | ||
func (s *SitemapIndex) WriteTo(w io.Writer) (n int64, err error) { | ||
cw := diagio.NewCounterWriter(w) | ||
|
||
_, err = cw.Write([]byte(xml.Header)) | ||
if err != nil { | ||
return cw.Count(), err | ||
} | ||
en := xml.NewEncoder(cw) | ||
en.Indent("", " ") | ||
err = en.Encode(s) | ||
cw.Write([]byte{'\n'}) | ||
return cw.Count(), err | ||
} | ||
|
||
var _ io.WriterTo = (*Sitemap)(nil) | ||
|
||
// ReadFrom reads and parses an XML encoded sitemap index from io.Reader. | ||
// Implements io.ReaderFrom. | ||
func (s *SitemapIndex) ReadFrom(r io.Reader) (n int64, err error) { | ||
de := xml.NewDecoder(r) | ||
err = de.Decode(s) | ||
return de.InputOffset(), err | ||
} | ||
|
||
var _ io.ReaderFrom = (*Sitemap)(nil) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package sitemap_test | ||
|
||
import ( | ||
"github.com/snabb/sitemap" | ||
"os" | ||
"time" | ||
) | ||
|
||
func ExampleSitemapIndex() { | ||
smi := sitemap.NewSitemapIndex() | ||
t := time.Unix(0, 0).UTC() | ||
smi.Add(&sitemap.URL{ | ||
Loc: "http://example.com/", | ||
LastMod: &t, | ||
}) | ||
smi.WriteTo(os.Stdout) | ||
// Output: | ||
// <?xml version="1.0" encoding="UTF-8"?> | ||
// <sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"> | ||
// <sitemap> | ||
// <loc>http://example.com/</loc> | ||
// <lastmod>1970-01-01T00:00:00Z</lastmod> | ||
// </sitemap> | ||
// </sitemapindex> | ||
} |