markdown formatting improvement

This commit is contained in:
Gabriel Kheisa 2025-02-12 18:49:02 +07:00
parent c867c9e730
commit fe3f7e33ea
2 changed files with 3 additions and 48 deletions

50
app.py
View File

@ -15,6 +15,7 @@ from flask_limiter.util import get_remote_address
import google.generativeai as genai
import requests
from dotenv import load_dotenv
import markdown
# Load environment variables
load_dotenv()
@ -111,54 +112,7 @@ def is_valid_pdf(pdf_path):
return False
def format_summary(summary):
"""Converts markdown-like text to HTML formatting, including nested lists (both ordered and unordered)."""
summary = re.sub(r'\*\*(.*?)\*\*', r'<strong>\1</strong>', summary) # Bold text
summary = re.sub(r'\*(?!\s)(.*?)\*', r'<em>\1</em>', summary) # Italic text (ignoring lists)
lines = summary.split('\n')
formatted_lines = []
list_stack = [] # Track nesting levels and types
for line in lines:
unordered_match = re.match(r'^(\s*)\*\s(.+)', line) # Matches "* item"
ordered_match = re.match(r'^(\s*)(\d+)\.\s(.+)', line) # Matches "1. item"
if unordered_match or ordered_match:
indent = unordered_match.group(1) if unordered_match else ordered_match.group(1)
level = len(indent) // 4 # Assume 4 spaces per indent level
list_type = '<ul>' if unordered_match else '<ol>'
list_tag = '<li>{}</li>'.format(unordered_match.group(2) if unordered_match else ordered_match.group(3))
# Close lists if necessary
while list_stack and len(list_stack) > level:
formatted_lines.append('</{}>'.format(list_stack.pop()))
# Open new lists if necessary
while len(list_stack) < level:
formatted_lines.append(list_type)
list_stack.append(list_type[1:3]) # Store 'ul' or 'ol'
# Handle list type switching (unordered ↔ ordered)
if list_stack and list_stack[-1] != list_type[1:3]:
formatted_lines.append('</{}>'.format(list_stack.pop()))
formatted_lines.append(list_type)
list_stack.append(list_type[1:3])
formatted_lines.append(list_tag)
else:
# Close any open lists before adding non-list content
while list_stack:
formatted_lines.append('</{}>'.format(list_stack.pop()))
formatted_lines.append(line.replace("\n", "<br>"))
# Close any remaining lists
while list_stack:
formatted_lines.append('</{}>'.format(list_stack.pop()))
return '\n'.join(formatted_lines)
return markdown.markdown(summary)
def resize_image(image, max_size=1080):
width, height = image.size

View File

@ -9,3 +9,4 @@ flask-limiter==3.10.1
google-generativeai==0.8.3
requests==2.31.0
python-dotenv==1.0.1
markdown==3.7