From b4087b4f8d6a903f65e3062dd493310302e96a49 Mon Sep 17 00:00:00 2001
From: Bob Mottram
Date: Wed, 20 Sep 2023 13:50:29 +0100
Subject: [PATCH] Handle mixed RTL with line breaks
---
content.py | 19 ++++++++++++++++++-
tests.py | 15 +++++++++++++++
2 files changed, 33 insertions(+), 1 deletion(-)
diff --git a/content.py b/content.py
index 52eb33391..36cc39cff 100644
--- a/content.py
+++ b/content.py
@@ -2207,9 +2207,9 @@ def format_mixed_right_to_left(content: str,
# not a RTL language
if language_right_to_left(language):
return content
- paragraphs = content.split('')
result = ''
changed = False
+ paragraphs = content.split('
')
for text_html in paragraphs:
if '
' not in text_html:
continue
@@ -2220,6 +2220,23 @@ def format_mixed_right_to_left(content: str,
text_html = text_html.replace('
', '', 1)
changed = True
result += text_html
+ if not changed:
+ paragraphs = content.split('
')
+ ctr = 0
+ for text_html in paragraphs:
+ ctr += 1
+ if ctr < len(paragraphs):
+ text_html += '
'
+ text_plain = remove_html(text_html)
+ if is_right_to_left_text(text_plain):
+ text_html = '' + text_html
+ if ctr < len(paragraphs):
+ text_html = \
+ text_html.replace('
', '
', 1)
+ else:
+ text_html += ''
+ changed = True
+ result += text_html
if not changed:
return content
return result
diff --git a/tests.py b/tests.py
index df9fca23a..1a7851c11 100644
--- a/tests.py
+++ b/tests.py
@@ -8130,6 +8130,9 @@ def _test_format_mixed_rtl() -> None:
expected = 'This is some English
' + \
'هذه عربية
' + \
'And more English
'
+ if result != expected:
+ print('Expected: ' + expected)
+ print('Result: ' + result)
assert result == expected
content = 'This is some only English
'
@@ -8148,6 +8151,18 @@ def _test_format_mixed_rtl() -> None:
result = format_mixed_right_to_left(content, 'ar')
assert result == content
+ content = 'This is some English
' + \
+ 'هذه عربية
' + \
+ 'And more English'
+ result = format_mixed_right_to_left(content, 'en')
+ expected = 'This is some English
' + \
+ 'هذه عربية
' + \
+ 'And more English'
+ if result != expected:
+ print('Expected: ' + expected)
+ print('Result: ' + result)
+ assert result == expected
+
def run_all_tests():
base_dir = os.getcwd()