/lib/pygments/__init__.py

https://gitlab.com/technomancer7/aos · Python · 83 lines · 31 code · 9 blank · 43 comment · 10 complexity · d63eb56c771cb600ef2e727bb25ec228 MD5 · raw file

  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. from io import StringIO, BytesIO
  22. __version__ = '2.11.2'
  23. __docformat__ = 'restructuredtext'
  24. __all__ = ['lex', 'format', 'highlight']
  25. def lex(code, lexer):
  26. """
  27. Lex ``code`` with ``lexer`` and return an iterable of tokens.
  28. """
  29. try:
  30. return lexer.get_tokens(code)
  31. except TypeError as err:
  32. if (isinstance(err.args[0], str) and
  33. ('unbound method get_tokens' in err.args[0] or
  34. 'missing 1 required positional argument' in err.args[0])):
  35. raise TypeError('lex() argument must be a lexer instance, '
  36. 'not a class')
  37. raise
  38. def format(tokens, formatter, outfile=None): # pylint: disable=redefined-builtin
  39. """
  40. Format a tokenlist ``tokens`` with the formatter ``formatter``.
  41. If ``outfile`` is given and a valid file object (an object
  42. with a ``write`` method), the result will be written to it, otherwise
  43. it is returned as a string.
  44. """
  45. try:
  46. if not outfile:
  47. realoutfile = getattr(formatter, 'encoding', None) and BytesIO() or StringIO()
  48. formatter.format(tokens, realoutfile)
  49. return realoutfile.getvalue()
  50. else:
  51. formatter.format(tokens, outfile)
  52. except TypeError as err:
  53. if (isinstance(err.args[0], str) and
  54. ('unbound method format' in err.args[0] or
  55. 'missing 1 required positional argument' in err.args[0])):
  56. raise TypeError('format() argument must be a formatter instance, '
  57. 'not a class')
  58. raise
  59. def highlight(code, lexer, formatter, outfile=None):
  60. """
  61. Lex ``code`` with ``lexer`` and format it with the formatter ``formatter``.
  62. If ``outfile`` is given and a valid file object (an object
  63. with a ``write`` method), the result will be written to it, otherwise
  64. it is returned as a string.
  65. """
  66. return format(lex(code, lexer), formatter, outfile)