def wrap(text, width):
    """
    A word-wrap function that preserves existing line breaks
    and most spaces in the text. Expects that existing line
    breaks are posix newlines (\n).

    Inserted newlines will appear with a backslash instead.
    """
    return string.replace(string.replace(string.replace(string.replace(
            string.replace(
             reduce(
                lambda line,
                word,
                width=width: '%s%s%s' %
                  (line,
                   ' \n'[(len(line)-line.rfind('\n')-1 +
                     len(word.split('\n',1)[0]) >= width)],
                   word),
                  string.replace(string.replace(
                                string.replace(text,
                                         ".",
                                         ".___MARK "),
                                 ";",
                                 ";___MARK "),
                        "\n",
                        "___EOL ").split(' ')
                 ),
            "___MARK ", ""),
           "___MARK", ""),
        "\n", "\\\n"),
        "___EOL\\\n", "\n"),
       "___EOL ", "\n")

    A word-wrap function that preserves existing line breaks
    and most spaces in the text. Expects that existing line
    breaks are posix newlines (
).

    Inserted newlines will appear with a backslash instead.
    
recipes/Python/440619_word_wrap_long_line_Javlimited_line_length/recipe-440619.py