Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/pygments/formatter.py: 46%

28 statements  

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

1""" 

2 pygments.formatter 

3 ~~~~~~~~~~~~~~~~~~ 

4 

5 Base formatter class. 

6 

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

8 :license: BSD, see LICENSE for details. 

9""" 

10 

11import codecs 

12 

13from pygments.util import get_bool_opt 

14from pygments.styles import get_style_by_name 

15 

16__all__ = ['Formatter'] 

17 

18 

19def _lookup_style(style): 

20 if isinstance(style, str): 

21 return get_style_by_name(style) 

22 return style 

23 

24 

25class Formatter: 

26 """ 

27 Converts a token stream to text. 

28 

29 Options accepted: 

30 

31 ``style`` 

32 The style to use, can be a string or a Style subclass 

33 (default: "default"). Not used by e.g. the 

34 TerminalFormatter. 

35 ``full`` 

36 Tells the formatter to output a "full" document, i.e. 

37 a complete self-contained document. This doesn't have 

38 any effect for some formatters (default: false). 

39 ``title`` 

40 If ``full`` is true, the title that should be used to 

41 caption the document (default: ''). 

42 ``encoding`` 

43 If given, must be an encoding name. This will be used to 

44 convert the Unicode token strings to byte strings in the 

45 output. If it is "" or None, Unicode strings will be written 

46 to the output file, which most file-like objects do not 

47 support (default: None). 

48 ``outencoding`` 

49 Overrides ``encoding`` if given. 

50 """ 

51 

52 #: Name of the formatter 

53 name = None 

54 

55 #: Shortcuts for the formatter 

56 aliases = [] 

57 

58 #: fn match rules 

59 filenames = [] 

60 

61 #: If True, this formatter outputs Unicode strings when no encoding 

62 #: option is given. 

63 unicodeoutput = True 

64 

65 def __init__(self, **options): 

66 self.style = _lookup_style(options.get('style', 'default')) 

67 self.full = get_bool_opt(options, 'full', False) 

68 self.title = options.get('title', '') 

69 self.encoding = options.get('encoding', None) or None 

70 if self.encoding in ('guess', 'chardet'): 

71 # can happen for e.g. pygmentize -O encoding=guess 

72 self.encoding = 'utf-8' 

73 self.encoding = options.get('outencoding') or self.encoding 

74 self.options = options 

75 

76 def get_style_defs(self, arg=''): 

77 """ 

78 Return the style definitions for the current style as a string. 

79 

80 ``arg`` is an additional argument whose meaning depends on the 

81 formatter used. Note that ``arg`` can also be a list or tuple 

82 for some formatters like the html formatter. 

83 """ 

84 return '' 

85 

86 def format(self, tokensource, outfile): 

87 """ 

88 Format ``tokensource``, an iterable of ``(tokentype, tokenstring)`` 

89 tuples and write it into ``outfile``. 

90 """ 

91 if self.encoding: 

92 # wrap the outfile in a StreamWriter 

93 outfile = codecs.lookup(self.encoding)[3](outfile) 

94 return self.format_unencoded(tokensource, outfile)