/Misc/pygments-main/pygments/__init__.py

https://gitlab.com/somiyagawa/ANNIS · Python · 92 lines · 35 code · 13 blank · 44 comment · 13 complexity · 7ce55ddbf420a5748e58ef990d47d08b MD5 · raw file

  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 tip`_ is installable with ``easy_install Pygments==dev``.
  17. .. _Pygments tip:
  18. http://bitbucket.org/birkenfeld/pygments-main/get/tip.zip#egg=Pygments-dev
  19. :copyright: Copyright 2006-2015 by the Pygments team, see AUTHORS.
  20. :license: BSD, see LICENSE for details.
  21. """
  22. __version__ = '2.0.2'
  23. __docformat__ = 'restructuredtext'
  24. __all__ = ['lex', 'format', 'highlight']
  25. import sys
  26. from pygments.util import StringIO, BytesIO
  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):
  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))