Skip to content
This repository was archived by the owner on Feb 16, 2025. It is now read-only.

Commit e3a48ca

Browse files
committed
4.0.0 (lots of QOL changes)
- -f flag now requires the path to come after it, and removes the directory requirement - remove unused --no-warn argument - --clean argument now requires a directory specification instead of a file - cleaned up code - updated documentation (command reference) Signed-off-by: Ryan Brue <[email protected]>
1 parent 7d28d26 commit e3a48ca

File tree

6 files changed

+45
-56
lines changed

6 files changed

+45
-56
lines changed

Cargo.lock

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ authors = ["Ryan Brue <[email protected]>"]
44
repository = "https://github.com/ryanabx/simple-ssg"
55
license = "MIT"
66
readme = "README.md"
7-
version = "3.2.0"
7+
version = "4.0.0"
88
edition = "2021"
99
description = "Plain and simple static site generator for Djot and Markdown light markup languages"
1010

docs/command_reference.dj

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,17 @@ cargo build --release
1919
## Usage
2020

2121
```shell
22-
simple-ssg [OPTIONS] <TARGET_PATH>
23-
```
22+
Plain and simple static site generator for Djot and Markdown light markup languages
2423

25-
### Options
24+
Usage: simple-ssg [OPTIONS] [DIRECTORY]
25+
26+
Arguments:
27+
[DIRECTORY] Path to the directory to use to generate the site (not required if -f is specified)
2628

