From f40908deab1b57612d1834a898d570d6b2bf3b73 Mon Sep 17 00:00:00 2001 From: n1kolas Date: Fri, 25 Apr 2025 10:45:36 +0200 Subject: [PATCH 1/2] fix: remove single asterisk at start of usage example --- src/parser/php.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/parser/php.rs b/src/parser/php.rs index a1df873..e22d6f9 100644 --- a/src/parser/php.rs +++ b/src/parser/php.rs @@ -392,7 +392,16 @@ impl PhpParser { let example = comment_text[code_start..end_index].trim(); let cleaned_example = example .lines() - .map(|line| line.trim_start().strip_prefix("* ").unwrap_or(line)) + .map(|line| { + let trimmed = line.trim_start(); + if let Some(stripped) = trimmed.strip_prefix("* ") { + stripped + } else if let Some(stripped) = trimmed.strip_prefix("*") { + stripped + } else { + trimmed + } + }) .collect::>(); let result = &cleaned_example[..cleaned_example.len() - 1] From 15b4a4756348893d1ef64ac1532a80c8d4fe2b3c Mon Sep 17 00:00:00 2001 From: n1kolas Date: Fri, 25 Apr 2025 12:51:46 +0200 Subject: [PATCH 2/2] refactor: use regex for usage example extraction --- src/parser/php.rs | 43 +++++++++++++++++++------------------------ 1 file changed, 19 insertions(+), 24 deletions(-) diff --git a/src/parser/php.rs b/src/parser/php.rs index e22d6f9..2ca31df 100644 --- a/src/parser/php.rs +++ b/src/parser/php.rs @@ -375,7 +375,7 @@ impl PhpParser { node.utf8_text(self.source.as_bytes()).unwrap_or("") } - /// Helper function to extract usage example from the preceding comment + /// Helper function to extract usage example from the preceding comment. fn extract_usage_example_from_comment(&self, comment_node: &Node) -> Option { if comment_node.kind() != "comment" { return None; @@ -385,31 +385,26 @@ impl PhpParser { let start_tag = "@code"; let end_tag = "@endcode"; - if let Some(start_index) = comment_text.find(start_tag) { - if let Some(end_index) = comment_text.find(end_tag) { - if end_index > start_index { - let code_start = start_index + start_tag.len(); - let example = comment_text[code_start..end_index].trim(); - let cleaned_example = example - .lines() - .map(|line| { - let trimmed = line.trim_start(); - if let Some(stripped) = trimmed.strip_prefix("* ") { - stripped - } else if let Some(stripped) = trimmed.strip_prefix("*") { - stripped - } else { - trimmed - } - }) - .collect::>(); - - let result = &cleaned_example[..cleaned_example.len() - 1] + if let (Some(start_index), Some(end_index)) = + (comment_text.find(start_tag), comment_text.find(end_tag)) + { + if end_index > start_index { + let code_start = start_index + start_tag.len(); + let example = comment_text[code_start..end_index].trim(); + + // Regex to replace "* " or "*" from the beginning of a line. + let re = Regex::new(r"^\s*\*\s?").unwrap(); + let cleaned_example = example + .lines() + .map(|line| re.replace(line, "").to_string()) + .collect::>(); + + return Some( + cleaned_example[..cleaned_example.len() - 1] .join("\n") .trim() - .to_string(); - return Some(result.to_string()); - } + .to_string(), + ); } } None