sitemap

package module
v1.2.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 13, 2022 License: MIT Imports: 5 Imported by: 0

README

sitemap

GoDoc Go Report Card

The Go package sitemap provides tools for creating XML sitemaps (regular and news sitemaps) with image and video extensions as defined by Google 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. For the Google extensions, please refer to the following documentation:

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

Documentation:

https://godoc.org/github.com/semantosoph/sitemap

The Git repository is located at: https://github.com/semantosoph/sitemap

License

MIT

Documentation

Overview

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 and https://developers.google.com/search/docs/advanced/sitemaps/news-sitemap for the news sitemap documentation.

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.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ChangeFreq

type ChangeFreq string

ChangeFreq specifies change frequency of a sitemap entry. It is just a string.

const (
	Always  ChangeFreq = "always"
	Hourly  ChangeFreq = "hourly"
	Daily   ChangeFreq = "daily"
	Weekly  ChangeFreq = "weekly"
	Monthly ChangeFreq = "monthly"
	Yearly  ChangeFreq = "yearly"
	Never   ChangeFreq = "never"
)

Feel free to use these constants for ChangeFreq (or you can just supply a string directly).

type Date

type Date struct {
	time.Time
	Format string
}

Date specifies a proxy time object that we use to define our own XML encoder. Format is automatically set when adding an URL and read at encoding time.

func (Date) MarshalXML

func (d Date) MarshalXML(e *xml.Encoder, start xml.StartElement) error

MarshalXML marshals Date into the correct format

type Image added in v1.2.1

type Image struct {
	Loc         string `xml:"i:loc"`
	Caption     string `xml:"i:caption,omitempty"`
	Geolocation string `xml:"i:geo_location,omitempty"`
	Title       string `xml:"i:title,omitempty"`
	License     string `xml:"i:license,omitempty"`
}

Image subentry for sitemap URLs.

type News added in v1.2.0

type News struct {
	Pub          Publication `xml:"n:publication"`
	Date         Date        `xml:"n:publication_date"`
	Title        string      `xml:"n:title"`
	Access       string      `xml:"n:access,omitempty"`
	Genres       string      `xml:"n:genres,omitempty"`
	Keywords     string      `xml:"n:keywords,omitempty"`
	StockTickers string      `xml:"n:stock_tickers,omitempty"`
}

News is the XML Schema for the News Sitemap extension. Values for publication, date and title are required.

type NewsSitemap added in v1.2.0

type NewsSitemap struct {
	XMLName   xml.Name `xml:"urlset"`
	NewsXmlns string   `xml:"xmlns:n,attr"`
	Xmlns     string   `xml:"xmlns,attr"`

	URLs []*NewsURL `xml:"url"`

	Minify       bool `xml:"-"`
	SimplifyDate bool `xml:"-"`
}

NewsSitemap represents a complete news sitemap which can be marshaled to XML. New instances must be created with NewNewsSitemap() in order to set the name space attributes correctly. Minify can be set to make the output smaller, but less human readable, while SimplifyDate uses YYYY-MM-DD format in LastMod instead of the complete RFC3339 format.

func NewNewsSitemap added in v1.2.0

func NewNewsSitemap() *NewsSitemap

New returns a new Sitemap.

func (*NewsSitemap) Add added in v1.2.0

func (s *NewsSitemap) Add(u *NewsURL)

Add adds an URL to a Sitemap. Before adding, the URLs News date format ist set.

func (*NewsSitemap) ReadFrom added in v1.2.0

func (s *NewsSitemap) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads and parses an XML encoded sitemap from io.Reader. Implements io.ReaderFrom.

func (*NewsSitemap) WriteTo added in v1.2.0

func (s *NewsSitemap) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes XML encoded sitemap to given io.Writer. Implements io.WriterTo.

type NewsURL added in v1.2.0

type NewsURL struct {
	Loc  string `xml:"loc"`
	News News   `xml:"n:news"`
}

NewsURL is a url entry in a news sitemap.

type Price added in v1.2.1

type Price struct {
	Value      string `xml:",chardata"`
	Currency   string `xml:"currency,attr"`
	Type       string `xml:"type,attr"`
	Resolution string `xml:"resolution,attr"`
}

func (Price) MarshalXML added in v1.2.1

func (p Price) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)

MarshalXML marshals Price into the correct format, iff the value is not empty

type Publication added in v1.2.0

type Publication struct {
	Name string `xml:"n:name"`
	Lang string `xml:"n:language"`
}

Publication is the publication in which the news appear. Both values are required.

type Relationship added in v1.2.1

type Relationship struct {
	Value        string `xml:",chardata"`
	Relationship string `xml:"relationship,attr"`
}

