From 724a55ba4de8ba8e26d660a7c60eb0bf89ec9354 Mon Sep 17 00:00:00 2001 From: David Robillard Date: Sun, 19 Apr 2009 04:37:36 +0000 Subject: Prettier LaTeX output. git-svn-id: http://svn.drobilla.net/resp/tuplr@116 ad02d1e2-f140-0410-9f75-f8b11f17cedd --- highlight.py | 94 ++++++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 63 insertions(+), 31 deletions(-) diff --git a/highlight.py b/highlight.py index 8ed2376..a26d239 100755 --- a/highlight.py +++ b/highlight.py @@ -5,7 +5,7 @@ import pygments import re from pygments import highlight from pygments.lexers import SchemeLexer -from pygments.formatters import HtmlFormatter +from pygments.formatters import HtmlFormatter, LatexFormatter from pygments.lexer import RegexLexer, bygroups from pygments.token import * from pygments.style import Style @@ -22,12 +22,12 @@ class TuplrLexer(RegexLexer): keywords = [ 'fn', 'def', 'if', 'match', 'ns', 'type' ] builtins = [ 'cons', 'car', 'cdr' ] - valid_name = r'[a-zA-Z0-9!$%&*+,/:<=>?@^_~|-]+' + valid_name = r'[.a-zA-Z0-9!$%&*+,/:<=>?@^_~|-]+' tokens = { 'root' : [ - # types - (r':?[A-Z][a-zA-Z.]*|:\([A-Z][a-zA-Z.\ ]*\)', Keyword.Type), + # types + (r':?[A-Z][a-zA-Z.]*|:\([A-Z][a-zA-Z.\ ]*\)', Keyword.Type), # line comments (r';.*$', Comment.Single), @@ -62,7 +62,7 @@ class TuplrLexer(RegexLexer): # remaining functions (r'(?<=\()' + valid_name, Name.Function), - # remaining variables + # remaining variables (valid_name, Name.Variable), # parenthesis @@ -70,7 +70,7 @@ class TuplrLexer(RegexLexer): ], } -class TuplrStyle(Style): +class TuplrStyleDark(Style): default_style = "#FFF" background_color = "#222" styles = { @@ -78,38 +78,70 @@ class TuplrStyle(Style): Keyword: '#EE5', Name: '#DDD', Text: '#DDD', -# Name.Function: '#0F0', -# Name.Class: '#E55', String: '#F88', - Keyword.Type: '#5E5', - Punctuation: '#AAA', - Number: '#F88' + Keyword.Type: '#5E5', + Punctuation: '#AAA', + Number: '#F88' + } + +class TuplrStyleLight(Style): + default_style = "#FFF" + background_color = "#EEE" + styles = { + Comment: '#57C', + Keyword: '#AA0', + Name: '#777', + Text: '#777', + String: '#D66', + Keyword.Type: '#3C3', + Punctuation: '#888', + Number: '#D66' } if len(sys.argv) != 3: - print 'USAGE: %s IN OUT' % sys.argv[0] - sys.exit(1) + print 'USAGE: %s IN OUT' % sys.argv[0] + sys.exit(1) infile = open(sys.argv[1], 'r') text = infile.read() infile.close() -outfile = open(sys.argv[2], 'w') -print >>outfile, ''' - - - - %s - - - -''' -print >>outfile, highlight(text, TuplrLexer(), HtmlFormatter()) -print >>outfile, ''' - -''' -outfile.close() +if re.match('.*\.html$', sys.argv[2]): + outfile = open(sys.argv[2], 'w') + print >>outfile, ''' + + + + %s + + + + ''' + print >>outfile, highlight(text, TuplrLexer(), HtmlFormatter(style=TuplrStyleDark)) + print >>outfile, ''' + + ''' + outfile.close() + +if re.match('.*\.tex$', sys.argv[2]): + outfile = open(sys.argv[2], 'w') + print >>outfile, '''\\documentclass[10pt]{article} +\\usepackage{fancyvrb} +\\usepackage{color}''' + print >>outfile, LatexFormatter(style=TuplrStyleLight).get_style_defs() + print >>outfile, '\\newcommand\\PYlambda{$\\lambda$}' + print >>outfile, '\\newcommand\\PYbiglambda{$\\Lambda$}' + print >>outfile, '\\begin{document}' + out = highlight(text, TuplrLexer(), LatexFormatter(style=TuplrStyleLight)) + print out + print "******************************************************************************" + out = out.replace('[fn', '[@PYlambda') + out = out.replace('[Fn', '[@PYbiglambda') + print out + print >>outfile, out + print >>outfile, '\\end{document}' + outfile.close() -- cgit v1.2.1