eric6/ThirdParty/Pygments/pygments/formatters/irc.py

changeset 8258
82b608e352ec
parent 8257
28146736bbfc
child 8259
2bbec88047dd
equal deleted inserted replaced
8257:28146736bbfc 8258:82b608e352ec
1 # -*- coding: utf-8 -*-
2 """
3 pygments.formatters.irc
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 Formatter for IRC output
7
8 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details.
10 """
11
12 import sys
13
14 from pygments.formatter import Formatter
15 from pygments.token import Keyword, Name, Comment, String, Error, \
16 Number, Operator, Generic, Token, Whitespace
17 from pygments.util import get_choice_opt
18
19
20 __all__ = ['IRCFormatter']
21
22
23 #: Map token types to a tuple of color values for light and dark
24 #: backgrounds.
25 IRC_COLORS = {
26 Token: ('', ''),
27
28 Whitespace: ('gray', 'brightblack'),
29 Comment: ('gray', 'brightblack'),
30 Comment.Preproc: ('cyan', 'brightcyan'),
31 Keyword: ('blue', 'brightblue'),
32 Keyword.Type: ('cyan', 'brightcyan'),
33 Operator.Word: ('magenta', 'brightcyan'),
34 Name.Builtin: ('cyan', 'brightcyan'),
35 Name.Function: ('green', 'brightgreen'),
36 Name.Namespace: ('_cyan_', '_brightcyan_'),
37 Name.Class: ('_green_', '_brightgreen_'),
38 Name.Exception: ('cyan', 'brightcyan'),
39 Name.Decorator: ('brightblack', 'gray'),
40 Name.Variable: ('red', 'brightred'),
41 Name.Constant: ('red', 'brightred'),
42 Name.Attribute: ('cyan', 'brightcyan'),
43 Name.Tag: ('brightblue', 'brightblue'),
44 String: ('yellow', 'yellow'),
45 Number: ('blue', 'brightblue'),
46
47 Generic.Deleted: ('brightred', 'brightred'),
48 Generic.Inserted: ('green', 'brightgreen'),
49 Generic.Heading: ('**', '**'),
50 Generic.Subheading: ('*magenta*', '*brightmagenta*'),
51 Generic.Error: ('brightred', 'brightred'),
52
53 Error: ('_brightred_', '_brightred_'),
54 }
55
56
57 IRC_COLOR_MAP = {
58 'white': 0,
59 'black': 1,
60 'blue': 2,
61 'brightgreen': 3,
62 'brightred': 4,
63 'yellow': 5,
64 'magenta': 6,
65 'orange': 7,
66 'green': 7, #compat w/ ansi
67 'brightyellow': 8,
68 'lightgreen': 9,
69 'brightcyan': 9, # compat w/ ansi
70 'cyan': 10,
71 'lightblue': 11,
72 'red': 11, # compat w/ ansi
73 'brightblue': 12,
74 'brightmagenta': 13,
75 'brightblack': 14,
76 'gray': 15,
77 }
78
79 def ircformat(color, text):
80 if len(color) < 1:
81 return text
82 add = sub = ''
83 if '_' in color: # italic
84 add += '\x1D'
85 sub = '\x1D' + sub
86 color = color.strip('_')
87 if '*' in color: # bold
88 add += '\x02'
89 sub = '\x02' + sub
90 color = color.strip('*')
91 # underline (\x1F) not supported
92 # backgrounds (\x03FF,BB) not supported
93 if len(color) > 0: # actual color - may have issues with ircformat("red", "blah")+"10" type stuff
94 add += '\x03' + str(IRC_COLOR_MAP[color]).zfill(2)
95 sub = '\x03' + sub
96 return add + text + sub
97 return '<'+add+'>'+text+'</'+sub+'>'
98
99
100 class IRCFormatter(Formatter):
101 r"""
102 Format tokens with IRC color sequences
103
104 The `get_style_defs()` method doesn't do anything special since there is
105 no support for common styles.
106
107 Options accepted:
108
109 `bg`
110 Set to ``"light"`` or ``"dark"`` depending on the terminal's background
111 (default: ``"light"``).
112
113 `colorscheme`
114 A dictionary mapping token types to (lightbg, darkbg) color names or
115 ``None`` (default: ``None`` = use builtin colorscheme).
116
117 `linenos`
118 Set to ``True`` to have line numbers in the output as well
119 (default: ``False`` = no line numbers).
120 """
121 name = 'IRC'
122 aliases = ['irc', 'IRC']
123 filenames = []
124
125 def __init__(self, **options):
126 Formatter.__init__(self, **options)
127 self.darkbg = get_choice_opt(options, 'bg',
128 ['light', 'dark'], 'light') == 'dark'
129 self.colorscheme = options.get('colorscheme', None) or IRC_COLORS
130 self.linenos = options.get('linenos', False)
131 self._lineno = 0
132
133 def _write_lineno(self, outfile):
134 self._lineno += 1
135 outfile.write("\n%04d: " % self._lineno)
136
137 def _format_unencoded_with_lineno(self, tokensource, outfile):
138 self._write_lineno(outfile)
139
140 for ttype, value in tokensource:
141 if value.endswith("\n"):
142 self._write_lineno(outfile)
143 value = value[:-1]
144 color = self.colorscheme.get(ttype)
145 while color is None:
146 ttype = ttype[:-1]
147 color = self.colorscheme.get(ttype)
148 if color:
149 color = color[self.darkbg]
150 spl = value.split('\n')
151 for line in spl[:-1]:
152 self._write_lineno(outfile)
153 if line:
154 outfile.write(ircformat(color, line[:-1]))
155 if spl[-1]:
156 outfile.write(ircformat(color, spl[-1]))
157 else:
158 outfile.write(value)
159
160 outfile.write("\n")
161
162 def format_unencoded(self, tokensource, outfile):
163 if self.linenos:
164 self._format_unencoded_with_lineno(tokensource, outfile)
165 return
166
167 for ttype, value in tokensource:
168 color = self.colorscheme.get(ttype)
169 while color is None:
170 ttype = ttype[:-1]
171 color = self.colorscheme.get(ttype)
172 if color:
173 color = color[self.darkbg]
174 spl = value.split('\n')
175 for line in spl[:-1]:
176 if line:
177 outfile.write(ircformat(color, line))
178 outfile.write('\n')
179 if spl[-1]:
180 outfile.write(ircformat(color, spl[-1]))
181 else:
182 outfile.write(value)

eric ide

mercurial