__init__.py 2.9 KB

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