def contract_rosetta_escapes(text):
    """Replace Rosetta escape sequences with the real characters."""
    return helpers.text_replaced(text, {'[tab]': '\t',
                                        r'\[tab]': '[tab]',
                                        '[nbsp]': u'\u00a0',
                                        r'\[nbsp]': '[nbsp]',
                                        '[nnbsp]': u'\u202f',
                                        r'\[nnbsp]': '[nnbsp]'})
def expand_rosetta_escapes(unicode_text):
    """Replace characters needing a Rosetta escape sequences."""
    escapes = {u'\t': TranslationConstants.TAB_CHAR,
               u'[tab]': TranslationConstants.TAB_CHAR_ESCAPED,
               u'\u00a0': TranslationConstants.NO_BREAK_SPACE_CHAR,
               u'[nbsp]': TranslationConstants.NO_BREAK_SPACE_CHAR_ESCAPED,
               u'\u202f': TranslationConstants.NARROW_NO_BREAK_SPACE_CHAR,
               u'[nnbsp]':
    TranslationConstants.NARROW_NO_BREAK_SPACE_CHAR_ESCAPED}
    return helpers.text_replaced(unicode_text, escapes)
def convert_newlines_to_web_form(unicode_text):
    """Convert Unicode string to CR/LF line endings as used in web forms.

    Any style of line endings is accepted: MacOS-style CR, MS-DOS-style
    CR/LF, or rest-of-world-style LF.
    """
    if unicode_text is None:
        return None

    assert isinstance(unicode_text, unicode), (
        "The given text must be unicode instead of %s" % type(unicode_text))

    if unicode_text is None:
        return None
    elif u'\r\n' in unicode_text:
        # The text is already using the windows newline chars
        return unicode_text
    elif u'\n' in unicode_text:
        return helpers.text_replaced(unicode_text, {u'\n': u'\r\n'})
    else:
        return helpers.text_replaced(unicode_text, {u'\r': u'\r\n'})