sitemapgen

package module
v0.0.0-...-5c11e94 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2017 License: MIT Imports: 5 Imported by: 0

README

sitemapgen

sitemapgen is a Go library to easy generate sitemap.xml files that follows to sitemap protocol

sitemapgen also supports localization urls following the xhtml specification. Read more on google sitemap alternate language

Usage

import (
    "fmt"
    "github.com/moonwalker/sitemapgen"
)
// Create sitemap object to work with
sitemap := sitemapgen.CreateSitemap()

// Create url for an location
u := sitemapgen.CreateUrl("http://example.example/slug")

// Sets the changefreq for the url
u.SetChangeFreq(sitemapgen.WEEKLY)

// Add url to sitemap
sitemap.AddUrl(u)

// Write sitemap to file
err := sitemap.WriteToFile("sitemap.xml")
if err != nil {
    fmt.Printf("Error creating sitemap: %v\n", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Alternate

type Alternate struct {
	XMLName xml.Name `xml:"xhtml:link,allowempty"`
	Rel     string   `xml:"rel,attr"`
	Lang    string   `xml:"hreflang,attr"`
	Href    string   `xml:"href,attr"`
}

type ChangeFreq

type ChangeFreq string
const (
	ALWAYS  ChangeFreq = "always"
	HOURLY  ChangeFreq = "hourly"
	DAILY   ChangeFreq = "daily"
	WEEKLY  ChangeFreq = "weekly"
	MONTHLY ChangeFreq = "monthly"
	YEARLY  ChangeFreq = "yearly"
	NEVER   ChangeFreq = "never"

	ALTERNATE = "alternate"
)

type Sitemap

type Sitemap struct {
	XMLName    xml.Name `xml:"urlset"`
	XMLNS      string   `xml:"xmlns,attr"`
	XMLNSXHTML *string  `xml:"xmlns:xhtml,attr,omitempty"`
	Urls       []Url    `xml:",innerxml"`
}

func CreateSitemap

func CreateSitemap() Sitemap

CreateSitemap creates sitemap

func (*Sitemap) AddAlternateSupport

func (s *Sitemap) AddAlternateSupport()

func (*Sitemap) AddUrl

func (s *Sitemap) AddUrl(url Url)

AddUrl adds the url to the sitemap

func (*Sitemap) GetXMLOutput

func (s *Sitemap) GetXMLOutput() ([]byte, error)

GetXMLOutput generates the xml for the sitemap

func (*Sitemap) RemoveAlternateSupport

func (s *Sitemap) RemoveAlternateSupport()

func (*Sitemap) WriteToFile

func (s *Sitemap) WriteToFile(filename string) error

WriteToFile generates the sitemap.xml content and writes it to the specified file

type Url

type Url struct {
	XMLName    xml.Name     `xml:"url"`
	Location   string       `xml:"loc"`
	LastMod    *XMLTime     `xml:"lastmod,omitempty"`
	ChangeFreq *ChangeFreq  `xml:"changefreq,omitempty"`
	Priority   *float32     `xml:"priority,omitempty"`
	Alternates *[]Alternate `xml:",innerxml"`
}

func CreateUrl

func CreateUrl(location string) Url

CreateUrl creates a Url with the specified location

func (*Url) AddAlternate

func (u *Url) AddAlternate(lang string, href string)

func (*Url) SetChangeFreq

func (u *Url) SetChangeFreq(cf ChangeFreq)

SetChangeFreq defines how often url is expected to be updated

func (*Url) SetLastModified

func (u *Url) SetLastModified(t time.Time)

SetLastModified sets the date for when this url was last modified

func (*Url) SetLocation

func (u *Url) SetLocation(l string)

SetLocation sets the location for the url

func (*Url) SetPriority

func (u *Url) SetPriority(p float32)

SetPriority should get a value between 0.0 and 1.0

type XMLTime

type XMLTime struct {
	Time time.Time
}

func (XMLTime) MarshalText

func (t XMLTime) MarshalText() ([]byte, error)

MarshalText implements the encoding.TextMarshaler interface. The time is formatted in RFC 3339 format, witch is the same as sitemap stated ISO 8601

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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