summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMatthias Bussonnier <bussonniermatthias@gmail.com>2016-02-12 14:30:03 -0800
committerMatthias Bussonnier <bussonniermatthias@gmail.com>2016-02-12 14:30:03 -0800
commit9834d006ab8c196189a788ead199e0e1542da7b7 (patch)
tree1c0314831b2f0449c75a2237fea5ec6108fe8d72
parentbbe2d4e697e326ad231af247c235431acf710efc (diff)
downloadpygments-9834d006ab8c196189a788ead199e0e1542da7b7.tar.gz
Update to leave compatibility for other formatters.
-rw-r--r--doc/docs/styles.rst48
-rw-r--r--pygments/formatters/html.py21
-rw-r--r--pygments/formatters/terminal256.py22
-rw-r--r--pygments/style.py50
4 files changed, 96 insertions, 45 deletions
diff --git a/doc/docs/styles.rst b/doc/docs/styles.rst
index 766cf866..0076d062 100644
--- a/doc/docs/styles.rst
+++ b/doc/docs/styles.rst
@@ -152,16 +152,19 @@ Terminal Styles
.. versionadded:: 2.2
-Custom styles used with `Terminal256` formatter can also defines foreground
-colors using ansi-color. to do so use the `#ansigreen`, `#ansired` or any other
-colors defined in ``pygments.style.ansilist``. Foreground ANSI colors will be
-mapped to the corresponding `escape codes 30 to 37
+Custom styles used with `Terminal256` formatter can also defines colors using
+ansi-color. To do so use the `#ansigreen`, `#ansired` or any other colors
+defined in ``pygments.style.ansilist``. Foreground ANSI colors will be mapped
+to the corresponding `escape codes 30 to 37
<https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>`_ thus respecting any
-custom color mapping and themes provided by many terminal emulators.
+custom color mapping and themes provided by many terminal emulators. Light
+variant are treated for foreground color with and extra bold flag.
+`bg:#ansi<color>` will also be respected, except the light variant will be the
+same shade as their light variant.
See following example where the color of the string `"hello world"` is governed
-by the escape sequence `\x1b34;01m` (Ansi Blue) instead of an extended
-foreground color.
+by the escape sequence `\x1b34;01m` (Ansi Blue, Bold, `41` beeing red background)
+instead of an extended foreground & background color.
.. sourcecode:: pycon
@@ -172,19 +175,30 @@ foreground color.
>>> from pygments.formatters import Terminal256Formatter
>>> class MyStyle(Style):
- >>>
- >>> styles = {
- >>> Token.String: '#ansiblue',
- >>> }
+ styles = {
+ Token.String: '#ansiblue bg:#ansired',
+ }
>>> code = 'print("Hello World")'
>>> result = highlight(code, Python3Lexer(), Terminal256Formatter(style=MyStyle))
>>> print(result.encode())
- b'print(\x1b[34;01m"\x1b[39m\x1b[34;01mHello World\x1b[39m\x1b[34;01m"\x1b[39m)\n'
+ b'print(\x1b[34;41;01m"\x1b[39;49;00m\x1b[34;41;01mHello World\x1b[39;49;00m\x1b[34;41;01m"\x1b[39;49;00m)\n'
-Style that use `#ansi*` foreground colors might not correctly work with
+Style that use `#ansi*` colors might not correctly work with
formatters others than ``Terminal256``. `HtmlFormatter` is capable of handling
-some `#ansi*` code and will map to the corresponding HTML/CSS color. That is to
-say, `#ansiblue` will be converted to `color:blue` , `#ansired` to `color:red`.
-The behavior is undefined for argument like `#ansireset`, `#ansiunderline`,
-`#ansibold`... etc.
+some `#ansi*` code and will map to a fixed HTML/CSS color. For example,
+`#ansiblue` will be converted to `color:#0000ff` , `#ansired` to `color:#ff0000`.
+
+By definition of Ansi color the following color are considered "light" colors,
+and will be rendered by most terminal as bold:
+
+ - "darkgray", "red", "green", "yellow", "blue", "fuchsia", "turquoise",
+ "white"
+
+
+The following are considered "dark" color and will be rendered as non-bold:
+
+ - "black", "darkred", "darkgreen", "brown", "darkblue", "purple", "teal",
+ "lightgray"
+
+Exact behavior might depends on the terminal emulator you are using, and its settings.
diff --git a/pygments/formatters/html.py b/pygments/formatters/html.py
index 38e49f15..b03a4bd5 100644
--- a/pygments/formatters/html.py
+++ b/pygments/formatters/html.py
@@ -20,23 +20,6 @@ from pygments.token import Token, Text, STANDARD_TYPES
from pygments.util import get_bool_opt, get_int_opt, get_list_opt, \
StringIO, string_types, iteritems
-
-_deansify_map = {
- '#darkyellow':'#brown',
- '#darkteal': '#turquoise',
- '#fusia': '#fushia'
-}
-
-
-
-def _deansify(color):
- if color.startswith('#ansi'):
- color = color[5:]
- else:
- color = '#%s'% color
-
- return _deansify_map.get(color, color)
-
try:
import ctags
except ImportError:
@@ -461,7 +444,7 @@ class HtmlFormatter(Formatter):
name = self._get_css_class(ttype)
style = ''
if ndef['color']:
- style += 'color: %s; ' % _deansify(ndef['color'])
+ style += 'color: #%s; ' % ndef['color']
if ndef['bold']:
style += 'font-weight: bold; '
if ndef['italic']:
@@ -469,7 +452,7 @@ class HtmlFormatter(Formatter):
if ndef['underline']:
style += 'text-decoration: underline; '
if ndef['bgcolor']:
- style += 'background-color: %s; ' % _deansify(ndef['bgcolor'])
+ style += 'background-color: #%s; ' % ndef['bgcolor']
if ndef['border']:
style += 'border: 1px solid #%s; ' % ndef['border']
if style:
diff --git a/pygments/formatters/terminal256.py b/pygments/formatters/terminal256.py
index 913536c4..1aa19f25 100644
--- a/pygments/formatters/terminal256.py
+++ b/pygments/formatters/terminal256.py
@@ -50,11 +50,20 @@ class EscapeSequence:
attrs = []
if self.fg is not None:
if self.fg in ansilist:
- attrs.append(codes[self.fg[5:]][2:-1])
+ esc = codes[self.fg[5:]]
+ if ';01m' in esc:
+ self.bold = True
+ # extract fg color code.
+ attrs.append(esc[2:4])
else :
attrs.extend(("38", "5", "%i" % self.fg))
if self.bg is not None:
- attrs.extend(("48", "5", "%i" % self.bg))
+ if self.bg in ansilist:
+ esc = codes[self.bg[5:]]
+ # extract fg color code, add 10 for bg.
+ attrs.append(str(int(esc[2:4])+10))
+ else :
+ attrs.extend(("48", "5", "%i" % self.bg))
if self.bold:
attrs.append("01")
if self.underline:
@@ -201,9 +210,14 @@ class Terminal256Formatter(Formatter):
def _setup_styles(self):
for ttype, ndef in self.style:
escape = EscapeSequence()
- if ndef['color']:
+ # get foreground from ansicolor if set
+ if ndef['ansicolor']:
+ escape.fg = self._color_index(ndef['ansicolor'])
+ elif ndef['color']:
escape.fg = self._color_index(ndef['color'])
- if ndef['bgcolor']:
+ if ndef['bgansicolor']:
+ escape.bg = self._color_index(ndef['bgansicolor'])
+ elif ndef['bgcolor']:
escape.bg = self._color_index(ndef['bgcolor'])
if self.usebold and ndef['bold']:
escape.bold = True
diff --git a/pygments/style.py b/pygments/style.py
index 7a272b53..bc318354 100644
--- a/pygments/style.py
+++ b/pygments/style.py
@@ -11,11 +11,31 @@
from pygments.token import Token, STANDARD_TYPES
from pygments.util import add_metaclass
-from pygments.console import codes
-ansilist = ['#ansi'+x for x in codes.keys() if x]
+_ansimap = {
+ ##
+ '#ansiblack': '000000',
+ '#ansidarkred': '7f0000',
+ '#ansidarkgreen': '007f00',
+ '#ansibrown': '7f7fe0',
+ '#ansidarkblue': '00007f',
+ '#ansipurple': '7f007f',
+ '#ansiteal': '007f7f',
+ '#ansilightgray': 'e5e5e5',
+ ### normal
+ '#ansidarkgray': '555555',
+ '#ansired': 'ff0000',
+ '#ansigreen': '00ff00',
+ '#ansiyellow': 'ffff00',
+ '#ansiblue': '0000ff',
+ '#ansifuchsia': 'ff00ff',
+ '#ansiturquoise': '00ffff',
+ '#ansiwhite': 'ffffff',
+ }
+ansilist = list(_ansimap.keys())
+
class StyleMeta(type):
def __new__(mcs, name, bases, dct):
@@ -35,7 +55,13 @@ class StyleMeta(type):
return col[0]*2 + col[1]*2 + col[2]*2
elif text == '':
return ''
- assert False, "wrong color format %r" % text
+ didyoumean = ''
+ if 'ansi' in text:
+ import difflib
+ possibility = difflib.get_close_matches(text, ansilist, 1)
+ if possibility:
+ didyoumean = '. Did you mean {} ?'.format(possibility[0])
+ assert False, "wrong color format %r%s" % (text, didyoumean)
_styles = obj._styles = {}
@@ -84,16 +110,30 @@ class StyleMeta(type):
def style_for_token(cls, token):
t = cls._styles[token]
+ ansicolor = None
+ color = t[0]
+ if color.startswith('#ansi'):
+ ansicolor = color
+ color = _ansimap[color]
+ bgansicolor = None
+ bgcolor = t[4]
+ if bgcolor.startswith('#ansi'):
+ bgansicolor = bgcolor
+ bgcolor = _ansimap[bgcolor]
+
return {
- 'color': t[0] or None,
+ 'color': color or None,
'bold': bool(t[1]),
'italic': bool(t[2]),
'underline': bool(t[3]),
- 'bgcolor': t[4] or None,
+ 'bgcolor': bgcolor or None,
'border': t[5] or None,
'roman': bool(t[6]) or None,
'sans': bool(t[7]) or None,
'mono': bool(t[8]) or None,
+ 'ansicolor': ansicolor,
+ 'bgansicolor': bgansicolor,
+
}
def list_styles(cls):