# Path: tornado/escape.py
# def utf8(value):
#     """Converts a string argument to a byte string.
# 
#     If the argument is already a byte string or None, it is returned unchanged.
#     Otherwise it must be a unicode string and is encoded as utf8.
#     """
#     if isinstance(value, _UTF8_TYPES):
#         return value
#     assert isinstance(value, unicode)
#     return value.encode("utf-8")
# 
# def xhtml_escape(value):
#     """Escapes a string so it is valid within XML or XHTML."""
#     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
#                                 to_basestring(value))
# 
# def xhtml_unescape(value):
#     """Un-escapes an XML-escaped string."""
#     return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
# 
# def url_escape(value):
#     """Returns a valid URL-encoded version of the given value."""
#     return urllib.quote_plus(utf8(value))
# 
# def url_unescape(value, encoding='utf-8'):
#     """Decodes the given value from a URL.
# 
#     The argument may be either a byte or unicode string.
# 
#     If encoding is None, the result will be a byte string.  Otherwise,
#     the result is a unicode string in the specified encoding.
#     """
#     if encoding is None:
#         return urllib.unquote_plus(utf8(value))
#     else:
#         return unicode(urllib.unquote_plus(utf8(value)), encoding)
# 
# def to_unicode(value):
#     """Converts a string argument to a unicode string.
# 
#     If the argument is already a unicode string or None, it is returned
#     unchanged.  Otherwise it must be a byte string and is decoded as utf8.
#     """
#     if isinstance(value, _TO_UNICODE_TYPES):
#         return value
#     assert isinstance(value, bytes)
#     return value.decode("utf-8")
# 
# def json_decode(value):
#     """Returns Python objects for the given JSON string."""
#     return _json_decode(to_basestring(value))
# 
# def json_encode(value):
#     """JSON-encodes the given Python object."""
#     # JSON permits but does not require forward slashes to be escaped.
#     # This is useful when json data is emitted in a <script> tag
#     # in HTML, as it prevents </script> tags from prematurely terminating
#     # the javscript.  Some json libraries do this escaping by default,
#     # although python's standard library does not, so we do it here.
#     # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
#     return _json_encode(recursive_unicode(value)).replace("</", "<\\/")
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


