diff --git a/CHANGELOG.md b/CHANGELOG.md index da21c70..68d6759 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ + +**Table of Contents** *generated with [mtoc](https://github.com/containerscrew/mtoc)* +- [Changelog](#changelog) + - [[unreleased]](#[unreleased]) + - [[0.1.0] - 2024-08-28](#[0.1.0]---2024-08-28) + # Changelog All notable changes to this project will be documented in this file. diff --git a/src/cli.rs b/src/cli.rs index 1f13d85..0a9909a 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -18,4 +18,24 @@ pub struct Args { required = false )] pub directory: String, + + #[arg( + short = 'e', + long = "exclude-dir", + help = "Exclude directories to scan", + value_delimiter = ' ', + num_args = 1.., + required = false + )] + pub exclude: Option>, + + #[arg( + short = 'f', + long = "file", + help = "Only generate TOC for the specified file(s)", + value_delimiter = ' ', + num_args = 1.., + required = false + )] + pub file: Option>, } diff --git a/src/main.rs b/src/main.rs index d2c037f..6a33cb6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -19,8 +19,23 @@ fn main() -> io::Result<()> { // Parse command-line arguments let args = Args::parse(); - // Find markdown files in all the repo - let markdown_files = find_markdown_files(Path::new(&args.directory)); + // Exclude directories to scan + let exclude_dirs = args.exclude.as_deref().unwrap_or(&[]); + if !exclude_dirs.is_empty() { + eprintln!( + "{} {:?}", + "Excluding directories ".bright_red(), + exclude_dirs + ); + } + + // Only generate TOC for the specified file(s) + let markdown_files = if let Some(files) = args.file.as_deref() { + files.to_vec() + } else { + // Find markdown files in the given directory + find_markdown_files(Path::new(&args.directory), exclude_dirs) + }; for markdown_file in &markdown_files { // Read the original file content diff --git a/src/utils.rs b/src/utils.rs index f96fddf..b315553 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,15 +1,20 @@ use std::path::Path; -use walkdir::WalkDir; +use walkdir::{DirEntry, WalkDir}; + +// Check if a directory should be excluded +fn is_excluded_dir(entry: &DirEntry, exclude_dirs: &[String]) -> bool { + exclude_dirs.iter().any(|dir| entry.path().starts_with(dir)) +} // Find markdown files in the given directory -pub fn find_markdown_files(dir: &Path) -> Vec { +pub fn find_markdown_files(dir: &Path, exclude_dirs: &[String]) -> Vec { let mut markdown_files = Vec::new(); for entry in WalkDir::new(dir) .follow_links(true) .into_iter() .filter_map(|e| e.ok()) { - if entry.path().is_file() { + if entry.path().is_file() && !is_excluded_dir(&entry, exclude_dirs) { if let Some(ext) = entry.path().extension() { if ext == "md" { markdown_files.push(entry.path().to_string_lossy().to_string());