Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/formatters/irc.py: 93%
55 statements
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:45 +0000
« prev ^ index » next coverage.py v7.2.2, created at 2023-03-26 07:45 +0000
1"""
2 pygments.formatters.irc
3 ~~~~~~~~~~~~~~~~~~~~~~~
5 Formatter for IRC output
7 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS.
8 :license: BSD, see LICENSE for details.
9"""
11from pygments.formatter import Formatter
12from pygments.token import Keyword, Name, Comment, String, Error, \
13 Number, Operator, Generic, Token, Whitespace
14from pygments.util import get_choice_opt
17__all__ = ['IRCFormatter']
20#: Map token types to a tuple of color values for light and dark
21#: backgrounds.
22IRC_COLORS = {
23 Token: ('', ''),
25 Whitespace: ('gray', 'brightblack'),
26 Comment: ('gray', 'brightblack'),
27 Comment.Preproc: ('cyan', 'brightcyan'),
28 Keyword: ('blue', 'brightblue'),
29 Keyword.Type: ('cyan', 'brightcyan'),
30 Operator.Word: ('magenta', 'brightcyan'),
31 Name.Builtin: ('cyan', 'brightcyan'),
32 Name.Function: ('green', 'brightgreen'),
33 Name.Namespace: ('_cyan_', '_brightcyan_'),
34 Name.Class: ('_green_', '_brightgreen_'),
35 Name.Exception: ('cyan', 'brightcyan'),
36 Name.Decorator: ('brightblack', 'gray'),
37 Name.Variable: ('red', 'brightred'),
38 Name.Constant: ('red', 'brightred'),
39 Name.Attribute: ('cyan', 'brightcyan'),
40 Name.Tag: ('brightblue', 'brightblue'),
41 String: ('yellow', 'yellow'),
42 Number: ('blue', 'brightblue'),
44 Generic.Deleted: ('brightred', 'brightred'),
45 Generic.Inserted: ('green', 'brightgreen'),
46 Generic.Heading: ('**', '**'),
47 Generic.Subheading: ('*magenta*', '*brightmagenta*'),
48 Generic.Error: ('brightred', 'brightred'),
50 Error: ('_brightred_', '_brightred_'),
51}
54IRC_COLOR_MAP = {
55 'white': 0,
56 'black': 1,
57 'blue': 2,
58 'brightgreen': 3,
59 'brightred': 4,
60 'yellow': 5,
61 'magenta': 6,
62 'orange': 7,
63 'green': 7, #compat w/ ansi
64 'brightyellow': 8,
65 'lightgreen': 9,
66 'brightcyan': 9, # compat w/ ansi
67 'cyan': 10,
68 'lightblue': 11,
69 'red': 11, # compat w/ ansi
70 'brightblue': 12,
71 'brightmagenta': 13,
72 'brightblack': 14,
73 'gray': 15,
74}
76def ircformat(color, text):
77 if len(color) < 1:
78 return text
79 add = sub = ''
80 if '_' in color: # italic
81 add += '\x1D'
82 sub = '\x1D' + sub
83 color = color.strip('_')
84 if '*' in color: # bold
85 add += '\x02'
86 sub = '\x02' + sub
87 color = color.strip('*')
88 # underline (\x1F) not supported
89 # backgrounds (\x03FF,BB) not supported
90 if len(color) > 0: # actual color - may have issues with ircformat("red", "blah")+"10" type stuff
91 add += '\x03' + str(IRC_COLOR_MAP[color]).zfill(2)
92 sub = '\x03' + sub
93 return add + text + sub
94 return '<'+add+'>'+text+'</'+sub+'>'
97class IRCFormatter(Formatter):
98 r"""
99 Format tokens with IRC color sequences
101 The `get_style_defs()` method doesn't do anything special since there is
102 no support for common styles.
104 Options accepted:
106 `bg`
107 Set to ``"light"`` or ``"dark"`` depending on the terminal's background
108 (default: ``"light"``).
110 `colorscheme`
111 A dictionary mapping token types to (lightbg, darkbg) color names or
112 ``None`` (default: ``None`` = use builtin colorscheme).
114 `linenos`
115 Set to ``True`` to have line numbers in the output as well
116 (default: ``False`` = no line numbers).
117 """
118 name = 'IRC'
119 aliases = ['irc', 'IRC']
120 filenames = []
122 def __init__(self, **options):
123 Formatter.__init__(self, **options)
124 self.darkbg = get_choice_opt(options, 'bg',
125 ['light', 'dark'], 'light') == 'dark'
126 self.colorscheme = options.get('colorscheme', None) or IRC_COLORS
127 self.linenos = options.get('linenos', False)
128 self._lineno = 0
130 def _write_lineno(self, outfile):
131 if self.linenos:
132 self._lineno += 1
133 outfile.write("%04d: " % self._lineno)
135 def format_unencoded(self, tokensource, outfile):
136 self._write_lineno(outfile)
138 for ttype, value in tokensource:
139 color = self.colorscheme.get(ttype)
140 while color is None:
141 ttype = ttype[:-1]
142 color = self.colorscheme.get(ttype)
143 if color:
144 color = color[self.darkbg]
145 spl = value.split('\n')
146 for line in spl[:-1]:
147 if line:
148 outfile.write(ircformat(color, line))
149 outfile.write('\n')
150 self._write_lineno(outfile)
151 if spl[-1]:
152 outfile.write(ircformat(color, spl[-1]))
153 else:
154 outfile.write(value)