moin-parser.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. # -*- coding: utf-8 -*-
  2. """
  3. The Pygments MoinMoin Parser
  4. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  5. This is a MoinMoin parser plugin that renders source code to HTML via
  6. Pygments; you need Pygments 0.7 or newer for this parser to work.
  7. To use it, set the options below to match your setup and put this file in
  8. the data/plugin/parser subdirectory of your Moin instance, and give it the
  9. name that the parser directive should have. For example, if you name the
  10. file ``code.py``, you can get a highlighted Python code sample with this
  11. Wiki markup::
  12. {{{
  13. #!code python
  14. [...]
  15. }}}
  16. Additionally, if you set ATTACHMENTS below to True, Pygments will also be
  17. called for all attachments for whose filenames there is no other parser
  18. registered.
  19. You are responsible for including CSS rules that will map the Pygments CSS
  20. classes to colors. You can output a stylesheet file with `pygmentize`, put
  21. it into the `htdocs` directory of your Moin instance and then include it in
  22. the `stylesheets` configuration option in the Moin config, e.g.::
  23. stylesheets = [('screen', '/htdocs/pygments.css')]
  24. If you do not want to do that and are willing to accept larger HTML
  25. output, you can set the INLINESTYLES option below to True.
  26. :copyright: Copyright 2006-2010 by the Pygments team, see AUTHORS.
  27. :license: BSD, see LICENSE for details.
  28. """
  29. # Options
  30. # ~~~~~~~
  31. # Set to True if you want to highlight attachments, in addition to
  32. # {{{ }}} blocks.
  33. ATTACHMENTS = True
  34. # Set to True if you want inline CSS styles instead of classes
  35. INLINESTYLES = False
  36. import sys
  37. from pygments import highlight
  38. from pygments.lexers import get_lexer_by_name, get_lexer_for_filename, TextLexer
  39. from pygments.formatters import HtmlFormatter
  40. from pygments.util import ClassNotFound
  41. # wrap lines in <span>s so that the Moin-generated line numbers work
  42. class MoinHtmlFormatter(HtmlFormatter):
  43. def wrap(self, source, outfile):
  44. for line in source:
  45. yield 1, '<span class="line">' + line[1] + '</span>'
  46. htmlformatter = MoinHtmlFormatter(noclasses=INLINESTYLES)
  47. textlexer = TextLexer()
  48. codeid = [0]
  49. class Parser:
  50. """
  51. MoinMoin Pygments parser.
  52. """
  53. if ATTACHMENTS:
  54. extensions = '*'
  55. else:
  56. extensions = []
  57. Dependencies = []
  58. def __init__(self, raw, request, **kw):
  59. self.raw = raw
  60. self.req = request
  61. if "format_args" in kw:
  62. # called from a {{{ }}} block
  63. try:
  64. self.lexer = get_lexer_by_name(kw['format_args'].strip())
  65. except ClassNotFound:
  66. self.lexer = textlexer
  67. return
  68. if "filename" in kw:
  69. # called for an attachment
  70. filename = kw['filename']
  71. else:
  72. # called for an attachment by an older moin
  73. # HACK: find out the filename by peeking into the execution
  74. # frame which might not always work
  75. try:
  76. frame = sys._getframe(1)
  77. filename = frame.f_locals['filename']
  78. except:
  79. filename = 'x.txt'
  80. try:
  81. self.lexer = get_lexer_for_filename(filename)
  82. except ClassNotFound:
  83. self.lexer = textlexer
  84. def format(self, formatter):
  85. codeid[0] += 1
  86. id = "pygments_%s" % codeid[0]
  87. w = self.req.write
  88. w(formatter.code_area(1, id, start=1, step=1))
  89. w(formatter.rawHTML(highlight(self.raw, self.lexer, htmlformatter)))
  90. w(formatter.code_area(0, id))