File size: 5,658 Bytes
9abace2 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 | """
Markdown Parser
Part of SOVEREIGN PYTHON LLM ENGINE
Parse Markdown documents and convert to structured format.
"""
from typing import Any
from pathlib import Path
from dataclasses import dataclass
import re
@dataclass
class MarkdownDocument:
"""Parsed Markdown document"""
text: str
sections: list[dict[str, Any]]
links: list[dict[str, str]]
images: list[dict[str, str]]
code_blocks: list[dict[str, str]]
metadata: dict[str, Any]
class MarkdownParser:
"""
Markdown document parser.
Extracts:
- Sections (by heading level)
- Links
- Images
- Code blocks
- Front matter (YAML)
"""
def __init__(self):
pass
async def parse(self, file_path: Path) -> MarkdownDocument:
"""
Parse Markdown file.
Args:
file_path: Path to Markdown file
Returns:
MarkdownDocument
"""
text = file_path.read_text(encoding="utf-8")
return self.parse_text(text)
def parse_text(self, text: str) -> MarkdownDocument:
"""
Parse Markdown text.
Args:
text: Markdown content
Returns:
MarkdownDocument
"""
# Extract front matter (YAML)
metadata, text = self._extract_front_matter(text)
# Extract sections
sections = self._extract_sections(text)
# Extract links
links = self._extract_links(text)
# Extract images
images = self._extract_images(text)
# Extract code blocks
code_blocks = self._extract_code_blocks(text)
return MarkdownDocument(
text=text,
sections=sections,
links=links,
images=images,
code_blocks=code_blocks,
metadata=metadata
)
def _extract_front_matter(self, text: str) -> tuple[dict, str]:
"""Extract YAML front matter"""
if not text.startswith("---"):
return {}, text
# Find end of front matter
match = re.match(r"^---\n(.*?)\n---\n", text, re.DOTALL)
if not match:
return {}, text
yaml_content = match.group(1)
remaining_text = text[match.end():]
# Parse YAML (simple key-value extraction)
metadata = {}
for line in yaml_content.split("\n"):
if ":" in line:
key, value = line.split(":", 1)
metadata[key.strip()] = value.strip()
return metadata, remaining_text
def _extract_sections(self, text: str) -> list[dict[str, Any]]:
"""Extract sections by heading level"""
sections = []
lines = text.split("\n")
current_section = None
for line in lines:
# Check if heading
if line.startswith("#"):
# Save previous section
if current_section:
sections.append(current_section)
# Start new section
level = len(line) - len(line.lstrip("#"))
title = line.lstrip("#").strip()
current_section = {
"level": level,
"title": title,
"content": []
}
elif current_section is not None:
current_section["content"].append(line)
# Add last section
if current_section:
current_section["content"] = "\n".join(current_section["content"]).strip()
sections.append(current_section)
return sections
def _extract_links(self, text: str) -> list[dict[str, str]]:
"""Extract Markdown links"""
# Pattern: [text](url)
pattern = r'\[([^\]]+)\]\(([^\)]+)\)'
matches = re.findall(pattern, text)
links = []
for text, url in matches:
links.append({
"text": text,
"url": url
})
return links
def _extract_images(self, text: str) -> list[dict[str, str]]:
"""Extract Markdown images"""
# Pattern: 
pattern = r'!\[([^\]]*)\]\(([^\)]+)\)'
matches = re.findall(pattern, text)
images = []
for alt, src in matches:
images.append({
"alt": alt,
"src": src
})
return images
def _extract_code_blocks(self, text: str) -> list[dict[str, str]]:
"""Extract fenced code blocks"""
# Pattern: ```language\ncode\n```
pattern = r'```(\w*)\n(.*?)```'
matches = re.findall(pattern, text, re.DOTALL)
code_blocks = []
for language, code in matches:
code_blocks.append({
"language": language or "text",
"code": code.strip()
})
return code_blocks
# Tool registration helper
async def parse_markdown_tool(file_path: str) -> dict:
"""
Tool wrapper for Markdown parsing.
Args:
file_path: Path to Markdown file
Returns:
Dictionary with parsed content
"""
parser = MarkdownParser()
result = await parser.parse(Path(file_path))
return {
"text": result.text,
"sections_count": len(result.sections),
"links_count": len(result.links),
"images_count": len(result.images),
"code_blocks_count": len(result.code_blocks),
"metadata": result.metadata
}
|