diff options
author | gbrandl <devnull@localhost> | 2006-10-19 20:27:28 +0200 |
---|---|---|
committer | gbrandl <devnull@localhost> | 2006-10-19 20:27:28 +0200 |
commit | f4d019954468db777760d21f9243eca8b852c184 (patch) | |
tree | 328b8f8fac25338306b0e7b827686dcc7597df23 /pygments/formatters/other.py | |
download | pygments-f4d019954468db777760d21f9243eca8b852c184.tar.gz |
[svn] Name change, round 4 (rename SVN root folder).
Diffstat (limited to 'pygments/formatters/other.py')
-rw-r--r-- | pygments/formatters/other.py | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/pygments/formatters/other.py b/pygments/formatters/other.py new file mode 100644 index 00000000..eb8d72fc --- /dev/null +++ b/pygments/formatters/other.py @@ -0,0 +1,73 @@ +# -*- coding: utf-8 -*- +""" + pygments.formatters.other + ~~~~~~~~~~~~~~~~~~~~~~~~ + + Other formatters. + + :copyright: 2006 by Georg Brandl, Armin Ronacher. + :license: GNU LGPL, see LICENSE for more details. +""" + +from pygments.formatter import Formatter +import StringIO + +__all__ = ['NullFormatter', 'RawTokenFormatter'] + + +class NullFormatter(Formatter): + """ + Output the text unchanged without any formatting. + """ + def format(self, tokensource, outfile): + for ttype, value in tokensource: + outfile.write(value) + + +class RawTokenFormatter(Formatter): + """ + Output a raw token representation for storing token streams. + + The format is ``tokentype<TAB>repr(tokenstring)`` + + Additional options accepted: + + ``compress`` + If set to "gz" or "bz2", compress the token stream with + the given compression algorithm (default: ''). + """ + + def __init__(self, **options): + Formatter.__init__(self, **options) + self.compress = options.get('compress', '') + + def format(self, tokensource, outfile): + if self.compress == 'gz': + import gzip + outfile = gzip.GzipFile('', 'wb', 9, outfile) + write = outfile.write + flush = outfile.flush + elif self.compress == 'bz2': + import bz2 + compressor = bz2.BZ2Compressor(9) + def write(text): + outfile.write(compressor.compress(text)) + def flush(): + outfile.write(compressor.flush()) + outfile.flush() + else: + write = outfile.write + flush = outfile.flush + + lasttype = None + lastval = '' + for ttype, value in tokensource: + if ttype is lasttype: + lastval += value + else: + if lasttype: + write("%s\t%r\n" % (lasttype, lastval)) + lastval = value + lasttype = ttype + write("%s\t%r\n" % (lasttype, lastval)) + flush() |