File size: 6,842 Bytes
caa7511 | 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 | # utils/markdown_parser.py
import re
from typing import Dict, List, Any
class MarkdownParser:
def __init__(self, parse_tables=True, parse_code=True, parse_lists=True):
self.parse_tables = parse_tables
self.parse_code = parse_code
self.parse_lists = parse_lists
def parse(self, markdown_text: str) -> Dict[str, Any]:
"""Parse markdown text and return structured data"""
lines = markdown_text.split('\n')
structure = []
stats = {
'headers': 0,
'tables': 0,
'code_blocks': 0,
'lists': 0,
'blockquotes': 0,
'paragraphs': 0
}
i = 0
while i < len(lines):
line = lines[i]
# Parse headers
if line.startswith('#'):
header_data = self._parse_header(line)
if header_data:
structure.append(header_data)
stats['headers'] += 1
i += 1
# Parse code blocks
elif line.startswith('```') and self.parse_code:
code_data, lines_consumed = self._parse_code_block(lines[i:])
if code_data:
structure.append(code_data)
stats['code_blocks'] += 1
i += lines_consumed
# Parse tables
elif '|' in line and self.parse_tables:
table_data, lines_consumed = self._parse_table(lines[i:])
if table_data:
structure.append(table_data)
stats['tables'] += 1
i += lines_consumed
else:
i += 1
# Parse lists
elif (line.strip().startswith(('-', '*', '+')) or
re.match(r'^\d+\.', line.strip())) and self.parse_lists:
list_data, lines_consumed = self._parse_list(lines[i:])
if list_data:
structure.append(list_data)
stats['lists'] += 1
i += lines_consumed
# Parse blockquotes
elif line.startswith('>'):
quote_data = self._parse_blockquote(line)
if quote_data:
structure.append(quote_data)
stats['blockquotes'] += 1
i += 1
# Parse paragraphs
elif line.strip():
para_data = self._parse_paragraph(line)
if para_data:
structure.append(para_data)
stats['paragraphs'] += 1
i += 1
else:
i += 1
return {
'structure': structure,
'stats': stats,
'metadata': {
'total_elements': len(structure),
'has_tables': stats['tables'] > 0,
'has_code': stats['code_blocks'] > 0,
'has_lists': stats['lists'] > 0
}
}
def _parse_header(self, line: str) -> Dict[str, Any]:
"""Parse header line"""
match = re.match(r'^(#+)\s+(.+)$', line)
if match:
level = len(match.group(1))
content = match.group(2)
return {
'type': 'header',
'level': level,
'content': content,
'raw': line
}
return None
def _parse_code_block(self, lines: List[str]) -> tuple:
"""Parse code block"""
if not lines[0].startswith('```'):
return None, 1
language = lines[0][3:].strip()
code_lines = []
for i in range(1, len(lines)):
if lines[i].startswith('```'):
return {
'type': 'code',
'language': language or 'plaintext',
'content': '\n'.join(code_lines),
'raw': '\n'.join(lines[:i+1])
}, i + 1
code_lines.append(lines[i])
return None, 1
def _parse_table(self, lines: List[str]) -> tuple:
"""Parse markdown table"""
if '|' not in lines[0]:
return None, 1
table_lines = []
for i, line in enumerate(lines):
if '|' in line:
table_lines.append(line)
else:
break
if len(table_lines) < 2:
return None, 1
# Parse table structure
headers = [cell.strip() for cell in table_lines[0].split('|') if cell.strip()]
rows = []
for line in table_lines[2:]: # Skip separator line
if '|' in line:
row = [cell.strip() for cell in line.split('|') if cell.strip()]
if row:
rows.append(row)
if headers:
return {
'type': 'table',
'headers': headers,
'rows': rows,
'content': f"Table with {len(headers)} columns and {len(rows)} rows",
'raw': '\n'.join(table_lines)
}, len(table_lines)
return None, 1
def _parse_list(self, lines: List[str]) -> tuple:
"""Parse list items"""
list_items = []
i = 0
for line in lines:
if re.match(r'^[\s]*[-*+]\s+', line) or re.match(r'^[\s]*\d+\.\s+', line):
# Extract list item content
content = re.sub(r'^[\s]*[-*+]\s+', '', line)
content = re.sub(r'^[\s]*\d+\.\s+', '', content)
list_items.append(content.strip())
i += 1
elif not line.strip():
break
else:
break
if list_items:
return {
'type': 'list',
'items': list_items,
'content': f"List with {len(list_items)} items",
'raw': '\n'.join(lines[:i])
}, i
return None, 1
def _parse_blockquote(self, line: str) -> Dict[str, Any]:
"""Parse blockquote"""
if line.startswith('>'):
content = line[1:].strip()
return {
'type': 'blockquote',
'content': content,
'raw': line
}
return None
def _parse_paragraph(self, line: str) -> Dict[str, Any]:
"""Parse regular paragraph"""
if line.strip():
return {
'type': 'paragraph',
'content': line.strip(),
'raw': line
}
return None |