Skip to content

Commit

Permalink
fix: rebuild when config file changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rochacbruno committed Nov 23, 2024
1 parent 3b5c7d0 commit e6da771
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 62 deletions.
33 changes: 13 additions & 20 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,35 +36,28 @@ impl GroupedContent {
self.map.entry(key)
}

/// Sort tag map by number of contents
/// Sort archive map by date
/// Sort author map by author name
/// Sort stream map by stream name
pub fn sort_all(&mut self) {
for contents in self.map.values_mut() {
contents.sort_by(|a, b| b.date.cmp(&a.date));
}
}

pub fn iter(&self) -> impl Iterator<Item = (&String, Vec<Content>)> {
let mut vec = Vec::new();
// Assuming the content is already sorted by date on a previous call of .sort_all
let mut vec: Vec<(&String, Vec<Content>)> =
self.map.iter().map(|(k, v)| (k, v.clone())).collect();

match self.kind {
Kind::Tag => {
for (tag, contents) in &self.map {
let mut contents = contents.clone();
contents.sort_by(|a, b| b.date.cmp(&a.date));
vec.push((tag, contents));
}
// sort by number of contents
vec.sort_by(|a, b| b.1.len().cmp(&a.1.len()));
}
Kind::Archive => {
for (text, contents) in &self.map {
let mut contents = contents.clone();
contents.sort_by(|a, b| b.date.cmp(&a.date));
vec.push((text, contents));
}
// sort by year, newest first
vec.sort_by(|a, b| b.0.cmp(a.0));
}
Kind::Author | Kind::Stream => {
for (text, contents) in &self.map {
let mut contents = contents.clone();
contents.sort_by(|a, b| b.date.cmp(&a.date));
vec.push((text, contents));
}
// sort alphabetically
vec.sort_by(|a, b| a.0.cmp(b.0));
}
}
Expand Down
60 changes: 18 additions & 42 deletions src/site.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,15 +53,10 @@ impl Data {
pub fn sort_all(&mut self) {
self.posts.sort_by(|a, b| b.date.cmp(&a.date));
self.pages.sort_by(|a, b| b.title.cmp(&a.title));
}

pub fn clear_all(&mut self) {
self.posts.clear();
self.pages.clear();
self.tag.map.clear();
self.archive.map.clear();
self.author.map.clear();
self.stream.map.clear();
self.tag.sort_all();
self.archive.sort_all();
self.author.sort_all();
self.stream.sort_all();
}
}

Expand All @@ -78,8 +73,8 @@ pub fn generate(
config_path: &std::path::PathBuf,
input_folder: &std::path::Path,
output_folder: &Arc<std::path::PathBuf>,
watch: bool, // New parameter for watching,
serve: bool, // Is running on server mode
watch: bool,
serve: bool,
bind_address: &str,
) {
let config_str = fs::read_to_string(config_path).unwrap_or_else(|e| {
Expand All @@ -95,59 +90,40 @@ pub fn generate(
} else {
info!("Config loaded from: {}", config_path.display());
}
let site_data = Arc::new(Mutex::new(Data::new(&config_str)));

// Define the content directory
let content_dir = {
let site_data = site_data.lock().unwrap();
Some(input_folder.join(site_data.site.content_path.clone()))
}
.filter(|path| path.is_dir()) // Take if exists
.unwrap_or_else(|| input_folder.to_path_buf());
// Fallback to input_folder if not

// Function to trigger site regeneration
let rebuild_site = {
let rebuild = {
let start_time = std::time::Instant::now();

let content_dir = content_dir.clone();
let site_data = Arc::new(Mutex::new(Data::new(&config_str)));
let content_dir = {
let site_data = site_data.lock().unwrap();
Some(input_folder.join(site_data.site.content_path.clone()))
}
.filter(|path| path.is_dir()) // Take if exists
.unwrap_or_else(|| input_folder.to_path_buf()); // Fallback to input_folder if not
let output_folder = Arc::clone(output_folder);
let input_folder = input_folder.to_path_buf();
let site_data = site_data.clone();

move || {
let mut site_data = site_data.lock().unwrap();
// cleanup before rebuilding, otherwise we get duplicated content
site_data.clear_all();
let fragments = collect_fragments(&content_dir);
collect_content(&content_dir, &mut site_data, &fragments);

// Detect slug collision
detect_slug_collision(&site_data);

// Feed back_links
collect_back_links(&mut site_data);

site_data.sort_all();
detect_slug_collision(&site_data); // Detect slug collision and warn user
collect_back_links(&mut site_data);

// Create the output directory
let site_path = site_data.site.site_path.clone();
let output_path = output_folder.join(site_path);
if let Err(e) = fs::create_dir_all(&output_path) {
error!("Unable to create output directory: {}", e);
process::exit(1);
}

// Initialize Tera templates
let tera = initialize_tera(&input_folder, &site_data);

// Render templates
if let Err(e) = render_templates(&content_dir, &site_data, &tera, &output_path) {
error!("Failed to render templates: {}", e);
process::exit(1);
}

// Copy static folder if present
handle_static_artifacts(&input_folder, &site_data, &output_folder, &content_dir);

if site_data.site.enable_search {
Expand All @@ -162,7 +138,7 @@ pub fn generate(
};

// Initial site generation
rebuild_site();
rebuild();

// If watch flag is enabled, start hotwatch
if watch {
Expand All @@ -173,7 +149,7 @@ pub fn generate(
.watch(input_folder, move |event: Event| match event.kind {
EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_) => {
info!("Change detected. Rebuilding site...");
rebuild_site();
rebuild();
}
_ => {}
})
Expand Down

0 comments on commit e6da771

Please sign in to comment.