diff --git a/notifications_utils/formatters.py b/notifications_utils/formatters.py
index 0d5b70d3..ee5019e3 100644
--- a/notifications_utils/formatters.py
+++ b/notifications_utils/formatters.py
@@ -610,16 +610,16 @@ def add_language_divs(_content: str) -> str:
because the mistune parser has already run and put our [[lang]] tags inside
paragraphs.
"""
- select_anything = r"([\s\S]*)"
+ select_anything = r"([\s\S]*?)"
fr_regex = re.compile(
- f"{EMAIL_P_OPEN_TAG}{FR_OPEN}{EMAIL_P_CLOSE_TAG}{select_anything}{EMAIL_P_OPEN_TAG}{FR_CLOSE}{EMAIL_P_CLOSE_TAG}"
+ f"({EMAIL_P_OPEN_TAG})?{FR_OPEN}({EMAIL_P_CLOSE_TAG})?{select_anything}({EMAIL_P_OPEN_TAG})?{FR_CLOSE}({EMAIL_P_CLOSE_TAG})?" # noqa
) # matches
[[fr]]
anything[[/fr]]
- content = fr_regex.sub(r'\1
', _content) # \1 returns the "anything" content above
+ content = fr_regex.sub(r'\3
', _content) # \3 returns the "anything" content above (3rd group)
en_regex = re.compile(
- f"{EMAIL_P_OPEN_TAG}{EN_OPEN}{EMAIL_P_CLOSE_TAG}{select_anything}{EMAIL_P_OPEN_TAG}{EN_CLOSE}{EMAIL_P_CLOSE_TAG}"
+ f"({EMAIL_P_OPEN_TAG})?{EN_OPEN}({EMAIL_P_CLOSE_TAG})?{select_anything}({EMAIL_P_OPEN_TAG})?{EN_CLOSE}({EMAIL_P_CLOSE_TAG})?" # noqa
) # matches [[en]]
anything[[/en]]
- content = en_regex.sub(r'\1
', content) # \1 returns the "anything" content above
+ content = en_regex.sub(r'\3
', content) # \3 returns the "anything" content above (3rd group)
return content
diff --git a/notifications_utils/template.py b/notifications_utils/template.py
index 5bdb0236..cb8e3899 100644
--- a/notifications_utils/template.py
+++ b/notifications_utils/template.py
@@ -20,7 +20,6 @@
add_ircc_gc_seal,
add_language_divs,
add_prefix,
- add_newlines_around_lang_tags,
autolink_sms,
notify_email_markdown,
notify_email_preheader_markdown,
@@ -753,7 +752,6 @@ def get_html_email_body(template_content, template_values, redact_missing_person
)
.then(unlink_govuk_escaped)
.then(strip_unsupported_characters)
- .then(add_newlines_around_lang_tags)
.then(add_trailing_newline)
.then(notify_email_markdown)
.then(add_language_divs)
diff --git a/notifications_utils/version.py b/notifications_utils/version.py
index b462b3e0..427bffcb 100644
--- a/notifications_utils/version.py
+++ b/notifications_utils/version.py
@@ -1,2 +1,2 @@
-__version__ = "48.1.1"
+__version__ = "48.2.0"
# GDS version '34.0.1'
diff --git a/tests/test_formatters.py b/tests/test_formatters.py
index 1e83b22b..2efd665a 100644
--- a/tests/test_formatters.py
+++ b/tests/test_formatters.py
@@ -956,43 +956,88 @@ def test_normalise_whitespace():
assert normalise_whitespace("\u200C Your tax is\ndue\n\n") == "Your tax is due"
-@pytest.mark.parametrize("lang", ("en", "fr"))
-def test_add_language_divs_fr_replaces(lang: str):
- _content = (
- f'[[{lang}]]
'
- ''
- "title
"
- ''
- "Comment vas-tu aujourd'hui?
"
- f'[[/{lang}]]
'
- )
- content = (
- f''
- '
'
- "title
"
- '
'
- "Comment vas-tu aujourd'hui?
"
- )
- assert add_language_divs(_content) == content
-
-
-@pytest.mark.parametrize("lang", ("en", "fr"))
-def test_add_language_divs_fr_does_not_replace(lang: str):
- _content = f"[[{lang}]] asdf [[/{lang}]]"
- assert add_language_divs(_content) == _content
-
+class TestAddLanguageDivs:
+ @pytest.mark.parametrize("lang", ("en", "fr"))
+ def test_add_language_divs_fr_replaces(self, lang: str):
+ _content = (
+ f'[[{lang}]]
'
+ ''
+ "title
"
+ ''
+ "Comment vas-tu aujourd'hui?
"
+ f'[[/{lang}]]
'
+ )
+ content = (
+ f''
+ '
'
+ "title
"
+ '
'
+ "Comment vas-tu aujourd'hui?
"
+ )
+ assert add_language_divs(_content) == content
-@pytest.mark.parametrize(
- "input,output",
- (
- ("abc 123", "abc 123"),
- ("[[fr]]\n\nabc\n\n[[/fr]]", "\n\nabc\n\n"),
- ("[[en]]\n\nabc\n\n[[/en]]", "\n\nabc\n\n"),
- ("[[en]]\n\nabc\n\n[[/en]]\n\n[[fr]]\n\n123\n\n[[/fr]]", "\n\nabc\n\n\n\n\n\n123\n\n"),
- ),
-)
-def test_remove_language_divs(input: str, output: str):
- assert remove_language_divs(input) == output
+ @pytest.mark.parametrize(
+ "input,output",
+ (
+ ("abc 123", "abc 123"),
+ ("[[fr]]\n\nabc\n\n[[/fr]]", "\n\nabc\n\n"),
+ ("[[en]]\n\nabc\n\n[[/en]]", "\n\nabc\n\n"),
+ ("[[en]]\n\nabc\n\n[[/en]]\n\n[[fr]]\n\n123\n\n[[/fr]]", "\n\nabc\n\n\n\n\n\n123\n\n"),
+ ),
+ )
+ def test_remove_language_divs(self, input: str, output: str):
+ assert remove_language_divs(input) == output
+
+ def test_multiple_language_div_after_mistune(self):
+ testString = '[[fr]]
Le français suis l\'anglais
[[/fr]]
[[en]]
hi
[[/en]]
[[fr]]
Bonjour
[[/fr]]
' # noqa
+ testResult = 'Le français suis l\'anglais
Bonjour
' # noqa
+
+ assert add_language_divs(testString) == testResult
+
+ def test_multiple_language_div_without_mistune(self):
+ testString = f"""
+ [[fr]]
+ Le français suis l'anglais
+ [[/fr]]
+
+ [[en]]
+ hi
+ [[/en]]
+
+ [[fr]]
+ bonjour
+ [[/fr]]
+ """ # noqa
+
+ testResult = '\n \n Le français suis l\'anglais\n
\n\n \n hi\n
\n\n \n bonjour\n
\n ' # noqa
+ assert add_language_divs(testString) == testResult
+
+ def test_nested_language_divs_after_mistune(self):
+ testString = '[[fr]]
Le français suis l\'anglais
[[en]]
hi
[[/en]]
[[/fr]]
[[en]]
hi
[[fr]]
Bonjour
[[/fr]]
[[/en]]
' # noqa
+ testResult = 'Le français suis l\'anglais
' # noqa
+
+ assert add_language_divs(testString) == testResult
+
+ def test_nested_language_divs_without_mistune(self):
+ testString = f"""
+ [[fr]]
+ Le français suis l'anglais
+
+ [[en]]
+ English!
+ [[/en]]
+
+ [[/fr]]
+
+ [[en]]
+ English
+ [[fr]]
+ French!
+ [[/fr]]
+ [[/en]]
+ """ # noqa
+ testResult = '\n \n Le français suis l\'anglais\n \n
\n English!\n
\n \n
\n\n \n English\n
\n French!\n
\n
\n ' # noqa
+ assert add_language_divs(testString) == testResult
@pytest.mark.parametrize(
diff --git a/tests/test_template.py b/tests/test_template.py
index 979610df..46b07749 100644
--- a/tests/test_template.py
+++ b/tests/test_template.py
@@ -14,13 +14,10 @@ def test_lang_tags_in_templates():
"bad_content",
[
"[[en]\nEN text\n[[/en]]", # missing bracket
- "[[en]]EN text\n[[/en]]", # missing \n
- "[[en]]\nEN text[[/en]]", # missing \n
"[[EN]]\nEN text\n[[/EN]]", # tags not lowercase
"[[en]]\nEN text\n", # tag missing
"EN text\n[[/en]]", # tag missing
"((en))\nEN text\n((/en))", # wrong brackets
- "[[en]]EN text[[/en]]", # tags not on their own line
],
)
def test_lang_tags_in_templates_bad_content(bad_content: str):