func (Relationship) MarshalXML added in v1.2.1

func (r Relationship) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)

MarshalXML marshals Relationship into the correct format, iff the value is not empty

type Sitemap

type Sitemap struct {
	XMLName xml.Name `xml:"urlset"`
	Xmlns   string   `xml:"xmlns,attr"`
	XmlIns  string   `xml:"xmlns:i,attr,omitempty"`
	XmlVns  string   `xml:"xmlns:v,attr,omitempty"`

	URLs []*URL `xml:"url"`

	Minify       bool `xml:"-"`
	SimplifyDate bool `xml:"-"`
}

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. Minify can be set to make the output less human readable, while SimplifyDate uses YYYY-MM-DD format in LastMod instead of the complete RFC3339 format.

func New

func New() *Sitemap

New returns a new Sitemap.

func (*Sitemap) Add

func (s *Sitemap) Add(u *URL)

Add adds an URL to a Sitemap. Before adding, the LastMods Date format ist set.

func (*Sitemap) ReadFrom

func (s *Sitemap) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads and parses an XML encoded sitemap from io.Reader. Implements io.ReaderFrom.

func (*Sitemap) WriteTo

func (s *Sitemap) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes XML encoded sitemap to given io.Writer. Implements io.WriterTo.

type SitemapIndex

type SitemapIndex struct {
	XMLName xml.Name `xml:"sitemapindex"`
	Xmlns   string   `xml:"xmlns,attr"`

	URLs []*URL `xml:"sitemap"`

	Minify       bool `xml:"-"`
	SimplifyDate bool `xml:"-"`
}

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. Minify can be set to make the output less human readable, while SimplifyDate uses YYYY-MM-DD format in LastMod instead of the complete RFC3339 format.

func NewSitemapIndex

func NewSitemapIndex() *SitemapIndex

NewSitemapIndex returns new SitemapIndex.

func (*SitemapIndex) Add

func (s *SitemapIndex) Add(u *URL)

Add adds an URL to a Sitemap. Before adding, the LastMods Date format ist set.

func (*SitemapIndex) ReadFrom

func (s *SitemapIndex) ReadFrom(r io.Reader) (n int64, err error)

ReadFrom reads and parses an XML encoded sitemap index from io.Reader. Implements io.ReaderFrom.

func (*SitemapIndex) WriteTo

func (s *SitemapIndex) WriteTo(w io.Writer) (n int64, err error)

WriteTo writes XML encoded sitemap index to given io.Writer. Implements io.WriterTo.

type URL

type URL struct {
	Loc        string     `xml:"loc"`
	LastMod    Date       `xml:"lastmod,omitempty"`
	ChangeFreq ChangeFreq `xml:"changefreq,omitempty"`
	Priority   float32    `xml:"priority,omitempty"`
	Images     []Image    `xml:"i:image,omitempty"`
	Videos     []Video    `xml:"v:video,omitempty"`
}

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 Uploader added in v1.2.1

type Uploader struct {
	Value string `xml:",chardata"`
	Info  string `xml:"info,attr"`
}

func (Uploader) MarshalXML added in v1.2.1

func (u Uploader) MarshalXML(e *xml.Encoder, start xml.StartElement) (err error)

MarshalXML marshals Uploader into the correct format, iff the value is not empty

type Video added in v1.2.1

type Video struct {
	Thumbnail            string       `xml:"v:thumbnail_loc"`
	Title                string       `xml:"v:title"`
	Description          string       `xml:"v:description"`
	ContentLocation      string       `xml:"v:content_loc,omitempty"`
	PlayerLocation       string       `xml:"v:player_loc,omitempty"`
	Duration             int          `xml:"v:duration,omitempty"`
	ExpirationDate       Date         `xml:"v:expiration_date,omitempty"`
	PublicationDate      Date         `xml:"v:publication_date,omitempty"`
	Rating               float32      `xml:"v:rating,omitempty"`
	ViewCount            int          `xml:"v:view_count,omitempty"`
	FamilyFriendly       string       `xml:"v:family_friendly,omitempty"`
	Restriction          Relationship `xml:"v:restriction,omitempty"`
	Platform             Relationship `xml:"v:platform,omitempty"`
	Price                Price        `xml:"v:price,omitempty"`
	RequiresSubscription string       `xml:"v:requires_subscription,omitempty"`
	Uploader             Uploader     `xml:"v:uploader,omitempty"`
	Live                 string       `xml:"v:live,omitempty"`
	Tags                 []string     `xml:"v:tag,omitempty"`
	Categories           []string     `xml:"v:category,omitempty"`
}

Video subentry for sitemap URLs.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL