Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.11/site-packages/pygments/formatters/irc.py: 64%

Shortcuts on this page

r m x   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

55 statements  

1""" 

2 pygments.formatters.irc 

3 ~~~~~~~~~~~~~~~~~~~~~~~ 

4 

5 Formatter for IRC output 

6 

7 :copyright: Copyright 2006-present by the Pygments team, see AUTHORS. 

8 :license: BSD, see LICENSE for details. 

9""" 

10 

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 

15 

16 

17__all__ = ['IRCFormatter'] 

18 

19 

20#: Map token types to a tuple of color values for light and dark 

21#: backgrounds. 

22IRC_COLORS = { 

23 Token: ('', ''), 

24 

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'), 

43 

44 Generic.Deleted: ('brightred', 'brightred'), 

45 Generic.Inserted: ('green', 'brightgreen'), 

46 Generic.Heading: ('**', '**'), 

47 Generic.Subheading: ('*magenta*', '*brightmagenta*'), 

48 Generic.Error: ('brightred', 'brightred'), 

49 

50 Error: ('_brightred_', '_brightred_'), 

51} 

52 

53 

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} 

75 

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 

95 

96class IRCFormatter(Formatter): 

97 r""" 

98 Format tokens with IRC color sequences 

99 

100 The `get_style_defs()` method doesn't do anything special since there is 

101 no support for common styles. 

102 

103 Options accepted: 

104 

105 `bg` 

106 Set to ``"light"`` or ``"dark"`` depending on the terminal's background 

107 (default: ``"light"``). 

108 

109 `colorscheme` 

110 A dictionary mapping token types to (lightbg, darkbg) color names or 

111 ``None`` (default: ``None`` = use builtin colorscheme). 

112 

113 `linenos` 

114 Set to ``True`` to have line numbers in the output as well 

115 (default: ``False`` = no line numbers). 

116 """ 

117 name = 'IRC' 

118 aliases = ['irc', 'IRC'] 

119 filenames = [] 

120 

121 def __init__(self, **options): 

122 Formatter.__init__(self, **options) 

123 self.darkbg = get_choice_opt(options, 'bg', 

124 ['light', 'dark'], 'light') == 'dark' 

125 self.colorscheme = options.get('colorscheme', None) or IRC_COLORS 

126 self.linenos = options.get('linenos', False) 

127 self._lineno = 0 

128 

129 def _write_lineno(self, outfile): 

130 if self.linenos: 

131 self._lineno += 1 

132 outfile.write("%04d: " % self._lineno) 

133 

134 def format_unencoded(self, tokensource, outfile): 

135 self._write_lineno(outfile) 

136 

137 for ttype, value in tokensource: 

138 color = self.colorscheme.get(ttype) 

139 while color is None: 

140 ttype = ttype[:-1] 

141 color = self.colorscheme.get(ttype) 

142 if color: 

143 color = color[self.darkbg] 

144 spl = value.split('\n') 

145 for line in spl[:-1]: 

146 if line: 

147 outfile.write(ircformat(color, line)) 

148 outfile.write('\n') 

149 self._write_lineno(outfile) 

150 if spl[-1]: 

151 outfile.write(ircformat(color, spl[-1])) 

152 else: 

153 outfile.write(value)