Debian Codegraph @

doc/docs/styles.rst

raw
1 .. -*- mode: rst -*-
2
3 ======
4 Styles
5 ======
6
7 Pygments comes with :doc:`some builtin styles </styles/>` that work for both the
8 HTML and LaTeX formatter.
9
10 The builtin styles can be looked up with the `get_style_by_name` function:
11
12 .. sourcecode:: pycon
13
14 >>> from pygments.styles import get_style_by_name
15 >>> get_style_by_name('colorful')
16 <class 'pygments.styles.colorful.ColorfulStyle'>
17
18 You can pass a instance of a `Style` class to a formatter as the `style`
19 option in form of a string:
20
21 .. sourcecode:: pycon
22
23 >>> from pygments.styles import get_style_by_name
24 >>> from pygments.formatters import HtmlFormatter
25 >>> HtmlFormatter(style='colorful').style
26 <class 'pygments.styles.colorful.ColorfulStyle'>
27
28 Or you can also import your own style (which must be a subclass of
29 `pygments.style.Style`) and pass it to the formatter:
30
31 .. sourcecode:: pycon
32
33 >>> from yourapp.yourmodule import YourStyle
34 >>> from pygments.formatters import HtmlFormatter
35 >>> HtmlFormatter(style=YourStyle).style
36 <class 'yourapp.yourmodule.YourStyle'>
37
38
39 Creating Own Styles
40 ===================
41
42 See :doc:`styledevelopment`.
43
44
45 Builtin Styles
46 ==============
47
48 Pygments ships some builtin styles which are maintained by the Pygments team.
49
50 To get a list of known styles you can use this snippet:
51
52 .. sourcecode:: pycon
53
54 >>> from pygments.styles import STYLE_MAP
55 >>> STYLE_MAP.keys()
56 ['default', 'emacs', 'friendly', 'colorful']
57
58
59 Getting a list of available styles
60 ==================================
61
62 .. versionadded:: 0.6
63
64 Because it could be that a plugin registered a style, there is
65 a way to iterate over all styles:
66
67 .. sourcecode:: pycon
68
69 >>> from pygments.styles import get_all_styles
70 >>> styles = list(get_all_styles())
71
72
73 .. _AnsiTerminalStyle:
74
75 Terminal Styles
76 ===============
77
78 .. versionadded:: 2.2
79
80 Custom styles used with the 256-color terminal formatter can also map colors to
81 use the 8 default ANSI colors. To do so, use ``ansigreen``, ``ansibrightred`` or
82 any other colors defined in :data:`pygments.style.ansicolors`. Foreground ANSI
83 colors will be mapped to the corresponding `escape codes 30 to 37
84 <https://en.wikipedia.org/wiki/ANSI_escape_code#Colors>`_ thus respecting any
85 custom color mapping and themes provided by many terminal emulators. Light
86 variants are treated as foreground color with and an added bold flag.
87 ``bg:ansi<color>`` will also be respected, except the light variant will be the
88 same shade as their dark variant.
89
90 See the following example where the color of the string ``"hello world"`` is
91 governed by the escape sequence ``\x1b[34;01m`` (Ansi bright blue, Bold, 41 being red
92 background) instead of an extended foreground & background color.
93
94 .. sourcecode:: pycon
95
96 >>> from pygments import highlight
97 >>> from pygments.style import Style
98 >>> from pygments.token import Token
99 >>> from pygments.lexers import Python3Lexer
100 >>> from pygments.formatters import Terminal256Formatter
101
102 >>> class MyStyle(Style):
103 styles = {
104 Token.String: 'ansibrightblue bg:ansibrightred',
105 }
106
107 >>> code = 'print("Hello World")'
108 >>> result = highlight(code, Python3Lexer(), Terminal256Formatter(style=MyStyle))
109 >>> print(result.encode())
110 b'\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'
111
112 Colors specified using ``ansi*`` are converted to a default set of RGB colors
113 when used with formatters other than the terminal-256 formatter.
114
115 By definition of ANSI, the following colors are considered "light" colors, and
116 will be rendered by most terminals as bold:
117
118 - "brightblack" (darkgrey), "brightred", "brightgreen", "brightyellow", "brightblue",
119 "brightmagenta", "brightcyan", "white"
120
121 The following are considered "dark" colors and will be rendered as non-bold:
122
123 - "black", "red", "green", "yellow", "blue", "magenta", "cyan",
124 "gray"
125
126 Exact behavior might depends on the terminal emulator you are using, and its
127 settings.
128
129 .. _new-ansi-color-names:
130
131 .. versionchanged:: 2.4
132
133 The definition of the ANSI color names has changed.
134 New names are easier to understand and align to the colors used in other projects.
135
136 ===================== ====================
137 New names Pygments up to 2.3
138 ===================== ====================
139 ``ansiblack`` ``#ansiblack``
140 ``ansired`` ``#ansidarkred``
141 ``ansigreen`` ``#ansidarkgreen``
142 ``ansiyellow`` ``#ansibrown``
143 ``ansiblue`` ``#ansidarkblue``
144 ``ansimagenta`` ``#ansipurple``
145 ``ansicyan`` ``#ansiteal``
146 ``ansigray`` ``#ansilightgray``
147 ``ansibrightblack`` ``#ansidarkgray``
148 ``ansibrightred`` ``#ansired``
149 ``ansibrightgreen`` ``#ansigreen``
150 ``ansibrightyellow`` ``#ansiyellow``
151 ``ansibrightblue`` ``#ansiblue``
152 ``ansibrightmagenta`` ``#ansifuchsia``
153 ``ansibrightcyan`` ``#ansiturquoise``
154 ``ansiwhite`` ``#ansiwhite``
155 ===================== ====================
156
157 Old ANSI color names are deprecated but will still work.