/console/app/pygments/__init__.py

https://github.com/SMU-SIS/StoryServer · Python · 94 lines · 37 code · 12 blank · 45 comment · 13 complexity · 69f045faced6c194f2e799e81003790d 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 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 tip`_ is installable with ``easy_install Pygments==dev``.
  16. .. _Pygments tip: http://dev.pocoo.org/hg/pygments-main/archive/tip.tar.gz#egg=Pygments-dev
  17. :copyright: 2006-2007 by Georg Brandl, Armin Ronacher and others.
  18. :license: BSD, see LICENSE for more details.
  19. """
  20. __version__ = '0.11.1'
  21. __author__ = 'Georg Brandl <g.brandl@gmx.net>'
  22. __url__ = 'http://pygments.org/'
  23. __license__ = 'BSD License'
  24. __docformat__ = 'restructuredtext'
  25. __all__ = ['lex', 'format', 'highlight']
  26. import sys, os
  27. from StringIO import StringIO
  28. from cStringIO import StringIO as CStringIO
  29. def lex(code, lexer):
  30. """
  31. Lex ``code`` with ``lexer`` and return an iterable of tokens.
  32. """
  33. try:
  34. return lexer.get_tokens(code)
  35. except TypeError, err:
  36. if isinstance(err.args[0], str) and \
  37. 'unbound method get_tokens' in err.args[0]:
  38. raise TypeError('lex() argument must be a lexer instance, '
  39. 'not a class')
  40. raise
  41. def format(tokens, formatter, outfile=None):
  42. """
  43. Format a tokenlist ``tokens`` with the formatter ``formatter``.
  44. If ``outfile`` is given and a valid file object (an object
  45. with a ``write`` method), the result will be written to it, otherwise
  46. it is returned as a string.
  47. """
  48. try:
  49. if not outfile:
  50. # if we want Unicode output, we have to use Python StringIO
  51. realoutfile = formatter.encoding and CStringIO() or StringIO()
  52. formatter.format(tokens, realoutfile)
  53. return realoutfile.getvalue()
  54. else:
  55. formatter.format(tokens, outfile)
  56. except TypeError, err:
  57. if isinstance(err.args[0], str) and \
  58. 'unbound method format' in err.args[0]:
  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. Lex ``code`` with ``lexer`` and format it with the formatter
  65. ``formatter``. If ``filters`` are given they will be applied
  66. on the token stream.
  67. If ``outfile`` is given and a valid file object (an object
  68. with a ``write`` method), the result will be written to it, otherwise
  69. it is returned as a string.
  70. """
  71. return format(lex(code, lexer), formatter, outfile)
  72. if __name__ == '__main__':
  73. from pygments.cmdline import main
  74. sys.exit(main(sys.argv))