Attachment 'pygments-moin.parser-0.8.1.py'

Download

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

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-07-17 19:53:06, 3.5 KB) [[attachment:pygments-moin.parser-0.8.1.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.