formatter.py 2.9 KB

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