Skip to content

Commit

Permalink
Fix the style stripper to be more realistic with in-game renderer (#1268
Browse files Browse the repository at this point in the history
)

* Fix the style stripper to be more complient with the in-game stripper/renderer of the MP-styling

* Solve issues with unfinished color styles not stripping.
  • Loading branch information
tomvlk committed Aug 30, 2023
1 parent 18fa540 commit 10e75f1
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 18 deletions.
21 changes: 12 additions & 9 deletions pyplanet/utils/style.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import re
import struct

import binascii

STRIP_ALL = dict(letters='wnoitsgz<>', part=r'\$[lh]\[.+\]|\$[lh]|\$[0-9a-f]{3}')
STRIP_ALL = dict(letters='wnoitsgz<>', part=r'(?<!\$)\$[lh]\[.+\]|(?<!\$)\$[lh]|(?<!\$)\$[0-9a-f]{1,3}|(?<!\$)\$[f-y]{1}')
"""
Strip all custom maniaplanet styles + formatting.
"""

STRIP_COLORS = dict(letters='g', part=r'\$[0-9a-f]{3}')
STRIP_COLORS = dict(letters='g', part=r'(?<!\$)\$[0-9a-f]{1,3}')
"""
Strip colors from your input (including $g, color reset).
"""
Expand All @@ -28,7 +26,7 @@
Strip capital style ($t).
"""

STRIP_LINKS = dict(part=r'\$[lh]\[.+\]|\$[lh]')
STRIP_LINKS = dict(part=r'(?<!\$)\$[lh]\[.+\]|(?<!\$)\$[lh]')
"""
Strip links ($h and $l).
"""
Expand All @@ -39,7 +37,7 @@ def style_strip(text, *strip_methods, strip_styling_blocks=True, keep_reset=Fals
Strip styles from the Maniaplanet universe.
Examples:
.. code-block:: python
print("--- Strip: colours ---")
Expand Down Expand Up @@ -97,13 +95,18 @@ def style_strip(text, *strip_methods, strip_styling_blocks=True, keep_reset=Fals
letters += '<>'

if not regex:
regex = r'(\$[{letters}]{parts})+'.format(
regex = r'((?<!\$)\$[{letters}]{parts})+'.format(
letters=letters,
parts='|{}'.format('|'.join(parts)) if len(parts) > 0 else ''
)

# Strip and return.
return re.sub(regex, '', text, flags=re.IGNORECASE)
# Apply stripping strategy
text = re.sub(regex, '', text, flags=re.IGNORECASE)

# Replace double dollar by single dollars.
text = text.replace('$$', '$')

return text


def percentage_color(percentage): # pragma: no cover
Expand Down
38 changes: 29 additions & 9 deletions tests/unit/utils/test_style.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
def test_style_stripping():
# Colors
raw = '$i$fffMax$06fSmurf$f00.$fffes$$l$09f.$fffm$08f$a5x$n$w$o'
expect = '$iMaxSmurf.es$$l.m$a5x$n$w$o'
expect = '$iMaxSmurf.es$l.mx$n$w$o'
assert style.style_strip(raw, style.STRIP_COLORS) == expect

raw = '$l[some link]$i$FFFMax$06fSmurf$f00.$fffesl$09f.$fffm$08fx$l'
Expand All @@ -15,7 +15,6 @@ def test_style_stripping():
expect = '$l[some link]$iMaxSmurf.esl.mx'
assert style.style_strip(raw, style.STRIP_COLORS) == expect


# Links
raw = '$l$i$fffMax$06fSmurf$f00.$fffesl$09f.$fffm$08f$a5x$l'
expect = '$i$fffMax$06fSmurf$f00.$fffesl$09f.$fffm$08f$a5x'
Expand All @@ -26,39 +25,60 @@ def test_style_stripping():
assert style.style_strip(raw, style.STRIP_LINKS) == expect

raw = '$l[some link]$i$fffMax$06fSmurf$f00.$fffes$$l$09f.$fffm$08fx$l'
expect = '$i$fffMax$06fSmurf$f00.$fffes$$09f.$fffm$08fx'
expect = '$i$fffMax$06fSmurf$f00.$fffes$l$09f.$fffm$08fx'
assert style.style_strip(raw, style.STRIP_LINKS) == expect

raw = '$l[some link]$i$fffMax$06fSmurf$f00.$fffesl$09f.$fffm$08fx'
expect = '$i$fffMax$06fSmurf$f00.$fffesl$09f.$fffm$08fx'
assert style.style_strip(raw, style.STRIP_LINKS) == expect


# Sizes
raw = '$i$n$fffMax$06fSmurf$f00.$w$o$fffe$$nsl$09f.$w$fffm$08f$a5$ox'
expect = '$fffMax$06fSmurf$f00.$fffe$sl$09f.$fffm$08f$a5x'
expect = '$fffMax$06fSmurf$f00.$fffe$nsl$09f.$fffm$08f$a5x'
assert style.style_strip(raw, style.STRIP_SIZES) == expect


# All
raw = '$h$i$fffMax$06fSmurf$f00.$fffesl$09f.$fffm$08f$a5x$h'
expect = 'MaxSmurf.esl.m$a5x'
expect = 'MaxSmurf.esl.mx'
assert style.style_strip(raw) == expect

raw = '$l[some link]$i$fffMax$06fSmur$$f$f00.$fffesl$09f.$fffm$08fx$l'
expect = 'MaxSmur$$f.esl.mx'
expect = 'MaxSmur$f.esl.mx'
assert style.style_strip(raw) == expect

raw = '$l[some link]$i$fffMax$06fSmu$nrf$f00.$fffesl$09f.$fffm$08fx'
expect = 'MaxSmurf.esl.mx'
assert style.style_strip(raw) == expect


# Combination & Misc
raw = '$f80$i$oToffe$g$z$06fSmurf $z$sHello'
expect = 'Toffe$zSmurf $zHello'
assert style.style_strip(raw, keep_reset=True) == expect

raw = '$f80$i$oToffe$g$z$06fSmurf $z$sHello'
expect = 'ToffeSmurf Hello'
assert style.style_strip(raw) == expect

raw = '$f80$i$oToffe$g$z$06fSmurf $z$sHello'
expect = 'ToffeSmurf $sHello'
assert style.style_strip(raw, style.STRIP_SIZES, style.STRIP_COLORS) == expect


def test_bug_1249():
raw = '$$'
expect = '$'
assert style.style_strip(raw) == expect

raw = '$$ff0Test'
expect = '$ff0Test'
assert style.style_strip(raw) == expect

raw = 'Te$mst'
expect = 'Test'
assert style.style_strip(raw) == expect

raw = 'test mes$9sage'
expect = 'test message'
assert style.style_strip(raw, style.STRIP_COLORS) == expect
assert style.style_strip(raw) == expect

0 comments on commit 10e75f1

Please sign in to comment.