PageRenderTime 25ms CodeModel.GetById 21ms RepoModel.GetById 1ms app.codeStats 0ms

/pygments/__init__.py

https://bitbucket.org/birkenfeld/pygments-main/
Python | 91 lines | 33 code | 13 blank | 45 comment | 13 complexity | a4da408a41e498579b1c8080f13f6979 MD5 | raw file
Possible License(s): BSD-2-Clause
  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-2012 by the Pygments team, see AUTHORS.
  20. :license: BSD, see LICENSE for details.
  21. """
  22. __version__ = '1.4'
  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, err:
  34. if isinstance(err.args[0], str) and \
  35. 'unbound method get_tokens' 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):
  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. #print formatter, 'using', formatter.encoding
  49. realoutfile = formatter.encoding and BytesIO() or StringIO()
  50. formatter.format(tokens, realoutfile)
  51. return realoutfile.getvalue()
  52. else:
  53. formatter.format(tokens, outfile)
  54. except TypeError, err:
  55. if isinstance(err.args[0], str) and \
  56. 'unbound method format' 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)
  68. if __name__ == '__main__':
  69. from pygments.cmdline import main
  70. sys.exit(main(sys.argv))