Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pip/_vendor/pygments/__init__.py: 27%

26 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:48 +0000

1""" 

2 Pygments 

3 ~~~~~~~~ 

4 

5 Pygments is a syntax highlighting package written in Python. 

6 

7 It is a generic syntax highlighter for general use in all kinds of software 

8 such as forum systems, wikis or other applications that need to prettify 

9 source code. Highlights are: 

10 

11 * a wide range of common languages and markup formats is supported 

12 * special attention is paid to details, increasing quality by a fair amount 

13 * support for new languages and formats are added easily 

14 * a number of output formats, presently HTML, LaTeX, RTF, SVG, all image 

15 formats that PIL supports, and ANSI sequences 

16 * it is usable as a command-line tool and as a library 

17 * ... and it highlights even Brainfuck! 

18 

19 The `Pygments master branch`_ is installable with ``easy_install Pygments==dev``. 

20 

21 .. _Pygments master branch: 

22 https://github.com/pygments/pygments/archive/master.zip#egg=Pygments-dev 

23 

24 :copyright: Copyright 2006-2022 by the Pygments team, see AUTHORS. 

25 :license: BSD, see LICENSE for details. 

26""" 

27from io import StringIO, BytesIO 

28 

29__version__ = '2.14.0' 

30__docformat__ = 'restructuredtext' 

31 

32__all__ = ['lex', 'format', 'highlight'] 

33 

34 

35def lex(code, lexer): 

36 """ 

37 Lex ``code`` with ``lexer`` and return an iterable of tokens. 

38 """ 

39 try: 

40 return lexer.get_tokens(code) 

41 except TypeError: 

42 # Heuristic to catch a common mistake. 

43 from pip._vendor.pygments.lexer import RegexLexer 

44 if isinstance(lexer, type) and issubclass(lexer, RegexLexer): 

45 raise TypeError('lex() argument must be a lexer instance, ' 

46 'not a class') 

47 raise 

48 

49 

50def format(tokens, formatter, outfile=None): # pylint: disable=redefined-builtin 

51 """ 

52 Format a tokenlist ``tokens`` with the formatter ``formatter``. 

53 

54 If ``outfile`` is given and a valid file object (an object 

55 with a ``write`` method), the result will be written to it, otherwise 

56 it is returned as a string. 

57 """ 

58 try: 

59 if not outfile: 

60 realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO() 

61 formatter.format(tokens, realoutfile) 

62 return realoutfile.getvalue() 

63 else: 

64 formatter.format(tokens, outfile) 

65 except TypeError: 

66 # Heuristic to catch a common mistake. 

67 from pip._vendor.pygments.formatter import Formatter 

68 if isinstance(formatter, type) and issubclass(formatter, Formatter): 

69 raise TypeError('format() argument must be a formatter instance, ' 

70 'not a class') 

71 raise 

72 

73 

74def highlight(code, lexer, formatter, outfile=None): 

75 """ 

76 Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``. 

77 

78 If ``outfile`` is given and a valid file object (an object 

79 with a ``write`` method), the result will be written to it, otherwise 

80 it is returned as a string. 

81 """ 

82 return format(lex(code, lexer), formatter, outfile)