summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorQrius <[email protected]>2024-09-26 00:11:05 +0200
committerQrius <[email protected]>2024-09-26 00:11:05 +0200
commita7ae9cbc56d2a7d36443e6f7f8c51d90ae6fc214 (patch)
tree39f25e2fc1a7a72a1a3785e9c27d81040ac50771
parent087fde9d18fc24b8eec7c7191b18c730515f31af (diff)
downloadskaldpress-a7ae9cbc56d2a7d36443e6f7f8c51d90ae6fc214.tar.gz
skaldpress-a7ae9cbc56d2a7d36443e6f7f8c51d90ae6fc214.zip
Auto pick sensible output extension
-rw-r--r--src/skaldpress/error.rs1
-rw-r--r--src/skaldpress/main.rs25
2 files changed, 20 insertions, 6 deletions
diff --git a/src/skaldpress/error.rs b/src/skaldpress/error.rs
index ae769a5..4f16ffa 100644
--- a/src/skaldpress/error.rs
+++ b/src/skaldpress/error.rs
@@ -8,6 +8,7 @@ pub const SP_GEN_DEST_STRIP_PREFIX_ERROR: u8 = 4;
pub const SP_COMPILE_FILE_EXTENSION_ERROR: u8 = 7;
pub const SP_COMPILE_TEMPLATE_MACRO_PROCESS_ERROR: u8 = 9;
pub const SP_COMPILE_FILE_MACRO_PROCESS_ERROR: u8 = 10;
+pub const SP_COMPILE_FILE_TEMPLATE_EXTENSION_ERROR: u8 = 11;
#[derive(Debug)]
pub enum SkaldpressError {
diff --git a/src/skaldpress/main.rs b/src/skaldpress/main.rs
index c1b2006..2aa1790 100644
--- a/src/skaldpress/main.rs
+++ b/src/skaldpress/main.rs
@@ -6,8 +6,9 @@ use skaldpress::macro_processor::MacroProcessor;
use skaldpress::skaldpress::error::SkaldpressError;
use skaldpress::skaldpress::error::{
SP_COMPILE_FILE_EXTENSION_ERROR, SP_COMPILE_FILE_EXTENSION_ERROR_2,
- SP_COMPILE_FILE_MACRO_PROCESS_ERROR, SP_COMPILE_FILE_TEMPLATE_READ_ERROR,
- SP_COMPILE_TEMPLATE_MACRO_PROCESS_ERROR, SP_GEN_DEST_STRIP_PREFIX_ERROR,
+ SP_COMPILE_FILE_MACRO_PROCESS_ERROR, SP_COMPILE_FILE_TEMPLATE_EXTENSION_ERROR,
+ SP_COMPILE_FILE_TEMPLATE_READ_ERROR, SP_COMPILE_TEMPLATE_MACRO_PROCESS_ERROR,
+ SP_GEN_DEST_STRIP_PREFIX_ERROR,
};
use skaldpress::skaldpress::metadata_parser::extract_parse_yaml_metadata;
use skaldpress::skaldpress::metadata_parser::YamlValue;
@@ -19,6 +20,7 @@ const BUILD_DIR: &str = "build/";
struct CompiledFile {
content: String,
metadata: HashMap<String, YamlValue>,
+ extension: String,
}
/// Will attempt to compile a specific file, potentially storing some state about the file
@@ -60,13 +62,25 @@ fn compile_file(file_path: &Path) -> Result<CompiledFile, SkaldpressError> {
return Ok(CompiledFile {
content: file_content,
metadata: map,
+ extension: String::from(extension.to_str().unwrap_or("")),
});
};
let template_file = format!("{}{}", TEMPLATES_DIR, template);
let template = fs::read_to_string(&template_file).map_err(|e| {
- SkaldpressError::TemplateReadError(SP_COMPILE_FILE_TEMPLATE_READ_ERROR, e, template_file)
+ SkaldpressError::TemplateReadError(
+ SP_COMPILE_FILE_TEMPLATE_READ_ERROR,
+ e,
+ template_file.clone(),
+ )
})?;
+ let template_extension =
+ Path::new(&template_file)
+ .extension()
+ .ok_or(SkaldpressError::PathOperationError(
+ SP_COMPILE_FILE_TEMPLATE_EXTENSION_ERROR,
+ None,
+ ))?;
macro_processor.define_macro_string(String::from("CONTENT"), file_content);
let content = macro_processor
@@ -75,6 +89,7 @@ fn compile_file(file_path: &Path) -> Result<CompiledFile, SkaldpressError> {
Ok(CompiledFile {
content,
metadata: map,
+ extension: String::from(template_extension.to_str().unwrap_or("")),
})
}
@@ -93,9 +108,7 @@ fn compile_file_and_write(source_file_path: &Path) -> Result<(), Box<dyn std::er
.join(source_file_path.strip_prefix(CONTENT_DIR).map_err(|e| {
SkaldpressError::PathOperationError(SP_GEN_DEST_STRIP_PREFIX_ERROR, Some(Box::new(e)))
})?)
- .with_extension("html");
- // Here we need to do something about the extension, read it from the metadata of the template,
- // or the main file
+ .with_extension(cfile.extension);
let dest_dir = &dest_file_path
.parent()