__init__.py 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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-2023 by the Pygments team, see AUTHORS.
  19. :license: BSD, see LICENSE for details.
  20. """
  21. from io import StringIO, BytesIO
  22. __version__ = '2.17.2'
  23. __docformat__ = 'restructuredtext'
  24. __all__ = ['lex', 'format', 'highlight']
  25. def lex(code, lexer):
  26. """
  27. Lex `code` with the `lexer` (must be a `Lexer` instance)
  28. and return an iterable of tokens. Currently, this only calls
  29. `lexer.get_tokens()`.
  30. """
  31. try:
  32. return lexer.get_tokens(code)
  33. except TypeError:
  34. # Heuristic to catch a common mistake.
  35. from pygments.lexer import RegexLexer
  36. if isinstance(lexer, type) and issubclass(lexer, RegexLexer):
  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 ``tokens`` (an iterable of tokens) with the formatter ``formatter``
  43. (a `Formatter` instance).
  44. If ``outfile`` is given and a valid file object (an object with a
  45. ``write`` method), the result will be written to it, otherwise it
  46. is returned as a string.
  47. """
  48. try:
  49. if not outfile:
  50. realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
  51. formatter.format(tokens, realoutfile)
  52. return realoutfile.getvalue()
  53. else:
  54. formatter.format(tokens, outfile)
  55. except TypeError:
  56. # Heuristic to catch a common mistake.
  57. from pygments.formatter import Formatter
  58. if isinstance(formatter, type) and issubclass(formatter, Formatter):
  59. raise TypeError('format() argument must be a formatter instance, '
  60. 'not a class')
  61. raise
  62. def highlight(code, lexer, formatter, outfile=None):
  63. """
  64. This is the most high-level highlighting function. It combines `lex` and
  65. `format` in one function.
  66. """
  67. return format(lex(code, lexer), formatter, outfile)