Skip to content

Commit

Permalink
refactor(ssg): ♻️ renaming file
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastienrousseau committed Apr 6, 2024
1 parent 60c9a5e commit f7006a6
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 32 deletions.
115 changes: 84 additions & 31 deletions src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,33 @@

use rlg::log_level::LogLevel::ERROR;

use crate::modules::pdf::generate_pdf;
use crate::{
macro_cleanup_directories, macro_create_directories,
macro_log_info, macro_metadata_option, macro_set_rss_data_fields,
models::data::{FileData, PageData, RssData},
modules::{
cname::create_cname_data, html::generate_html, human::create_human_data, json::{cname, human, news_sitemap, sitemap, txt}, manifest::create_manifest_data, metadata::extract_and_prepare_metadata, navigation::NavigationGenerator, newssitemap::create_news_site_map_data, pdf::PdfGenerationParams, plaintext::generate_plain_text, rss::generate_rss, sitemap::create_site_map_data, tags::*, txt::create_txt_data
cname::create_cname_data,
html::generate_html,
human::create_human_data,
json::{cname, human, news_sitemap, sitemap, txt},
manifest::create_manifest_data,
metadata::extract_and_prepare_metadata,
navigation::NavigationGenerator,
news_sitemap::create_news_site_map_data,
pdf::PdfGenerationParams,
plaintext::generate_plain_text,
rss::generate_rss,
sitemap::create_site_map_data,
tags::*,
txt::create_txt_data,
},
utilities::{
file::add,
template::{render_page, PageOptions},
write::write_files_to_build_directory,
},
};
use crate::modules::pdf::generate_pdf;
use std::{collections::HashMap, error::Error, fs, path::Path};

/// Compiles files in a source directory, generates HTML pages from them, and
Expand Down Expand Up @@ -84,55 +97,94 @@ pub fn compile(
});

// Generate PDF
let (plain_text, plain_title, plain_description, plain_author, plain_creator, plain_keywords) = match generate_plain_text(
let (
plain_text,
plain_title,
plain_description,
plain_author,
plain_creator,
plain_keywords,
) = match generate_plain_text(
&file.content,
&macro_metadata_option!(metadata, "title"),
&macro_metadata_option!(metadata, "description"),
&macro_metadata_option!(metadata, "author"),
&macro_metadata_option!(metadata, "generator"),
&keywords.join(", "),
) {
Ok((plain_text, plain_title, plain_description, plain_author, plain_creator, plain_keywords)) => (plain_text, plain_title, plain_description, plain_author, plain_creator, plain_keywords),
Ok((
plain_text,
plain_title,
plain_description,
plain_author,
plain_creator,
plain_keywords,
)) => (
plain_text,
plain_title,
plain_description,
plain_author,
plain_creator,
plain_keywords,
),
Err(err) => {
let description = format!("Error generating Plain Text: {:?}", err);
let description = format!(
"Error generating Plain Text: {:?}",
err
);
macro_log_info!(
&ERROR,
"compiler.rs - Line 107",
&description,
&LogFormat::CLF
);
// Provide fallback values
(String::from("Fallback Plain Text content"), String::new(), String::new(), String::new(), String::new(), String::new())
(
String::from("Fallback Plain Text content"),
String::new(),
String::new(),
String::new(),
String::new(),
String::new(),
)
}
};

// Determine the filename without the extension
let filename_without_extension = Path::new(&file.name)
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or(&file.name);
let common_path = build_dir_path.to_str().unwrap();
let pdf_path = if filename_without_extension == "index" {
format!("{}/", common_path)
} else {
format!("{}/{}/", common_path, filename_without_extension)
};
if let Err(err) = generate_pdf(PdfGenerationParams {
plain_title: &plain_title,
plain_description: &plain_description,
plain_text: &plain_text,
plain_author: &plain_author,
plain_creator: &plain_creator,
plain_keywords: &plain_keywords,
output_dir: &pdf_path,
filename: filename_without_extension,
}) {
let description = format!("Error generating PDF: {:?}", err);
macro_log_info!(&ERROR, "compiler.rs - Line 81", &description, &LogFormat::CLF);
.file_stem()
.and_then(|stem| stem.to_str())
.unwrap_or(&file.name);
let common_path = build_dir_path.to_str().unwrap();
let pdf_path = if filename_without_extension == "index" {
format!("{}/", common_path)
} else {
format!(
"{}/{}/",
common_path, filename_without_extension
)
};
if let Err(err) = generate_pdf(PdfGenerationParams {
plain_title: &plain_title,
plain_description: &plain_description,
plain_text: &plain_text,
plain_author: &plain_author,
plain_creator: &plain_creator,
plain_keywords: &plain_keywords,
output_dir: &pdf_path,
filename: filename_without_extension,
}) {
let description =
format!("Error generating PDF: {:?}", err);
macro_log_info!(
&ERROR,
"compiler.rs - Line 81",
&description,
&LogFormat::CLF
);
// Handle the error here, for example, return early or log it
}


// Create page options
let mut page_options = PageOptions::new();
for (key, value) in metadata.iter() {
Expand Down Expand Up @@ -278,7 +330,8 @@ pub fn compile(
let sitemap_options = create_site_map_data(&metadata);

// Initialize a structure to store news sitemap-related information, using values from the metadata.
let news_sitemap_options = create_news_site_map_data(&metadata);
let news_sitemap_options =
create_news_site_map_data(&metadata);

let tags_data = generate_tags(&file, &metadata);

Expand Down Expand Up @@ -320,7 +373,7 @@ pub fn compile(
let cname_data = cname(&cname_options);
let human_data = human(&human_options);
let sitemap_data = sitemap(sitemap_options, site_path);
let news_sitemap_data = news_sitemap(news_sitemap_options);
let news_sitemap_data = news_sitemap(news_sitemap_options);
let json_data = serde_json::to_string(&json)
.unwrap_or_else(|e| {
eprintln!("Error serializing JSON: {}", e);
Expand Down Expand Up @@ -378,4 +431,4 @@ pub fn compile(
fs::rename(build_dir_path, site_path)?;

Ok(())
}
}
2 changes: 1 addition & 1 deletion src/modules/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub mod metadata;
pub mod navigation;

/// The `newssitemap` module generates the newssitemap content.
pub mod newssitemap;
pub mod news_sitemap;

/// The `plaintext` module contains functions for generating plaintext.
pub mod plaintext;
Expand Down
File renamed without changes.

0 comments on commit f7006a6

Please sign in to comment.