A rust library to generate sitemaps.
Go to file
2020-11-22 15:50:11 +01:00
.github sponsor 2020-11-22 15:50:11 +01:00
examples initial commit 2020-11-22 15:43:41 +01:00
src upd 2020-11-22 15:47:10 +01:00
.gitignore initial commit 2020-11-22 15:43:41 +01:00
Cargo.toml initial commit 2020-11-22 15:43:41 +01:00
LICENSE Initial commit 2020-11-22 15:43:03 +01:00
README.md upd 2020-11-22 15:47:10 +01:00

Sitewriter

A rust library to generate sitemaps.

It uses the quick-xml so it should be fast.

To handle the lastmod tag it uses chrono but it can be disabled with default-features = false.

Example

To run the examples use cargo run --example gen_sitemap

use chrono::prelude::*;
use sitewriter::*;

fn main() {
    let mut sitemap = Sitemap::new();
    sitemap.urls.push(Url::new("https://edgarluque.com/projects"));

    sitemap.urls.push(Url {
        loc: "https://edgarluque.com/",
        changefreq: Some(ChangeFreq::Daily),
        priority: Some(1.0),
        lastmod: Some(Utc::now()),
    });

    sitemap.urls.push(Url {
        loc: "https://edgarluque.com/blog",
        changefreq: Some(ChangeFreq::Weekly),
        priority: Some(0.8),
        lastmod: Some(Utc::now()),
    });

    sitemap.urls.push(Url {
        loc: "https://edgarluque.com/blog/sitewriter",
        changefreq: Some(ChangeFreq::Never),
        priority: Some(0.5),
        lastmod: Some(Utc.ymd(2020, 11, 22).and_hms(15, 10, 15)),
    });

    sitemap.urls.push(Url {
        loc: "https://edgarluque.com/blog/some-future-post",
        changefreq: Some(ChangeFreq::Never),
        priority: Some(0.5),
        lastmod: Some(Utc.from_utc_datetime(&Local.ymd(2020, 12, 5).and_hms(12, 30, 0).naive_utc())),
    });


    let result = sitemap.into_str();
    println!("{}", result);
}

Prints the following:

<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    <url>
        <loc>https://edgarluque.com/projects</loc>
    </url>
    <url>
        <loc>https://edgarluque.com/</loc>
        <lastmod>2020-11-22T14:36:30Z</lastmod>
        <priority>1.0</priority>
        <changefreq>daily</changefreq>
    </url>
    <url>
        <loc>https://edgarluque.com/blog</loc>
        <lastmod>2020-11-22T14:36:30Z</lastmod>
        <priority>0.8</priority>
        <changefreq>weekly</changefreq>
    </url>
    <url>
        <loc>https://edgarluque.com/blog/sitewriter</loc>
        <lastmod>2020-11-22T15:10:15Z</lastmod>
        <priority>0.5</priority>
        <changefreq>never</changefreq>
    </url>
    <url>
        <loc>https://edgarluque.com/blog/some-future-post</loc>
        <lastmod>2020-12-05T11:30:00Z</lastmod>
        <priority>0.5</priority>
        <changefreq>never</changefreq>
    </url>
</urlset>