Update report.py
Browse files
report.py
CHANGED
|
@@ -63,6 +63,8 @@ import matplotlib.pyplot as plt
|
|
| 63 |
import markdown
|
| 64 |
from xml.etree import ElementTree as ET
|
| 65 |
from PIL import Image as PILImage
|
|
|
|
|
|
|
| 66 |
|
| 67 |
class ReportGenerator:
|
| 68 |
def __init__(self):
|
|
@@ -134,18 +136,32 @@ class ReportGenerator:
|
|
| 134 |
# Convert markdown to HTML
|
| 135 |
html = markdown.markdown(recommendations)
|
| 136 |
|
| 137 |
-
#
|
| 138 |
-
|
| 139 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 140 |
if elem.tag == 'h3':
|
| 141 |
-
elements.append(Paragraph(elem.text, self.styles['Heading3']))
|
| 142 |
elif elem.tag == 'h4':
|
| 143 |
-
elements.append(Paragraph(elem.text, self.styles['Heading4']))
|
| 144 |
elif elem.tag == 'p':
|
| 145 |
-
|
|
|
|
| 146 |
elif elem.tag == 'ul':
|
| 147 |
for li in elem.findall('li'):
|
| 148 |
-
bullet_text = '• ' +
|
| 149 |
elements.append(Paragraph(bullet_text, self.styles['BodyText']))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 150 |
|
| 151 |
return elements
|
|
|
|
| 63 |
import markdown
|
| 64 |
from xml.etree import ElementTree as ET
|
| 65 |
from PIL import Image as PILImage
|
| 66 |
+
from xml.parsers.expat import ExpatError
|
| 67 |
+
from html import escape
|
| 68 |
|
| 69 |
class ReportGenerator:
|
| 70 |
def __init__(self):
|
|
|
|
| 136 |
# Convert markdown to HTML
|
| 137 |
html = markdown.markdown(recommendations)
|
| 138 |
|
| 139 |
+
# Wrap the HTML in a root element to ensure valid XML
|
| 140 |
+
wrapped_html = f"<root>{html}</root>"
|
| 141 |
+
|
| 142 |
+
try:
|
| 143 |
+
root = ET.fromstring(wrapped_html)
|
| 144 |
+
except ExpatError:
|
| 145 |
+
# If parsing fails, fallback to treating the entire content as plain text
|
| 146 |
+
elements.append(Paragraph(escape(recommendations), self.styles['BodyText']))
|
| 147 |
+
return elements
|
| 148 |
+
|
| 149 |
+
for elem in root:
|
| 150 |
if elem.tag == 'h3':
|
| 151 |
+
elements.append(Paragraph(elem.text or "", self.styles['Heading3']))
|
| 152 |
elif elem.tag == 'h4':
|
| 153 |
+
elements.append(Paragraph(elem.text or "", self.styles['Heading4']))
|
| 154 |
elif elem.tag == 'p':
|
| 155 |
+
text = ''.join(elem.itertext())
|
| 156 |
+
elements.append(Paragraph(text, self.styles['Justify']))
|
| 157 |
elif elem.tag == 'ul':
|
| 158 |
for li in elem.findall('li'):
|
| 159 |
+
bullet_text = '• ' + ''.join(li.itertext()).strip()
|
| 160 |
elements.append(Paragraph(bullet_text, self.styles['BodyText']))
|
| 161 |
+
else:
|
| 162 |
+
# For any other tags, just extract the text
|
| 163 |
+
text = ''.join(elem.itertext())
|
| 164 |
+
if text.strip():
|
| 165 |
+
elements.append(Paragraph(text, self.styles['BodyText']))
|
| 166 |
|
| 167 |
return elements
|