|
""" |
|
Cursor Rules Generator - MDC Formatter |
|
|
|
This module handles the formatting of rules in MDC format. |
|
""" |
|
|
|
from typing import Dict, Any, Optional |
|
|
|
class MDCFormatter: |
|
"""Formatter for MDC (Markdown with Context) format.""" |
|
|
|
def format_to_mdc( |
|
self, |
|
rule_type: str, |
|
content: str, |
|
description: str, |
|
parameters: Optional[Dict[str, Any]] = None |
|
) -> str: |
|
"""Format rule content to MDC format. |
|
|
|
Args: |
|
rule_type: The type of rule (Always, Auto Attached, Agent Requested, Manual) |
|
content: The main content of the rule |
|
description: A short description of the rule's purpose |
|
parameters: Additional parameters for formatting |
|
|
|
Returns: |
|
str: The formatted rule in MDC format |
|
""" |
|
|
|
if parameters is None: |
|
parameters = {} |
|
|
|
|
|
globs = parameters.get('globs', '') |
|
referenced_files = parameters.get('referenced_files', '') |
|
|
|
|
|
mdc = '---\n' |
|
mdc += f'description: {description}\n' |
|
|
|
if rule_type == 'Auto Attached' and globs: |
|
mdc += f'globs: {globs}\n' |
|
|
|
if rule_type == 'Always': |
|
mdc += 'alwaysApply: true\n' |
|
else: |
|
mdc += 'alwaysApply: false\n' |
|
|
|
mdc += '---\n\n' |
|
mdc += content + '\n' |
|
|
|
|
|
if referenced_files: |
|
mdc += '\n' + referenced_files |
|
|
|
return mdc |
|
|
|
def parse_mdc(self, mdc_content: str) -> Dict[str, Any]: |
|
"""Parse MDC content into a structured format. |
|
|
|
Args: |
|
mdc_content: The MDC content to parse |
|
|
|
Returns: |
|
Dict[str, Any]: A dictionary containing the parsed MDC content |
|
""" |
|
result = { |
|
'metadata': {}, |
|
'content': '', |
|
'referenced_files': [] |
|
} |
|
|
|
|
|
if mdc_content.startswith('---'): |
|
|
|
parts = mdc_content.split('---', 2) |
|
|
|
if len(parts) >= 3: |
|
|
|
metadata_lines = parts[1].strip().split('\n') |
|
for line in metadata_lines: |
|
if ':' in line: |
|
key, value = line.split(':', 1) |
|
result['metadata'][key.strip()] = value.strip() |
|
|
|
|
|
content = parts[2].strip() |
|
else: |
|
|
|
content = mdc_content |
|
else: |
|
|
|
content = mdc_content |
|
|
|
|
|
lines = content.split('\n') |
|
content_lines = [] |
|
referenced_files = [] |
|
|
|
for line in lines: |
|
if line.strip().startswith('@'): |
|
referenced_files.append(line.strip()) |
|
else: |
|
content_lines.append(line) |
|
|
|
result['content'] = '\n'.join(content_lines).strip() |
|
result['referenced_files'] = referenced_files |
|
|
|
|
|
if result['metadata'].get('alwaysApply') == 'true': |
|
result['rule_type'] = 'Always' |
|
elif 'globs' in result['metadata']: |
|
result['rule_type'] = 'Auto Attached' |
|
else: |
|
|
|
result['rule_type'] = 'Manual' |
|
|
|
return result |
|
|