rendered paste body# -*- coding: utf-8 -*-""" pygments.formatters.irc ~~~~~~~~~~~~~~~~~~~~~~~~~~~~"""from pygments.formatter import Formatterfrom pygments.token import Keyword, Name, Comment, String, Error, \ Number, Operator, Generic, Token, Whitespacefrom pygments.util import get_choice_opt__all__ = ['IRCFormatter']WHITE = 0BLACK = 1NAVY = 2GREEN = 3RED = 4BROWN = 5PURPLE = 6ORANGE = 7YELLOW = 8LIME = 9TEAL = 10CYAN = 11BLUE = 12PINK = 13DGREY = 14GREY = 15IRC_COLORS = { Token: (BLACK, WHITE), Whitespace: (GREY, DGREY), Comment: (DGREY, DGREY), Comment.Preproc: (TEAL, CYAN), Keyword: (NAVY, BLUE), Keyword.Type: (TEAL, CYAN), Operator.Word: (PURPLE, PINK), Name.Builtin: (TEAL, CYAN), Name.Function: (GREEN, LIME), Name.Namespace: (TEAL, CYAN), Name.Class: (GREEN, LIME), Name.Exception: (TEAL, CYAN), Name.Decorator: (DGREY, GREY), Name.Variable: (BROWN, RED), Name.Constant: (BROWN, RED), Name.Attribute: (TEAL, CYAN), Name.Tag: (BLUE, BLUE), String: (BROWN, PINK), Number: (NAVY, BLUE), Generic.Deleted: (GREEN, GREEN), Generic.Inserted: (GREEN, LIME), Generic.Heading: (BLACK, WHITE), Generic.Subheading: (PURPLE, PINK), Generic.Error: (RED, RED), Error: (RED, RED),}def ircformat((fg, bg), text): return "\x03%i,%02i%s" % (fg, bg, text)class IRCFormatter(Formatter): name = 'IRC' aliases = ['irc', 'mirc'] filenames = [] def __init__(self, **options): Formatter.__init__(self, **options) self.darkbg = get_choice_opt(options, 'bg', ['light', 'dark'], 'light') == 'dark' self.colorscheme = IRC_COLORS def format(self, tokensource, outfile): if not self.encoding and hasattr(outfile, "encoding") and \ hasattr(outfile, "isatty") and outfile.isatty(): self.encoding = outfile.encoding return Formatter.format(self, tokensource, outfile) def format_unencoded(self, tokensource, outfile): for ttype, value in tokensource: color = self.colorscheme.get(ttype) while color is None: ttype = ttype[:-1] color = self.colorscheme.get(ttype) if color: color = color[self.darkbg] spl = value.split('\n') for line in spl[:-1]: if line: outfile.write(ircformat((color, self.darkbg), line)) outfile.write('\n') if spl[-1]: outfile.write(ircformat((color, self.darkbg), spl[-1])) else: outfile.write(value)