mariusjabami commited on
Commit
4d59daf
·
verified ·
1 Parent(s): ce28d48

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +23 -6
app.py CHANGED
@@ -7,6 +7,9 @@ from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
7
  from reportlab.lib.pagesizes import A4
8
  from reportlab.lib.utils import ImageReader
9
  from reportlab.pdfgen import canvas
 
 
 
10
 
11
  load_dotenv()
12
  api_token = os.environ.get("SYNAP")
@@ -448,13 +451,27 @@ Agora, seguindo rigorosamente TODAS as instruções acima, gere o desenvolviment
448
  print("erro:", e)
449
  print("Tentando novamente")
450
 
451
- def limpar(self, texto):
452
- texto = texto.replace("<img>", "&lt;img&gt;")
453
- texto = texto.replace("<p>", "&lt;p&gt;").replace("</p>", "&lt;/p&gt;")
454
- texto = texto.replace("<div>", "&lt;div&gt;").replace("</div>", "&lt;/div&gt;")
455
- texto = texto.replace("<h1>", "&lt;h1&gt;").replace("</h1>", "&lt;/h1&gt;")
456
- return texto
 
 
 
 
 
 
 
 
 
 
 
 
 
457
 
 
458
 
459
  def save(self):
460
  self.c.save()
 
7
  from reportlab.lib.pagesizes import A4
8
  from reportlab.lib.utils import ImageReader
9
  from reportlab.pdfgen import canvas
10
+ import re
11
+ from html import escape
12
+
13
 
14
  load_dotenv()
15
  api_token = os.environ.get("SYNAP")
 
451
  print("erro:", e)
452
  print("Tentando novamente")
453
 
454
+
455
+ def limpar(self, texto: str) -> str:
456
+ if not texto:
457
+ return ""
458
+
459
+ # Escapa qualquer coisa perigosa primeiro
460
+ texto = escape(texto)
461
+
462
+ # Reverte apenas as tags PERMITIDAS pelo ReportLab
463
+ texto = texto.replace("&lt;br/&gt;", "<br/>")
464
+ texto = texto.replace("&lt;br&gt;", "<br/>")
465
+ texto = texto.replace("&lt;strong&gt;", "<strong>")
466
+ texto = texto.replace("&lt;/strong&gt;", "</strong>")
467
+
468
+ # Normaliza <br>
469
+ texto = re.sub(r"<br\s*/?>", "<br/>", texto, flags=re.IGNORECASE)
470
+
471
+ # Remove excesso de quebras
472
+ texto = re.sub(r"(?:<br/>){3,}", "<br/><br/>", texto)
473
 
474
+ return texto.strip()
475
 
476
  def save(self):
477
  self.c.save()