Skip to content

Commit

Permalink
fix newlines before section titles | add comments | update build steps
Browse files Browse the repository at this point in the history
  • Loading branch information
ayvi-0001 committed Mar 31, 2024
1 parent d867e6a commit 0571984
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 23 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
dev/
debug/
target/
/target
Cargo.lock
.vscode
.vscode
.devcontainer
7 changes: 7 additions & 0 deletions Cargo.lock

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

26 changes: 20 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
```mmd
%% timeline.mmd
gantt
%% Example from https://mermaid.js.org/syntax/gantt.html#syntax.
title Adding GANTT diagram functionality to mermaid
dateFormat YYYY-MM-DD
Expand Down Expand Up @@ -34,19 +33,34 @@ gantt
Add another diagram to demo page :48h
```

### Cmd
### Build

```shell
cargo run timeline.mmd # edited in-place
```sh
# windows
cargo build --release --target x86_64-pc-windows-msvc
chmod +x target/x86_64-pc-windows-msvc/release/fmt-mmd-gantt.exe
mv target/x86_64-pc-windows-msvc/release/fmt-mmd-gantt.exe .
```

```sh
# linux
cargo build --release --target x86_64-unknown-linux-gnu
chmod +x target/x86_64-unknown-linux-gnu/release/fmt-mmd-gantt
mv target/x86_64-unknown-linux-gnu/release/fmt-mmd-gantt .
```

### Run

cargo run timeline.mmd new.mmd # write to destination
```shell
./fmt-mmd-gantt.exe timeline.mmd # edited in-place
./fmt-mmd-gantt.exe timeline.mmd new.mmd # write to destination
# remove .exe ^ if linux
```

### After

```mmd
gantt
%% Example from https://mermaid.js.org/syntax/gantt.html#syntax.
title Adding GANTT diagram functionality to mermaid
dateFormat YYYY-MM-DD
Expand Down
9 changes: 9 additions & 0 deletions build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# windows
cargo build --release --target x86_64-pc-windows-msvc
chmod +x target/x86_64-pc-windows-msvc/release/fmt-mmd-gan.exett
mv target/x86_64-pc-windows-msvc/release/fmt-mmd-gantt.exe .

# linux
# cargo build --release --target x86_64-unknown-linux-gnu
# chmod +x target/x86_64-unknown-linux-gnu/release/fmt-mmd-gantt
# mv target/x86_64-unknown-linux-gnu/release/fmt-mmd-gantt .
37 changes: 23 additions & 14 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ fn get_task_lines(lines: Vec<&str>) -> Vec<&str> {
return task_lines;
}

fn push_task_lines(
fn push_task_line(
line: &str,
map_item_lenths: &HashMap<&str, usize>,
mut new_lines: Vec<String>,
Expand All @@ -173,6 +173,7 @@ fn push_task_lines(
map_item_lenths.clone(),
);
// Indent task titles 2 levels.
// Format commented out tasks so that they align with other tasks, but keep them as comments.
if task_line.starts_with("%%") {
let comment_task_split: Vec<&str> = task_line
.strip_prefix("%%")
Expand All @@ -192,6 +193,13 @@ fn push_task_lines(
return new_lines;
}

fn push_section_line(line: &str, mut new_lines: Vec<String>) -> Vec<String> {
// Add section titles indented 1 level with 1 leading newline.
let section_title: &str = line.strip_prefix("section").unwrap().trim();
new_lines.push(String::from(format!("\n section {}", section_title)));
return new_lines;
}

/// Returns a mapping of the max length for tasks names and metadata tags.
/// Used for padding whitespace chars and aligning columns.
fn get_max_item_lengths<'a>(tags: [&str; 4], lines: Vec<&'a str>) -> HashMap<&'a str, usize> {
Expand Down Expand Up @@ -280,9 +288,9 @@ fn create_or_replace_file(file_name: &String, contents: String) -> std::io::Resu
Ok(())
}

type MapIter<'l> = Map<IntoIter<&'l str>, for<'a> fn(&'a str) -> &'a str>;
type MapIntoIter<'l> = Map<IntoIter<&'l str>, for<'a> fn(&'a str) -> &'a str>;

fn line_is_before_section(line: &str, idx: usize, c_map_lines: &MapIter) -> bool {
fn line_is_empty_before_section(line: &str, idx: usize, c_map_lines: &MapIntoIter) -> bool {
return line.is_empty()
&& c_map_lines.clone().collect::<Vec<_>>()[idx.add(1)].contains("section");
}
Expand All @@ -296,34 +304,35 @@ fn line_is_task(line: &str) -> bool {
}

fn line_is_title(line: &str) -> bool {
// Add gantt title at top of file with no indent and add any commented lines that are not tasks.
return line.starts_with("gantt") || (line.starts_with("%%") && !line.contains(":"));
}

fn generate_new_lines(lines: Vec<&str>) -> Vec<String> {
let mut new_lines: Vec<String> = vec![];
let map_item_lenths: HashMap<&str, usize> = get_max_item_lengths(TASK_TAGS, lines.clone());
let map_lines: MapIter = lines.into_iter().map(str::trim);
let c_map_lines: MapIter = map_lines.clone();
let map_lines: MapIntoIter = lines.into_iter().map(str::trim);
let c_map_lines: MapIntoIter = map_lines.clone();

for (_idx, line) in map_lines.enumerate() {
if line_is_title(line) {
// Add gantt title at top with no indent/any commented, non-task lines.
new_lines.push(String::from(line));
} else if line_is_before_section(line, _idx, &c_map_lines) {
new_lines.push(String::new());
} else if line_is_keyword(line) {
new_lines.push(format!(" {}", line)); // Add any remaining keyword lines idented 1 level.
// Add any remaining keyword lines idented 1 level.
new_lines.push(format!(" {}", line));
} else if line_is_empty_before_section(line, _idx, &c_map_lines) {
// Ignore all empty lines if before section, leading newline will be add with section keyword.
continue;
} else if line.contains("section") {
new_lines.push(String::from(format!(" {}", line))); // Indent sections 1 level.
new_lines = push_section_line(line, new_lines);
} else if line_is_task(line) {
new_lines = push_task_lines(line, &map_item_lenths, new_lines);
new_lines = push_task_line(line, &map_item_lenths, new_lines);
} else if !line.is_empty() {
// For any uncaught scenarios, add the line as is.
// For any remaining scenarios not caught above, add the line as is.
new_lines.push(String::from(line));
}
}
// Add 1 empty line at end of file.
new_lines.push(String::new());
new_lines.push(String::new()); // Add 1 empty line at end of file.
return new_lines;
}

Expand Down
1 change: 0 additions & 1 deletion timeline.mmd
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
gantt
%% Example from https://mermaid.js.org/syntax/gantt.html#syntax.
title Adding GANTT diagram functionality to mermaid
dateFormat YYYY-MM-DD

Expand Down

0 comments on commit 0571984

Please sign in to comment.