diff options
author | David Robillard <d@drobilla.net> | 2009-04-19 01:14:23 +0000 |
---|---|---|
committer | David Robillard <d@drobilla.net> | 2009-04-19 01:14:23 +0000 |
commit | 2df6a44d12a2fb5f7b373d9ee4cc99f0632b2e53 (patch) | |
tree | 0696d1165d0a2aefa4afd3a2a0b05ab528af7ba5 | |
parent | 9dc74f15acec8daac019f21ec0834827a5c5fd51 (diff) | |
download | resp-2df6a44d12a2fb5f7b373d9ee4cc99f0632b2e53.tar.gz resp-2df6a44d12a2fb5f7b373d9ee4cc99f0632b2e53.tar.bz2 resp-2df6a44d12a2fb5f7b373d9ee4cc99f0632b2e53.zip |
Add pygments highlighting script.
git-svn-id: http://svn.drobilla.net/resp/tuplr@113 ad02d1e2-f140-0410-9f75-f8b11f17cedd
-rwxr-xr-x | highlight.py | 113 |
1 files changed, 113 insertions, 0 deletions
diff --git a/highlight.py b/highlight.py new file mode 100755 index 0000000..d5d15de --- /dev/null +++ b/highlight.py @@ -0,0 +1,113 @@ +#!/usr/bin/env python +# -*- coding: utf-8 -*- +import sys +import pygments +import re +from pygments import highlight +from pygments.lexers import SchemeLexer +from pygments.formatters import HtmlFormatter +from pygments.lexer import RegexLexer, bygroups +from pygments.token import * +from pygments.style import Style + +#from pygments.styles import STYLE_MAP +#print STYLE_MAP.keys() + +class TuplrLexer(RegexLexer): + name = 'Tuplr' + aliases = ['tuplr', 'tpr'] + filenames = ['*.tpr'] + mimetypes = ['text/x-tuplr', 'application/x-tuplr'] + + keywords = [ 'fn', 'def', 'if', 'match', 'ns', 'type' ] + builtins = [ 'cons', 'car', 'cdr' ] + + valid_name = r'[a-zA-Z0-9!$%&*+,/:<=>?@^_~|-]+' + + tokens = { + 'root' : [ + # types + (r':?[A-Z][a-zA-Z]*|:\([A-Z][a-zA-Z\ ]*\)', Keyword.Type), + + # line comments + (r';.*$', Comment.Single), + + # whitespace + (r'\s+', Text), + + # numbers + (r'-?\d+\.\d+', Number.Float), + (r'-?\d+', Number.Integer), + + # strings, symbols and characters + (r'"(\\\\|\\"|[^"])*"', String), + (r"'" + valid_name, String.Symbol), + (r"#\\([()/'\".'_!ยง$%& ?=+-]{1}|[a-zA-Z0-9]+)", String.Char), + + # constants + (r'(#t|#f)', Name.Constant), + + # highlight keywords + ('(%s)' % '|'.join([ + re.escape(entry) + ' ' for entry in keywords]), + Keyword + ), + + # highlight builtins + ("(?<=\()(%s)" % '|'.join([ + re.escape(entry) + ' ' for entry in builtins]), + Name.Builtin + ), + + # remaining functions + (r'(?<=\()' + valid_name, Name.Function), + + # remaining variables + (valid_name, Name.Variable), + + # parenthesis + (r'(\(|\))', Punctuation), + ], + } + +class TuplrStyle(Style): + default_style = "#FFF" + background_color = "#222" + styles = { + Comment: '#79E', + Keyword: '#EE5', + Name: '#DDD', + Name.Function: '#0F0', + Name.Class: '#5E5', + String: '#F88', + Keyword.Type: '#5E5', + Punctuation: '#AAA', + Number: '#F88' + } + +if len(sys.argv) != 3: + 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, '''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" + "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml"> +<head> + <title>%s</title> + <style type="text/css">''' % sys.argv[1] +print >>outfile, HtmlFormatter(style=TuplrStyle).get_style_defs('.highlight') +print >>outfile, ''' </style> +</head> +<body> +''' +print >>outfile, highlight(text, TuplrLexer(), HtmlFormatter()) +print >>outfile, '''</body> +</html> +''' +outfile.close() + |