From 091f5d0e2311ef5c5fb9bbdd09e4ab7ed00807e4 Mon Sep 17 00:00:00 2001 From: "vyacheslav.popov" Date: Tue, 4 Apr 2023 12:06:25 +0400 Subject: [PATCH] feat: add ability to add custom comment symbol as string --- src/config.rs | 2 +- src/parser.rs | 65 ++++++++++++++++++++++++++++++++++++++++++++------- 2 files changed, 57 insertions(+), 10 deletions(-) diff --git a/src/config.rs b/src/config.rs index c6b9b35..e43ce99 100644 --- a/src/config.rs +++ b/src/config.rs @@ -65,7 +65,7 @@ pub struct Config { * - `comment_end_string` - a string which marks the end of a comments block. Example: \*/ */ pub comment_start_string: Option, - pub comment_prefix: Option, + pub comment_prefix: Option, pub comment_end_string: Option, /** * @Article Configuration diff --git a/src/parser.rs b/src/parser.rs index eb19bac..e211c41 100644 --- a/src/parser.rs +++ b/src/parser.rs @@ -240,7 +240,7 @@ impl Keyword { pub struct Parser { state_machine: ParserStateMachine, - comment_symbol: char, + comment_symbol: String, start_comment: String, end_comment: String, code_block: String, @@ -255,7 +255,7 @@ impl Parser { let start_comment = config .comment_start_string .unwrap_or_else(|| "/**".to_string()); - let comment_symbol = config.comment_prefix.unwrap_or('*'); + let comment_symbol = config.comment_prefix.unwrap_or("*".to_string()); let end_comment = config .comment_end_string .unwrap_or_else(|| "*/".to_string()); @@ -329,7 +329,7 @@ impl Parser { fn trim_article_line(&self, line: String) -> String { line.trim_start() - .trim_start_matches(self.comment_symbol) + .trim_start_matches(&self.comment_symbol) .trim_start() .to_string() } @@ -361,13 +361,22 @@ impl Parser { fn parse_text<'a>(&self, line: &'a str) -> &'a str { let empty_comment_line = format!("{} ", self.comment_symbol); let trimmed_line = line.trim_start(); + let space_symbol = ' '; - if trimmed_line.starts_with(&empty_comment_line) { - &trimmed_line[2..] - } else if trimmed_line.starts_with(' ') || trimmed_line.starts_with(self.comment_symbol) { - &trimmed_line[1..] - } else { - trimmed_line + match trimmed_line { + l if l.starts_with(&empty_comment_line) => + { + &l[empty_comment_line.chars().count()..] + } + l if trimmed_line.starts_with(space_symbol) => + { + &l[1..] + } + l if l.starts_with(&self.comment_symbol) => + { + &l[self.comment_symbol.chars().count()..] + } + _ => &trimmed_line } } @@ -569,6 +578,44 @@ pub fn test () {} assert_eq!(articles, expected_result); } +#[test] +fn parse_articles_with_custom_comment_symbol() { + let mut parser = Parser::new({ + config::Config { + project_path: "test".to_string(), + files_patterns: vec!["test".to_string()], + docs_folder: None, + repository_host: None, + comment_start_string: Some("/@-".to_string()), + comment_prefix: Some("@-".to_string()), + comment_end_string: Some("@-/".to_string()), + mdbook: None, + book_name: None, + book_build_dir: None, + repositories: None, + plugins_dir: None, + } + }); + let file_content = " +/@- + @- @Article Test article + @- some text +@-/ + pub fn test () {} +"; + + let articles = parser.parse_file(file_content, ""); + let expected_result = vec![Article { + topic: String::from("Test article"), + content: String::from("some text"), + path: "".to_string(), + start_line: 3, + end_line: 4, + }]; + + assert_eq!(articles, expected_result); +} + #[test] fn ignore_comments_with_ignore_mark() { let mut parser = Parser::new(get_test_config());