def linebreaks(value):
 value = re.sub('\\r\\n|\\r|\\n', '\n', value)
 paras = re.split('\n{2,}', value)
 paras = [('<p>%s</p>' % p.strip().replace('\n', '<br />')) for p in paras]
 return '\n\n'.join(paras)

def linebreaks ( value ) : value = re . sub ( '\\r\\n|\\r|\\n' , '\n' , value ) paras = re . split ( '\n{2,}' , value ) paras = [ ( '<p>%s</p>' % p . strip ( ) . replace ( '\n' , '<br />' ) ) for p in paras ] return '\n\n' . join ( paras )
converts newlines into <p> and <br />s .