Skip to content

Commit

Permalink
feat: format directory
Browse files Browse the repository at this point in the history
  • Loading branch information
hougesen committed Mar 5, 2024
1 parent 472d812 commit 48eb6bb
Show file tree
Hide file tree
Showing 4 changed files with 211 additions and 5 deletions.
141 changes: 141 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
31 changes: 26 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand All @@ -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);

Expand All @@ -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(())
Expand Down
43 changes: 43 additions & 0 deletions test/input.md
Original file line number Diff line number Diff line change
@@ -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





```

0 comments on commit 48eb6bb

Please sign in to comment.