Skip to content

Commit

Permalink
Allow specify a file & ignore directories
Browse files Browse the repository at this point in the history
  • Loading branch information
containerscrew committed Aug 29, 2024
1 parent 124ba4d commit 091a332
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 5 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
<!-- START OF TOC !DO NOT EDIT THIS CONTENT MANUALLY-->
**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)
<!-- END OF TOC -->
# Changelog

All notable changes to this project will be documented in this file.
Expand Down
20 changes: 20 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Vec<String>>,

#[arg(
short = 'f',
long = "file",
help = "Only generate TOC for the specified file(s)",
value_delimiter = ' ',
num_args = 1..,
required = false
)]
pub file: Option<Vec<String>>,
}
19 changes: 17 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 8 additions & 3 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<String> {
pub fn find_markdown_files(dir: &Path, exclude_dirs: &[String]) -> Vec<String> {
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());
Expand Down

0 comments on commit 091a332

Please sign in to comment.