From 4b07ddfe1a65c1263ea3e4a5209d5ff8ed53e135 Mon Sep 17 00:00:00 2001 From: Viktor Ashirov Date: Mon, 19 Dec 2022 20:30:05 +0100 Subject: [PATCH] Handle multiline snippets better Add support for code blocks using markdown syntax. Lines that are part of the snippet and match the variables regex won't be interpeted as a variable. Additionally, add `.cheat.md` as a file extension for cheats so that it can be rendered on GitHub with proper code formatting and syntax highlighting. Fixes #806 --- src/filesystem.rs | 2 +- src/parser.rs | 10 +++++++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/filesystem.rs b/src/filesystem.rs index 4a34ba9e..95736d65 100644 --- a/src/filesystem.rs +++ b/src/filesystem.rs @@ -18,7 +18,7 @@ pub fn all_cheat_files(path: &Path) -> Vec { .into_iter() .filter_map(|e| e.ok()) .map(|e| e.path().to_str().unwrap_or("").to_string()) - .filter(|e| e.ends_with(".cheat")) + .filter(|e| e.ends_with(".cheat") || e.ends_with(".cheat.md")) .collect::>() } diff --git a/src/parser.rs b/src/parser.rs index 085c990e..d059c121 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -243,6 +243,8 @@ impl<'a> Parser<'a> { let mut variable_cmd = String::from(""); + let mut inside_snippet: bool = false; + for (line_nr, line_result) in lines.enumerate() { let line = line_result .with_context(|| format!("Failed to read line number {} in cheatsheet `{}`", line_nr, id))?; @@ -284,7 +286,9 @@ impl<'a> Parser<'a> { item.comment = without_prefix(&line); } // variable - else if !variable_cmd.is_empty() || (line.starts_with('$') && line.contains(':')) { + else if !variable_cmd.is_empty() + || (line.starts_with('$') && line.contains(':')) && !inside_snippet + { should_break = self.write_cmd(&item).is_err(); item.snippet = String::from(""); @@ -306,6 +310,10 @@ impl<'a> Parser<'a> { .insert_suggestion(&item.tags, variable, (String::from(command), opts)); } } + // markdown snippet + else if line.starts_with("```") { + inside_snippet = !inside_snippet; + } // snippet else { if !(&item.snippet).is_empty() {