def wordwrap(text, linelength=80):
    """Wrap on a word boundary."""
    out = []
    l = 0
    for w in text.split(' '):
        if w.find('\n') >= 0:
            l = 0
        if l + len(w) > linelength:
            out.append('\n')
            l = 0
        out.append(w)
        l += len(w)
    return ' '.join(out)
