sitewriter/README.md

90 lines
2.8 KiB
Markdown
Raw Normal View History

2020-11-22 14:43:41 +00:00
# Sitewriter
2021-02-01 09:48:24 +00:00
[![Rust](https://github.com/edg-l/sitewriter/workflows/Rust/badge.svg)](https://github.com/edg-l/sitewriter/actions)
2020-11-22 15:42:16 +00:00
[![crates.io](http://meritbadge.herokuapp.com/sitewriter)](https://crates.io/crates/sitewriter)
2021-02-01 09:48:24 +00:00
[![License](https://img.shields.io/github/license/edg-l/sitewriter)](https://github.com/edg-l/sitewriter/blob/master/LICENSE)
2020-11-27 13:32:25 +00:00
[![codecov](https://codecov.io/gh/edg-l/sitewriter/branch/master/graph/badge.svg?token=JKOQCRSCZU)](https://codecov.io/gh/edg-l/sitewriter)
2020-11-22 15:42:16 +00:00
2020-11-22 14:43:41 +00:00
A rust library to generate sitemaps.
It uses the [quick-xml](https://github.com/tafia/quick-xml) so it should be fast.
## Example
2020-11-22 14:47:10 +00:00
To run the examples use `cargo run --example gen_sitemap`
2020-11-22 14:43:41 +00:00
```rust
use chrono::prelude::*;
use sitewriter::*;
fn main() {
let mut sitemap = Sitemap::new();
2020-11-23 09:25:21 +00:00
sitemap.urls.push(Url::new("https://edgarluque.com/projects".to_owned()));
2020-11-22 14:43:41 +00:00
sitemap.urls.push(Url {
2020-11-23 09:25:21 +00:00
loc: "https://edgarluque.com/".to_owned(),
2020-11-22 14:43:41 +00:00
changefreq: Some(ChangeFreq::Daily),
priority: Some(1.0),
lastmod: Some(Utc::now()),
});
sitemap.urls.push(Url {
2020-11-23 09:25:21 +00:00
loc: "https://edgarluque.com/blog".to_owned(),
2020-11-22 14:43:41 +00:00
changefreq: Some(ChangeFreq::Weekly),
priority: Some(0.8),
lastmod: Some(Utc::now()),
});
sitemap.urls.push(Url {
2020-11-23 09:25:21 +00:00
loc: "https://edgarluque.com/blog/sitewriter".to_owned(),
2020-11-22 14:43:41 +00:00
changefreq: Some(ChangeFreq::Never),
priority: Some(0.5),
lastmod: Some(Utc.ymd(2020, 11, 22).and_hms(15, 10, 15)),
});
sitemap.urls.push(Url {
2020-11-23 09:25:21 +00:00
loc: "https://edgarluque.com/blog/some-future-post".to_owned(),
2020-11-22 14:43:41 +00:00
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);
}
```
2020-11-22 14:47:10 +00:00
Prints the following:
```xml
<?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>
```