Skip to content

Commit

Permalink
Merge branch 'feat-sitemap'
Browse files Browse the repository at this point in the history
  • Loading branch information
IndigoCurnick committed Jun 23, 2024
2 parents bc7b199 + 40392cd commit 5f126d2
Show file tree
Hide file tree
Showing 27 changed files with 1,597 additions and 973 deletions.
3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blog-tools"
version = "0.1.2"
version = "0.2.0"
edition = "2021"
authors = ["Indigo Curnick <indigocurnick@gmail.com>"]
description = "A collection of tools that helps make blogs in Rust"
Expand All @@ -17,6 +17,7 @@ walkdir = "2.3.2"
chrono = { version = "0.4.23", features = ["serde"] }
markdown = "1.0.0-alpha.11"
tl = "0.7.8"
xml = "0.8.20"


[dev-dependencies]
Expand Down
12 changes: 7 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -27,7 +27,9 @@ The JSON must conform to the following schema
"keywords": Optional<[String]>,
"canonical_link": Optional<String>,
"author_name": Optional<String>,
"author_webpage": Optional<String>
"author_webpage": Optional<String>,
"last_modified": Optional<Date>, (ISO 8601)
"priority": Optional<float>
}
```

Expand All @@ -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

Expand All @@ -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());
}
```

