Skip to content

Instantly share code, notes, and snippets.

@clausecker
Created September 9, 2010 12:29
Show Gist options
  • Save clausecker/571809 to your computer and use it in GitHub Desktop.
Save clausecker/571809 to your computer and use it in GitHub Desktop.
#This program was created by Georg Brandl
#<[email protected]> (!)
#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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment