71 lines
1.9 KiB
Rust
Executable file
71 lines
1.9 KiB
Rust
Executable file
#!/usr/bin/env rust-script
|
|
//! This is a regular crate doc comment, but it also contains a partial
|
|
//! Cargo manifest. Note the use of a *fenced* code block, and the
|
|
//! `cargo` "language".
|
|
//!
|
|
//! ```cargo
|
|
//! [dependencies]
|
|
//! regex = "1"
|
|
//! walkdir = "2"
|
|
//! ```
|
|
|
|
use std::{
|
|
fs::{self, File},
|
|
io::{self, Write},
|
|
path::Path,
|
|
};
|
|
|
|
use regex::Regex;
|
|
use walkdir::WalkDir;
|
|
|
|
fn main() -> io::Result<()> {
|
|
// Define the regex pattern to extract classes
|
|
let re = Regex::new(r"(?: \.(?P<tag>[\w-]+))").expect("Invalid regex pattern");
|
|
|
|
// Define the source directory to scan
|
|
let source_dir = "./src"; // Change this to your source directory
|
|
|
|
// Define the output file
|
|
let output_file = "./style/extracted-classes.txt";
|
|
let mut file = File::create(output_file)?;
|
|
|
|
println!("Scanning directory: {}", source_dir);
|
|
|
|
// Walk through the directory structure
|
|
for entry in WalkDir::new(source_dir)
|
|
.into_iter()
|
|
.filter_map(|e| e.ok())
|
|
.filter(|e| {
|
|
e.file_type().is_file() && e.path().extension().map_or(false, |ext| ext == "rs")
|
|
})
|
|
{
|
|
let path = entry.path();
|
|
println!("Processing file: {}", path.display());
|
|
let content = fs::read_to_string(path)?;
|
|
let mut classes = Vec::new();
|
|
|
|
// Extract all classes using the regex
|
|
for cap in re.captures_iter(&content) {
|
|
if let Some(tag_match) = cap.name("tag") {
|
|
let class = tag_match.as_str().to_string();
|
|
classes.push(class.clone());
|
|
}
|
|
}
|
|
|
|
// If classes were found, write them to the output file
|
|
if !classes.is_empty() {
|
|
writeln!(
|
|
file,
|
|
"class=\"{}\" <!-- {} -->",
|
|
classes.join(" "),
|
|
path.display()
|
|
)?;
|
|
}
|
|
}
|
|
|
|
println!(
|
|
"Class extraction complete. Output written to {}",
|
|
output_file
|
|
);
|
|
Ok(())
|
|
}
|