rename "old urls" to "alternate urls"

This commit is contained in:
Gered 2023-06-28 17:12:36 -04:00
parent 7ad5beb717
commit 1fbdab9c58
4 changed files with 22 additions and 22 deletions

View file

@ -9,7 +9,7 @@
"file_path": "joke.md", "file_path": "joke.md",
"title": "Joke", "title": "Joke",
"url": "/joke/", "url": "/joke/",
"old_urls": ["/trying-to-be-funny/"] "alternate_urls": ["/trying-to-be-funny/"]
} }
] ]
} }

View file

@ -13,7 +13,7 @@
"date": "2023-02-01", "date": "2023-02-01",
"slug": "commonmark-testing", "slug": "commonmark-testing",
"tags": ["testing"], "tags": ["testing"],
"old_urls": ["/testing/commonmark/"] "alternate_urls": ["/testing/commonmark/"]
}, },
{ {
"file_path": "2023-03-20-lorem-ipsum.md", "file_path": "2023-03-20-lorem-ipsum.md",

View file

@ -25,7 +25,7 @@ pub struct Page {
pub file_path: PathBuf, pub file_path: PathBuf,
pub title: String, pub title: String,
pub url: String, pub url: String,
pub old_urls: Option<Vec<String>>, pub alternate_urls: Option<Vec<String>>,
} }
#[derive(Debug, Clone, serde::Deserialize)] #[derive(Debug, Clone, serde::Deserialize)]
@ -40,7 +40,7 @@ pub struct Post {
#[serde(deserialize_with = "crate::util::deserialize_string_to_naivedatetime")] #[serde(deserialize_with = "crate::util::deserialize_string_to_naivedatetime")]
pub date: chrono::NaiveDateTime, pub date: chrono::NaiveDateTime,
pub slug: String, pub slug: String,
pub old_urls: Option<Vec<String>>, pub alternate_urls: Option<Vec<String>>,
pub tags: Option<Vec<String>>, pub tags: Option<Vec<String>>,
} }

View file

@ -45,28 +45,28 @@ pub enum SiteError {
TeraError(#[from] tera::Error), TeraError(#[from] tera::Error),
} }
pub struct OldUrlMappings { pub struct AlternateUrlMappings {
mapping: HashMap<UriPath, UriPath>, mapping: HashMap<UriPath, UriPath>,
} }
impl OldUrlMappings { impl AlternateUrlMappings {
pub fn new() -> Self { pub fn new() -> Self {
OldUrlMappings { mapping: HashMap::new() } AlternateUrlMappings { mapping: HashMap::new() }
} }
#[inline] #[inline]
pub fn get(&self, old_url: &UriPath) -> Option<&UriPath> { pub fn get(&self, alternate_url: &UriPath) -> Option<&UriPath> {
self.mapping.get(old_url) self.mapping.get(alternate_url)
} }
#[inline] #[inline]
pub fn add_mapping(&mut self, old_url: &UriPath, new_url: &UriPath) { pub fn add_mapping(&mut self, alternate_url: &UriPath, current_url: &UriPath) {
self.mapping.insert(old_url.clone(), new_url.clone()); self.mapping.insert(alternate_url.clone(), current_url.clone());
} }
pub fn add_mappings(&mut self, old_urls: &[UriPath], new_url: &UriPath) { pub fn add_mappings(&mut self, alternate_urls: &[UriPath], current_url: &UriPath) {
for old_url in old_urls.iter() { for url in alternate_urls.iter() {
self.add_mapping(old_url, new_url); self.add_mapping(url, current_url);
} }
} }
} }
@ -176,14 +176,14 @@ pub struct SiteContent {
pub posts: Vec<Post>, pub posts: Vec<Post>,
pub pages_by_url: HashMap<UriPath, usize>, pub pages_by_url: HashMap<UriPath, usize>,
pub posts_by_url: HashMap<UriPath, usize>, pub posts_by_url: HashMap<UriPath, usize>,
pub old_url_mappings: OldUrlMappings, pub alternate_url_mappings: AlternateUrlMappings,
pub post_tag_mappings: PostsByTag, pub post_tag_mappings: PostsByTag,
pub rss: RssMetadata, pub rss: RssMetadata,
} }
impl SiteContent { impl SiteContent {
pub fn new(pages_config: config::Pages, posts_config: config::Posts) -> Result<Self, SiteError> { pub fn new(pages_config: config::Pages, posts_config: config::Posts) -> Result<Self, SiteError> {
let mut old_url_mappings = OldUrlMappings::new(); let mut alternate_url_mappings = AlternateUrlMappings::new();
let mut post_tag_mappings = PostsByTag::new(); let mut post_tag_mappings = PostsByTag::new();
// load pages // load pages
@ -192,8 +192,8 @@ impl SiteContent {
for (index, page_config) in pages_config.pages.iter().enumerate() { for (index, page_config) in pages_config.pages.iter().enumerate() {
let page = Page::try_from(page_config.clone())?; let page = Page::try_from(page_config.clone())?;
if let Some(old_urls) = &page_config.old_urls { if let Some(old_urls) = &page_config.alternate_urls {
old_url_mappings.add_mappings(old_urls, &page.url); alternate_url_mappings.add_mappings(old_urls, &page.url);
} }
pages_by_url.insert(page.url.clone(), index); pages_by_url.insert(page.url.clone(), index);
@ -207,8 +207,8 @@ impl SiteContent {
for (index, post_config) in posts_config.posts.iter().sorted_by(|a, b| b.date.cmp(&a.date)).enumerate() { for (index, post_config) in posts_config.posts.iter().sorted_by(|a, b| b.date.cmp(&a.date)).enumerate() {
let post = Post::try_from(post_config.clone())?; let post = Post::try_from(post_config.clone())?;
if let Some(old_urls) = &post_config.old_urls { if let Some(old_urls) = &post_config.alternate_urls {
old_url_mappings.add_mappings(old_urls, &post.url); alternate_url_mappings.add_mappings(old_urls, &post.url);
} }
posts_by_url.insert(post.url.clone(), index); posts_by_url.insert(post.url.clone(), index);
@ -218,7 +218,7 @@ impl SiteContent {
let rss = RssMetadata::from(posts_config.rss); let rss = RssMetadata::from(posts_config.rss);
Ok(SiteContent { pages, posts, pages_by_url, posts_by_url, old_url_mappings, post_tag_mappings, rss }) Ok(SiteContent { pages, posts, pages_by_url, posts_by_url, alternate_url_mappings, post_tag_mappings, rss })
} }
pub fn get_page_by_url(&self, url: &UriPath) -> Option<&Page> { pub fn get_page_by_url(&self, url: &UriPath) -> Option<&Page> {
@ -230,7 +230,7 @@ impl SiteContent {
} }
pub fn get_content_at(&self, url: &UriPath) -> Option<Content> { pub fn get_content_at(&self, url: &UriPath) -> Option<Content> {
if let Some(new_url) = self.old_url_mappings.get(url) { if let Some(new_url) = self.alternate_url_mappings.get(url) {
Some(Content::Redirect(new_url.clone())) Some(Content::Redirect(new_url.clone()))
} else if let Some(post) = self.get_post_by_url(url) { } else if let Some(post) = self.get_post_by_url(url) {
Some(Content::Post(post)) Some(Content::Post(post))