Skip to content

Commit

Permalink
Wrap TransToken.translate_html() in markupsafe.Markup
Browse files Browse the repository at this point in the history
  • Loading branch information
TeamSpen210 committed Dec 17, 2024
1 parent e760dbd commit ceec3c6
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/BEE2.spec
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,8 @@ EXCLUDES = [
# Pulls in all of pytest etc, not required.
'trio.testing',

'markupsafe', # Used by TransToken.translate_html(), only relevant for Jinja.

'unittest', # Imported in __name__==__main__..
'doctest',
'optparse',
Expand Down
15 changes: 10 additions & 5 deletions src/transtoken.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,11 @@
# The prefix for all Valve's editor keys.
PETI_KEY_PREFIX: Final = 'PORTAL2_PuzzleEditor'

try:
from markupsafe import Markup
except ImportError: # App doesn't need this, doesn't use Jinja.
Markup = str # type: ignore


class ListStyle(Enum):
"""Kind of comma-separated list to produce."""
Expand Down Expand Up @@ -270,16 +275,16 @@ def __str__(self) -> str:
else:
return text

def translate_html(self) -> str:
def translate_html(self) -> Markup:
"""Translate to text, escaping parameters for HTML.
Any non-token parameters will have HTML syntax escaped.
"""
text = self._convert_token(CURRENT_LANG.value)
if self.parameters:
return HTML_FORMAT.vformat(text, (), self.parameters)
return Markup(HTML_FORMAT.vformat(text, (), self.parameters))
else:
return text
return Markup(text)


TransToken.BLANK = TransToken.untranslated('')
Expand Down Expand Up @@ -389,7 +394,7 @@ def __str__(self) -> str:
return sep.join(items)

@override
def translate_html(self) -> str:
def translate_html(self) -> Markup:
"""Translate to text, escaping parameters for HTML.
Any non-token parameters in children will have HTML syntax escaped.
Expand All @@ -400,7 +405,7 @@ def translate_html(self) -> str:
items = [child.translate_html() for child in self.children]
if self.sort:
items.sort()
return sep.join(items)
return Markup(sep.join(items))


@attrs.frozen(eq=False)
Expand Down

0 comments on commit ceec3c6

Please sign in to comment.