From 05719848b637c1156f66404402432d0c6ebb4477 Mon Sep 17 00:00:00 2001 From: ayvi-0001 Date: Sun, 31 Mar 2024 11:44:02 -0700 Subject: [PATCH] fix newlines before section titles | add comments | update build steps --- .gitignore | 5 +++-- Cargo.lock | 7 +++++++ README.md | 26 ++++++++++++++++++++------ build.sh | 9 +++++++++ src/main.rs | 37 +++++++++++++++++++++++-------------- timeline.mmd | 1 - 6 files changed, 62 insertions(+), 23 deletions(-) create mode 100644 Cargo.lock create mode 100644 build.sh diff --git a/.gitignore b/.gitignore index 8d43f06..5214c22 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ +dev/ debug/ target/ /target -Cargo.lock -.vscode \ No newline at end of file +.vscode +.devcontainer diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..06137f5 --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,7 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "fmt-mmd-gantt" +version = "0.1.0" diff --git a/README.md b/README.md index 4751398..c75646c 100644 --- a/README.md +++ b/README.md @@ -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 @@ -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 diff --git a/build.sh b/build.sh new file mode 100644 index 0000000..201f628 --- /dev/null +++ b/build.sh @@ -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 . diff --git a/src/main.rs b/src/main.rs index 75202c3..8d3eb0f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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, @@ -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("%%") @@ -192,6 +193,13 @@ fn push_task_lines( return new_lines; } +fn push_section_line(line: &str, mut new_lines: Vec) -> Vec { + // 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> { @@ -280,9 +288,9 @@ fn create_or_replace_file(file_name: &String, contents: String) -> std::io::Resu Ok(()) } -type MapIter<'l> = Map, for<'a> fn(&'a str) -> &'a str>; +type MapIntoIter<'l> = Map, 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::>()[idx.add(1)].contains("section"); } @@ -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 { let mut new_lines: Vec = 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; } diff --git a/timeline.mmd b/timeline.mmd index 3a4474b..8b06464 100644 --- a/timeline.mmd +++ b/timeline.mmd @@ -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