Anonymous avatar Anonymous committed 43ca90f

[svn] Improve Moin parser and add docs.

Comments (0)

Files changed (3)

docs/src/index.txt

 
   - `Using Pygments in ReST documents <rstdirective.txt>`_
 
+  - `Using Pygments with MoinMoin <moinmoin.txt>`_
+
 - About Pygments
 
   - `Changelog <changelog.txt>`_

docs/src/moinmoin.txt

+.. -*- mode: rst -*-
+
+============================
+Using Pygments with MoinMoin
+============================
+
+From Pygments 0.7, the source distribution ships a `Moin`_ parser plugin that
+can be used to get Pygments highlighting in Moin wiki pages.
+
+To use it, copy the file `external/moin-parser.py` from the Pygments
+distribution to the `data/plugin/parser` subdirectory of your Moin instance,
+edit the options at the top of the file and give the file the name that the
+parser directive should have. For example, if you name the file ``code.py``, you
+can get a highlighted Python code sample with this Wiki markup::
+
+    {{{
+    #!code python
+    [...]
+    }}}
+
+where ``python`` is the Pygments name of the lexer to use.
+
+Additionally, if you set the ``ATTACHMENTS`` option to True, Pygments will also
+be called for all attachments for whose filenames there is no other parser
+registered.
+
+You are responsible for including CSS rules that will map the Pygments CSS
+classes to colors. You can output a stylesheet file with `pygmentize`, put it
+into the `htdocs` directory of your Moin instance and then include it in the
+`stylesheets` configuration option in the Moin config, e.g.::
+
+    stylesheets = [('screen', '/htdocs/pygments.css')]
+
+If you do not want to do that and are willing to accept larger HTML output, you
+can set the ``INLINESTYLES`` option to True.
+
+
+.. _Moin: http://moinmoin.wikiwikiweb.de/

external/moin-parser.py

     The Pygments MoinMoin Parser
     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 
-    This is a MoinMoin parser plugin that renders source code
-    to HTML via Pygments.
+    This is a MoinMoin parser plugin that renders source code to HTML via
+    Pygments; you need Pygments 0.7 or newer for this parser to work.
 
-    To use it, put it in the data/plugin/parser subdirectory of
-    your Moin instance, and give it the name that the parser directive
-    should have. For example, if you name the file ``code.py``, you
-    can get a highlighted Python code sample with this Wiki markup::
+    To use it, set the options below to match your setup and put this file in
+    the data/plugin/parser subdirectory of your Moin instance, and give it the
+    name that the parser directive should have. For example, if you name the
+    file ``code.py``, you can get a highlighted Python code sample with this
+    Wiki markup::
 
         {{{
         #!code python
         [...]
         }}}
 
-    Additionally, if you set ATTACHMENTS below to True, Pygments will
-    also be called for all attachments for whose filenames there is no other
-    parser registered.
+    Additionally, if you set ATTACHMENTS below to True, Pygments will also be
+    called for all attachments for whose filenames there is no other parser
+    registered.
 
-    You are responsible for including CSS rules that will map the Pygments
-    CSS classes to colors. You can output a stylesheet file with `pygmentize`,
-    put it into the `htdocs` directory of your Moin instance and then include
-    it in the `stylesheets` configuration option in the Moin config, e.g.::
+    You are responsible for including CSS rules that will map the Pygments CSS
+    classes to colors. You can output a stylesheet file with `pygmentize`, put
+    it into the `htdocs` directory of your Moin instance and then include it in
+    the `stylesheets` configuration option in the Moin config, e.g.::
 
         stylesheets = [('screen', '/htdocs/pygments.css')]
 
-    If you do not want to do that and are willing to accept slightly larger
-    HTML output, you can set the INLINESTYLES option below to True.
+    If you do not want to do that and are willing to accept larger HTML
+    output, you can set the INLINESTYLES option below to True.
 
     :copyright: 2007 by Georg Brandl.
     :license: BSD, see LICENSE for more details.
 # {{{ }}} blocks.
 ATTACHMENTS = True
 
-# Set to False if you don't want to have line numbers in the output. 
-LINENOS = True
-
 # Set to True if you want inline CSS styles instead of classes
 INLINESTYLES = False
 
 import sys
 
 from pygments import highlight
-from pygments.util import ObjectNotFound
 from pygments.lexers import get_lexer_by_name, get_lexer_for_filename, TextLexer
 from pygments.formatters import HtmlFormatter
+from pygments.util import ObjectNotFound
 
-formatter = HtmlFormatter(linenos=LINENOS, noclasses=INLINESTYLES)
+
+# wrap lines in <span>s so that the Moin-generated line numbers work
+class MoinHtmlFormatter(HtmlFormatter):
+    def wrap(self, source, outfile):
+        for line in source:
+            yield 1, '<span class="line">' + line[1] + '</span>'
+
+htmlformatter = MoinHtmlFormatter(noclasses=INLINESTYLES)
 textlexer = TextLexer()
+codeid = [0]
 
 
 class Parser:
             self.lexer = textlexer
 
     def format(self, formatter):
-        self.req.write(
-            formatter.rawHTML(highlight(self.raw, self.lexer, formatter)))
+        codeid[0] += 1
+        id = "pygments_%s" % codeid[0]
+        w = self.req.write
+        w(formatter.code_area(1, id, start=1, step=1))
+        w(formatter.rawHTML(highlight(self.raw, self.lexer, htmlformatter)))
+        w(formatter.code_area(0, id))
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.