Handling ansi-escaping of || in EvForm, since ansi-parsing gets called twice, this needs some extra care. Resolves #888.

This commit is contained in:
Griatch 2015-11-29 23:50:45 +01:00
parent 9587f400ac
commit c7b26ff6a6
2 changed files with 20 additions and 1 deletions

View file

@ -148,9 +148,17 @@ from evennia.utils.ansi import ANSIString
# as an identifier). These should be listed in regex form.
INVALID_FORMCHARS = r"\s\/\|\\\*\_\-\#\<\>\~\^\:\;\.\,"
# if there is an ansi-escape (||) we have to replace this with ||| to make sure
# to properly escape down the line
_ANSI_ESCAPE = re.compile(r"\|\|")
def _to_ansi(obj, regexable=False):
"convert to ANSIString"
if isinstance(obj, basestring):
# since ansi will be parsed twice (here and in the normal ansi send), we have to
# escape the |-structure twice.
obj = _ANSI_ESCAPE.sub(r"||||", obj)
if isinstance(obj, dict):
return dict((key, _to_ansi(value, regexable=regexable)) for key, value in obj.items())
elif hasattr(obj, "__iter__"):
@ -443,5 +451,5 @@ def _test():
"B": tableB})
# unicode is required since the example contains non-ascii characters
print(unicode(form))
#print(unicode(form))
return form

View file

@ -343,3 +343,14 @@ class TestNestedInlineFuncs(TestCase):
self.assertEqual(nested_inlinefuncs.parse_inlinefunc(
'this should be $pad("""escaped,""" and """instead,""" cropped $crop(with a long,5) text., 80)'),
"this should be escaped, and instead, cropped with text. ")
from evennia.utils import evform
class TestEvForm(TestCase):
def test_form(self):
self.assertEqual(unicode(evform._test()),
u'.------------------------------------------------.\n| |\n| Name: \x1b[1m\x1b[32mTom\x1b[1m\x1b[32m \x1b[1m\x1b[32mthe\x1b[1m\x1b[32m \x1b[0m Player: \x1b[1m\x1b[33mGriatch\x1b[0m \x1b[1m\x1b[32m\x1b[1m\x1b[32m\x1b[1m\x1b[32m\x1b[1m\x1b[32m\x1b[0m |\n| \x1b[1m\x1b[32mBouncer\x1b[0m\x1b[0m |\n| |\n >----------------------------------------------<\n| |\n| Desc: A sturdy \x1b[0m STR: 12 \x1b[0m DEX: 10 \x1b[0m |\n| fellow\x1b[0m INT: 5 \x1b[0m STA: 18 \x1b[0m |\n| LUC: 10 MAG: 3 |\n| |\n >----------------------------------------------<\n| | |\n| HP|MV |M\x1b[0m | Skill |Value |Exp \x1b[0m |\n| ~~+~~~+~ | ~~~~~~~~~~~~+~~~~~~~~~~+~~~~~~~~~~~ |\n| **|***\x1b[0m|*\x1b[0m | Shooting |12 |550/1200 \x1b[0m\x1b[0m |\n| |**\x1b[0m |*\x1b[0m | Herbalism |14 |990/1400 \x1b[0m\x1b[0m |\n| | |*\x1b[0m | Smithing |9 |205/900 \x1b[0m |\n| | |\n ------------------------------------------------\n')
def test_ansi_escape(self):
# note that in a msg() call, the result would be the correct |-----,
# in a print, ansi only gets called once, so ||----- is the result
self.assertEqual(unicode(evform.EvForm(form={"FORM":"\n||-----"})), "||-----")