formatter.py 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. """
  2. pygments.formatter
  3. ~~~~~~~~~~~~~~~~~~
  4. Base formatter class.
  5. :copyright: Copyright 2006-2021 by the Pygments team, see AUTHORS.
  6. :license: BSD, see LICENSE for details.
  7. """
  8. import codecs
  9. from pygments.util import get_bool_opt
  10. from pygments.styles import get_style_by_name
  11. __all__ = ['Formatter']
  12. def _lookup_style(style):
  13. if isinstance(style, str):
  14. return get_style_by_name(style)
  15. return style
  16. class Formatter:
  17. """
  18. Converts a token stream to text.
  19. Options accepted:
  20. ``style``
  21. The style to use, can be a string or a Style subclass
  22. (default: "default"). Not used by e.g. the
  23. TerminalFormatter.
  24. ``full``
  25. Tells the formatter to output a "full" document, i.e.
  26. a complete self-contained document. This doesn't have
  27. any effect for some formatters (default: false).
  28. ``title``
  29. If ``full`` is true, the title that should be used to
  30. caption the document (default: '').
  31. ``encoding``
  32. If given, must be an encoding name. This will be used to
  33. convert the Unicode token strings to byte strings in the
  34. output. If it is "" or None, Unicode strings will be written
  35. to the output file, which most file-like objects do not
  36. support (default: None).
  37. ``outencoding``
  38. Overrides ``encoding`` if given.
  39. """
  40. #: Name of the formatter
  41. name = None
  42. #: Shortcuts for the formatter
  43. aliases = []
  44. #: fn match rules
  45. filenames = []
  46. #: If True, this formatter outputs Unicode strings when no encoding
  47. #: option is given.
  48. unicodeoutput = True
  49. def __init__(self, **options):
  50. self.style = _lookup_style(options.get('style', 'default'))
  51. self.full = get_bool_opt(options, 'full', False)
  52. self.title = options.get('title', '')
  53. self.encoding = options.get('encoding', None) or None
  54. if self.encoding in ('guess', 'chardet'):
  55. # can happen for e.g. pygmentize -O encoding=guess
  56. self.encoding = 'utf-8'
  57. self.encoding = options.get('outencoding') or self.encoding
  58. self.options = options
  59. def get_style_defs(self, arg=''):
  60. """
  61. Return the style definitions for the current style as a string.
  62. ``arg`` is an additional argument whose meaning depends on the
  63. formatter used. Note that ``arg`` can also be a list or tuple
  64. for some formatters like the html formatter.
  65. """
  66. return ''
  67. def format(self, tokensource, outfile):
  68. """
  69. Format ``tokensource``, an iterable of ``(tokentype, tokenstring)``
  70. tuples and write it into ``outfile``.
  71. """
  72. if self.encoding:
  73. # wrap the outfile in a StreamWriter
  74. outfile = codecs.lookup(self.encoding)[3](outfile)
  75. return self.format_unencoded(tokensource, outfile)