Skip to content

Commit

Permalink
Add support for sitemap indexes.
Browse files Browse the repository at this point in the history
  • Loading branch information
snabb committed Oct 22, 2017
1 parent 036d02a commit 9b9f4b7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion LICENSE
@@ -1,4 +1,4 @@
Copyright © 2016 Janne Snabb snabb AT epipe.com
Copyright © 2016-2017 Janne Snabb snabb AT epipe.com

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
Expand Down
8 changes: 5 additions & 3 deletions README.md
Expand Up @@ -3,9 +3,11 @@ sitemap

[![GoDoc](https://godoc.org/github.com/snabb/sitemap?status.svg)](https://godoc.org/github.com/snabb/sitemap)

The Go package sitemap provides tools for creating an XML sitemap and
writing it to an io.Writer (such as http.ResponseWriter). Please see
http://www.sitemaps.org/ for description of sitemap contents.
The Go package sitemap provides tools for creating XML sitemaps
and sitemap indexes and writing them to an io.Writer (such as
http.ResponseWriter).

Please see http://www.sitemaps.org/ for description of sitemap contents.

The package implements io.WriterTo and io.ReaderFrom interfaces.

Expand Down
17 changes: 12 additions & 5 deletions sitemap.go
@@ -1,6 +1,8 @@
// Package sitemap provides tools for creating an XML sitemap and writing it
// to an io.Writer (such as http.ResponseWriter). Please see
// http://www.sitemaps.org/ for description of sitemap contents.
// Package sitemap provides tools for creating XML sitemaps
// and sitemap indexes and writing them to io.Writer (such as
// http.ResponseWriter).
//
// Please see http://www.sitemaps.org/ for description of sitemap contents.
package sitemap

import (
Expand All @@ -24,15 +26,20 @@ const (
Never ChangeFreq = "never"
)

// Single URL entry in sitemap. LastMod is a pointer to time.Time because
// omitempty does not work otherwise. Loc is the only mandatory item.
// Single URL entry in sitemap or sitemap index. LastMod is a pointer
// to time.Time because omitempty does not work otherwise. Loc is the
// only mandatory item. ChangeFreq and Priority must be left empty when
// using with a sitemap index.
type URL struct {
Loc string `xml:"loc"`
LastMod *time.Time `xml:"lastmod,omitempty"`
ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
Priority float32 `xml:"priority,omitempty"`
}

// Sitemap represents a complete sitemap which can be marshaled to XML.
// New instances must be created with New() in order to set the xmlns
// attribute correctly.
type Sitemap struct {
XMLName xml.Name `xml:"urlset"`
Xmlns string `xml:"xmlns,attr"`
Expand Down
59 changes: 59 additions & 0 deletions sitemapindex.go
@@ -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)
25 changes: 25 additions & 0 deletions sitemapindex_test.go
@@ -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>
}

0 comments on commit 9b9f4b7

Please sign in to comment.