use crate::parser::Document;
use crate::theme::Theme;
use crate::i18n::I18nManager;
use crate::site::SiteConfig;
use crate::error::{KrikResult, KrikError, ThemeError, ThemeErrorKind, GenerationError, GenerationErrorKind};
use std::collections::HashMap;
use std::path::{Path, PathBuf};
#[derive(Debug)]
pub struct SiteGenerator {
pub source_dir: PathBuf,
pub output_dir: PathBuf,
pub theme: Theme,
pub i18n: I18nManager,
pub site_config: SiteConfig,
pub documents: Vec<Document>,
}
impl SiteGenerator {
pub fn new<P: AsRef<Path>>(
source_dir: P,
output_dir: P,
theme_dir: Option<P>,
) -> KrikResult<Self> {
let source_dir = source_dir.as_ref().to_path_buf();
let output_dir = output_dir.as_ref().to_path_buf();
let theme = if let Some(theme_path) = theme_dir {
let path = theme_path.as_ref().to_path_buf();
Theme::load_from_path(&path)
.map_err(|_| KrikError::Theme(ThemeError {
kind: ThemeErrorKind::NotFound,
theme_path: path.clone(),
context: format!("Loading custom theme from {}", path.display()),
}))?
} else {
let default_path = PathBuf::from("themes/default");
Theme::load_from_path(&default_path).unwrap_or_else(|_| {
Theme {
config: crate::theme::ThemeConfig {
name: "default".to_string(),
version: "1.0.0".to_string(),
author: None,
description: None,
templates: HashMap::new(),
},
templates: tera::Tera::new("themes/default/templates/**/*").unwrap_or_default(),
theme_path: default_path,
}
})
};
let i18n = I18nManager::new("en".to_string());
let site_config = SiteConfig::load_from_path(&source_dir);
Ok(Self {
source_dir,
output_dir,
theme,
i18n,
site_config,
documents: Vec::new(),
})
}
pub fn scan_files(&mut self) -> KrikResult<()> {
super::markdown::scan_files(&self.source_dir, &mut self.documents)
.map_err(|e| match e {
KrikError::Generation(gen_err) => KrikError::Generation(gen_err),
other => other,
})
}
pub fn generate_site(&self) -> KrikResult<()> {
if !self.output_dir.exists() {
std::fs::create_dir_all(&self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::OutputDirError(e),
context: format!("Creating output directory: {}", self.output_dir.display()),
}))?;
}
super::assets::copy_non_markdown_files(&self.source_dir, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::AssetCopyError {
source: self.source_dir.clone(),
target: self.output_dir.clone(),
error: std::io::Error::new(std::io::ErrorKind::Other, format!("Asset copy failed: {}", e)),
},
context: "Copying non-markdown assets".to_string(),
}))?;
super::assets::copy_theme_assets(&self.theme, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::AssetCopyError {
source: self.theme.theme_path.clone(),
target: self.output_dir.clone(),
error: std::io::Error::new(std::io::ErrorKind::Other, format!("Theme asset copy failed: {}", e)),
},
context: "Copying theme assets".to_string(),
}))?;
super::templates::generate_pages(&self.documents, &self.theme, &self.i18n, &self.site_config, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::OutputDirError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Page generation failed: {}", e)
)),
context: "Generating HTML pages from documents".to_string(),
}))?;
super::templates::generate_index(&self.documents, &self.theme, &self.site_config, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::OutputDirError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("Index page generation failed: {}", e)
)),
context: "Generating index page with post listings".to_string(),
}))?;
super::feeds::generate_feed(&self.documents, &self.site_config, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::FeedError(format!("Atom feed generation failed: {}", e)),
context: "Generating Atom feed for posts".to_string(),
}))?;
super::sitemap::generate_sitemap(&self.documents, &self.site_config, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::SitemapError(format!("XML sitemap generation failed: {}", e)),
context: "Generating XML sitemap with multilingual support".to_string(),
}))?;
super::robots::generate_robots(&self.site_config, &self.output_dir)
.map_err(|e| KrikError::Generation(GenerationError {
kind: GenerationErrorKind::OutputDirError(std::io::Error::new(
std::io::ErrorKind::Other,
format!("robots.txt generation failed: {}", e)
)),
context: "Generating robots.txt with sitemap reference".to_string(),
}))?;
self.generate_pdfs_if_available();
Ok(())
}
fn generate_pdfs_if_available(&self) {
if super::pdf::PdfGenerator::is_available() {
match super::pdf::PdfGenerator::new() {
Ok(pdf_generator) => {
match pdf_generator.generate_pdfs(&self.documents, &self.source_dir, &self.output_dir, &self.site_config) {
Ok(generated_pdfs) => {
if !generated_pdfs.is_empty() {
println!("Generated {} PDF files alongside their HTML counterparts",
generated_pdfs.len());
}
}
Err(e) => eprintln!("Warning: PDF generation failed: {}", e),
}
}
Err(e) => eprintln!("Warning: Could not initialize PDF generator: {}", e),
}
} else {
println!("PDF generation skipped: pandoc and/or typst not available in PATH");
}
}
}