eric6/ThirdParty/Pygments/pygments/__init__.py

changeset 6942
2602857055c5
parent 6651
e8f3b5568b21
child 7547
21b0534faebc
equal deleted inserted replaced
6941:f99d60d6b59b 6942:2602857055c5
1 # -*- coding: utf-8 -*-
2 """
3 Pygments
4 ~~~~~~~~
5
6 Pygments is a syntax highlighting package written in Python.
7
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:
11
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!
19
20 The `Pygments tip`_ is installable with ``easy_install Pygments==dev``.
21
22 .. _Pygments tip:
23 http://bitbucket.org/birkenfeld/pygments-main/get/tip.zip#egg=Pygments-dev
24
25 :copyright: Copyright 2006-2017 by the Pygments team, see AUTHORS.
26 :license: BSD, see LICENSE for details.
27 """
28 import sys
29
30 from pygments.util import StringIO, BytesIO
31
32 __version__ = '2.3.1'
33 __docformat__ = 'restructuredtext'
34
35 __all__ = ['lex', 'format', 'highlight']
36
37
38 def lex(code, lexer):
39 """
40 Lex ``code`` with ``lexer`` and return an iterable of tokens.
41 """
42 try:
43 return lexer.get_tokens(code)
44 except TypeError as err:
45 if (isinstance(err.args[0], str) and
46 ('unbound method get_tokens' in err.args[0] or
47 'missing 1 required positional argument' in err.args[0])):
48 raise TypeError('lex() argument must be a lexer instance, '
49 'not a class')
50 raise
51
52
53 def format(tokens, formatter, outfile=None): # pylint: disable=redefined-builtin
54 """
55 Format a tokenlist ``tokens`` with the formatter ``formatter``.
56
57 If ``outfile`` is given and a valid file object (an object
58 with a ``write`` method), the result will be written to it, otherwise
59 it is returned as a string.
60 """
61 try:
62 if not outfile:
63 realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
64 formatter.format(tokens, realoutfile)
65 return realoutfile.getvalue()
66 else:
67 formatter.format(tokens, outfile)
68 except TypeError as err:
69 if (isinstance(err.args[0], str) and
70 ('unbound method format' in err.args[0] or
71 'missing 1 required positional argument' in err.args[0])):
72 raise TypeError('format() argument must be a formatter instance, '
73 'not a class')
74 raise
75
76
77 def highlight(code, lexer, formatter, outfile=None):
78 """
79 Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``.
80
81 If ``outfile`` is given and a valid file object (an object
82 with a ``write`` method), the result will be written to it, otherwise
83 it is returned as a string.
84 """
85 return format(lex(code, lexer), formatter, outfile)
86
87
88 if __name__ == '__main__': # pragma: no cover
89 from pygments.cmdline import main
90 sys.exit(main(sys.argv))

eric ide

mercurial