added infrastructure code for later javascript translating (currently not active)

git-svn-id: http://code.djangoproject.com/svn/django/trunk@1529 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Georg Bauer 2005-12-04 12:06:16 +00:00
parent 946bd1e370
commit 5917fdcf2d
4 changed files with 243 additions and 15 deletions

View file

@ -1,5 +1,7 @@
import re
from django.conf.settings import DEFAULT_CHARSET
# Capitalizes the first letter of a string.
capfirst = lambda x: x and x[0].upper() + x[1:]
@ -90,3 +92,20 @@ def compress_string(s):
zfile.write(s)
zfile.close()
return zbuf.getvalue()
ustring_re = re.compile(u"([\u0080-\uffff])")
def javascript_quote(s):
def fix(match):
return r"\u%04x" % ord(match.group(1))
if type(s) == str:
s = s.decode(DEFAULT_ENCODING)
elif type(s) != unicode:
raise TypeError, s
s = s.replace('\\', '\\\\')
s = s.replace('\n', '\\n')
s = s.replace('\t', '\\t')
s = s.replace("'", "\\'")
return str(ustring_re.sub(fix, s))