27-
```
2829
Options:
29-
-f Process a single file instead of a directory
30+
-f <FILE> Process a single file instead of a directory
3031
-o <OUTPUT_PATH> Optional output path override. Defaults to ./output for directories
3132
--clean Clean the output directory before generating the site. Useful for multiple runs
32-
--no-warn Disallow any warnings
3333
--web-prefix <WEB_PREFIX> Specify the website prefix (defaults to local paths i.e. `./`)
3434
-t, --template <TEMPLATE> Specify a built in template to use (will override a template.html in any directory!). defaults to whatever templates are found in template.html in the directories [possible values: github-markdown, force-none]
3535
-h, --help Print help

src/main.rs

+33-33
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::{
77
path::{Path, PathBuf},
88
};
99
use templates::BuiltInTemplate;
10-
use utils::warn_or_error;
1110
use walkdir::WalkDir;
1211

1312
use clap::Parser;
@@ -22,20 +21,17 @@ mod utils;
2221
#[derive(Parser, Debug)]
2322
#[command(version, about, long_about = None)]
2423
struct ConsoleArgs {
25-
/// Path to the directory to use to generate the site
26-
target_path: PathBuf,
24+
/// Path to the directory to use to generate the site (not required if -f is specified)
25+
directory: Option<PathBuf>,
2726
/// Process a single file instead of a directory
2827
#[arg(short, conflicts_with = "clean")]
29-
file: bool,
28+
file: Option<PathBuf>,
3029
/// Optional output path override. Defaults to ./output for directories
3130
#[arg(short)]
3231
output_path: Option<PathBuf>,
3332
/// Clean the output directory before generating the site. Useful for multiple runs
3433
#[arg(long, conflicts_with = "file")]
3534
clean: bool,
36-
/// Disallow any warnings
37-
#[arg(long)]
38-
no_warn: bool,
3935
/// Specify the website prefix (defaults to local paths i.e. `./`)
4036
#[arg(long)]
4137
web_prefix: Option<String>,
@@ -54,19 +50,32 @@ fn main() -> anyhow::Result<()> {
5450
}
5551

5652
fn run_program(args: ConsoleArgs) -> anyhow::Result<()> {
57-
let output_path = args.output_path.unwrap_or(if args.file {
58-
env::current_dir()?
53+
let (target_path, output_path) = if args.directory.is_some() && args.file.is_some() {
54+
return Err(anyhow!(
55+
"Cannot specify both a directory and a path! (Specified {} and -f {})",
56+
args.directory.unwrap().display(),
57+
args.file.unwrap().display()
58+
));
59+
} else if let Some(dir) = args.directory {
60+
if dir.is_file() {
61+
return Err(anyhow!(
62+
"Path {} is a file. Specify -f <FILE> if this was intended.",
63+
dir.display()
64+
));
65+
}
66+
(dir, env::current_dir()?.join("output"))
67+
} else if let Some(path) = args.file {
68+
if path.is_dir() {
69+
return Err(anyhow!("Path {} is a directory. Specify <DIRECTORY> without the -f positional argument if this was intended.", path.display()));
70+
}
71+
(path, env::current_dir()?)
5972
} else {
60-
env::current_dir()?.join("output")
61-
});
62-
if args.target_path.is_file() && !args.file {
6373
return Err(anyhow!(
64-
"Target path {} is a file! If you meant to specify a file, please specify the -f flag.",
65-
args.target_path.display()
74+
"Must specify either a directory <DIRECTORY> or a path with -f <PATH>"
6675
));
67-
}
76+
};
6877
// Clean the output directory if clean is specified
69-
if args.clean && args.target_path.is_dir() {
78+
if args.clean {
7079
log::debug!(
7180
"Clean argument specified, cleaning output path {:?}...",
7281
&output_path
@@ -78,9 +87,8 @@ fn run_program(args: ConsoleArgs) -> anyhow::Result<()> {
7887
}
7988
}
8089
generate_site(
81-
&args.target_path,
90+
&target_path,
8291
&output_path,
83-
args.no_warn,
8492
args.web_prefix.as_deref(),
8593
args.template,
8694
)?;
@@ -103,7 +111,6 @@ pub enum FirstPassResult {
103111
fn generate_site(
104112
target_path: &Path,
105113
output_path: &Path,
106-
no_warn: bool,
107114
web_prefix: Option<&str>,
108115
template: Option<BuiltInTemplate>,
109116
) -> anyhow::Result<()> {
@@ -118,7 +125,7 @@ fn generate_site(
118125
log::info!("1/3: Site generation and indexing...");
119126
if target_path.is_dir() && output_path.is_dir() {
120127
if !utils::check_has_index(target_path) {
121-
warn_or_error(SsgError::IndexPageNotFound, no_warn)?;
128+
log::warn!("{}", SsgError::IndexPageNotFound);
122129
}
123130
for entry in WalkDir::new(target_path) {
124131
match entry {
@@ -128,12 +135,11 @@ fn generate_site(
128135
output_path,
129136
&template,
130137
web_prefix,
131-
no_warn,
132138
direntry.depth(),
133139
&mut first_pass_results,
134140
)?,
135141
Err(e) => {
136-
warn_or_error(SsgError::DirEntryError(e), no_warn)?;
142+
log::warn!("{}", SsgError::DirEntryError(e));
137143
}
138144
}
139145
}
@@ -144,7 +150,6 @@ fn generate_site(
144150
output_path,
145151
&template,
146152
web_prefix,
147-
no_warn,
148153
1,
149154
&mut first_pass_results,
150155
)?;
@@ -192,14 +197,13 @@ fn process_path(
192197
output_path: &Path,
193198
template: &Option<BuiltInTemplate>,
194199
web_prefix: Option<&str>,
195-
no_warn: bool,
196200
depth: usize,
197201
first_pass_results: &mut Vec<FirstPassResult>,
198202
) -> anyhow::Result<()> {
199203
let relative = match entity.strip_prefix(target_path) {
200204
Ok(relative) => relative.to_path_buf(),
201205
Err(_) => {
202-
warn_or_error(SsgError::PathNotRelative(entity.to_path_buf()), no_warn)?;
206+
log::warn!("{}", SsgError::PathNotRelative(entity.to_path_buf()));
203207
return Ok(());
204208
}
205209
};
@@ -232,11 +236,9 @@ fn process_path(
232236
);
233237
let input_str = std::fs::read_to_string(entity)?;
234238
let html = match entity.extension().map(|x| x.to_str().unwrap()) {
235-
Some("md") => {
236-
process_markdown(&input_str, entity.parent().unwrap(), no_warn, web_prefix)?
237-
}
239+
Some("md") => process_markdown(&input_str, entity.parent().unwrap(), web_prefix)?,
238240
Some("dj") | Some("djot") => {
239-
process_djot(&input_str, entity.parent().unwrap(), no_warn, web_prefix)?
241+
process_djot(&input_str, entity.parent().unwrap(), web_prefix)?
240242
}
241243
_ => unreachable!(),
242244
};
@@ -257,7 +259,6 @@ fn process_path(
257259
fn process_markdown(
258260
markdown_input: &str,
259261
file_parent_dir: &Path,
260-
no_warn: bool,
261262
web_prefix: Option<&str>,
262263
) -> anyhow::Result<String> {
263264
let events = pulldown_cmark::Parser::new(markdown_input)
@@ -277,7 +278,7 @@ fn process_markdown(
277278
{
278279
let new_path = Path::new(&inner).with_extension("html");
279280
if !referenced_path.exists() {
280-
warn_or_error(SsgError::LinkError(referenced_path), no_warn)?;
281+
log::warn!("{}", SsgError::LinkError(referenced_path))
281282
}
282283
let dest_url = CowStr::Boxed(
283284
format!("{}{}", web_prefix.unwrap_or(""), new_path.to_string_lossy())
@@ -311,7 +312,6 @@ fn process_markdown(
311312
fn process_djot(
312313
djot_input: &str,
313314
file_parent_dir: &Path,
314-
no_warn: bool,
315315
web_prefix: Option<&str>,
316316
) -> anyhow::Result<String> {
317317
let events = jotdown::Parser::new(djot_input)
@@ -338,7 +338,7 @@ fn process_djot(
338338
attributes,
339339
))
340340
} else {
341-
warn_or_error(SsgError::LinkError(referenced_path), no_warn)?;
341+
log::warn!("{}", SsgError::LinkError(referenced_path));
342342
Ok(Event::Start(Container::Link(text, link_type), attributes))
343343
}
344344
} else {

src/tests.rs

+4-6
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,12 @@ fn site_with_links() -> anyhow::Result<()> {
4343
djot_file_3.flush()?;
4444
log::trace!("Djot files written");
4545
let args = ConsoleArgs {
46-
target_path: temp_dir.join("target"),
46+
directory: Some(temp_dir.join("target")),
47+
file: None,
4748
output_path: Some(temp_dir.join("output")),
4849
clean: false,
49-
no_warn: true,
5050
web_prefix: None,
5151
template: Some(crate::templates::BuiltInTemplate::ForceNone),
52-
file: false,
5352
};
5453
log::trace!("Running program");
5554
crate::run_program(args)?;
@@ -98,13 +97,12 @@ fn site_warn_without_index() -> anyhow::Result<()> {
9897
djot_file_2.flush()?;
9998

10099
let args = ConsoleArgs {
101-
target_path: temp_dir.join("target"),
100+
directory: Some(temp_dir.join("target")),
102101
output_path: Some(temp_dir.join("output")),
103102
clean: false,
104-
no_warn: true,
105103
web_prefix: None,
106104
template: Some(crate::templates::BuiltInTemplate::ForceNone),
107-
file: false,
105+
file: None,
108106
};
109107
crate::run_program(args)?;
110108
Ok(())

src/utils.rs

-9
Original file line numberDiff line numberDiff line change
@@ -9,15 +9,6 @@ pub fn check_has_index(target_path: &Path) -> bool {
99
|| target_path.join("index.md").exists()
1010
}
1111

12-
pub fn warn_or_error(error: crate::errors::SsgError, no_warn: bool) -> anyhow::Result<()> {
13-
if no_warn {
14-
Err(error.into())
15-
} else {
16-
log::warn!("{}", error);
17-
Ok(())
18-
}
19-
}
20-
2112
pub fn get_template_if_exists(
2213
djot_document_path: &Path,
2314
root_path: &Path,

0 commit comments

Comments
 (0)