comparison MoinMoin/support/pygments/formatters/terminal.py @ 6097:815981fad7fd

upgrade pygments from 1.6 to 2.1.3
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Mon, 05 Sep 2016 23:55:33 +0200
parents b48a69886ca4
children
comparison
equal deleted inserted replaced
6096:86a41c2bedec 6097:815981fad7fd
3 pygments.formatters.terminal 3 pygments.formatters.terminal
4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 4 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
5 5
6 Formatter for terminal output with ANSI sequences. 6 Formatter for terminal output with ANSI sequences.
7 7
8 :copyright: Copyright 2006-2013 by the Pygments team, see AUTHORS. 8 :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
9 :license: BSD, see LICENSE for details. 9 :license: BSD, see LICENSE for details.
10 """ 10 """
11 11
12 import sys 12 import sys
13 13
14 from pygments.formatter import Formatter 14 from pygments.formatter import Formatter
15 from pygments.token import Keyword, Name, Comment, String, Error, \ 15 from pygments.token import Keyword, Name, Comment, String, Error, \
16 Number, Operator, Generic, Token, Whitespace 16 Number, Operator, Generic, Token, Whitespace
17 from pygments.console import ansiformat 17 from pygments.console import ansiformat
18 from pygments.util import get_choice_opt 18 from pygments.util import get_choice_opt
19 19
20 20
21 __all__ = ['TerminalFormatter'] 21 __all__ = ['TerminalFormatter']
47 47
48 Generic.Deleted: ('red', 'red'), 48 Generic.Deleted: ('red', 'red'),
49 Generic.Inserted: ('darkgreen', 'green'), 49 Generic.Inserted: ('darkgreen', 'green'),
50 Generic.Heading: ('**', '**'), 50 Generic.Heading: ('**', '**'),
51 Generic.Subheading: ('*purple*', '*fuchsia*'), 51 Generic.Subheading: ('*purple*', '*fuchsia*'),
52 Generic.Prompt: ('**', '**'),
52 Generic.Error: ('red', 'red'), 53 Generic.Error: ('red', 'red'),
53 54
54 Error: ('_red_', '_red_'), 55 Error: ('_red_', '_red_'),
55 } 56 }
56 57
71 (default: ``"light"``). 72 (default: ``"light"``).
72 73
73 `colorscheme` 74 `colorscheme`
74 A dictionary mapping token types to (lightbg, darkbg) color names or 75 A dictionary mapping token types to (lightbg, darkbg) color names or
75 ``None`` (default: ``None`` = use builtin colorscheme). 76 ``None`` (default: ``None`` = use builtin colorscheme).
77
78 `linenos`
79 Set to ``True`` to have line numbers on the terminal output as well
80 (default: ``False`` = no line numbers).
76 """ 81 """
77 name = 'Terminal' 82 name = 'Terminal'
78 aliases = ['terminal', 'console'] 83 aliases = ['terminal', 'console']
79 filenames = [] 84 filenames = []
80 85
81 def __init__(self, **options): 86 def __init__(self, **options):
82 Formatter.__init__(self, **options) 87 Formatter.__init__(self, **options)
83 self.darkbg = get_choice_opt(options, 'bg', 88 self.darkbg = get_choice_opt(options, 'bg',
84 ['light', 'dark'], 'light') == 'dark' 89 ['light', 'dark'], 'light') == 'dark'
85 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS 90 self.colorscheme = options.get('colorscheme', None) or TERMINAL_COLORS
91 self.linenos = options.get('linenos', False)
92 self._lineno = 0
86 93
87 def format(self, tokensource, outfile): 94 def format(self, tokensource, outfile):
88 # hack: if the output is a terminal and has an encoding set, 95 # hack: if the output is a terminal and has an encoding set,
89 # use that to avoid unicode encode problems 96 # use that to avoid unicode encode problems
90 if not self.encoding and hasattr(outfile, "encoding") and \ 97 if not self.encoding and hasattr(outfile, "encoding") and \
91 hasattr(outfile, "isatty") and outfile.isatty() and \ 98 hasattr(outfile, "isatty") and outfile.isatty() and \
92 sys.version_info < (3,): 99 sys.version_info < (3,):
93 self.encoding = outfile.encoding 100 self.encoding = outfile.encoding
94 return Formatter.format(self, tokensource, outfile) 101 return Formatter.format(self, tokensource, outfile)
95 102
103 def _write_lineno(self, outfile):
104 self._lineno += 1
105 outfile.write("%s%04d: " % (self._lineno != 1 and '\n' or '', self._lineno))
106
107 def _get_color(self, ttype):
108 # self.colorscheme is a dict containing usually generic types, so we
109 # have to walk the tree of dots. The base Token type must be a key,
110 # even if it's empty string, as in the default above.
111 colors = self.colorscheme.get(ttype)
112 while colors is None:
113 ttype = ttype.parent
114 colors = self.colorscheme.get(ttype)
115 return colors[self.darkbg]
116
96 def format_unencoded(self, tokensource, outfile): 117 def format_unencoded(self, tokensource, outfile):
118 if self.linenos:
119 self._write_lineno(outfile)
120
97 for ttype, value in tokensource: 121 for ttype, value in tokensource:
98 color = self.colorscheme.get(ttype) 122 color = self._get_color(ttype)
99 while color is None: 123
100 ttype = ttype[:-1] 124 for line in value.splitlines(True):
101 color = self.colorscheme.get(ttype) 125 if color:
102 if color: 126 outfile.write(ansiformat(color, line.rstrip('\n')))
103 color = color[self.darkbg] 127 else:
104 spl = value.split('\n') 128 outfile.write(line.rstrip('\n'))
105 for line in spl[:-1]: 129 if line.endswith('\n'):
106 if line: 130 if self.linenos:
107 outfile.write(ansiformat(color, line)) 131 self._write_lineno(outfile)
108 outfile.write('\n') 132 else:
109 if spl[-1]: 133 outfile.write('\n')
110 outfile.write(ansiformat(color, spl[-1])) 134
111 else: 135 if self.linenos:
112 outfile.write(value) 136 outfile.write("\n")