mirror of
https://github.com/edg-l/sitewriter.git
synced 2024-09-19 09:12:24 +00:00
remove feature abuse
This commit is contained in:
parent
ccfe1c57a3
commit
e53654d64e
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
name = "sitewriter"
|
name = "sitewriter"
|
||||||
version = "0.2.4"
|
version = "0.3.0"
|
||||||
authors = ["Edgar L. <contact@edgarluque.com>"]
|
authors = ["Edgar <git@edgarluque.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "A sitemap writing library."
|
description = "A sitemap writing library."
|
||||||
readme = "README.md"
|
readme = "README.md"
|
||||||
|
@ -11,9 +11,6 @@ license = "MIT"
|
||||||
keywords = ["sitemap", "sitemapindex", "xml", "parser"]
|
keywords = ["sitemap", "sitemapindex", "xml", "parser"]
|
||||||
categories = ["parsing"]
|
categories = ["parsing"]
|
||||||
|
|
||||||
[features]
|
|
||||||
default = ["chrono"]
|
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
chrono = { version = "0.4", optional = true }
|
chrono = "0.4"
|
||||||
quick-xml = "0.20"
|
quick-xml = "0.20"
|
||||||
|
|
|
@ -3,7 +3,9 @@ use sitewriter::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let mut sitemap = Sitemap::new();
|
let mut sitemap = Sitemap::new();
|
||||||
sitemap.urls.push(Url::new("https://edgarluque.com/projects".to_owned()));
|
sitemap
|
||||||
|
.urls
|
||||||
|
.push(Url::new("https://edgarluque.com/projects".to_owned()));
|
||||||
|
|
||||||
sitemap.urls.push(Url {
|
sitemap.urls.push(Url {
|
||||||
loc: "https://edgarluque.com/".to_owned(),
|
loc: "https://edgarluque.com/".to_owned(),
|
||||||
|
@ -30,10 +32,11 @@ fn main() {
|
||||||
loc: "https://edgarluque.com/blog/some-future-post".to_owned(),
|
loc: "https://edgarluque.com/blog/some-future-post".to_owned(),
|
||||||
changefreq: Some(ChangeFreq::Never),
|
changefreq: Some(ChangeFreq::Never),
|
||||||
priority: Some(0.5),
|
priority: Some(0.5),
|
||||||
lastmod: Some(Utc.from_utc_datetime(&Local.ymd(2020, 12, 5).and_hms(12, 30, 0).naive_utc())),
|
lastmod: Some(
|
||||||
|
Utc.from_utc_datetime(&Local.ymd(2020, 12, 5).and_hms(12, 30, 0).naive_utc()),
|
||||||
|
),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
let result = sitemap.into_str();
|
let result = sitemap.into_str();
|
||||||
println!("{}", result);
|
println!("{}", result);
|
||||||
}
|
}
|
||||||
|
|
105
src/lib.rs
105
src/lib.rs
|
@ -1,5 +1,5 @@
|
||||||
//! ## A rust library to generate sitemaps.
|
//! ## A rust library to generate sitemaps.
|
||||||
//!
|
//!
|
||||||
//! It uses the [quick-xml](https://github.com/tafia/quick-xml) so it should be fast.
|
//! It uses the [quick-xml](https://github.com/tafia/quick-xml) so it should be fast.
|
||||||
//!
|
//!
|
||||||
//! To handle the [`Url::lastmod`] tag it uses [chrono](https://docs.rs/chrono/) but it can be disabled with `default-features = false`.
|
//! To handle the [`Url::lastmod`] tag it uses [chrono](https://docs.rs/chrono/) but it can be disabled with `default-features = false`.
|
||||||
|
@ -9,47 +9,46 @@
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use chrono::prelude::*;
|
//! use chrono::prelude::*;
|
||||||
//! use sitewriter::*;
|
//! use sitewriter::*;
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn main() {
|
||||||
//! let mut sitemap = Sitemap::new();
|
//! let mut sitemap = Sitemap::new();
|
||||||
//! sitemap.urls.push(Url::new("https://edgarluque.com/projects".to_owned()));
|
//! sitemap.urls.push(Url::new("https://edgarluque.com/projects".to_owned()));
|
||||||
//!
|
//!
|
||||||
//! sitemap.urls.push(Url {
|
//! sitemap.urls.push(Url {
|
||||||
//! loc: "https://edgarluque.com/".to_owned(),
|
//! loc: "https://edgarluque.com/".to_owned(),
|
||||||
//! changefreq: Some(ChangeFreq::Daily),
|
//! changefreq: Some(ChangeFreq::Daily),
|
||||||
//! priority: Some(1.0),
|
//! priority: Some(1.0),
|
||||||
//! lastmod: Some(Utc::now()),
|
//! lastmod: Some(Utc::now()),
|
||||||
//! });
|
//! });
|
||||||
//!
|
//!
|
||||||
//! sitemap.urls.push(Url {
|
//! sitemap.urls.push(Url {
|
||||||
//! loc: "https://edgarluque.com/blog".to_owned(),
|
//! loc: "https://edgarluque.com/blog".to_owned(),
|
||||||
//! changefreq: Some(ChangeFreq::Weekly),
|
//! changefreq: Some(ChangeFreq::Weekly),
|
||||||
//! priority: Some(0.8),
|
//! priority: Some(0.8),
|
||||||
//! lastmod: Some(Utc::now()),
|
//! lastmod: Some(Utc::now()),
|
||||||
//! });
|
//! });
|
||||||
//!
|
//!
|
||||||
//! sitemap.urls.push(Url {
|
//! sitemap.urls.push(Url {
|
||||||
//! loc: "https://edgarluque.com/blog/sitewriter".to_owned(),
|
//! loc: "https://edgarluque.com/blog/sitewriter".to_owned(),
|
||||||
//! changefreq: Some(ChangeFreq::Never),
|
//! changefreq: Some(ChangeFreq::Never),
|
||||||
//! priority: Some(0.5),
|
//! priority: Some(0.5),
|
||||||
//! lastmod: Some(Utc.ymd(2020, 11, 22).and_hms(15, 10, 15)),
|
//! lastmod: Some(Utc.ymd(2020, 11, 22).and_hms(15, 10, 15)),
|
||||||
//! });
|
//! });
|
||||||
//!
|
//!
|
||||||
//! sitemap.urls.push(Url {
|
//! sitemap.urls.push(Url {
|
||||||
//! loc: "https://edgarluque.com/blog/some-future-post".to_owned(),
|
//! loc: "https://edgarluque.com/blog/some-future-post".to_owned(),
|
||||||
//! changefreq: Some(ChangeFreq::Never),
|
//! changefreq: Some(ChangeFreq::Never),
|
||||||
//! priority: Some(0.5),
|
//! priority: Some(0.5),
|
||||||
//! lastmod: Some(Utc.from_utc_datetime(&Local.ymd(2020, 12, 5).and_hms(12, 30, 0).naive_utc())),
|
//! lastmod: Some(Utc.from_utc_datetime(&Local.ymd(2020, 12, 5).and_hms(12, 30, 0).naive_utc())),
|
||||||
//! });
|
//! });
|
||||||
//!
|
//!
|
||||||
//!
|
//!
|
||||||
//! let result = sitemap.into_str();
|
//! let result = sitemap.into_str();
|
||||||
//! println!("{}", result);
|
//! println!("{}", result);
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|
||||||
#[cfg(feature = "chrono")]
|
use chrono::{DateTime, SecondsFormat, Utc};
|
||||||
use chrono::{DateTime, Utc, SecondsFormat};
|
|
||||||
|
|
||||||
use quick_xml::{
|
use quick_xml::{
|
||||||
events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event},
|
events::{BytesDecl, BytesEnd, BytesStart, BytesText, Event},
|
||||||
|
@ -58,7 +57,6 @@ use quick_xml::{
|
||||||
|
|
||||||
use std::fmt::Display;
|
use std::fmt::Display;
|
||||||
use std::io::Cursor;
|
use std::io::Cursor;
|
||||||
use std::borrow::Cow;
|
|
||||||
|
|
||||||
/// How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.
|
/// How frequently the page is likely to change. This value provides general information to search engines and may not correlate exactly to how often they crawl the page.
|
||||||
#[derive(Debug, Eq, PartialEq)]
|
#[derive(Debug, Eq, PartialEq)]
|
||||||
|
@ -101,14 +99,8 @@ pub struct Url {
|
||||||
///
|
///
|
||||||
/// This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.
|
/// This URL must begin with the protocol (such as http) and end with a trailing slash, if your web server requires it. This value must be less than 2,048 characters.
|
||||||
pub loc: String,
|
pub loc: String,
|
||||||
#[cfg(feature = "chrono")]
|
|
||||||
/// The date of last modification of the file.
|
/// The date of last modification of the file.
|
||||||
pub lastmod: Option<DateTime<Utc>>,
|
pub lastmod: Option<DateTime<Utc>>,
|
||||||
#[cfg(not(feature = "chrono"))]
|
|
||||||
/// The date of last modification of the file.
|
|
||||||
///
|
|
||||||
/// This date should be in W3C Datetime format. This format allows you to omit the time portion, if desired, and use YYYY-MM-DD.
|
|
||||||
pub lastmod: Option<String>,
|
|
||||||
/// How frequently the page is likely to change.
|
/// How frequently the page is likely to change.
|
||||||
pub changefreq: Option<ChangeFreq>,
|
pub changefreq: Option<ChangeFreq>,
|
||||||
/// The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
|
/// The priority of this URL relative to other URLs on your site. Valid values range from 0.0 to 1.0.
|
||||||
|
@ -124,7 +116,7 @@ impl Url {
|
||||||
loc,
|
loc,
|
||||||
lastmod: None,
|
lastmod: None,
|
||||||
changefreq: None,
|
changefreq: None,
|
||||||
priority: None
|
priority: None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -151,9 +143,7 @@ fn write_tag<T: std::io::Write>(writer: &mut Writer<T>, tag: &str, text: &str) {
|
||||||
impl Sitemap {
|
impl Sitemap {
|
||||||
/// Create a new sitemap.
|
/// Create a new sitemap.
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Self {
|
Self { urls: Vec::new() }
|
||||||
urls: Vec::new(),
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates the sitemap using the provided writer.
|
/// Generates the sitemap using the provided writer.
|
||||||
|
@ -181,17 +171,12 @@ impl Sitemap {
|
||||||
.expect("error opening url");
|
.expect("error opening url");
|
||||||
write_tag(&mut writer, "loc", &url.loc);
|
write_tag(&mut writer, "loc", &url.loc);
|
||||||
|
|
||||||
#[cfg(feature = "chrono")]
|
if let Some(lastmod) = &url.lastmod {
|
||||||
{
|
write_tag(
|
||||||
if let Some(lastmod) = &url.lastmod {
|
&mut writer,
|
||||||
write_tag(&mut writer, "lastmod", &lastmod.to_rfc3339_opts(SecondsFormat::Secs, true));
|
"lastmod",
|
||||||
}
|
&lastmod.to_rfc3339_opts(SecondsFormat::Secs, true),
|
||||||
}
|
);
|
||||||
#[cfg(not(feature = "chrono"))]
|
|
||||||
{
|
|
||||||
if let Some(lastmod) = &url.lastmod {
|
|
||||||
write_tag(&mut writer, "lastmod", lastmod);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
if let Some(priority) = &url.priority {
|
if let Some(priority) = &url.priority {
|
||||||
write_tag(&mut writer, "priority", &format!("{:.1}", priority))
|
write_tag(&mut writer, "priority", &format!("{:.1}", priority))
|
||||||
|
@ -213,17 +198,17 @@ impl Sitemap {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates the sitemap.
|
/// Generates the sitemap.
|
||||||
pub fn into_bytes(&self) -> Cow<[u8]> {
|
pub fn into_bytes(self) -> Vec<u8> {
|
||||||
let inner = Cursor::new(Vec::new());
|
let inner = Cursor::new(Vec::new());
|
||||||
let result = self.generate(inner);
|
let result = self.generate(inner);
|
||||||
Cow::Owned(result.into_inner())
|
result.into_inner()
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Generates the sitemap returning a string.
|
/// Generates the sitemap returning a string.
|
||||||
pub fn into_str(&self) -> Cow<str> {
|
pub fn into_str(self) -> String {
|
||||||
let bytes = self.into_bytes();
|
let bytes = self.into_bytes();
|
||||||
let res = std::str::from_utf8(&bytes).expect("error parsing sitemap bytes to str").to_owned();
|
let res = std::str::from_utf8(&bytes).expect("error parsing sitemap bytes to str");
|
||||||
Cow::Owned(res)
|
res.to_owned()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -237,15 +222,30 @@ mod tests {
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
|
|
||||||
let mut sitemap = Sitemap::new();
|
let mut sitemap = Sitemap::new();
|
||||||
sitemap.urls.push(Url::new("https://domain.com/".to_owned()));
|
sitemap
|
||||||
|
.urls
|
||||||
|
.push(Url::new("https://domain.com/".to_owned()));
|
||||||
|
|
||||||
sitemap.urls.push(Url {
|
sitemap.urls.push(Url {
|
||||||
loc: "https://domain.com/url".to_owned(),
|
loc: "https://domain.com/url".to_owned(),
|
||||||
changefreq: Some(ChangeFreq::Daily),
|
changefreq: Some(ChangeFreq::Daily),
|
||||||
priority: Some(0.8),
|
priority: Some(0.8),
|
||||||
lastmod: Some(Utc::now())
|
lastmod: Some(Utc::now()),
|
||||||
}
|
});
|
||||||
);
|
|
||||||
|
sitemap.urls.push(Url {
|
||||||
|
loc: "https://domain.com/aa".to_owned(),
|
||||||
|
changefreq: Some(ChangeFreq::Monthly),
|
||||||
|
priority: None,
|
||||||
|
lastmod: None,
|
||||||
|
});
|
||||||
|
|
||||||
|
sitemap.urls.push(Url {
|
||||||
|
loc: "https://domain.com/bb".to_owned(),
|
||||||
|
changefreq: None,
|
||||||
|
priority: None,
|
||||||
|
lastmod: None,
|
||||||
|
});
|
||||||
|
|
||||||
sitemap.into_str();
|
sitemap.into_str();
|
||||||
}
|
}
|
||||||
|
@ -260,21 +260,4 @@ mod tests {
|
||||||
assert_eq!(format!("{}", ChangeFreq::Yearly), "yearly");
|
assert_eq!(format!("{}", ChangeFreq::Yearly), "yearly");
|
||||||
assert_eq!(format!("{}", ChangeFreq::Never), "never");
|
assert_eq!(format!("{}", ChangeFreq::Never), "never");
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(not(feature = "chrono"))]
|
|
||||||
#[test]
|
|
||||||
fn it_works() {
|
|
||||||
let mut sitemap = Sitemap::new();
|
|
||||||
sitemap.urls.push(Url::new("https://domain.com/".to_owned()));
|
|
||||||
|
|
||||||
sitemap.urls.push(Url {
|
|
||||||
loc: "https://domain.com/url".to_owned(),
|
|
||||||
changefreq: Some(ChangeFreq::Daily),
|
|
||||||
priority: Some(0.8),
|
|
||||||
lastmod: Some("2020-11-22".to_owned()),
|
|
||||||
}
|
|
||||||
);
|
|
||||||
|
|
||||||
sitemap.into_str();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue