Skip to content

Commit

Permalink
Fix false language code in {{exemple}}
Browse files Browse the repository at this point in the history
  • Loading branch information
hubertbossot committed Apr 21, 2024
1 parent b1e0b89 commit 1d21f49
Showing 1 changed file with 25 additions and 3 deletions.
28 changes: 25 additions & 3 deletions src/wiktionary/fr_wiktionary_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1004,9 +1004,7 @@ def add_language_code_with_named_parameter_to_template(
has_subtemplate_included = False
if page_content.find('}}') > page_content.find('{{') != -1:
# TODO Infinite loop in [[tomme]] with ^date\|[^{}]*({{(.*?)}}|.)+[^{}]*\|lang=
regex_has_subtemplate = r'^' + \
re.escape(current_template) + \
r'\|[^{}]*({{(.*?)}}|.)+[^{}]*\| *lang *='
regex_has_subtemplate = r'^' + re.escape(current_template) + r'\|[^{}]*({{(.*?)}}|.)+[^{}]*\| *lang *='
if re.search(regex_has_subtemplate, page_content):
has_subtemplate_included = True

Expand Down Expand Up @@ -1036,6 +1034,30 @@ def add_language_code_with_named_parameter_to_template(
else:
if debug_level > 0:
print(' "lang=" already present')

regex_lang = r'^[^{}]+\| *lang(?:gue|1)? *= *([a-zA-Z\-]*)'
p = re.compile(regex_lang)
m = p.match(page_content)
if m is None:
if debug_level > 0:
print(' weird case')
return next_template(final_page_content, page_content)

start = end = 0
old_language_code = ''
if m.span(1) is not None:
[start, end] = m.span(1)
old_language_code = page_content[start:end]
if debug_level > 0:
print(' "lang=" ' + old_language_code)

if language_code == old_language_code:
return next_template(final_page_content, page_content)

if debug_level > 0:
print(' "lang=" correction to ' + language_code)
page_content = page_content[:start] + language_code + page_content[end:]

return next_template(final_page_content, page_content)


Expand Down

0 comments on commit 1d21f49

Please sign in to comment.