Spaces:
Sleeping
Sleeping
Commit
·
1791aa5
1
Parent(s):
019d6a8
Upd code writer pdf rendering
Browse files- utils/pdf.py +78 -3
utils/pdf.py
CHANGED
|
@@ -50,6 +50,11 @@ def _parse_markdown_content(content: str, heading1_style, heading2_style, headin
|
|
| 50 |
elif line.startswith('```'):
|
| 51 |
# Extract language if specified
|
| 52 |
language = line[3:].strip() if len(line) > 3 else 'text'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
code_lines = []
|
| 54 |
i += 1
|
| 55 |
while i < len(lines) and not lines[i].strip().startswith('```'):
|
|
@@ -133,6 +138,73 @@ def _parse_markdown_content(content: str, heading1_style, heading2_style, headin
|
|
| 133 |
return story
|
| 134 |
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
def _format_code_block(code_text: str, language: str) -> str:
|
| 137 |
"""
|
| 138 |
Format code blocks with syntax highlighting for different languages
|
|
@@ -487,9 +559,12 @@ async def generate_report_pdf(report_content: str, user_id: str, project_id: str
|
|
| 487 |
backColor=colors.HexColor('#f8f9fa'),
|
| 488 |
borderColor=colors.HexColor('#dee2e6'),
|
| 489 |
borderWidth=1,
|
| 490 |
-
borderPadding=
|
| 491 |
-
leftIndent=
|
| 492 |
-
rightIndent=
|
|
|
|
|
|
|
|
|
|
| 493 |
)
|
| 494 |
|
| 495 |
# Parse markdown content
|
|
|
|
| 50 |
elif line.startswith('```'):
|
| 51 |
# Extract language if specified
|
| 52 |
language = line[3:].strip() if len(line) > 3 else 'text'
|
| 53 |
+
|
| 54 |
+
# Auto-detect language if not specified
|
| 55 |
+
if language == 'text':
|
| 56 |
+
language = _detect_language_from_content(lines, i)
|
| 57 |
+
|
| 58 |
code_lines = []
|
| 59 |
i += 1
|
| 60 |
while i < len(lines) and not lines[i].strip().startswith('```'):
|
|
|
|
| 138 |
return story
|
| 139 |
|
| 140 |
|
| 141 |
+
def _detect_language_from_content(lines: list, start_index: int) -> str:
|
| 142 |
+
"""
|
| 143 |
+
Auto-detect programming language from code content
|
| 144 |
+
"""
|
| 145 |
+
# Look at the next few lines to detect language
|
| 146 |
+
sample_lines = []
|
| 147 |
+
for i in range(start_index + 1, min(start_index + 10, len(lines))):
|
| 148 |
+
if lines[i].strip().startswith('```'):
|
| 149 |
+
break
|
| 150 |
+
sample_lines.append(lines[i])
|
| 151 |
+
|
| 152 |
+
sample_text = '\n'.join(sample_lines)
|
| 153 |
+
|
| 154 |
+
# Python detection
|
| 155 |
+
if (re.search(r'\bdef\s+\w+', sample_text) or
|
| 156 |
+
re.search(r'\bclass\s+\w+', sample_text) or
|
| 157 |
+
re.search(r'\bimport\s+\w+', sample_text) or
|
| 158 |
+
re.search(r'\bfrom\s+\w+', sample_text)):
|
| 159 |
+
return 'python'
|
| 160 |
+
|
| 161 |
+
# JavaScript detection
|
| 162 |
+
if (re.search(r'\bfunction\s+\w+', sample_text) or
|
| 163 |
+
re.search(r'\bvar\s+\w+', sample_text) or
|
| 164 |
+
re.search(r'\blet\s+\w+', sample_text) or
|
| 165 |
+
re.search(r'\bconst\s+\w+', sample_text) or
|
| 166 |
+
re.search(r'=>', sample_text)):
|
| 167 |
+
return 'javascript'
|
| 168 |
+
|
| 169 |
+
# Java detection
|
| 170 |
+
if (re.search(r'\bpublic\s+class', sample_text) or
|
| 171 |
+
re.search(r'\bprivate\s+\w+', sample_text) or
|
| 172 |
+
re.search(r'\bSystem\.out\.print', sample_text) or
|
| 173 |
+
re.search(r'\bimport\s+java\.', sample_text)):
|
| 174 |
+
return 'java'
|
| 175 |
+
|
| 176 |
+
# JSON detection
|
| 177 |
+
if (re.search(r'^\s*[{}]', sample_text) or
|
| 178 |
+
re.search(r'"[^"]*"\s*:', sample_text) or
|
| 179 |
+
re.search(r'\btrue\b|\bfalse\b|\bnull\b', sample_text)):
|
| 180 |
+
return 'json'
|
| 181 |
+
|
| 182 |
+
# XML/HTML detection
|
| 183 |
+
if (re.search(r'<[^>]+>', sample_text) or
|
| 184 |
+
re.search(r'<[^>]+>', sample_text)):
|
| 185 |
+
return 'xml'
|
| 186 |
+
|
| 187 |
+
# SQL detection
|
| 188 |
+
if (re.search(r'\bSELECT\b', sample_text, re.IGNORECASE) or
|
| 189 |
+
re.search(r'\bFROM\b', sample_text, re.IGNORECASE) or
|
| 190 |
+
re.search(r'\bWHERE\b', sample_text, re.IGNORECASE) or
|
| 191 |
+
re.search(r'\bINSERT\b', sample_text, re.IGNORECASE)):
|
| 192 |
+
return 'sql'
|
| 193 |
+
|
| 194 |
+
# YAML detection
|
| 195 |
+
if (re.search(r'^\s*\w+:', sample_text) or
|
| 196 |
+
re.search(r'^\s*-\s+', sample_text)):
|
| 197 |
+
return 'yaml'
|
| 198 |
+
|
| 199 |
+
# Bash detection
|
| 200 |
+
if (re.search(r'^\s*#!', sample_text) or
|
| 201 |
+
re.search(r'\$\w+', sample_text) or
|
| 202 |
+
re.search(r'^\s*\w+.*\|', sample_text)):
|
| 203 |
+
return 'bash'
|
| 204 |
+
|
| 205 |
+
return 'text'
|
| 206 |
+
|
| 207 |
+
|
| 208 |
def _format_code_block(code_text: str, language: str) -> str:
|
| 209 |
"""
|
| 210 |
Format code blocks with syntax highlighting for different languages
|
|
|
|
| 559 |
backColor=colors.HexColor('#f8f9fa'),
|
| 560 |
borderColor=colors.HexColor('#dee2e6'),
|
| 561 |
borderWidth=1,
|
| 562 |
+
borderPadding=8,
|
| 563 |
+
leftIndent=12,
|
| 564 |
+
rightIndent=12,
|
| 565 |
+
spaceBefore=6,
|
| 566 |
+
spaceAfter=6,
|
| 567 |
+
leading=11
|
| 568 |
)
|
| 569 |
|
| 570 |
# Parse markdown content
|