summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.lock32
-rw-r--r--Cargo.toml2
-rw-r--r--src/main.rs88
3 files changed, 66 insertions, 56 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 451856b..d03ab98 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -13,37 +13,37 @@ version = "3.2.0"
source = "git+git://benbridle.com/markdown?tag=v3.2.0#883a2a63023ea9b1e4b2bb51831ea1dafcb7346a"
[[package]]
+name = "paste"
+version = "1.0.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "57c0d7b74b563b49d38dae00a0c37d4d6de9b432382b2892f0574ddcae73fd0a"
+
+[[package]]
name = "recipe"
version = "1.4.0"
source = "git+git://benbridle.com/recipe?tag=v1.4.0#652aaee3130e2ee02742fdcc248ddd1bee285737"
[[package]]
+name = "switchboard"
+version = "1.0.0"
+source = "git+git://benbridle.com/switchboard?tag=v1.0.0#ea70fa89659e5cf1a9d4ca6ea31fb67f7a2cc633"
+dependencies = [
+ "log",
+ "paste",
+]
+
+[[package]]
name = "toaster"
version = "1.8.0"
dependencies = [
"log",
"markdown",
"recipe",
+ "switchboard",
"vagabond",
- "xflags",
]
[[package]]
name = "vagabond"
version = "1.1.0"
source = "git+git://benbridle.com/vagabond?tag=v1.1.0#6e759a3abb3bc3e5da42d69a6f20ec2c31eb33de"
-
-[[package]]
-name = "xflags"
-version = "0.4.0-pre.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "f6a40f95e4e200baabdfe8b813e3ee754b58407a677141bd2890c28ef4a89c21"
-dependencies = [
- "xflags-macros",
-]
-
-[[package]]
-name = "xflags-macros"
-version = "0.4.0-pre.2"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-checksum = "8a6d9b56f406f5754a3808524166b6e6bdfe219c0526e490cfc39ecc0582a4e6"
diff --git a/Cargo.toml b/Cargo.toml
index a84de6e..af5d9e9 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,7 +8,7 @@ vagabond = { git = "git://benbridle.com/vagabond", tag = "v1.1.0" }
markdown = { git = "git://benbridle.com/markdown", tag = "v3.2.0" }
recipe = { git = "git://benbridle.com/recipe", tag = "v1.4.0" }
log = { git = "git://benbridle.com/log", tag = "v1.1.1" }
-xflags = "0.4.0-pre.1"
+switchboard = { git = "git://benbridle.com/switchboard", tag = "v1.0.0" }
[profile.release]
lto=true
diff --git a/src/main.rs b/src/main.rs
index 82814e4..dc432ad 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -12,29 +12,59 @@ use std::collections::HashSet;
use std::time::SystemTime;
use log::{info, warn, error, fatal};
+use switchboard::{Switchboard, SwitchQuery};
+
+
+fn print_help() -> ! {
+ eprintln!("\
+Usage: toaster <source> <destination>
+
+Generate a website from a structured directory of markdown files.
+
+Arguments:
+ source Source directory with markdown files
+ destination Path to output directory
+
+Switches:
+ --delete Delete the destination directory first if it exists
+ --html Generate HTML output
+ --version, -v Print information as each file is parsed
+ --version Print the program version and exit
+ --help, -h Print help
+");
+ std::process::exit(0);
+}
+
+fn print_version() -> ! {
+ let version = env!("CARGO_PKG_VERSION");
+ eprintln!("toaster, version {version}");
+ eprintln!("written by ben bridle");
+ std::process::exit(0);
+}
fn main() {
- let args = Arguments::from_env_or_exit();
- if args.version {
- let version = env!("CARGO_PKG_VERSION");
- eprintln!("toaster, version {version}");
- std::process::exit(0);
+ let mut args = Switchboard::from_env();
+ if args.named("help").short('h').as_bool() {
+ print_help();
}
- if args.verbose {
- log::set_log_level(log::LogLevel::Info);
+ if args.named("version").as_bool() {
+ print_version();
}
- if args.source.is_none() || args.destination.is_none() {
- fatal!("Provide a source directory and a destination directory.")
+ if args.named("verbose").short('v').as_bool() {
+ log::set_log_level(log::LogLevel::Info);
}
- let source_directory = match args.source.as_ref().unwrap().canonicalize() {
- Ok(source_directory) => source_directory,
- Err(err) => fatal!("{:?}: {err}", args.source.unwrap()),
+ let source = args.positional("source").as_path();
+ let destination = args.positional("destination").as_path();
+ let delete_existing = args.named("delete").as_bool();
+ let export_html = args.named("html").as_bool();
+
+ let source = match source.canonicalize() {
+ Ok(source) => source,
+ Err(err) => fatal!("{source:?}: {err}"),
};
- let destination_directory = args.destination.unwrap();
-
- let website = Website::from_path(&source_directory);
+ let website = Website::from_path(&source);
// Check for duplicate output paths for pages.
let mut destinations: HashSet<&str> = HashSet::new();
@@ -55,10 +85,10 @@ fn main() {
}
}
- let mut destination = destination_directory.clone();
+ let mut destination = destination;
destination.push(make_url_safe(&website.name));
- if args.delete && Entry::from_path(&destination).is_ok() {
+ if delete_existing && Entry::from_path(&destination).is_ok() {
info!("Deleting existing destination directory {destination:?}");
remove(&destination).unwrap_or_else(|_|
error!("Failed to delete existing destination directory {destination:?}"));
@@ -69,7 +99,7 @@ fn main() {
let mut destination = destination.clone();
destination.push(&page.full_url);
// Convert document to different formats.
- if args.html {
+ if export_html {
let html = generate_html(&page.document, page, &website);
write_file(&html, &destination, "html", page.last_modified);
}
@@ -97,7 +127,7 @@ fn main() {
let mut destination = destination.clone();
destination.push(&redirect.full_url);
let path = &redirect.redirect;
- if args.html {
+ if export_html {
if !path.contains("://") {
if let Some(path) = website.has_page(redirect, &path, "html") {
write_file(&generate_html_redirect(&path), &destination, "html", redirect.last_modified);
@@ -138,23 +168,3 @@ pub fn make_url_safe(text: &str) -> String {
}
-
-xflags::xflags! {
- /// Generate a website from a structured directory of markdown files.
- cmd arguments {
- /// Source directory with markdown files
- optional source: PathBuf
- /// Path to output directory
- optional destination: PathBuf
- /// Delete the destination directory first if it exists
- optional --delete
- /// Generate HTML output
- optional --html
- /// Generate Gemtext output
- optional --gmi
- /// Print information as each file is parsed
- optional -v, --verbose
- /// Print the program version and exit
- optional --version
- }
-}