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

Archive #96

Merged
merged 4 commits into from
Oct 29, 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
1 change: 1 addition & 0 deletions .github/marmite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ menu:
- ['Docs', 'tag-docs.html']
- ['Contributors', 'contributors.html']
- ['Tags', 'tags.html']
- ['Archive', 'archive.html']
- ['Github', 'https://github.com/rochacbruno/marmite/']
extra:
comments:
Expand Down
1 change: 1 addition & 0 deletions example/marmite.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ menu:
- ["About", "about.html"]
- ["Pages", "pages.html"]
- ["Tags", "tags.html"]
- ["Archive", "archive.html"]
- ["Github", "https://github.com/rochacbruno/marmite"]


Expand Down
2 changes: 1 addition & 1 deletion example/templates/group.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<article class="group-list">
<ul class="content-tags">
{% for content in group_content -%}
<li><a href="./tag-{{ content.0 | trim | slugify }}.html">{{ content.0 }}</a><span class="tag-count"> [{{content.1}}]</span></li>
<li><a href="./{{link_prefix}}-{{ content.0 | trim | slugify }}.html">{{ content.0 }}</a><span class="tag-count"> [{{content.1}}]</span></li>
{%- endfor %}
</ul>
</article>
Expand Down
7 changes: 7 additions & 0 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ pub struct Marmite {
pub tags_content_title: String,
#[serde(default = "default_archives_title")]
pub archives_title: String,
#[serde(default = "default_archives_content_title")]
pub archives_content_title: String,

#[serde(default = "default_content_path")]
pub content_path: String,
Expand Down Expand Up @@ -89,6 +91,10 @@ fn default_archives_title() -> String {
"Archive".to_string()
}

fn default_archives_content_title() -> String {
"Posts from '$year'".to_string()
}

fn default_site_path() -> String {
String::new()
}
Expand Down Expand Up @@ -121,6 +127,7 @@ fn default_menu() -> Option<Vec<(String, String)>> {
vec![
("Pages".to_string(), "pages.html".to_string()),
("Tags".to_string(), "tags.html".to_string()),
("Archive".to_string(), "archive.html".to_string()),
]
.into()
}
Expand Down
2 changes: 1 addition & 1 deletion src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub fn get_title<'a>(frontmatter: &'a Frontmatter, html: &'a str) -> String {
}
}

pub fn get_description<'a>(frontmatter: &'a Frontmatter) -> Option<String> {
pub fn get_description(frontmatter: &Frontmatter) -> Option<String> {
if let Some(description) = frontmatter.get("description") {
return Some(description.to_string());
}
Expand Down
52 changes: 52 additions & 0 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use crate::embedded::{generate_static, EMBEDDED_TERA};
use crate::markdown::{get_content, process_file};
use crate::server;
use crate::tera_functions::UrlFor;
use chrono::Datelike;
use fs_extra::dir::{copy as dircopy, CopyOptions};
use hotwatch::{Event, EventKind, Hotwatch};
use log::{debug, error, info};
use serde::Serialize;
use std::collections::HashMap;
use std::path::Path;
use std::{fs, process, sync::Arc, sync::Mutex};
use tera::{Context, Tera};
Expand Down Expand Up @@ -287,6 +289,7 @@ fn render_templates(

// Render tagged_contents
handle_tag_pages(output_dir, site_data, &global_context, tera)?;
handle_archive_pages(output_dir, site_data, &global_context, tera)?;

Ok(())
}
Expand Down Expand Up @@ -560,6 +563,7 @@ fn handle_tag_pages(
unique_tags.sort_by(|a, b| b.1.cmp(&a.1));
tag_list_context.insert("group_content", &unique_tags);
tag_list_context.insert("current_page", "tags.html");
tag_list_context.insert("link_prefix", "tag");
render_html(
"group.html",
"tags.html",
Expand All @@ -570,6 +574,54 @@ fn handle_tag_pages(
Ok(())
}

fn handle_archive_pages(
output_dir: &Path,
site_data: &Data,
global_context: &Context,
tera: &Tera,
) -> Result<(), String> {
let mut unique_years: Vec<(String, usize)> = Vec::new();
let mut grouped_posts: HashMap<String, Vec<Content>> = HashMap::new();
let posts = site_data.posts.clone();
for post in posts {
if let Some(date) = post.date {
let year = date.year().to_string();
grouped_posts.entry(year).or_default().push(post);
}
}

// render each year page
for (year, contents) in &grouped_posts {
handle_list_page(
global_context,
&site_data.site.archives_content_title.replace("$year", year),
contents,
site_data,
tera,
output_dir,
format!("archive-{year}").as_ref(),
)?;
unique_years.push((year.to_owned(), contents.len()));
}

// Render archive.html group page
unique_years.sort_by(|a, b| b.cmp(a));
let mut archive_context = global_context.clone();
archive_context.insert("title", &site_data.site.archives_title);
archive_context.insert("group_content", &unique_years);
archive_context.insert("current_page", "archive.html");
archive_context.insert("link_prefix", "archive");
render_html(
"group.html",
"archive.html",
tera,
&archive_context,
output_dir,
)?;

Ok(())
}

fn render_html(
template: &str,
filename: &str,
Expand Down