2025-09-23 04:28:29 +02:00
|
|
|
"""
|
|
|
|
|
Front matter parsing functionality for MarkiTect.
|
|
|
|
|
|
|
|
|
|
This module provides YAML front matter parsing for markdown files,
|
|
|
|
|
separating metadata from content.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
import re
|
|
|
|
|
import yaml
|
|
|
|
|
from typing import Dict, Tuple, Any
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class FrontMatterParser:
|
|
|
|
|
"""Parser for YAML front matter in markdown files."""
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
"""Initialize the front matter parser."""
|
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
def parse(self, content: str) -> Tuple[Dict[str, Any], str]:
|
|
|
|
|
"""
|
|
|
|
|
Parse front matter from markdown content.
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
content: Raw markdown content that may include YAML front matter
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Tuple of (front_matter_dict, markdown_content)
|
|
|
|
|
- front_matter_dict: Parsed YAML as dictionary, empty dict if none
|
|
|
|
|
- markdown_content: Markdown content with front matter removed
|
|
|
|
|
"""
|
|
|
|
|
if not content.strip():
|
|
|
|
|
return {}, content
|
|
|
|
|
|
|
|
|
|
# Check if content starts with front matter delimiter
|
|
|
|
|
if not content.strip().startswith('---'):
|
|
|
|
|
return {}, content
|
|
|
|
|
|
|
|
|
|
# Pattern to match YAML front matter
|
|
|
|
|
# Must start with --- at beginning of string, end with --- on its own line
|
|
|
|
|
pattern = r'^---\s*\n(.*?)\n---\s*\n(.*)$'
|
|
|
|
|
match = re.match(pattern, content, re.DOTALL)
|
|
|
|
|
|
|
|
|
|
if not match:
|
|
|
|
|
# No valid front matter found
|
|
|
|
|
return {}, content
|
|
|
|
|
|
|
|
|
|
yaml_content = match.group(1)
|
|
|
|
|
markdown_content = match.group(2)
|
|
|
|
|
|
|
|
|
|
# Parse YAML content
|
|
|
|
|
try:
|
|
|
|
|
front_matter = yaml.safe_load(yaml_content)
|
|
|
|
|
if front_matter is None:
|
|
|
|
|
front_matter = {}
|
|
|
|
|
except yaml.YAMLError:
|
|
|
|
|
# Invalid YAML - return empty dict and preserve content
|
|
|
|
|
front_matter = {}
|
|
|
|
|
|
2025-10-06 03:03:38 +02:00
|
|
|
return front_matter, markdown_content
|