From 40392cd6423d9956bc8227a9b83f83d00383c8ed Mon Sep 17 00:00:00 2001 From: Indigo Curnick Date: Sun, 23 Jun 2024 18:56:01 +0100 Subject: [PATCH] completed the sitemap generation code, examples and documentation --- Cargo.toml | 2 +- README.md | 12 +++-- examples/low/main.rs | 31 ++++++++++-- examples/medium/main.rs | 3 +- src/common/mod.rs | 6 +-- src/common/types.rs | 6 ++- src/high/parse.rs | 2 - src/high/types.rs | 19 +------- src/lib.rs | 29 +++++++---- src/low/types.rs | 93 +++++++++++++++++++++++++++-------- src/medium/parse.rs | 70 +-------------------------- src/medium/types.rs | 94 ++++++++++++++++++++++++------------ src/sitemap/mod.rs | 6 +++ src/{ => sitemap}/sitemap.rs | 49 ++++--------------- src/sitemap/types.rs | 45 +++++++++++++++++ src/types.rs | 47 +++++++++++++++++- tests/mod.rs | 14 ------ 17 files changed, 308 insertions(+), 220 deletions(-) create mode 100644 src/sitemap/mod.rs rename src/{ => sitemap}/sitemap.rs (73%) create mode 100644 src/sitemap/types.rs delete mode 100644 tests/mod.rs diff --git a/Cargo.toml b/Cargo.toml index 390c47c..1b39cc8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "blog-tools" -version = "0.1.2" +version = "0.2.0" edition = "2021" authors = ["Indigo Curnick "] description = "A collection of tools that helps make blogs in Rust" diff --git a/README.md b/README.md index 9537bda..a35960f 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ next to a `blog.json`. The JSON must conform to the following schema -```rust,ignore +```json,ignore { "title": String, "date": ISO 8601 Date i.e. YYYY-MM-DD, @@ -27,7 +27,9 @@ The JSON must conform to the following schema "keywords": Optional<[String]>, "canonical_link": Optional, "author_name": Optional, -"author_webpage": Optional +"author_webpage": Optional, +"last_modified": Optional, (ISO 8601) +"priority": Optional } ``` @@ -36,7 +38,6 @@ The JSON must conform to the following schema In `blog-tools` all slugs are /{date}/{sub-slug}. Make sure the "slug" filed in the JSON is *just* the final sub-slug -- `blog-tools` will automatically handle the date for you ## How This Crate is Organised @@ -56,7 +57,7 @@ the best way to do this is using a lazy static like so ```rust,ignore lazy_static! { pub static ref STATIC_BLOG_ENTRIES: HighBlog = - get_high_blog(PathBuf::from(BLOG_ROOT), None, None); + get_high_blog(PathBuf::from(BLOG_ROOT), None, None, URL, &SitemapOptions::default()); } ``` @@ -66,7 +67,7 @@ blog posts themselves. These will need to be rendered when requested ```rust,ignore lazy_static! { pub static ref STATIC_BLOG_ENTRIES: MediumBlog = - get_medium_blog(PathBuf::from(BLOG_ROOT), None, None); + get_medium_blog(PathBuf::from(BLOG_ROOT), None, None, URL, &SitemapOptions::default()); } let this_blog = match all_blogs.hash.get(&complete_slug) { @@ -88,6 +89,7 @@ everything at runtime. let preview = preview_blogs(PathBuf::from_str(BLOG_ROOT).unwrap(), 2, None); let tags = get_blog_tag_list(PathBuf::from_str(BLOG_ROOT).unwrap()); let blog_post = render_blog_post(PathBuf::from_str(BLOG_ROOT).unwrap(), date, slug, None).unwrap(); +let sitemap = create_sitemap(BLOG_ROOT, URL, &SitemapOptions::default()); ``` This method can have serious runtime performance implecations, but might be diff --git a/examples/low/main.rs b/examples/low/main.rs index e87526e..33de822 100644 --- a/examples/low/main.rs +++ b/examples/low/main.rs @@ -1,14 +1,17 @@ -use blog_tools::low::{ - get_blog_tag_list, preview_blogs, preview_blogs_tagged, render_blog_post, PreviewBlogEntry, +use blog_tools::{ + low::{ + get_blog_tag_list, preview_blogs, preview_blogs_tagged, render_blog_post, PreviewBlogEntry, + }, + sitemap::{create_sitemap, SitemapOptions}, }; use rocket::{ fs::{relative, FileServer}, - response::Redirect, + response::{content::RawXml, Redirect}, Request, Route, }; use rocket_dyn_templates::Template; use serde::{Deserialize, Serialize}; -use std::{path::PathBuf, str::FromStr}; +use std::{fs, path::PathBuf, str::FromStr}; pub static BLOG_ROOT: &str = "examples/blog"; @@ -82,6 +85,24 @@ fn tag_page(slug: String) -> Option