use regex::Regex;
pub fn extract_list_from_section(
content: &str,
section_heading: &str,
keyword: &str,
) -> Vec<String> {
let section_content = match extract_section_content(content, section_heading) {
Some(s) => s,
None => return Vec::new(),
};
parse_keyword_list(§ion_content, keyword)
}
pub fn extract_section_content(content: &str, section_heading: &str) -> Option<String> {
let section_heading_re = Regex::new(r"^#{2,6}\s+(.+)$").expect("Invalid section heading regex");
let heading_lower = section_heading.to_lowercase();
let lines: Vec<&str> = content.lines().collect();
let mut in_section = false;
let mut section_lines = Vec::new();
let mut section_level = 0;
for line in lines {
if let Some(caps) = section_heading_re.captures(line) {
let current_heading = caps.get(1).map(|m| m.as_str()).unwrap_or("");
let current_level = line.chars().take_while(|&c| c == '#').count();
if current_heading.to_lowercase().trim() == heading_lower.trim() {
in_section = true;
section_level = current_level;
continue;
} else if in_section && current_level <= section_level {
break;
}
}
if in_section {
section_lines.push(line);
}
}
if section_lines.is_empty() {
None
} else {
Some(section_lines.join("\n"))
}
}
pub fn parse_keyword_list(content: &str, keyword: &str) -> Vec<String> {
let keyword_list_re =
Regex::new(r"^\s*[-*]\s+\*\*([^*]+)\*\*:\s*(.+)$").expect("Invalid keyword list regex");
let keyword_lower = keyword.to_lowercase();
for line in content.lines() {
if let Some(caps) = keyword_list_re.captures(line) {
let line_keyword = caps.get(1).map(|m| m.as_str()).unwrap_or("");
let values = caps.get(2).map(|m| m.as_str()).unwrap_or("");
if line_keyword.to_lowercase().trim() == keyword_lower.trim() {
return parse_comma_list(values);
}
}
}
Vec::new()
}
pub fn parse_comma_list(input: &str) -> Vec<String> {
let separator_re = Regex::new(r"[,;]\s*|\s+and\s+|\s+or\s+").expect("Invalid separator regex");
separator_re
.split(input)
.map(|s| s.trim().to_string())
.filter(|s| !s.is_empty())
.collect()
}
pub fn extract_all_list_items(content: &str, section_heading: &str) -> Vec<String> {
let section = match extract_section_content(content, section_heading) {
Some(s) => s,
None => return Vec::new(),
};
let list_item_re = Regex::new(r"^\s*[-*]\s+(.+)$").expect("Invalid list item regex");
section
.lines()
.filter_map(|line| {
list_item_re
.captures(line)
.and_then(|caps| caps.get(1))
.map(|m| m.as_str().trim().to_string())
})
.collect()
}
pub fn normalize_id(id: &str) -> String {
id.trim().to_lowercase().replace([' ', '_'], "-")
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_extract_list_basic() {
let content = r#"
# Concept Card
Description here.
## Related Concepts
- **Prerequisite**: major-triad, minor-key
- **Leads to**: modal-mixture
- **See also**: borrowed-chords
"#;
let prereqs = extract_list_from_section(content, "Related Concepts", "Prerequisite");
assert_eq!(prereqs, vec!["major-triad", "minor-key"]);
let leads_to = extract_list_from_section(content, "Related Concepts", "Leads to");
assert_eq!(leads_to, vec!["modal-mixture"]);
let see_also = extract_list_from_section(content, "Related Concepts", "See also");
assert_eq!(see_also, vec!["borrowed-chords"]);
}
#[test]
fn test_extract_list_case_insensitive() {
let content = r#"
## Dependencies
- **REQUIRES**: concept-a, concept-b
"#;
let items = extract_list_from_section(content, "dependencies", "requires");
assert_eq!(items, vec!["concept-a", "concept-b"]);
}
#[test]
fn test_extract_list_missing_section() {
let content = "# Just a heading\n\nNo related concepts section.";
let items = extract_list_from_section(content, "Related Concepts", "Prerequisite");
assert!(items.is_empty());
}
#[test]
fn test_extract_list_missing_keyword() {
let content = r#"
## Related Concepts
- **See also**: something
"#;
let items = extract_list_from_section(content, "Related Concepts", "Prerequisite");
assert!(items.is_empty());
}
#[test]
fn test_extract_list_multiple_sections() {
let content = r#"
## Section One
- **Items**: a, b
## Section Two
- **Items**: c, d
## Section Three
Content here.
"#;
let section_one = extract_list_from_section(content, "Section One", "Items");
assert_eq!(section_one, vec!["a", "b"]);
let section_two = extract_list_from_section(content, "Section Two", "Items");
assert_eq!(section_two, vec!["c", "d"]);
}
#[test]
fn test_extract_section_basic() {
let content = r#"
## Intro
This is the intro.
## Details
More details.
"#;
let intro = extract_section_content(content, "Intro").unwrap();
assert!(intro.contains("This is the intro"));
assert!(!intro.contains("More details"));
}
#[test]
fn test_extract_section_with_subsections() {
let content = r#"
## Main Section
Content.
### Subsection
Subsection content.
## Next Section
Different content.
"#;
let main = extract_section_content(content, "Main Section").unwrap();
assert!(main.contains("Content"));
assert!(main.contains("Subsection content")); assert!(!main.contains("Different content"));
}
#[test]
fn test_extract_section_not_found() {
let content = "## Other Section\n\nContent.";
assert!(extract_section_content(content, "Missing").is_none());
}
#[test]
fn test_parse_keyword_list_basic() {
let content = "- **Tags**: rust, programming, async";
let tags = parse_keyword_list(content, "Tags");
assert_eq!(tags, vec!["rust", "programming", "async"]);
}
#[test]
fn test_parse_keyword_list_single_item() {
let content = "- **Author**: Claude";
let authors = parse_keyword_list(content, "Author");
assert_eq!(authors, vec!["Claude"]);
}
#[test]
fn test_parse_keyword_list_asterisk_bullets() {
let content = "* **Items**: one, two, three";
let items = parse_keyword_list(content, "Items");
assert_eq!(items, vec!["one", "two", "three"]);
}
#[test]
fn test_parse_comma_list_basic() {
let items = parse_comma_list("apple, banana, cherry");
assert_eq!(items, vec!["apple", "banana", "cherry"]);
}
#[test]
fn test_parse_comma_list_semicolon() {
let items = parse_comma_list("one; two; three");
assert_eq!(items, vec!["one", "two", "three"]);
}
#[test]
fn test_parse_comma_list_and_or() {
let items = parse_comma_list("red and blue or green");
assert_eq!(items, vec!["red", "blue", "green"]);
}
#[test]
fn test_parse_comma_list_whitespace() {
let items = parse_comma_list(" item1 , item2 , item3 ");
assert_eq!(items, vec!["item1", "item2", "item3"]);
}
#[test]
fn test_parse_comma_list_empty() {
let items = parse_comma_list("");
assert!(items.is_empty());
}
#[test]
fn test_extract_all_list_items_basic() {
let content = r#"
## Topics
- First item
- Second item
- Third item
## Other
"#;
let items = extract_all_list_items(content, "Topics");
assert_eq!(items.len(), 3);
assert_eq!(items[0], "First item");
assert_eq!(items[2], "Third item");
}
#[test]
fn test_extract_all_list_items_with_formatting() {
let content = r#"
## List
- **Bold item**
- *Italic item*
- `Code item`
"#;
let items = extract_all_list_items(content, "List");
assert_eq!(items.len(), 3);
assert_eq!(items[0], "**Bold item**");
}
#[test]
fn test_normalize_id_spaces() {
assert_eq!(normalize_id("Major Triad"), "major-triad");
}
#[test]
fn test_normalize_id_underscores() {
assert_eq!(normalize_id("some_concept"), "some-concept");
}
#[test]
fn test_normalize_id_mixed() {
assert_eq!(normalize_id(" Mixed_Case Concept "), "mixed-case-concept");
}
#[test]
fn test_normalize_id_already_normalized() {
assert_eq!(normalize_id("already-normalized"), "already-normalized");
}
#[test]
fn test_music_theory_related_concepts() {
let content = r#"
---
concept: Picardy Third
category: chromaticism
---
# Picardy Third
A Picardy third is a major chord used at the end of a piece in minor key.
## Related Concepts
- **Prerequisite**: major-triad, minor-key
- **Leads to**: modal-mixture
- **See also**: borrowed-chords
"#;
let prereqs = extract_list_from_section(content, "Related Concepts", "Prerequisite");
assert_eq!(prereqs, vec!["major-triad", "minor-key"]);
}
#[test]
fn test_math_dependencies() {
let content = r#"
# Fundamental Theorem of Calculus
## Dependencies
- **Requires**: derivative-definition, integral-definition, continuous-functions
- **Implies**: antiderivative-existence
- **Related**: mean-value-theorem, integration-by-parts
"#;
let requires = extract_list_from_section(content, "Dependencies", "Requires");
assert_eq!(
requires,
vec![
"derivative-definition",
"integral-definition",
"continuous-functions"
]
);
let implies = extract_list_from_section(content, "Dependencies", "Implies");
assert_eq!(implies, vec!["antiderivative-existence"]);
}
#[test]
fn test_rust_learning_path() {
let content = r#"
# Async/Await Syntax
## Learning Path
- **Must know first**: futures, polling, pinning
- **Unlocks**: async-fn, async-block, tokio
- **See also**: threads, channels
"#;
let must_know = extract_list_from_section(content, "Learning Path", "Must know first");
assert_eq!(must_know, vec!["futures", "polling", "pinning"]);
let unlocks = extract_list_from_section(content, "Learning Path", "Unlocks");
assert_eq!(unlocks, vec!["async-fn", "async-block", "tokio"]);
}
}