Expand All @@ -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) {
Expand All @@ -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
Expand Down
Binary file added examples/assets/favicon.ico
Binary file not shown.
31 changes: 26 additions & 5 deletions examples/high/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
use std::fs;
use std::path::PathBuf;

use blog_tools::high::{get_high_blog, HighBlog, HighBlogEntry};
use blog_tools::sitemap::SitemapOptions;
use blog_tools::Blog;
use lazy_static::lazy_static;
use rocket::{
fs::{relative, FileServer},
response::Redirect,
response::{content::RawXml, Redirect},
Request, Route,
};
use rocket_dyn_templates::Template;
Expand Down Expand Up @@ -41,6 +44,13 @@ fn blog_index() -> Option<Template> {
Some(Template::render("blog_index", context.into_json()))
}

#[get("/sitemap.xml")]
fn sitemap() -> RawXml<String> {
let blog = get_blog_context();

return RawXml(blog.sitemap.clone());
}

#[get("/blog/<date>/<slug>", rank = 2)]
fn blog_article(date: String, slug: String) -> Option<Template> {
let mut context = rocket_dyn_templates::tera::Context::new();
Expand All @@ -62,7 +72,7 @@ fn tag_page(slug: String) -> Option<Template> {
let mut these_blogs: Vec<&HighBlogEntry> = vec![];

for blog in &all_blogs.entries {
if blog.tags.contains(&slug) {
if blog.get_tags().contains(&slug) {
these_blogs.push(&blog);
}
}
Expand All @@ -87,14 +97,25 @@ async fn error(req: &Request<'_>) -> Redirect {
}

fn get_all_routes() -> Vec<Route> {
return routes![blog_index, blog_article, tag_page];
return routes![blog_index, blog_article, tag_page, sitemap];
}

pub static BLOG_ROOT: &str = "examples/blog";
pub static URL: &str = "www.example.xyz";

lazy_static! {
pub static ref STATIC_BLOG_ENTRIES: HighBlog =
get_high_blog(PathBuf::from(BLOG_ROOT), None, None).unwrap();
pub static ref STATIC_BLOG_ENTRIES: HighBlog = get_high_blog(
PathBuf::from(BLOG_ROOT),
None,
None,
&URL.to_string(),
&SitemapOptions {
include_tags: true,
sitemap_base: Some(fs::read_to_string("examples/xml/sitemap.xml").unwrap()),
..Default::default()
}
)
.unwrap();
}

fn get_blog_context() -> &'static HighBlog {
Expand Down
31 changes: 26 additions & 5 deletions examples/low/main.rs
Original file line number Diff line number Diff line change
@@ -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";

Expand Down Expand Up @@ -82,6 +85,24 @@ fn tag_page(slug: String) -> Option<Template> {
Some(Template::render("tags", context.into_json()))
}

pub static URL: &str = "www.example.xyz";

#[get("/sitemap.xml")]
fn sitemap() -> RawXml<String> {
let base_sitemap = fs::read_to_string("examples/xml/sitemap.xml").unwrap();
let sitemap = create_sitemap(
BLOG_ROOT,
&URL.to_string(),
&SitemapOptions {
include_tags: true,
sitemap_base: Some(base_sitemap),
..Default::default()
},
);

return RawXml(sitemap.unwrap());
}

#[catch(404)]
async fn not_found(req: &Request<'_>) -> Redirect {
let mut context = rocket_dyn_templates::tera::Context::new();
Expand All @@ -97,5 +118,5 @@ async fn error(req: &Request<'_>) -> Redirect {
}

fn get_all_routes() -> Vec<Route> {
return routes![blog_index, blog_article, tag_page];
return routes![blog_index, blog_article, tag_page, sitemap];
}
36 changes: 29 additions & 7 deletions examples/medium/main.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
use std::{path::PathBuf, str::FromStr};
use std::{fs, path::PathBuf, str::FromStr};

use blog_tools::medium::{get_medium_blog, MediumBlog, MediumBlogEntry};
use blog_tools::{
medium::{get_medium_blog, MediumBlog, MediumBlogEntry},
sitemap::SitemapOptions,
Blog,
};
use lazy_static::lazy_static;
use rocket::{
fs::{relative, FileServer},
response::Redirect,
response::{content::RawXml, Redirect},
Request, Route,
};
use rocket_dyn_templates::Template;
Expand Down Expand Up @@ -34,6 +38,13 @@ async fn main() {
}
}

#[get("/sitemap.xml")]
fn sitemap() -> RawXml<String> {
let blog = get_blog_context();

return RawXml(blog.sitemap.clone());
}

#[get("/blog")]
fn blog_index() -> Option<Template> {
let mut context = rocket_dyn_templates::tera::Context::new();
Expand Down Expand Up @@ -68,7 +79,7 @@ fn tag_page(slug: String) -> Option<Template> {
let mut these_blogs: Vec<&MediumBlogEntry> = vec![];

for blog in &all_blogs.entries {
if blog.tags.contains(&slug) {
if blog.get_tags().contains(&slug) {
these_blogs.push(&blog);
}
}
Expand All @@ -93,14 +104,25 @@ async fn error(req: &Request<'_>) -> Redirect {
}

fn get_all_routes() -> Vec<Route> {
return routes![blog_index, blog_article, tag_page];
return routes![blog_index, blog_article, tag_page, sitemap];
}

pub static BLOG_ROOT: &str = "examples/blog";
pub static URL: &str = "www.example.xyz";

lazy_static! {
pub static ref STATIC_BLOG_ENTRIES: MediumBlog =
get_medium_blog(PathBuf::from(BLOG_ROOT), None, None).unwrap();
pub static ref STATIC_BLOG_ENTRIES: MediumBlog = get_medium_blog(
PathBuf::from(BLOG_ROOT),
None,
None,
&URL.to_string(),
&SitemapOptions {
include_tags: true,
sitemap_base: Some(fs::read_to_string("examples/xml/sitemap.xml").unwrap()),
..Default::default()
}
)
.unwrap();
}

fn get_blog_context() -> &'static MediumBlog {
Expand Down
7 changes: 7 additions & 0 deletions examples/xml/sitemap.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<urlset>
<url>
<loc>www.example.xyz</loc>
<lastmod>19-08-2023</lastmod>
<priority>1.0</priority>
</url>
</urlset>
75 changes: 0 additions & 75 deletions out.txt

This file was deleted.

Loading

0 comments on commit 5f126d2

Please sign in to comment.