Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate sitemap and robots.txt #30

Merged
merged 1 commit into from
Jun 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ form_urlencoded = "1.2.1"
anyhow = "1.0.86"
thiserror = "1.0.61"
fs_extra = "1.3.0"
xml = "0.8.20"
7 changes: 7 additions & 0 deletions configuration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,10 @@ markdown:

# Copy everything in this directory to the output directory
static_path: "static"

# Baseurl
base_url: "http://vincent.jousse.org"

author: "Vincent Jousse"

website_title: "Vince's"
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pub struct Settings {
pub create_index_for: Vec<String>,
pub markdown: markup::Markdown,
pub static_path: String,
pub base_url: String,
pub author: String,
pub website_title: String,
}

pub fn get_configuration() -> Result<Settings, config::ConfigError> {
Expand Down
24 changes: 0 additions & 24 deletions src/configuration.rs

This file was deleted.

17 changes: 14 additions & 3 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use crate::post::{FrontMatter, Post, PostStatus};
use crate::rss::write_atom_for_posts;
use crate::site::Site;
use crate::sitemap::write_sitemap_for_posts;
use anyhow::Result;
use form_urlencoded::byte_serialize;
use gray_matter::engine::YAML;
Expand Down Expand Up @@ -50,11 +51,21 @@

write_posts_html(&pages, site);

let mut posts_and_pages: Vec<Post> = Vec::new();
posts_and_pages.extend(posts.clone());
posts_and_pages.extend(pages.clone());

write_sitemap_for_posts(
&posts_and_pages,
&site.settings.base_url.as_str(),

Check warning on line 60 in src/content.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/content.rs:60:9 | 60 | &site.settings.base_url.as_str(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `site.settings.base_url.as_str()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
Path::new(&format!("{}/sitemap.xml", &site.settings.output_path)[..]),
)?;

write_atom_for_posts(
&posts,
"https://vincent.jousse.org",
"Vincent Jousse",
"Vince's",
&site.settings.base_url.as_str(),

Check warning on line 66 in src/content.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/content.rs:66:9 | 66 | &site.settings.base_url.as_str(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `site.settings.base_url.as_str()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
&site.settings.author.as_str(),

Check warning on line 67 in src/content.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/content.rs:67:9 | 67 | &site.settings.author.as_str(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `site.settings.author.as_str()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
&site.settings.website_title.as_str(),

Check warning on line 68 in src/content.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/content.rs:68:9 | 68 | &site.settings.website_title.as_str(), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `site.settings.website_title.as_str()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow
Path::new(&format!("{}/atom.xml", &site.settings.output_path)[..]),
&site.settings,
)?;
Expand Down Expand Up @@ -187,17 +198,17 @@
for post in posts {
for directory in &post.ancestor_directories_paths {
if let Some(dir_index) = directory.strip_prefix(&site.settings.posts_path) {
if dir_index != "" {

Check warning on line 201 in src/content.rs

View workflow job for this annotation

GitHub Actions / clippy

comparison to empty slice

warning: comparison to empty slice --> src/content.rs:201:20 | 201 | if dir_index != "" { | ^^^^^^^^^^^^^^^ help: using `!is_empty` is clearer and more explicit: `!dir_index.is_empty()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#comparison_to_empty = note: `#[warn(clippy::comparison_to_empty)]` on by default
if site
.settings
.create_index_for
.contains(&dir_index.to_string())
{
let key = dir_index.to_string();
let posts = indexes_to_create.entry(key).or_default();
posts.push(post);
}
}

Check warning on line 211 in src/content.rs

View workflow job for this annotation

GitHub Actions / clippy

this `if` statement can be collapsed

warning: this `if` statement can be collapsed --> src/content.rs:201:17 | 201 | / if dir_index != "" { 202 | | if site 203 | | .settings 204 | | .create_index_for ... | 210 | | } 211 | | } | |_________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#collapsible_if = note: `#[warn(clippy::collapsible_if)]` on by default help: collapse nested if block | 201 ~ if dir_index != "" && site 202 + .settings 203 + .create_index_for 204 + .contains(&dir_index.to_string()) { 205 + let key = dir_index.to_string(); 206 + let posts = indexes_to_create.entry(key).or_default(); 207 + posts.push(post); 208 + } |
};
}
}
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod errors;
pub mod post;
pub mod rss;
pub mod site;
pub mod sitemap;
pub mod startup;
pub mod templates;

Expand Down
69 changes: 69 additions & 0 deletions src/sitemap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
use crate::post::Post;

use anyhow::Result;
use std::fs::File;
use std::path::Path;

use xml::writer::{EmitterConfig, XmlEvent};

pub fn write_sitemap_for_posts(posts: &[Post], base: &str, path: &Path) -> Result<()> {
let file = File::create(path).expect("Unable to create file");
let mut writer = EmitterConfig::new()
.perform_indent(true)
.create_writer(file);

let start_event: XmlEvent = XmlEvent::start_element("urlset")
.attr("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
.into();
writer.write(start_event)?;

let mut sorted_posts = posts.to_vec();
sorted_posts.sort_by(|p1, p2| p1.front_matter.date.cmp(&p2.front_matter.date));

for post in sorted_posts {
let start_event: XmlEvent = XmlEvent::start_element("url").into();
writer.write(start_event)?;

// <loc>
let start_event: XmlEvent = XmlEvent::start_element("loc").into();
writer.write(start_event)?;

// Avoid //
let url = format!(
"{}/{}",
base,
if let "/" = post.url_path.as_str() {
""
} else {
post.url_path.as_str()
}
);
let event = xml::writer::XmlEvent::Characters(url.as_str());
writer.write(event)?;

let end_event: XmlEvent = XmlEvent::end_element().into();
writer.write(end_event)?;

// </loc>

// <lastmod>
let start_event: XmlEvent = XmlEvent::start_element("lastmod").into();
writer.write(start_event)?;

let event = xml::writer::XmlEvent::Characters(post.date_rfc3339.as_str());
writer.write(event)?;

let end_event: XmlEvent = XmlEvent::end_element().into();
writer.write(end_event)?;

// </loc>
let end_event: XmlEvent = XmlEvent::end_element().into();
writer.write(end_event)?;
// </lastmod>
}

let end_event: XmlEvent = XmlEvent::end_element().into();
writer.write(end_event)?;

Ok(())
}
4 changes: 4 additions & 0 deletions static/robots.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
User-agent: *
Allow: /

Sitemap: https://vincent.jousse.org/sitemap.xml
Loading