def lookahead(iterable): # https://stackoverflow.com/a/1630350 """Pass through all values from the given iterable, augmented by the information if there are more values to come after the current one (True), or if it is the last value (False). """ # Get an iterator and pull the first value. it = iter(iterable) try: last = next(it) except StopIteration: return # Run the iterator to exhaustion (starting from the second value). for val in it: # Report the *previous* value (more to come). yield last, False last = val # Report the last value. yield last, True def email2html(mailtext:str,extraclasses="",ispatch=False): from html import escape if ispatch: from pygments import highlight from pygments.lexers import DiffLexer from pygments.formatters import HtmlFormatter extraclasses+=" highlight" res='

'%extraclasses mailtext=mailtext.replace("\r\n","\n") # we assume unix lineendings so remove all CRLF if ispatch: mail=highlight(mailtext,lexer=DiffLexer(),formatter=HtmlFormatter(style="monokai")) else: mail=escape(mailtext) lastlf=0 while '\n' in mail: newlf=mail.find('\n',lastlf) if 70newlf+1 and mail[newlf+1]=="\n": # two newlines replacement="

" elif newlf-lastlf<=1: # ignore the second newline of 2 replacement='' else: replacement='
' mail=mail[:newlf]+replacement+mail[newlf+1:] lastlf=newlf+len(replacement) res+=mail res+="

" return res