mirror of
https://github.com/nextlevelbuilder/ui-ux-pro-max-skill.git
synced 2026-11-28 22:30:32 +00:00
feat(i18n): add Arabic query synonym normalization and RTL-safe HTML preview rendering (#586)
* feat(i18n): add Arabic query synonym normalization and RTL-safe HTML preview rendering * fix(i18n): sync .claude assets, scope RTL letter-spacing, drop dead rtl_ready key, and add unit tests Signed-off-by: Dr. Mohammed Lubbad <engmlubbad@gmail.com> * fix(i18n): update runtimeFingerprint after rebasing onto main Signed-off-by: Dr. Mohammed Lubbad <engmlubbad@gmail.com> --------- Signed-off-by: Dr. Mohammed Lubbad <engmlubbad@gmail.com>
This commit is contained in:
parent
f76d1653d7
commit
6ec2bb5833
@ -271,8 +271,40 @@ _SYNONYMS = {
|
||||
"organisation": "organization",
|
||||
"behaviour": "behavior",
|
||||
"ux/ui": "ux ui",
|
||||
# Arabic & MENA bilingual domain/style/product synonyms
|
||||
"تداول": "trading crypto fintech",
|
||||
"مالية": "fintech finance banking",
|
||||
"مالي": "fintech finance banking",
|
||||
"بنك": "banking fintech finance",
|
||||
"مصرف": "banking fintech finance",
|
||||
"ذكاء اصطناعي": "ai artificial intelligence",
|
||||
"عيادة": "healthcare medical clinic",
|
||||
"طبي": "healthcare medical clinic",
|
||||
"طبية": "healthcare medical clinic",
|
||||
"مستشفى": "healthcare hospital medical",
|
||||
"متجر": "ecommerce store shopping",
|
||||
"تجارة إلكترونية": "ecommerce shopping retail",
|
||||
"لوحة تحكم": "dashboard analytics saas",
|
||||
"تحليلات": "analytics dashboard data",
|
||||
"عقارات": "real estate property",
|
||||
"تعليم": "education learning course",
|
||||
"أكاديمية": "education learning academy",
|
||||
"تدريب": "education learning training",
|
||||
"فندق": "hotel hospitality booking",
|
||||
"سياحة": "travel hospitality booking",
|
||||
"مطعم": "restaurant food delivery",
|
||||
"أمن سيبراني": "cybersecurity security",
|
||||
"ألعاب": "gaming game",
|
||||
"معرض أعمال": "portfolio agency creative",
|
||||
"داكن": "dark mode",
|
||||
"مظلم": "dark mode",
|
||||
"زجاجي": "glassmorphism glass",
|
||||
"بسيط": "minimalism clean",
|
||||
}
|
||||
|
||||
# Arabic-scoped diacritics (harakat/tashkeel) and tatweel; no-op on non-Arabic text.
|
||||
_ARABIC_DIACRITICS_RE = re.compile(r"[\u0617-\u061A\u064B-\u0652\u0640]")
|
||||
|
||||
_SYNONYM_PATTERNS = [
|
||||
(re.compile(r"(?<!\w)" + re.escape(variant) + r"(?!\w)", re.IGNORECASE), canonical)
|
||||
for variant, canonical in sorted(_SYNONYMS.items(), key=lambda item: len(item[0]), reverse=True)
|
||||
@ -280,8 +312,8 @@ _SYNONYM_PATTERNS = [
|
||||
|
||||
|
||||
def _normalize(text):
|
||||
"""Apply longest-first synonym substitution at token boundaries."""
|
||||
normalized = str(text)
|
||||
"""Apply Arabic diacritic stripping and longest-first synonym substitution at token boundaries."""
|
||||
normalized = _ARABIC_DIACRITICS_RE.sub("", str(text))
|
||||
for pattern, canonical in _SYNONYM_PATTERNS:
|
||||
normalized = pattern.sub(canonical, normalized)
|
||||
return normalized
|
||||
|
||||
@ -109,6 +109,17 @@ def _role_contrast(colors: dict, key: str):
|
||||
def format_preview_html(design_system: dict) -> str:
|
||||
"""Render a dependency-free HTML page for the generated design system."""
|
||||
project = design_system.get("project_name") or "Design system"
|
||||
# Detect RTL preview via explicit direction/lang metadata or Arabic script in project_name.
|
||||
is_arabic = bool(
|
||||
design_system.get("direction") == "rtl"
|
||||
or design_system.get("lang") == "ar"
|
||||
or re.search(r"[\u0600-\u06FF]", str(project))
|
||||
)
|
||||
html_lang = "ar" if is_arabic else "en"
|
||||
html_dir = ' dir="rtl"' if is_arabic else ""
|
||||
eyebrow_spacing = "0" if is_arabic else "0.08em"
|
||||
eyebrow_transform = "none" if is_arabic else "uppercase"
|
||||
badge_spacing = "0" if is_arabic else "0.04em"
|
||||
category = design_system.get("category") or ""
|
||||
style = design_system.get("style") or {}
|
||||
colors = design_system.get("colors") or {}
|
||||
@ -168,15 +179,17 @@ def format_preview_html(design_system: dict) -> str:
|
||||
)
|
||||
ratio_html = (
|
||||
f'<p class="swatch-contrast">vs {_text(pair_label)} '
|
||||
f'<code>{_text(pair_hex)}</code>: {_text(_format_ratio(ratio))} '
|
||||
f'<span class="badge {badge_class}">{_text(level)}</span></p>'
|
||||
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(pair_hex)}</code>: '
|
||||
f'<span dir="ltr" style="unicode-bidi:isolate">{_text(_format_ratio(ratio))}</span> '
|
||||
f'<span class="badge {badge_class}" dir="ltr" style="unicode-bidi:isolate">{_text(level)}</span></p>'
|
||||
)
|
||||
swatches.append(
|
||||
f'<li class="swatch">'
|
||||
f'<div class="swatch-chip" style="background:{hex_val}" aria-hidden="true"></div>'
|
||||
f'<div class="swatch-meta">'
|
||||
f'<p class="swatch-role">{_text(label)}</p>'
|
||||
f'<p><code>{_text(hex_val)}</code> <code>{_text(css_var)}</code></p>'
|
||||
f'<p><code dir="ltr" style="unicode-bidi:isolate">{_text(hex_val)}</code> '
|
||||
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(css_var)}</code></p>'
|
||||
f'{ratio_html}'
|
||||
f'</div></li>'
|
||||
)
|
||||
@ -207,7 +220,7 @@ def format_preview_html(design_system: dict) -> str:
|
||||
anti_html = "".join(anti_items)
|
||||
|
||||
return f"""<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="{html_lang}"{html_dir}>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
@ -245,8 +258,8 @@ def format_preview_html(design_system: dict) -> str:
|
||||
}}
|
||||
.eyebrow {{
|
||||
margin: 0 0 0.35rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: {eyebrow_transform};
|
||||
letter-spacing: {eyebrow_spacing};
|
||||
font-size: 0.75rem;
|
||||
color: {muted_fg};
|
||||
}}
|
||||
@ -255,6 +268,9 @@ def format_preview_html(design_system: dict) -> str:
|
||||
line-height: 1.2;
|
||||
margin: 0 0 0.5rem;
|
||||
}}
|
||||
html[dir="rtl"] h1, html[dir="rtl"] h2, html[dir="rtl"] h3, html[dir="rtl"] .specimen-heading {{
|
||||
letter-spacing: 0;
|
||||
}}
|
||||
h1 {{ font-size: 2.25rem; }}
|
||||
h2 {{ font-size: 1.5rem; }}
|
||||
h3 {{ font-size: 1.2rem; }}
|
||||
@ -292,12 +308,12 @@ def format_preview_html(design_system: dict) -> str:
|
||||
code {{ font-size: 0.85em; }}
|
||||
.badge {{
|
||||
display: inline-block;
|
||||
margin-left: 0.35rem;
|
||||
margin-inline-start: 0.35rem;
|
||||
padding: 0.05rem 0.4rem;
|
||||
border-radius: 999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.04em;
|
||||
letter-spacing: {badge_spacing};
|
||||
}}
|
||||
.badge-aaa {{ background: {primary}; color: {on_primary}; }}
|
||||
.badge-aa {{ background: {muted}; color: {muted_fg}; }}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"schemaVersion": 1,
|
||||
"status": "provisional-baseline-regression-gate",
|
||||
"baselineRevision": "97eb2a2",
|
||||
"runtimeFingerprint": "e074c9b35d8385e2fb8ce6ba42ac1f9d225c93a982c1dbfc55da9b18a3f1ac73",
|
||||
"runtimeFingerprint": "6f50331454236ff817a9617fdabbf8393309276e3b98e0005c0e007ccc933dbd",
|
||||
"oracleFingerprint": "02e7226428c64a995ec24ae104ce4d186f5f91bf0f062556e90ef9b0fcb6d67a",
|
||||
"approvingMaintainer": "repository maintainer approved Phase 1 testing checkpoint; second judgment review remains required before final-target promotion",
|
||||
"units": "All metrics are ratios in [0,1]. Precision treats missing ranks as non-relevant. Negative abstention is measured across the hard-negative by domain/stack cross-product.",
|
||||
|
||||
@ -162,5 +162,40 @@ class RtlCliTest(unittest.TestCase):
|
||||
self.assertIn("product", completed.stderr)
|
||||
|
||||
|
||||
class ArabicSynonymsAndRtlPreviewTest(unittest.TestCase):
|
||||
def test_arabic_diacritics_and_tatweel_stripped_in_normalize(self):
|
||||
from core import _normalize
|
||||
norm = _normalize("مَـتْـجَـر ذَكَاء اصْطِنَاعِي دَاكِن")
|
||||
self.assertIn("ecommerce", norm)
|
||||
self.assertIn("artificial intelligence", norm)
|
||||
self.assertIn("dark mode", norm)
|
||||
|
||||
def test_arabic_query_resolves_domain_and_style(self):
|
||||
res_product = search("عيادة طبية", "product", 3)
|
||||
self.assertGreater(res_product["count"], 0)
|
||||
res_style = search("زجاجي داكن", "style", 3)
|
||||
self.assertGreater(res_style["count"], 0)
|
||||
|
||||
def test_preview_html_arabic_rtl_and_ltr_scoping(self):
|
||||
from preview import format_preview_html
|
||||
ar_html = format_preview_html({
|
||||
"project_name": "منصة التداول الذكي",
|
||||
"category": "Fintech",
|
||||
"colors": {"primary": "#1E40AF", "on_primary": "#FFFFFF"},
|
||||
})
|
||||
self.assertIn('<html lang="ar" dir="rtl">', ar_html)
|
||||
self.assertIn('html[dir="rtl"] h1, html[dir="rtl"] h2', ar_html)
|
||||
self.assertIn('letter-spacing: 0;', ar_html)
|
||||
self.assertIn('<code dir="ltr" style="unicode-bidi:isolate">#1E40AF</code>', ar_html)
|
||||
|
||||
en_html = format_preview_html({
|
||||
"project_name": "Acme Dashboard",
|
||||
"category": "SaaS",
|
||||
"colors": {"primary": "#1E40AF", "on_primary": "#FFFFFF"},
|
||||
})
|
||||
self.assertIn('<html lang="en">', en_html)
|
||||
self.assertNotIn('dir="rtl">', en_html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -271,8 +271,40 @@ _SYNONYMS = {
|
||||
"organisation": "organization",
|
||||
"behaviour": "behavior",
|
||||
"ux/ui": "ux ui",
|
||||
# Arabic & MENA bilingual domain/style/product synonyms
|
||||
"تداول": "trading crypto fintech",
|
||||
"مالية": "fintech finance banking",
|
||||
"مالي": "fintech finance banking",
|
||||
"بنك": "banking fintech finance",
|
||||
"مصرف": "banking fintech finance",
|
||||
"ذكاء اصطناعي": "ai artificial intelligence",
|
||||
"عيادة": "healthcare medical clinic",
|
||||
"طبي": "healthcare medical clinic",
|
||||
"طبية": "healthcare medical clinic",
|
||||
"مستشفى": "healthcare hospital medical",
|
||||
"متجر": "ecommerce store shopping",
|
||||
"تجارة إلكترونية": "ecommerce shopping retail",
|
||||
"لوحة تحكم": "dashboard analytics saas",
|
||||
"تحليلات": "analytics dashboard data",
|
||||
"عقارات": "real estate property",
|
||||
"تعليم": "education learning course",
|
||||
"أكاديمية": "education learning academy",
|
||||
"تدريب": "education learning training",
|
||||
"فندق": "hotel hospitality booking",
|
||||
"سياحة": "travel hospitality booking",
|
||||
"مطعم": "restaurant food delivery",
|
||||
"أمن سيبراني": "cybersecurity security",
|
||||
"ألعاب": "gaming game",
|
||||
"معرض أعمال": "portfolio agency creative",
|
||||
"داكن": "dark mode",
|
||||
"مظلم": "dark mode",
|
||||
"زجاجي": "glassmorphism glass",
|
||||
"بسيط": "minimalism clean",
|
||||
}
|
||||
|
||||
# Arabic-scoped diacritics (harakat/tashkeel) and tatweel; no-op on non-Arabic text.
|
||||
_ARABIC_DIACRITICS_RE = re.compile(r"[\u0617-\u061A\u064B-\u0652\u0640]")
|
||||
|
||||
_SYNONYM_PATTERNS = [
|
||||
(re.compile(r"(?<!\w)" + re.escape(variant) + r"(?!\w)", re.IGNORECASE), canonical)
|
||||
for variant, canonical in sorted(_SYNONYMS.items(), key=lambda item: len(item[0]), reverse=True)
|
||||
@ -280,8 +312,8 @@ _SYNONYM_PATTERNS = [
|
||||
|
||||
|
||||
def _normalize(text):
|
||||
"""Apply longest-first synonym substitution at token boundaries."""
|
||||
normalized = str(text)
|
||||
"""Apply Arabic diacritic stripping and longest-first synonym substitution at token boundaries."""
|
||||
normalized = _ARABIC_DIACRITICS_RE.sub("", str(text))
|
||||
for pattern, canonical in _SYNONYM_PATTERNS:
|
||||
normalized = pattern.sub(canonical, normalized)
|
||||
return normalized
|
||||
|
||||
@ -109,6 +109,17 @@ def _role_contrast(colors: dict, key: str):
|
||||
def format_preview_html(design_system: dict) -> str:
|
||||
"""Render a dependency-free HTML page for the generated design system."""
|
||||
project = design_system.get("project_name") or "Design system"
|
||||
# Detect RTL preview via explicit direction/lang metadata or Arabic script in project_name.
|
||||
is_arabic = bool(
|
||||
design_system.get("direction") == "rtl"
|
||||
or design_system.get("lang") == "ar"
|
||||
or re.search(r"[\u0600-\u06FF]", str(project))
|
||||
)
|
||||
html_lang = "ar" if is_arabic else "en"
|
||||
html_dir = ' dir="rtl"' if is_arabic else ""
|
||||
eyebrow_spacing = "0" if is_arabic else "0.08em"
|
||||
eyebrow_transform = "none" if is_arabic else "uppercase"
|
||||
badge_spacing = "0" if is_arabic else "0.04em"
|
||||
category = design_system.get("category") or ""
|
||||
style = design_system.get("style") or {}
|
||||
colors = design_system.get("colors") or {}
|
||||
@ -168,15 +179,17 @@ def format_preview_html(design_system: dict) -> str:
|
||||
)
|
||||
ratio_html = (
|
||||
f'<p class="swatch-contrast">vs {_text(pair_label)} '
|
||||
f'<code>{_text(pair_hex)}</code>: {_text(_format_ratio(ratio))} '
|
||||
f'<span class="badge {badge_class}">{_text(level)}</span></p>'
|
||||
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(pair_hex)}</code>: '
|
||||
f'<span dir="ltr" style="unicode-bidi:isolate">{_text(_format_ratio(ratio))}</span> '
|
||||
f'<span class="badge {badge_class}" dir="ltr" style="unicode-bidi:isolate">{_text(level)}</span></p>'
|
||||
)
|
||||
swatches.append(
|
||||
f'<li class="swatch">'
|
||||
f'<div class="swatch-chip" style="background:{hex_val}" aria-hidden="true"></div>'
|
||||
f'<div class="swatch-meta">'
|
||||
f'<p class="swatch-role">{_text(label)}</p>'
|
||||
f'<p><code>{_text(hex_val)}</code> <code>{_text(css_var)}</code></p>'
|
||||
f'<p><code dir="ltr" style="unicode-bidi:isolate">{_text(hex_val)}</code> '
|
||||
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(css_var)}</code></p>'
|
||||
f'{ratio_html}'
|
||||
f'</div></li>'
|
||||
)
|
||||
@ -207,7 +220,7 @@ def format_preview_html(design_system: dict) -> str:
|
||||
anti_html = "".join(anti_items)
|
||||
|
||||
return f"""<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="{html_lang}"{html_dir}>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
@ -245,8 +258,8 @@ def format_preview_html(design_system: dict) -> str:
|
||||
}}
|
||||
.eyebrow {{
|
||||
margin: 0 0 0.35rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: {eyebrow_transform};
|
||||
letter-spacing: {eyebrow_spacing};
|
||||
font-size: 0.75rem;
|
||||
color: {muted_fg};
|
||||
}}
|
||||
@ -255,6 +268,9 @@ def format_preview_html(design_system: dict) -> str:
|
||||
line-height: 1.2;
|
||||
margin: 0 0 0.5rem;
|
||||
}}
|
||||
html[dir="rtl"] h1, html[dir="rtl"] h2, html[dir="rtl"] h3, html[dir="rtl"] .specimen-heading {{
|
||||
letter-spacing: 0;
|
||||
}}
|
||||
h1 {{ font-size: 2.25rem; }}
|
||||
h2 {{ font-size: 1.5rem; }}
|
||||
h3 {{ font-size: 1.2rem; }}
|
||||
@ -292,12 +308,12 @@ def format_preview_html(design_system: dict) -> str:
|
||||
code {{ font-size: 0.85em; }}
|
||||
.badge {{
|
||||
display: inline-block;
|
||||
margin-left: 0.35rem;
|
||||
margin-inline-start: 0.35rem;
|
||||
padding: 0.05rem 0.4rem;
|
||||
border-radius: 999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.04em;
|
||||
letter-spacing: {badge_spacing};
|
||||
}}
|
||||
.badge-aaa {{ background: {primary}; color: {on_primary}; }}
|
||||
.badge-aa {{ background: {muted}; color: {muted_fg}; }}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"schemaVersion": 1,
|
||||
"status": "provisional-baseline-regression-gate",
|
||||
"baselineRevision": "97eb2a2",
|
||||
"runtimeFingerprint": "e074c9b35d8385e2fb8ce6ba42ac1f9d225c93a982c1dbfc55da9b18a3f1ac73",
|
||||
"runtimeFingerprint": "6f50331454236ff817a9617fdabbf8393309276e3b98e0005c0e007ccc933dbd",
|
||||
"oracleFingerprint": "02e7226428c64a995ec24ae104ce4d186f5f91bf0f062556e90ef9b0fcb6d67a",
|
||||
"approvingMaintainer": "repository maintainer approved Phase 1 testing checkpoint; second judgment review remains required before final-target promotion",
|
||||
"units": "All metrics are ratios in [0,1]. Precision treats missing ranks as non-relevant. Negative abstention is measured across the hard-negative by domain/stack cross-product.",
|
||||
|
||||
@ -162,5 +162,40 @@ class RtlCliTest(unittest.TestCase):
|
||||
self.assertIn("product", completed.stderr)
|
||||
|
||||
|
||||
class ArabicSynonymsAndRtlPreviewTest(unittest.TestCase):
|
||||
def test_arabic_diacritics_and_tatweel_stripped_in_normalize(self):
|
||||
from core import _normalize
|
||||
norm = _normalize("مَـتْـجَـر ذَكَاء اصْطِنَاعِي دَاكِن")
|
||||
self.assertIn("ecommerce", norm)
|
||||
self.assertIn("artificial intelligence", norm)
|
||||
self.assertIn("dark mode", norm)
|
||||
|
||||
def test_arabic_query_resolves_domain_and_style(self):
|
||||
res_product = search("عيادة طبية", "product", 3)
|
||||
self.assertGreater(res_product["count"], 0)
|
||||
res_style = search("زجاجي داكن", "style", 3)
|
||||
self.assertGreater(res_style["count"], 0)
|
||||
|
||||
def test_preview_html_arabic_rtl_and_ltr_scoping(self):
|
||||
from preview import format_preview_html
|
||||
ar_html = format_preview_html({
|
||||
"project_name": "منصة التداول الذكي",
|
||||
"category": "Fintech",
|
||||
"colors": {"primary": "#1E40AF", "on_primary": "#FFFFFF"},
|
||||
})
|
||||
self.assertIn('<html lang="ar" dir="rtl">', ar_html)
|
||||
self.assertIn('html[dir="rtl"] h1, html[dir="rtl"] h2', ar_html)
|
||||
self.assertIn('letter-spacing: 0;', ar_html)
|
||||
self.assertIn('<code dir="ltr" style="unicode-bidi:isolate">#1E40AF</code>', ar_html)
|
||||
|
||||
en_html = format_preview_html({
|
||||
"project_name": "Acme Dashboard",
|
||||
"category": "SaaS",
|
||||
"colors": {"primary": "#1E40AF", "on_primary": "#FFFFFF"},
|
||||
})
|
||||
self.assertIn('<html lang="en">', en_html)
|
||||
self.assertNotIn('dir="rtl">', en_html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
@ -271,8 +271,40 @@ _SYNONYMS = {
|
||||
"organisation": "organization",
|
||||
"behaviour": "behavior",
|
||||
"ux/ui": "ux ui",
|
||||
# Arabic & MENA bilingual domain/style/product synonyms
|
||||
"تداول": "trading crypto fintech",
|
||||
"مالية": "fintech finance banking",
|
||||
"مالي": "fintech finance banking",
|
||||
"بنك": "banking fintech finance",
|
||||
"مصرف": "banking fintech finance",
|
||||
"ذكاء اصطناعي": "ai artificial intelligence",
|
||||
"عيادة": "healthcare medical clinic",
|
||||
"طبي": "healthcare medical clinic",
|
||||
"طبية": "healthcare medical clinic",
|
||||
"مستشفى": "healthcare hospital medical",
|
||||
"متجر": "ecommerce store shopping",
|
||||
"تجارة إلكترونية": "ecommerce shopping retail",
|
||||
"لوحة تحكم": "dashboard analytics saas",
|
||||
"تحليلات": "analytics dashboard data",
|
||||
"عقارات": "real estate property",
|
||||
"تعليم": "education learning course",
|
||||
"أكاديمية": "education learning academy",
|
||||
"تدريب": "education learning training",
|
||||
"فندق": "hotel hospitality booking",
|
||||
"سياحة": "travel hospitality booking",
|
||||
"مطعم": "restaurant food delivery",
|
||||
"أمن سيبراني": "cybersecurity security",
|
||||
"ألعاب": "gaming game",
|
||||
"معرض أعمال": "portfolio agency creative",
|
||||
"داكن": "dark mode",
|
||||
"مظلم": "dark mode",
|
||||
"زجاجي": "glassmorphism glass",
|
||||
"بسيط": "minimalism clean",
|
||||
}
|
||||
|
||||
# Arabic-scoped diacritics (harakat/tashkeel) and tatweel; no-op on non-Arabic text.
|
||||
_ARABIC_DIACRITICS_RE = re.compile(r"[\u0617-\u061A\u064B-\u0652\u0640]")
|
||||
|
||||
_SYNONYM_PATTERNS = [
|
||||
(re.compile(r"(?<!\w)" + re.escape(variant) + r"(?!\w)", re.IGNORECASE), canonical)
|
||||
for variant, canonical in sorted(_SYNONYMS.items(), key=lambda item: len(item[0]), reverse=True)
|
||||
@ -280,8 +312,8 @@ _SYNONYM_PATTERNS = [
|
||||
|
||||
|
||||
def _normalize(text):
|
||||
"""Apply longest-first synonym substitution at token boundaries."""
|
||||
normalized = str(text)
|
||||
"""Apply Arabic diacritic stripping and longest-first synonym substitution at token boundaries."""
|
||||
normalized = _ARABIC_DIACRITICS_RE.sub("", str(text))
|
||||
for pattern, canonical in _SYNONYM_PATTERNS:
|
||||
normalized = pattern.sub(canonical, normalized)
|
||||
return normalized
|
||||
|
||||
@ -109,6 +109,17 @@ def _role_contrast(colors: dict, key: str):
|
||||
def format_preview_html(design_system: dict) -> str:
|
||||
"""Render a dependency-free HTML page for the generated design system."""
|
||||
project = design_system.get("project_name") or "Design system"
|
||||
# Detect RTL preview via explicit direction/lang metadata or Arabic script in project_name.
|
||||
is_arabic = bool(
|
||||
design_system.get("direction") == "rtl"
|
||||
or design_system.get("lang") == "ar"
|
||||
or re.search(r"[\u0600-\u06FF]", str(project))
|
||||
)
|
||||
html_lang = "ar" if is_arabic else "en"
|
||||
html_dir = ' dir="rtl"' if is_arabic else ""
|
||||
eyebrow_spacing = "0" if is_arabic else "0.08em"
|
||||
eyebrow_transform = "none" if is_arabic else "uppercase"
|
||||
badge_spacing = "0" if is_arabic else "0.04em"
|
||||
category = design_system.get("category") or ""
|
||||
style = design_system.get("style") or {}
|
||||
colors = design_system.get("colors") or {}
|
||||
@ -168,15 +179,17 @@ def format_preview_html(design_system: dict) -> str:
|
||||
)
|
||||
ratio_html = (
|
||||
f'<p class="swatch-contrast">vs {_text(pair_label)} '
|
||||
f'<code>{_text(pair_hex)}</code>: {_text(_format_ratio(ratio))} '
|
||||
f'<span class="badge {badge_class}">{_text(level)}</span></p>'
|
||||
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(pair_hex)}</code>: '
|
||||
f'<span dir="ltr" style="unicode-bidi:isolate">{_text(_format_ratio(ratio))}</span> '
|
||||
f'<span class="badge {badge_class}" dir="ltr" style="unicode-bidi:isolate">{_text(level)}</span></p>'
|
||||
)
|
||||
swatches.append(
|
||||
f'<li class="swatch">'
|
||||
f'<div class="swatch-chip" style="background:{hex_val}" aria-hidden="true"></div>'
|
||||
f'<div class="swatch-meta">'
|
||||
f'<p class="swatch-role">{_text(label)}</p>'
|
||||
f'<p><code>{_text(hex_val)}</code> <code>{_text(css_var)}</code></p>'
|
||||
f'<p><code dir="ltr" style="unicode-bidi:isolate">{_text(hex_val)}</code> '
|
||||
f'<code dir="ltr" style="unicode-bidi:isolate">{_text(css_var)}</code></p>'
|
||||
f'{ratio_html}'
|
||||
f'</div></li>'
|
||||
)
|
||||
@ -207,7 +220,7 @@ def format_preview_html(design_system: dict) -> str:
|
||||
anti_html = "".join(anti_items)
|
||||
|
||||
return f"""<!DOCTYPE html>
|
||||
<html lang="en">
|
||||
<html lang="{html_lang}"{html_dir}>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
@ -245,8 +258,8 @@ def format_preview_html(design_system: dict) -> str:
|
||||
}}
|
||||
.eyebrow {{
|
||||
margin: 0 0 0.35rem;
|
||||
text-transform: uppercase;
|
||||
letter-spacing: 0.08em;
|
||||
text-transform: {eyebrow_transform};
|
||||
letter-spacing: {eyebrow_spacing};
|
||||
font-size: 0.75rem;
|
||||
color: {muted_fg};
|
||||
}}
|
||||
@ -255,6 +268,9 @@ def format_preview_html(design_system: dict) -> str:
|
||||
line-height: 1.2;
|
||||
margin: 0 0 0.5rem;
|
||||
}}
|
||||
html[dir="rtl"] h1, html[dir="rtl"] h2, html[dir="rtl"] h3, html[dir="rtl"] .specimen-heading {{
|
||||
letter-spacing: 0;
|
||||
}}
|
||||
h1 {{ font-size: 2.25rem; }}
|
||||
h2 {{ font-size: 1.5rem; }}
|
||||
h3 {{ font-size: 1.2rem; }}
|
||||
@ -292,12 +308,12 @@ def format_preview_html(design_system: dict) -> str:
|
||||
code {{ font-size: 0.85em; }}
|
||||
.badge {{
|
||||
display: inline-block;
|
||||
margin-left: 0.35rem;
|
||||
margin-inline-start: 0.35rem;
|
||||
padding: 0.05rem 0.4rem;
|
||||
border-radius: 999px;
|
||||
font-size: 0.75rem;
|
||||
font-weight: 700;
|
||||
letter-spacing: 0.04em;
|
||||
letter-spacing: {badge_spacing};
|
||||
}}
|
||||
.badge-aaa {{ background: {primary}; color: {on_primary}; }}
|
||||
.badge-aa {{ background: {muted}; color: {muted_fg}; }}
|
||||
|
||||
@ -2,7 +2,7 @@
|
||||
"schemaVersion": 1,
|
||||
"status": "provisional-baseline-regression-gate",
|
||||
"baselineRevision": "97eb2a2",
|
||||
"runtimeFingerprint": "e074c9b35d8385e2fb8ce6ba42ac1f9d225c93a982c1dbfc55da9b18a3f1ac73",
|
||||
"runtimeFingerprint": "6f50331454236ff817a9617fdabbf8393309276e3b98e0005c0e007ccc933dbd",
|
||||
"oracleFingerprint": "02e7226428c64a995ec24ae104ce4d186f5f91bf0f062556e90ef9b0fcb6d67a",
|
||||
"approvingMaintainer": "repository maintainer approved Phase 1 testing checkpoint; second judgment review remains required before final-target promotion",
|
||||
"units": "All metrics are ratios in [0,1]. Precision treats missing ranks as non-relevant. Negative abstention is measured across the hard-negative by domain/stack cross-product.",
|
||||
|
||||
@ -162,5 +162,40 @@ class RtlCliTest(unittest.TestCase):
|
||||
self.assertIn("product", completed.stderr)
|
||||
|
||||
|
||||
class ArabicSynonymsAndRtlPreviewTest(unittest.TestCase):
|
||||
def test_arabic_diacritics_and_tatweel_stripped_in_normalize(self):
|
||||
from core import _normalize
|
||||
norm = _normalize("مَـتْـجَـر ذَكَاء اصْطِنَاعِي دَاكِن")
|
||||
self.assertIn("ecommerce", norm)
|
||||
self.assertIn("artificial intelligence", norm)
|
||||
self.assertIn("dark mode", norm)
|
||||
|
||||
def test_arabic_query_resolves_domain_and_style(self):
|
||||
res_product = search("عيادة طبية", "product", 3)
|
||||
self.assertGreater(res_product["count"], 0)
|
||||
res_style = search("زجاجي داكن", "style", 3)
|
||||
self.assertGreater(res_style["count"], 0)
|
||||
|
||||
def test_preview_html_arabic_rtl_and_ltr_scoping(self):
|
||||
from preview import format_preview_html
|
||||
ar_html = format_preview_html({
|
||||
"project_name": "منصة التداول الذكي",
|
||||
"category": "Fintech",
|
||||
"colors": {"primary": "#1E40AF", "on_primary": "#FFFFFF"},
|
||||
})
|
||||
self.assertIn('<html lang="ar" dir="rtl">', ar_html)
|
||||
self.assertIn('html[dir="rtl"] h1, html[dir="rtl"] h2', ar_html)
|
||||
self.assertIn('letter-spacing: 0;', ar_html)
|
||||
self.assertIn('<code dir="ltr" style="unicode-bidi:isolate">#1E40AF</code>', ar_html)
|
||||
|
||||
en_html = format_preview_html({
|
||||
"project_name": "Acme Dashboard",
|
||||
"category": "SaaS",
|
||||
"colors": {"primary": "#1E40AF", "on_primary": "#FFFFFF"},
|
||||
})
|
||||
self.assertIn('<html lang="en">', en_html)
|
||||
self.assertNotIn('dir="rtl">', en_html)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user