| 1 |
.. -*- mode: rst -*- |
| 2 |
|
| 3 |
====================== |
| 4 |
Command Line Interface |
| 5 |
====================== |
| 6 |
|
| 7 |
You can use Pygments from the shell, provided you installed the |
| 8 |
:program:`pygmentize` script:: |
| 9 |
|
| 10 |
$ pygmentize test.py |
| 11 |
print "Hello World" |
| 12 |
|
| 13 |
will print the file test.py to standard output, using the Python lexer |
| 14 |
(inferred from the file name extension) and the terminal formatter (because |
| 15 |
you didn't give an explicit formatter name). |
| 16 |
|
| 17 |
.. note:: |
| 18 |
|
| 19 |
If you are on Windows, an extra tool may be needed for colored output to |
| 20 |
work in the terminal. You can make sure Pygments is installed with |
| 21 |
Windows console coloring support by installing Pygments with the ``windows-terminal`` |
| 22 |
extra (e.g., ``pip install pygments[windows-terminal]``). |
| 23 |
|
| 24 |
:program:`pygmentize` attempts to |
| 25 |
detect the maximum number of colors that the terminal supports. The difference |
| 26 |
between color formatters for 16 and 256 colors is immense, but there is a less |
| 27 |
noticeable difference between color formatters for 256 and 16 million colors. |
| 28 |
|
| 29 |
Here's the process of how it detects the maxiumum number of colors |
| 30 |
supported by your terminal. If the ``COLORTERM`` environment variable is set to |
| 31 |
either ``truecolor`` or ``24bit``, it will use a 16 million color representation |
| 32 |
(like ``terminal16m``). Next, it will try to find ``256`` is anywhere in the |
| 33 |
environment variable ``TERM``, which it will use a 256-color representaion |
| 34 |
(such as ``terminal256``). When neither of those are found, it falls back to a |
| 35 |
the 16 color representation (like ``terminal``). |
| 36 |
|
| 37 |
If you want HTML output:: |
| 38 |
|
| 39 |
$ pygmentize -f html -l python -o test.html test.py |
| 40 |
|
| 41 |
As you can see, the -l option explicitly selects a lexer. As seen above, if you |
| 42 |
give an input file name and it has an extension that Pygments recognizes, you can |
| 43 |
omit this option. |
| 44 |
|
| 45 |
The ``-o`` option gives an output file name. If it is not given, output is |
| 46 |
written to stdout. |
| 47 |
|
| 48 |
The ``-f`` option selects a formatter (as with ``-l``, it can also be omitted |
| 49 |
if an output file name is given and has a supported extension). |
| 50 |
If no output file name is given and ``-f`` is omitted, the |
| 51 |
:class:`.TerminalFormatter` is used. |
| 52 |
|
| 53 |
The above command could therefore also be given as:: |
| 54 |
|
| 55 |
$ pygmentize -o test.html test.py |
| 56 |
|
| 57 |
To create a full HTML document, including line numbers and stylesheet (using the |
| 58 |
"emacs" style), highlighting the Python file ``test.py`` to ``test.html``:: |
| 59 |
|
| 60 |
$ pygmentize -O full,style=emacs,linenos=1 -o test.html test.py |
| 61 |
|
| 62 |
|
| 63 |
Options and filters |
| 64 |
------------------- |
| 65 |
|
| 66 |
Lexer and formatter options can be given using the ``-O`` option:: |
| 67 |
|
| 68 |
$ pygmentize -f html -O style=colorful,linenos=1 -l python test.py |
| 69 |
|
| 70 |
Be sure to enclose the option string in quotes if it contains any special shell |
| 71 |
characters, such as spaces or expansion wildcards like ``*``. If an option |
| 72 |
expects a list value, separate the list entries with spaces (you'll have to |
| 73 |
quote the option value in this case too, so that the shell doesn't split it). |
| 74 |
|
| 75 |
Since the ``-O`` option argument is split at commas and expects the split values |
| 76 |
to be of the form ``name=value``, you can't give an option value that contains |
| 77 |
commas or equals signs. Therefore, an option ``-P`` is provided (as of Pygments |
| 78 |
0.9) that works like ``-O`` but can only pass one option per ``-P``. Its value |
| 79 |
can then contain all characters:: |
| 80 |
|
| 81 |
$ pygmentize -P "heading=Pygments, the Python highlighter" ... |
| 82 |
|
| 83 |
Filters are added to the token stream using the ``-F`` option:: |
| 84 |
|
| 85 |
$ pygmentize -f html -l pascal -F keywordcase:case=upper main.pas |
| 86 |
|
| 87 |
As you see, options for the filter are given after a colon. As for ``-O``, the |
| 88 |
filter name and options must be one shell word, so there may not be any spaces |
| 89 |
around the colon. |
| 90 |
|
| 91 |
|
| 92 |
Generating styles |
| 93 |
----------------- |
| 94 |
|
| 95 |
Formatters normally don't output full style information. For example, the HTML |
| 96 |
formatter by default only outputs ``<span>`` tags with ``class`` attributes. |
| 97 |
Therefore, there's a special ``-S`` option for generating style definitions. |
| 98 |
Usage is as follows:: |
| 99 |
|
| 100 |
$ pygmentize -f html -S colorful -a .syntax |
| 101 |
|
| 102 |
generates a CSS style sheet (because you selected the HTML formatter) for |
| 103 |
the "colorful" style prepending a ".syntax" selector to all style rules. |
| 104 |
|
| 105 |
For an explanation what ``-a`` means for :doc:`a particular formatter |
| 106 |
<formatters>`, look for the `arg` argument for the formatter's |
| 107 |
:meth:`.get_style_defs() <pygments.formatter.Formatter.get_style_defs()>` method. |
| 108 |
|
| 109 |
|
| 110 |
Getting lexer names |
| 111 |
------------------- |
| 112 |
|
| 113 |
.. versionadded:: 1.0 |
| 114 |
|
| 115 |
The ``-N`` option guesses a lexer name for a given filename, so that :: |
| 116 |
|
| 117 |
$ pygmentize -N setup.py |
| 118 |
|
| 119 |
will print out ``python``. It won't highlight anything yet. If no specific |
| 120 |
lexer is known for that filename, ``text`` is printed. |
| 121 |
|
| 122 |
Additionally, there is the ``-C`` option, which is just like like ``-N``, except |
| 123 |
that it prints out a lexer name based solely on a given content from standard |
| 124 |
input. |
| 125 |
|
| 126 |
|
| 127 |
Guessing the lexer from the file contents |
| 128 |
----------------------------------------- |
| 129 |
|
| 130 |
The ``-g`` option will try to guess the correct lexer from the file contents, |
| 131 |
or pass through as plain text if nothing can be guessed. This option also looks |
| 132 |
for Vim modelines in the text, and for *some* languages, shebangs. Usage is as |
| 133 |
follows:: |
| 134 |
|
| 135 |
$ pygmentize -g setup.py |
| 136 |
|
| 137 |
Note though, that this option is not very relaiable, and probably should be |
| 138 |
used only if Pygments is not able to guess the correct lexer from the file's |
| 139 |
extension. |
| 140 |
|
| 141 |
|
| 142 |
Highlighting stdin until EOF |
| 143 |
---------------------------- |
| 144 |
|
| 145 |
The ``-s`` option processes lines one at a time until EOF, rather than waiting |
| 146 |
to process the entire file. This only works for stdin, only for lexers with no |
| 147 |
line-spanning constructs, and is intended for streaming input such as you get |
| 148 |
from `tail -f`. Usage is as follows:: |
| 149 |
|
| 150 |
$ tail -f sql.log | pygmentize -s -l sql |
| 151 |
|
| 152 |
|
| 153 |
Custom Lexers and Formatters |
| 154 |
---------------------------- |
| 155 |
|
| 156 |
.. versionadded:: 2.2 |
| 157 |
|
| 158 |
The ``-x`` flag enables custom lexers and formatters to be loaded |
| 159 |
from files relative to the current directory. Create a file with a class named |
| 160 |
CustomLexer or CustomFormatter, then specify it on the command line:: |
| 161 |
|
| 162 |
$ pygmentize -l your_lexer.py -f your_formatter.py -x |
| 163 |
|
| 164 |
You can also specify the name of your class with a colon:: |
| 165 |
|
| 166 |
$ pygmentize -l your_lexer.py:SomeLexer -x |
| 167 |
|
| 168 |
For more information, see :doc:`the Pygments documentation on Lexer development |
| 169 |
<lexerdevelopment>`. |
| 170 |
|
| 171 |
|
| 172 |
Getting help |
| 173 |
------------ |
| 174 |
|
| 175 |
The ``-L`` option lists lexers, formatters, along with their short |
| 176 |
names and supported file name extensions, styles and filters. If you want to see |
| 177 |
only one category, give it as an argument:: |
| 178 |
|
| 179 |
$ pygmentize -L filters |
| 180 |
|
| 181 |
will list only all installed filters. |
| 182 |
|
| 183 |
.. versionadded:: 2.11 |
| 184 |
|
| 185 |
The ``--json`` option can be used in conjunction with the ``-L`` option to |
| 186 |
output its contents as JSON. Thus, to print all the installed styles and their |
| 187 |
description in JSON, use the command:: |
| 188 |
|
| 189 |
$ pygmentize -L styles --json |
| 190 |
|
| 191 |
The ``-H`` option will give you detailed information (the same that can be found |
| 192 |
in this documentation) about a lexer, formatter or filter. Usage is as follows:: |
| 193 |
|
| 194 |
$ pygmentize -H formatter html |
| 195 |
|
| 196 |
will print the help for the HTML formatter, while :: |
| 197 |
|
| 198 |
$ pygmentize -H lexer python |
| 199 |
|
| 200 |
will print the help for the Python lexer, etc. |
| 201 |
|
| 202 |
|
| 203 |
A note on encodings |
| 204 |
------------------- |
| 205 |
|
| 206 |
.. versionadded:: 0.9 |
| 207 |
|
| 208 |
Pygments tries to be smart regarding encodings in the formatting process: |
| 209 |
|
| 210 |
* If you give an ``encoding`` option, it will be used as the input and |
| 211 |
output encoding. |
| 212 |
|
| 213 |
* If you give an ``outencoding`` option, it will override ``encoding`` |
| 214 |
as the output encoding. |
| 215 |
|
| 216 |
* If you give an ``inencoding`` option, it will override ``encoding`` |
| 217 |
as the input encoding. |
| 218 |
|
| 219 |
* If you don't give an encoding and have given an output file, the default |
| 220 |
encoding for lexer and formatter is the terminal encoding or the default |
| 221 |
locale encoding of the system. As a last resort, ``latin1`` is used (which |
| 222 |
will pass through all non-ASCII characters). |
| 223 |
|
| 224 |
* If you don't give an encoding and haven't given an output file (that means |
| 225 |
output is written to the console), the default encoding for lexer and |
| 226 |
formatter is the terminal encoding (``sys.stdout.encoding``). |