Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add ability to add custom comment symbol as string #40

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
pub comment_prefix: Option<char>,
pub comment_prefix: Option<String>,
pub comment_end_string: Option<String>,
/**
* @Article Configuration
Expand Down
65 changes: 56 additions & 9 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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());
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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());
Expand Down