Spaces:
Runtime error
Runtime error
| # utils/html.py | |
| import re | |
| def esc(text: str) -> str: | |
| if not text: | |
| return "" | |
| return str(text).replace("&", "&").replace("<", "<").replace(">", ">") | |
| def b(text: str) -> str: | |
| return f"<b>{esc(text)}</b>" | |
| def i(text: str) -> str: | |
| return f"<i>{esc(text)}</i>" | |
| def bq(text: str, expandable: bool = False) -> str: | |
| if expandable: | |
| return f"<blockquote expandable>{text}</blockquote>" | |
| return f"<blockquote>{text}</blockquote>" | |
| def code(text: str) -> str: | |
| return f"<code>{esc(text)}</code>" | |
| def link(label: str, url: str) -> str: | |
| safe_label = esc(label) | |
| return f'<a href="{url}">{safe_label}</a>' | |
| def clean_steam_html(html_str: str) -> str: | |
| """تحويل HTML متطلبات Steam إلى نص عربي نظيف.""" | |
| if not html_str: | |
| return "" | |
| text = re.sub(r'<br\s*/?>', '\n', html_str) | |
| text = re.sub(r'<li>', '• ', text) | |
| text = re.sub(r'</li>', '\n', text) | |
| text = re.sub(r'<strong>(.*?)</strong>', r'*\1*', text) | |
| text = re.sub(r'<ul[^>]*>', '', text) | |
| text = re.sub(r'</ul>', '', text) | |
| text = re.sub(r'<[^>]+>', '', text) | |
| text = re.sub(r'&', '&', text) | |
| text = re.sub(r'<', '<', text) | |
| text = re.sub(r'>', '>', text) | |
| text = re.sub(r' ', ' ', text) | |
| text = re.sub(r'\n{3,}', '\n\n', text) | |
| return text.strip() | |