#This program was created by Georg Brandl # (!) #This program was raised into public domain by its owner. #You can use it everywhere whithout permission, but please #provide this information. # #How to use: #lhs_highlighter.py file.lhs > file.tex # #The program converts Haskell snippets in literate sourcecode #to highlighted markup. The program will automatically add the #required formatting stuff and outputs latex markup. import sys from pygments import highlight from pygments.lexers.functional import HaskellLexer from pygments.formatters.latex import LatexFormatter lx = HaskellLexer() fmt = LatexFormatter(style='emacs') preamble = fmt.get_style_defs() outfile = sys.stdout incode = False code = '' for line in open(sys.argv[1]): if line.strip() == '\\begin{document}': outfile.write('\\usepackage{fancyvrb}\n') outfile.write('\\usepackage{xcolor}\n') outfile.write(preamble) outfile.write(line) elif line.strip().startswith('\\begin{code}'): incode = True elif line.strip().startswith('\\end{code}') and incode: highlight(code, lx, fmt, outfile) incode = False code = '' elif incode: code += line else: outfile.write(line)