Probably functional. Testing
This commit is contained in:
+4
-46
@@ -14,56 +14,14 @@ class RulesEngine:
|
||||
|
||||
def _load_rules(self) -> List[dict]:
|
||||
"""Load all rule files"""
|
||||
if not self.rules_dir.exists():
|
||||
self.rules_dir.mkdir(parents=True)
|
||||
self._create_default_rules()
|
||||
|
||||
rules = []
|
||||
for rule_file in self.rules_dir.glob('*.json'):
|
||||
with open(rule_file, 'r', encoding='utf-8') as f:
|
||||
rules.append(json.load(f))
|
||||
if self.rules_dir.exists():
|
||||
for rule_file in self.rules_dir.glob('*.json'):
|
||||
with open(rule_file, 'r', encoding='utf-8') as f:
|
||||
rules.append(json.load(f))
|
||||
|
||||
return rules
|
||||
|
||||
def _create_default_rules(self):
|
||||
"""Create default rule files"""
|
||||
# Tags normalization rule
|
||||
tags_rule = {
|
||||
"name": "tags_normalization",
|
||||
"description": "Normalize tags to lowercase with underscores",
|
||||
"type": "tags"
|
||||
}
|
||||
|
||||
tag_to_tags_rule = {
|
||||
"name": "tag_to_tags",
|
||||
"description": "Rename 'tag' key to 'tags'",
|
||||
"type": "rename"
|
||||
}
|
||||
|
||||
lowercase_keys_rule = {
|
||||
"name": "lowercase_keys",
|
||||
"description": "All frontmatter keys must be lowercase",
|
||||
"type": "keys"
|
||||
}
|
||||
|
||||
summary_to_description_rule = {
|
||||
"name": "summary_to_description",
|
||||
"description": "Rename 'summary' key to 'description'",
|
||||
"type": "rename"
|
||||
}
|
||||
|
||||
with open(self.rules_dir / 'tags_normalization.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(tags_rule, f, indent=2)
|
||||
|
||||
with open(self.rules_dir / 'tag_to_tags.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(tag_to_tags_rule, f, indent=2)
|
||||
|
||||
with open(self.rules_dir / 'lowercase_keys.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(lowercase_keys_rule, f, indent=2)
|
||||
|
||||
with open(self.rules_dir / 'summary_to_description.json', 'w', encoding='utf-8') as f:
|
||||
json.dump(summary_to_description_rule, f, indent=2)
|
||||
|
||||
def apply_rules(self, frontmatter: dict) -> Tuple[dict, bool]:
|
||||
"""Apply all rules to frontmatter, return (modified_fm, is_conformant)"""
|
||||
if not frontmatter:
|
||||
|
||||
Reference in New Issue
Block a user