Handle mixed RTL with line breaks

merge-requests/30/head
Bob Mottram 2023-09-20 13:50:29 +01:00
parent b77389400d
commit b4087b4f8d
2 changed files with 33 additions and 1 deletions

View File

@ -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('<p>')
result = ''
changed = False
paragraphs = content.split('<p>')
for text_html in paragraphs:
if '</p>' not in text_html:
continue
@ -2220,6 +2220,23 @@ def format_mixed_right_to_left(content: str,
text_html = text_html.replace('</p>', '</div></p>', 1)
changed = True
result += text_html
if not changed:
paragraphs = content.split('<br><br>')
ctr = 0
for text_html in paragraphs:
ctr += 1
if ctr < len(paragraphs):
text_html += '<br><br>'
text_plain = remove_html(text_html)
if is_right_to_left_text(text_plain):
text_html = '<div dir="rtl">' + text_html
if ctr < len(paragraphs):
text_html = \
text_html.replace('<br><br>', '</div><br><br>', 1)
else:
text_html += '</div>'
changed = True
result += text_html
if not changed:
return content
return result

View File

@ -8130,6 +8130,9 @@ def _test_format_mixed_rtl() -> None:
expected = '<p>This is some English</p>' + \
'<p><div dir="rtl">هذه عربية</div></p>' + \
'<p>And more English</p>'
if result != expected:
print('Expected: ' + expected)
print('Result: ' + result)
assert result == expected
content = '<p>This is some only English</p>'
@ -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<br><br>' + \
'هذه عربية<br><br>' + \
'And more English'
result = format_mixed_right_to_left(content, 'en')
expected = 'This is some English<br><br>' + \
'<div dir="rtl">هذه عربية</div><br><br>' + \
'And more English'
if result != expected:
print('Expected: ' + expected)
print('Result: ' + result)
assert result == expected
def run_all_tests():
base_dir = os.getcwd()