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

changeset 6942
2602857055c5
parent 5713
6762afd9f963
child 7547
21b0534faebc
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2 """
3 pygments.formatters.irc
4 ~~~~~~~~~~~~~~~~~~~~~~~
5
6 Formatter for IRC output
7
8 :copyright: Copyright 2006-2017 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: ('lightgray', 'darkgray'),
29 Comment: ('lightgray', 'darkgray'),
30 Comment.Preproc: ('teal', 'turquoise'),
31 Keyword: ('darkblue', 'blue'),
32 Keyword.Type: ('teal', 'turquoise'),
33 Operator.Word: ('purple', 'fuchsia'),
34 Name.Builtin: ('teal', 'turquoise'),
35 Name.Function: ('darkgreen', 'green'),
36 Name.Namespace: ('_teal_', '_turquoise_'),
37 Name.Class: ('_darkgreen_', '_green_'),
38 Name.Exception: ('teal', 'turquoise'),
39 Name.Decorator: ('darkgray', 'lightgray'),
40 Name.Variable: ('darkred', 'red'),
41 Name.Constant: ('darkred', 'red'),
42 Name.Attribute: ('teal', 'turquoise'),
43 Name.Tag: ('blue', 'blue'),
44 String: ('brown', 'brown'),
45 Number: ('darkblue', 'blue'),
46
47 Generic.Deleted: ('red', 'red'),
48 Generic.Inserted: ('darkgreen', 'green'),
49 Generic.Heading: ('**', '**'),
50 Generic.Subheading: ('*purple*', '*fuchsia*'),
51 Generic.Error: ('red', 'red'),
52
53 Error: ('_red_', '_red_'),
54 }
55
56
57 IRC_COLOR_MAP = {
58 'white': 0,
59 'black': 1,
60 'darkblue': 2,
61 'green': 3,
62 'red': 4,
63 'brown': 5,
64 'purple': 6,
65 'orange': 7,
66 'darkgreen': 7, #compat w/ ansi
67 'yellow': 8,
68 'lightgreen': 9,
69 'turquoise': 9, # compat w/ ansi
70 'teal': 10,
71 'lightblue': 11,
72 'darkred': 11, # compat w/ ansi
73 'blue': 12,
74 'fuchsia': 13,
75 'darkgray': 14,
76 'lightgray': 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