__init__.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. # -*- coding: utf-8 -*-
  2. """
  3. Pygments
  4. ~~~~~~~~
  5. Pygments is a syntax highlighting package written in Python.
  6. It is a generic syntax highlighter for general use in all kinds of software
  7. such as forum systems, wikis or other applications that need to prettify
  8. source code. Highlights are:
  9. * a wide range of common languages and markup formats is supported
  10. * special attention is paid to details, increasing quality by a fair amount
  11. * support for new languages and formats are added easily
  12. * a number of output formats, presently HTML, LaTeX, RTF, SVG, all image
  13. formats that PIL supports, and ANSI sequences
  14. * it is usable as a command-line tool and as a library
  15. * ... and it highlights even Brainfuck!
  16. The `Pygments master branch`_ is installable with ``easy_install Pygments==dev``.
  17. .. _Pygments master branch:
  18. https://github.com/pygments/pygments/archive/master.zip#egg=Pygments-dev
  19. :copyright: Copyright 2006-2019 by the Pygments team, see AUTHORS.
  20. :license: BSD, see LICENSE for details.
  21. """
  22. import sys
  23. from pygments.util import StringIO, BytesIO
  24. __version__ = '2.5.2'
  25. __docformat__ = 'restructuredtext'
  26. __all__ = ['lex', 'format', 'highlight']
  27. def lex(code, lexer):
  28. """
  29. Lex ``code`` with ``lexer`` and return an iterable of tokens.
  30. """
  31. try:
  32. return lexer.get_tokens(code)
  33. except TypeError as err:
  34. if (isinstance(err.args[0], str) and
  35. ('unbound method get_tokens' in err.args[0] or
  36. 'missing 1 required positional argument' in err.args[0])):
  37. raise TypeError('lex() argument must be a lexer instance, '
  38. 'not a class')
  39. raise
  40. def format(tokens, formatter, outfile=None): # pylint: disable=redefined-builtin
  41. """
  42. Format a tokenlist ``tokens`` with the formatter ``formatter``.
  43. If ``outfile`` is given and a valid file object (an object
  44. with a ``write`` method), the result will be written to it, otherwise
  45. it is returned as a string.
  46. """
  47. try:
  48. if not outfile:
  49. realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
  50. formatter.format(tokens, realoutfile)
  51. return realoutfile.getvalue()
  52. else:
  53. formatter.format(tokens, outfile)
  54. except TypeError as err:
  55. if (isinstance(err.args[0], str) and
  56. ('unbound method format' in err.args[0] or
  57. 'missing 1 required positional argument' in err.args[0])):
  58. raise TypeError('format() argument must be a formatter instance, '
  59. 'not a class')
  60. raise
  61. def highlight(code, lexer, formatter, outfile=None):
  62. """
  63. Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``.
  64. If ``outfile`` is given and a valid file object (an object
  65. with a ``write`` method), the result will be written to it, otherwise
  66. it is returned as a string.
  67. """
  68. return format(lex(code, lexer), formatter, outfile)
  69. if __name__ == '__main__': # pragma: no cover
  70. from pygments.cmdline import main
  71. sys.exit(main(sys.argv))