Skip to content

Commit

Permalink
Fix bug 1294737: Recognize string format named placeables (#452)
Browse files Browse the repository at this point in the history
  • Loading branch information
MikkCZ authored and mathjazz committed Aug 18, 2016
1 parent 4850754 commit 94ae3c9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
22 changes: 22 additions & 0 deletions pontoon/base/tests/test_placeables.py
Expand Up @@ -64,3 +64,25 @@ def test_python_new_format_placeables(self):
mark_placeables(u'Hello {someone.name[0]}'),
u'Hello <mark class="placeable" title="Python format string">{someone.name[0]}</mark>'
)

def test_python_format_named_placeables(self):
"""Test detection of format string with named placeables."""
assert_equal(
mark_placeables(u'Hello %(name)s'),
u'Hello <mark class="placeable" title="Python format string">%(name)s</mark>'
)

assert_equal(
mark_placeables(u'Rolling %(number)d dices'),
u'Rolling <mark class="placeable" title="Python format string">%(number)d</mark> dices'
)

assert_equal(
mark_placeables(u'Hello %(name)S'),
u'Hello <mark class="placeable" title="Python format string">%(name)S</mark>'
)

assert_equal(
mark_placeables(u'Rolling %(number)D dices'),
u'Rolling <mark class="placeable" title="Python format string">%(number)D</mark> dices'
)
11 changes: 11 additions & 0 deletions pontoon/base/utils.py
Expand Up @@ -69,6 +69,13 @@ class SpacesPlaceable(base.Ph):
parse = classmethod(general.regex_parse)


class PythonFormatNamedPlaceable(base.Ph):
"""Placeable handling named format string in python"""
istranslatable = False
regex = re.compile(r'%\([[\w\d\!\.,\[\]%:$<>\+\-= ]*\)[+|-|0\d+|#]?[\.\d+]?[s|d|e|f|g|o|x|c|%]', re.IGNORECASE)
parse = classmethod(general.regex_parse)


class PythonFormatPlaceable(base.Ph):
"""Placeable handling new format strings in python"""
istranslatable = False
Expand All @@ -89,6 +96,7 @@ def mark_placeables(text):
# The spaces placeable can match '\n ' and mask the newline,
# so it has to come later.
SpacesPlaceable.parse,
PythonFormatNamedPlaceable.parse,
PythonFormatPlaceable.parse,
general.XMLTagPlaceable.parse,
general.AltAttrPlaceable.parse,
Expand Down Expand Up @@ -130,6 +138,7 @@ def mark_placeables(text):
'CamelCasePlaceable': "Camel case string",
'XMLTagPlaceable': "XML tag",
'OptionPlaceable': "Command line option",
'PythonFormatNamedPlaceable': "Python format string",
'PythonFormatPlaceable': "Python format string"
}

Expand Down Expand Up @@ -170,6 +179,8 @@ def mark_placeables(text):
}.get(placeable),
'PythonFormatPlaceable':
placeable.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'),
'PythonFormatNamedPlaceable':
placeable.replace('&', '&amp;').replace('<', '&lt;').replace('>', '&gt;'),
'XMLEntityPlaceable': placeable.replace('&', '&amp;'),
'XMLTagPlaceable':
placeable.replace('<', '&lt;').replace('>', '&gt;'),
Expand Down

0 comments on commit 94ae3c9

Please sign in to comment.