File size: 1,354 Bytes
8d21059
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# utils/html.py
import re


def esc(text: str) -> str:
    if not text:
        return ""
    return str(text).replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")


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'&amp;', '&', text)
    text = re.sub(r'&lt;', '<', text)
    text = re.sub(r'&gt;', '>', text)
    text = re.sub(r'&nbsp;', ' ', text)
    text = re.sub(r'\n{3,}', '\n\n', text)
    return text.strip()