GithubHelp home page GithubHelp logo

sitemap's Introduction

sitemap

Example

main_go:

package main

import (
	"github.com/KNJ/sitemap"
	"log"
	"net/url"
	"os"
	"path/filepath"
	"strconv"
)

func main() {
	wd, err := os.Getwd()
	if err != nil {
		log.Fatal(err)
	}
	idx := sitemap.NewIndex().Configure(urlPrefix("https://wazly.com"))
	us1 := sitemap.NewURLSet().Configure(prefix("https://wazly.com/works/"))
	for i := 0; i < 50; i++ {
		loc := strconv.Itoa(i)
		us1.AddURL(sitemap.URL{loc, 0.8, "monthly"})
	}
	us2 := sitemap.NewURLSet().Configure(prefix("https://wazly.com/todo?page="), limit(200))
	for i := 0; i < 1000; i++ {
		loc := strconv.Itoa(i)
		us2.AddURL(sitemap.URL{loc, 0.5, "daily"})
	}
	idx.Add("works", *us1)
	idx.Add("todo", *us2)
	err = idx.Generate(&sitemap.FileDriver{BasePath: filepath.Join(wd, "xml")})
	if err != nil {
		log.Fatal(err)
	}
}

func urlPrefix(s string) func(*sitemap.Index) {
	u, err := url.Parse(s)
	if err != nil {
		log.Fatal(err)
	}
	return func(idx *sitemap.Index) {
		idx.URLPrefix = u
	}
}

func prefix(s string) func(*sitemap.URLSet) {
	return func(us *sitemap.URLSet) {
		us.Prefix = s
	}
}

func limit(i int) func(*sitemap.URLSet) {
	return func(us *sitemap.URLSet) {
		us.Limit = i
	}
}

Output

./xml/sitemap_index.xml:

<?xml version="1.0" encoding="UTF-8"?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <sitemap>
        <loc>https://wazly.com/works_0.xml</loc>
        <lastmod>2018-10-06</lastmod>
    </sitemap>
    <sitemap>
        <loc>https://wazly.com/todo_0.xml</loc>
        <lastmod>2018-10-06</lastmod>
    </sitemap>
    <sitemap>
        <loc>https://wazly.com/todo_1.xml</loc>
        <lastmod>2018-10-06</lastmod>
    </sitemap>
    <sitemap>
        <loc>https://wazly.com/todo_2.xml</loc>
        <lastmod>2018-10-06</lastmod>
    </sitemap>
    <sitemap>
        <loc>https://wazly.com/todo_3.xml</loc>
        <lastmod>2018-10-06</lastmod>
    </sitemap>
    <sitemap>
        <loc>https://wazly.com/todo_4.xml</loc>
        <lastmod>2018-10-06</lastmod>
    </sitemap>
</sitemapindex>

./xml/todo_0.xml:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://wazly.com/todo?page=0</loc>
        <priority>0.5</priority>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc>https://wazly.com/todo?page=1</loc>
        <priority>0.5</priority>
        <changefreq>daily</changefreq>
    </url>
    
...

    <url>
        <loc>https://wazly.com/todo?page=198</loc>
        <priority>0.5</priority>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc>https://wazly.com/todo?page=199</loc>
        <priority>0.5</priority>
        <changefreq>daily</changefreq>
    </url>
</urlset>    

sitemap's People

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.