Coverage for /home/martinb/.local/share/virtualenvs/camcops/lib/python3.6/site-packages/pygments/__init__.py : 35%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
1# -*- coding: utf-8 -*-
2"""
3 Pygments
4 ~~~~~~~~
6 Pygments is a syntax highlighting package written in Python.
8 It is a generic syntax highlighter for general use in all kinds of software
9 such as forum systems, wikis or other applications that need to prettify
10 source code. Highlights are:
12 * a wide range of common languages and markup formats is supported
13 * special attention is paid to details, increasing quality by a fair amount
14 * support for new languages and formats are added easily
15 * a number of output formats, presently HTML, LaTeX, RTF, SVG, all image
16 formats that PIL supports, and ANSI sequences
17 * it is usable as a command-line tool and as a library
18 * ... and it highlights even Brainfuck!
20 The `Pygments master branch`_ is installable with ``easy_install Pygments==dev``.
22 .. _Pygments master branch:
23 https://github.com/pygments/pygments/archive/master.zip#egg=Pygments-dev
25 :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
26 :license: BSD, see LICENSE for details.
27"""
28import sys
29from io import StringIO, BytesIO
31__version__ = '2.7.4'
32__docformat__ = 'restructuredtext'
34__all__ = ['lex', 'format', 'highlight']
37def lex(code, lexer):
38 """
39 Lex ``code`` with ``lexer`` and return an iterable of tokens.
40 """
41 try:
42 return lexer.get_tokens(code)
43 except TypeError as err:
44 if (isinstance(err.args[0], str) and
45 ('unbound method get_tokens' in err.args[0] or
46 'missing 1 required positional argument' in err.args[0])):
47 raise TypeError('lex() argument must be a lexer instance, '
48 'not a class')
49 raise
52def format(tokens, formatter, outfile=None): # pylint: disable=redefined-builtin
53 """
54 Format a tokenlist ``tokens`` with the formatter ``formatter``.
56 If ``outfile`` is given and a valid file object (an object
57 with a ``write`` method), the result will be written to it, otherwise
58 it is returned as a string.
59 """
60 try:
61 if not outfile:
62 realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
63 formatter.format(tokens, realoutfile)
64 return realoutfile.getvalue()
65 else:
66 formatter.format(tokens, outfile)
67 except TypeError as err:
68 if (isinstance(err.args[0], str) and
69 ('unbound method format' in err.args[0] or
70 'missing 1 required positional argument' in err.args[0])):
71 raise TypeError('format() argument must be a formatter instance, '
72 'not a class')
73 raise
76def highlight(code, lexer, formatter, outfile=None):
77 """
78 Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``.
80 If ``outfile`` is given and a valid file object (an object
81 with a ``write`` method), the result will be written to it, otherwise
82 it is returned as a string.
83 """
84 return format(lex(code, lexer), formatter, outfile)