# -*- coding: utf-8 -*- """ pygments.formatters.irc ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ """ from pygments.formatter import Formatter from pygments.token import Keyword, Name, Comment, String, Error, \ Number, Operator, Generic, Token, Whitespace from pygments.util import get_choice_opt __all__ = ['IRCFormatter'] WHITE = 0 BLACK = 1 NAVY = 2 GREEN = 3 RED = 4 BROWN = 5 PURPLE = 6 ORANGE = 7 YELLOW = 8 LIME = 9 TEAL = 10 CYAN = 11 BLUE = 12 PINK = 13 DGREY = 14 GREY = 15 IRC_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)