| 1 |
""" |
| 2 |
The Pygments MoinMoin Parser |
| 3 |
~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 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 |
|
| 8 |
To use it, set the options below to match your setup and put this file in |
| 9 |
the data/plugin/parser subdirectory of your Moin instance, and give it the |
| 10 |
name that the parser directive should have. For example, if you name the |
| 11 |
file ``code.py``, you can get a highlighted Python code sample with this |
| 12 |
Wiki markup:: |
| 13 |
|
| 14 |
{{{ |
| 15 |
#!code python |
| 16 |
[...] |
| 17 |
}}} |
| 18 |
|
| 19 |
Additionally, if you set ATTACHMENTS below to True, Pygments will also be |
| 20 |
called for all attachments for whose filenames there is no other parser |
| 21 |
registered. |
| 22 |
|
| 23 |
You are responsible for including CSS rules that will map the Pygments CSS |
| 24 |
classes to colors. You can output a stylesheet file with `pygmentize`, put |
| 25 |
it into the `htdocs` directory of your Moin instance and then include it in |
| 26 |
the `stylesheets` configuration option in the Moin config, e.g.:: |
| 27 |
|
| 28 |
stylesheets = [('screen', '/htdocs/pygments.css')] |
| 29 |
|
| 30 |
If you do not want to do that and are willing to accept larger HTML |
| 31 |
output, you can set the INLINESTYLES option below to True. |
| 32 |
|
| 33 |
:copyright: Copyright 2006-present by the Pygments team, see AUTHORS. |
| 34 |
:license: BSD, see LICENSE for details. |
| 35 |
""" |
| 36 |
|
| 37 |
import sys |
| 38 |
|
| 39 |
from pygments import highlight |
| 40 |
from pygments.lexers import get_lexer_by_name, get_lexer_for_filename, TextLexer |
| 41 |
from pygments.formatters import HtmlFormatter |
| 42 |
from pygments.util import ClassNotFound |
| 43 |
|
| 44 |
|
| 45 |
# Options |
| 46 |
# ~~~~~~~ |
| 47 |
|
| 48 |
# Set to True if you want to highlight attachments, in addition to |
| 49 |
# {{{ }}} blocks. |
| 50 |
ATTACHMENTS = True |
| 51 |
|
| 52 |
# Set to True if you want inline CSS styles instead of classes |
| 53 |
INLINESTYLES = False |
| 54 |
|
| 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 Exception: |
| 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 = f"pygments_{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)) |