import tornado.escape
import unittest
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import b
    (u"http://\u27a1.ws/\u4a39", {},
     u'<a href="http://\u27a1.ws/\u4a39">http://\u27a1.ws/\u4a39</a>'),

    ("<tag>http://example.com</tag>", {},
     u'&lt;tag&gt;<a href="http://example.com">http://example.com</a>&lt;/tag&gt;'),

    ("Just a www.example.com link.", {},
     u'Just a <a href="http://www.example.com">www.example.com</a> link.'),

    ("Just a www.example.com link.", 
     {"require_protocol": True},
     u'Just a www.example.com link.'),

    ("A http://reallylong.com/link/that/exceedsthelenglimit.html",
     {"require_protocol": True, "shorten": True},
     u'A <a href="http://reallylong.com/link/that/exceedsthelenglimit.html" title="http://reallylong.com/link/that/exceedsthelenglimit.html">http://reallylong.com/link...</a>'),

    ("A http://reallylongdomainnamethatwillbetoolong.com/hi!",
     {"shorten": True},
     u'A <a href="http://reallylongdomainnamethatwillbetoolong.com/hi" title="http://reallylongdomainnamethatwillbetoolong.com/hi">http://reallylongdomainnametha...</a>!'),

    ("A file:///passwords.txt and http://web.com link", {},
     u'A file:///passwords.txt and <a href="http://web.com">http://web.com</a> link'),

    ("A file:///passwords.txt and http://web.com link",
     {"permitted_protocols": ["file"]},
     u'A <a href="file:///passwords.txt">file:///passwords.txt</a> and http://web.com link'),

    ("www.external-link.com",
     {"extra_params": 'rel="nofollow" class="external"'},

# Path: tornado/escape.py
# def utf8(value):
#     """Converts a string argument to a byte string.
# 
#     If the argument is already a byte string or None, it is returned unchanged.
#     Otherwise it must be a unicode string and is encoded as utf8.
#     """
#     if isinstance(value, _UTF8_TYPES):
#         return value
#     assert isinstance(value, unicode)
#     return value.encode("utf-8")
# 
# def xhtml_escape(value):
#     """Escapes a string so it is valid within XML or XHTML."""
#     return _XHTML_ESCAPE_RE.sub(lambda match: _XHTML_ESCAPE_DICT[match.group(0)],
#                                 to_basestring(value))
# 
# def xhtml_unescape(value):
#     """Un-escapes an XML-escaped string."""
#     return re.sub(r"&(#?)(\w+?);", _convert_entity, _unicode(value))
# 
# def url_escape(value):
#     """Returns a valid URL-encoded version of the given value."""
#     return urllib.quote_plus(utf8(value))
# 
# def url_unescape(value, encoding='utf-8'):
#     """Decodes the given value from a URL.
# 
#     The argument may be either a byte or unicode string.
# 
#     If encoding is None, the result will be a byte string.  Otherwise,
#     the result is a unicode string in the specified encoding.
#     """
#     if encoding is None:
#         return urllib.unquote_plus(utf8(value))
#     else:
#         return unicode(urllib.unquote_plus(utf8(value)), encoding)
# 
# def to_unicode(value):
#     """Converts a string argument to a unicode string.
# 
#     If the argument is already a unicode string or None, it is returned
#     unchanged.  Otherwise it must be a byte string and is decoded as utf8.
#     """
#     if isinstance(value, _TO_UNICODE_TYPES):
#         return value
#     assert isinstance(value, bytes)
#     return value.decode("utf-8")
# 
# def json_decode(value):
#     """Returns Python objects for the given JSON string."""
#     return _json_decode(to_basestring(value))
# 
# def json_encode(value):
#     """JSON-encodes the given Python object."""
#     # JSON permits but does not require forward slashes to be escaped.
#     # This is useful when json data is emitted in a <script> tag
#     # in HTML, as it prevents </script> tags from prematurely terminating
#     # the javscript.  Some json libraries do this escaping by default,
#     # although python's standard library does not, so we do it here.
#     # http://stackoverflow.com/questions/1580647/json-why-are-forward-slashes-escaped
#     return _json_encode(recursive_unicode(value)).replace("</", "<\\/")
# 
# Path: tornado/util.py
# def b(s):
#     return s.encode('latin1')


# Path: tornado/test/escape_test.py
import tornado.escape
import unittest
from tornado.escape import utf8, xhtml_escape, xhtml_unescape, url_escape, url_unescape, to_unicode, json_decode, json_encode
from tornado.util import b

    (u"http://\u27a1.ws/\u4a39", {},
     u'<a href="http://\u27a1.ws/\u4a39">http://\u27a1.ws/\u4a39</a>'),

    ("<tag>http://example.com</tag>", {},
     u'&lt;tag&gt;<a href="http://example.com">http://example.com</a>&lt;/tag&gt;'),

    ("Just a www.example.com link.", {},
     u'Just a <a href="http://www.example.com">www.example.com</a> link.'),

    ("Just a www.example.com link.", 
     {"require_protocol": True},
     u'Just a www.example.com link.'),

    ("A http://reallylong.com/link/that/exceedsthelenglimit.html",
     {"require_protocol": True, "shorten": True},
     u'A <a href="http://reallylong.com/link/that/exceedsthelenglimit.html" title="http://reallylong.com/link/that/exceedsthelenglimit.html">http://reallylong.com/link...</a>'),

    ("A http://reallylongdomainnamethatwillbetoolong.com/hi!",
     {"shorten": True},
     u'A <a href="http://reallylongdomainnamethatwillbetoolong.com/hi" title="http://reallylongdomainnamethatwillbetoolong.com/hi">http://reallylongdomainnametha...</a>!'),

    ("A file:///passwords.txt and http://web.com link", {},
     u'A file:///passwords.txt and <a href="http://web.com">http://web.com</a> link'),

    ("A file:///passwords.txt and http://web.com link",
     {"permitted_protocols": ["file"]},
     u'A <a href="file:///passwords.txt">file:///passwords.txt</a> and http://web.com link'),

    ("www.external-link.com",
     {"extra_params": 'rel="nofollow" class="external"'},

     u'<a href="http://www.external-link.com" rel="nofollow" class="external">www.external-link.com</a>'),