]> git.huck.website - metaforge.git/commitdiff
added: single file flag and functions
authorHuck Boles <huck@huck.website>
Sat, 20 May 2023 17:34:12 +0000 (12:34 -0500)
committerHuck Boles <huck@huck.website>
Sat, 20 May 2023 17:34:12 +0000 (12:34 -0500)
src/lib.rs
src/main.rs
src/options/arg_parser.rs
src/options/opt_struct.rs

index 04df235206d98f4a76d665c126110696659cf707..a99ab4cd51b35b87876217d529ed9947e7932766 100644 (file)
@@ -23,16 +23,16 @@ pub fn get_opts() -> Result<Options> {
 
     let exists = opts.build.exists();
     if exists && opts.clean {
-        std::fs::remove_dir_all(&opts.build)?;
-        std::fs::create_dir(&opts.build)?;
+        fs::remove_dir_all(&opts.build)?;
+        fs::create_dir(&opts.build)?;
     } else if !exists {
-        std::fs::create_dir(&opts.build)?;
+        fs::create_dir(&opts.build)?;
     }
 
     Ok(opts)
 }
 
-pub fn build_dir(opts: &Options) -> Result<()> {
+pub fn build_site(opts: &Options) -> Result<()> {
     let mut source = DirNode::build(opts.source.clone(), opts)?;
 
     let global_init = MetaFile::new(opts);
@@ -42,6 +42,20 @@ pub fn build_dir(opts: &Options) -> Result<()> {
     source.build_dir()
 }
 
+pub fn single_file(opts: &Options) -> Result<String> {
+    let path = opts.file.as_ref().ok_or(MetaError::Unknown)?;
+    let source = match fs::read_to_string(&path) {
+        Ok(str) => Ok(str),
+        Err(_) => Err(eyre::Error::from(MetaError::FileNotFound {
+            path: path.to_string_lossy().to_string(),
+        })),
+    }?;
+
+    let file = parse_string(source, opts)?;
+
+    Ok(build_metafile(&file)?)
+}
+
 pub fn new_site(opts: &Options) -> Result<()> {
     macro_rules! exist_or_build(
         ($p:expr) => {
index efd4f0a2e4fcf3bc1f572018ff1e3b4077b68469..d51cac30f49814193ba63910d20816436125a484 100644 (file)
@@ -2,8 +2,14 @@ fn main() -> eyre::Result<()> {
     let opts = metaforge::get_opts()?;
 
     if opts.new {
-        metaforge::new_site(&opts)
+        return metaforge::new_site(&opts);
+    }
+
+    if let Some(_) = &opts.file {
+        let str = metaforge::single_file(&opts)?;
+        println!("{str}");
+        Ok(())
     } else {
-        metaforge::build_dir(&opts)
+        return metaforge::build_site(&opts);
     }
 }
index c40aa109aee321dfb3117d0f7bfd2977501dd961..a24b7277a3f90805963520e0da6947b0258ddd8c 100644 (file)
@@ -6,43 +6,46 @@ use clap::Parser;
 #[command(about = "A customizable template driven static site generator")]
 #[command(long_about = None)]
 pub struct Opts {
-    /// Root directory [CURRENT_DIR]
+    /// root directory [CURRENT_DIR]
     #[arg(short, long, value_name = "ROOT_DIR")]
     pub root: Option<String>,
-    /// Source file directory [CURRENT_DIR/source]
+    /// source file directory [CURRENT_DIR/source]
     #[arg(short, long, value_name = "SOURCE_DIR")]
     pub source: Option<String>,
-    /// Build directory [CURRENT_DIR/build]
+    /// build directory [current_dir/build]
     #[arg(short, long, value_name = "BUILD_DIR")]
     pub build: Option<String>,
-    /// Pattern directory [CURRENT_DIR/pattern]
+    /// pattern directory [current_dir/pattern]
     #[arg(short, long, value_name = "PATTERN_DIR")]
     pub pattern: Option<String>,
-    /// Create a new skeleton directory
+    /// only build a single file
+    #[arg(short, long, value_name = "FILENAME")]
+    pub file: Option<String>,
+    /// create a new skeleton directory
     #[arg(long, default_value_t = false)]
     pub new: bool,
-    /// Enable extra output. Repeated flags give more info
+    /// enable extra output. Repeated flags give more info
     #[arg(short, long, action = clap::ArgAction::Count)]
     pub verbose: u8,
-    /// Minimal output
+    /// minimal output
     #[arg(short, long, default_value_t = false)]
     pub quiet: bool,
-    /// Don't stop on file failure [FALSE]
+    /// don't stop on file failure [FALSE]
     #[arg(long, default_value_t = false)]
     pub force: bool,
-    /// Stop on undefined variables and arrays [FALSE]
+    /// stop on undefined variables and arrays [FALSE]
     #[arg(long, default_value_t = false)]
     pub undefined: bool,
-    /// Clean build directory before building site [FALSE]
+    /// clean build directory before building site [FALSE]
     #[arg(long, default_value_t = false)]
     pub clean: bool,
-    /// Don't call pandoc on source files
+    /// don't call pandoc on source files
     #[arg(long, default_value_t = false)]
     pub no_pandoc: bool,
-    /// Output filetype [html]
+    /// output filetype [html]
     #[arg(short, long, value_name = "OUTPUT_FILETYPE")]
     pub output: Option<String>,
-    /// Input filetype [markdown]
+    /// input filetype [markdown]
     #[arg(short, long, value_name = "INPUT_FILETYPE")]
     pub input: Option<String>,
 }
index eab5a408e76297be21b8676ab60d8631d50eaad9..0f6da5abe1ab3a12b52345d64e3bb41e1c83ddbf 100644 (file)
@@ -7,6 +7,7 @@ pub struct Options {
     pub source: PathBuf,
     pub build: PathBuf,
     pub pattern: PathBuf,
+    pub file: Option<PathBuf>,
     pub input: String,
     pub output: String,
     pub verbose: u8,
@@ -25,6 +26,7 @@ impl Options {
             source: PathBuf::new(),
             build: PathBuf::new(),
             pattern: PathBuf::new(),
+            file: None,
             input: String::default(),
             output: String::default(),
             verbose: 0,
@@ -75,6 +77,10 @@ impl TryFrom<crate::Opts> for Options {
             opts.pattern = opts.root.join("pattern");
         }
 
+        if let Some(file) = value.file.as_deref() {
+            opts.file = Some(PathBuf::from(file).canonicalize()?);
+        }
+
         if let Some(input) = value.input {
             opts.input = input;
         } else {