From 48eb6bb95d9e567579e8ba13fc094401e3ceab8e Mon Sep 17 00:00:00 2001 From: Mads Hougesen Date: Tue, 5 Mar 2024 18:46:37 +0100 Subject: [PATCH] feat: format directory --- Cargo.lock | 141 ++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/main.rs | 31 +++++++++-- test/input.md | 43 +++++++++++++++ 4 files changed, 211 insertions(+), 5 deletions(-) create mode 100644 test/input.md diff --git a/Cargo.lock b/Cargo.lock index 3a39567f..32329531 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -65,6 +65,16 @@ version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" +[[package]] +name = "bstr" +version = "1.9.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "05efc5cfd9110c8416e471df0e96702d58690178e206e61b7173706673c93706" +dependencies = [ + "memchr", + "serde", +] + [[package]] name = "cfg-if" version = "1.0.0" @@ -117,6 +127,31 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7" +[[package]] +name = "crossbeam-deque" +version = "0.8.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613f8cc01fe9cf1a3eb3d7f488fd2fa8388403e97039e2f73692932e291a770d" +dependencies = [ + "crossbeam-epoch", + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-epoch" +version = "0.9.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5b82ac4a3c2ca9c3460964f020e1402edd5753411d7737aa39c3714ad1b5420e" +dependencies = [ + "crossbeam-utils", +] + +[[package]] +name = "crossbeam-utils" +version = "0.8.19" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "248e3bacc7dc6baa3b21e405ee045c3047101a49145e7e9eca583ab4c2ca5345" + [[package]] name = "errno" version = "0.3.8" @@ -133,12 +168,41 @@ version = "2.0.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" +[[package]] +name = "globset" +version = "0.4.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "57da3b9b5b85bd66f31093f8c408b90a74431672542466497dcbdfdc02034be1" +dependencies = [ + "aho-corasick", + "bstr", + "log", + "regex-automata", + "regex-syntax", +] + [[package]] name = "heck" version = "0.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8" +[[package]] +name = "ignore" +version = "0.4.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b46810df39e66e925525d6e38ce1e7f6e1d208f72dc39757880fcb66e2c58af1" +dependencies = [ + "crossbeam-deque", + "globset", + "log", + "memchr", + "regex-automata", + "same-file", + "walkdir", + "winapi-util", +] + [[package]] name = "lazy_static" version = "1.4.0" @@ -157,6 +221,12 @@ version = "0.4.13" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" +[[package]] +name = "log" +version = "0.4.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c" + [[package]] name = "markdown" version = "0.3.0" @@ -173,6 +243,7 @@ name = "mdsf" version = "0.0.0" dependencies = [ "clap", + "ignore", "markdown", "tempfile", ] @@ -249,6 +320,35 @@ dependencies = [ "windows-sys", ] +[[package]] +name = "same-file" +version = "1.0.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "93fc1dc3aaa9bfed95e02e6eadabb4baf7e3078b0bd1b4d7b6b0b68378900502" +dependencies = [ + "winapi-util", +] + +[[package]] +name = "serde" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3fb1c873e1b9b056a4dc4c0c198b24c3ffa059243875552b2bd0933b1aee4ce2" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.197" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7eb0b34b42edc17f6b7cac84a52a1c5f0e1bb2227e997ca9011ea3dd34e8610b" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "strsim" version = "0.11.0" @@ -290,6 +390,47 @@ version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a" +[[package]] +name = "walkdir" +version = "2.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "29790946404f91d9c5d06f9874efddea1dc06c5efe94541a7d6863108e3a5e4b" +dependencies = [ + "same-file", + "winapi-util", +] + +[[package]] +name = "winapi" +version = "0.3.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5c839a674fcd7a98952e593242ea400abe93992746761e38641405d28b00f419" +dependencies = [ + "winapi-i686-pc-windows-gnu", + "winapi-x86_64-pc-windows-gnu", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" + +[[package]] +name = "winapi-util" +version = "0.1.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" + [[package]] name = "windows-sys" version = "0.52.0" diff --git a/Cargo.toml b/Cargo.toml index c81a272e..b4cdb64c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,5 +13,6 @@ categories = ["development-tools"] [dependencies] clap = { version = "4.5.1", features = ["derive"] } +ignore = "0.4.22" markdown = "0.3.0" tempfile = "3.10.1" diff --git a/src/main.rs b/src/main.rs index 5b1f08c9..33da9fa0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,4 +1,4 @@ -use clap::Parser; +use clap::{builder::OsStr, Parser}; use config::{Cli, Commands, FormatCommandArguments}; use formatters::format_snippet; use markdown::{generate_markdown, Block}; @@ -7,7 +7,9 @@ mod config; mod formatters; fn format_file(path: &std::path::Path) -> std::io::Result<()> { - let input = std::fs::read_to_string("input.md")?; + println!("Formatting {:#?}", path); + + let input = std::fs::read_to_string(path)?; let tokens = markdown::tokenize(&input); @@ -23,14 +25,33 @@ fn format_file(path: &std::path::Path) -> std::io::Result<()> { } } - output.push(Block::Raw(String::new())); + output.push(Block::Paragraph(Vec::new())); + + let mut s = generate_markdown(output).trim().to_owned(); + + s.push('\n'); - std::fs::write(path, generate_markdown(output)) + std::fs::write(path, s) } fn format_command(args: FormatCommandArguments) -> std::io::Result<()> { if args.path.is_file() { - return format_file(&args.path); + format_file(&args.path)?; + } else { + for entry in ignore::WalkBuilder::new(args.path) + .git_ignore(true) + .require_git(false) + .hidden(true) + .build() + { + if let Ok(d) = entry { + let file_path = d.path(); + + if file_path.extension() == Some(&OsStr::from("md")) { + format_file(file_path)?; + } + } + } } Ok(()) diff --git a/test/input.md b/test/input.md new file mode 100644 index 00000000..57223d95 --- /dev/null +++ b/test/input.md @@ -0,0 +1,43 @@ +# example + +```rust + + +pub async + fn add( + a: + i32, + + b: i32) + + -> i32 { + a + + + b + } +``` + +```lua + + local function add ( a , b +) + +return a +b + + +end + + +``` + +```python + +def add ( + a : int , b:int )->int : + return a +b + + + + + +```