@@ -32,36 +32,30 @@ def parse(self, file_path: str, context: Dict[str, Any]) -> Optional[RuleMetadat
3232 try :
3333 with open (file_path , encoding = "utf-8" ) as f :
3434 content = f .read ()
35+ except (FileNotFoundError , PermissionError , UnicodeDecodeError ) as e :
36+ print (f"Error reading { file_path } : { e } " )
37+ return None
3538
36- # Extract HTML comment frontmatter
37- metadata = self ._parse_frontmatter (content )
38- if not metadata :
39- return None
40-
41- # Extract category from directory structure
42- path = Path (file_path )
43- category = self ._extract_category (path , context .get ("project_root" ))
44-
45- # Get relative path from project root
46- project_root = Path (context .get ("project_root" , "." ))
47- relative_path = path .relative_to (project_root )
48-
49- return RuleMetadata (
50- file_path = file_path ,
51- relative_path = str (relative_path ),
52- title = metadata .get ("name" , path .stem .replace ("-" , " " ).title ()),
53- description = metadata .get ("description" , "" ),
54- scope_patterns = [], # Code review rules don't have file scope
55- always_apply = False , # Code review rules are always contextual
56- category = category ,
57- raw_content = content ,
58- metadata = metadata ,
59- )
60-
61- except Exception as e :
62- print (f"Error parsing { file_path } : { e } " )
39+ # Extract HTML comment frontmatter
40+ metadata = self ._parse_frontmatter (content )
41+ if not metadata :
6342 return None
6443
44+ # Generate title from filename
45+ title = metadata .get ("name" , Path (file_path ).stem .replace ("-" , " " ).title ())
46+
47+ return RuleMetadata (
48+ file_path = file_path ,
49+ relative_path = context .get ("relative_path" , file_path ),
50+ title = title ,
51+ description = metadata .get ("description" , "" ),
52+ scope_patterns = [],
53+ always_apply = False ,
54+ category = context .get ("category" , "root" ),
55+ raw_content = content ,
56+ metadata = metadata ,
57+ )
58+
6559 def _parse_frontmatter (self , content : str ) -> Dict [str , str ]:
6660 """Parse HTML comment frontmatter from markdown content."""
6761 # Match HTML comment block at start of file
@@ -83,16 +77,3 @@ def _parse_frontmatter(self, content: str) -> Dict[str, str]:
8377 metadata [key .strip ()] = value .strip ()
8478
8579 return metadata
86-
87- def _extract_category (self , file_path : Path , project_root : Optional [str ]) -> str :
88- """Extract category from directory structure."""
89- parts = file_path .parts
90- try :
91- code_review_idx = parts .index (".code_review" )
92- # Category is the directory immediately after .code_review
93- if code_review_idx + 1 < len (parts ) - 1 : # -1 because last part is filename
94- return parts [code_review_idx + 1 ]
95- except (ValueError , IndexError ):
96- pass
97-
98- return "root"
0 commit comments