Attachment 'AgelSrc.py'
Download 1 # -*- coding: iso-8859-1 -*-
2 """
3 = AgelSrc.py =
4 Simple source code highlighting parser.
5
6 == Install ==
7 1. `$ easy_install Pygments`
8 This parser uses highlighting facility of Pygments library.
9 Thanks to [http://pygments.pocoo.org/docs/authors/ authors].
10 So you must install this great library with EasyInstall (or install manually)
11 2. download and install `AgelSrc.py` file to your `wiki/data/plugin/parser/`.
12 You may need to restart your MoinMoin processes. (Recommended)
13 3. Enjoy! :-)
14
15 == Usage ==
16 {{{#!AgelSrc filetype=python
17 print 'hello, world!'
18 }}}
19
20 ...or...
21
22 {{{#!AgelSrc
23 #!/usr/bin/env python
24 print 'hello, world!'
25 }}}
26
27 Second method may can fail if you didn't supply enough information for
28 Pygments to //guess// syntax for your source code. (use 1st usage.)
29
30 == Author ==
31 * Jonghyouk Yun <ageldama@gmail.com> 2007Jan23Tue0200 KST
32
33 == License ==
34 * GNU GPL, http://www.gnu.org/copyleft/gpl.html for details.
35
36
37 """
38
39 from MoinMoin import wikiutil
40
41 from pygments import highlight
42 from pygments.lexers import get_lexer_by_name, guess_lexer
43 from pygments.formatters import HtmlFormatter
44
45 ### correction for multilingual sources...
46 import sys; reload(sys)
47 sys.setdefaultencoding('utf8')
48
49
50 class Parser:
51 extensions = '*'
52 Dependencies = []
53
54 def __init__(self, raw, request, **kw):
55 self.raw = raw
56 self.request = request
57 self.form = request.form
58 self._ = request.getText
59 ## bind `format_args` to self.*
60 ## 'format_args':'a=1,b=2' => self.a=1; self.b=2
61 for arg in kw.get('format_args', '').split(','):
62 if arg.find('=') > -1:
63 key, value = arg.split('=')
64 setattr(self, key, wikiutil.escape(value.strip(), quote=1))
65
66 def format(self, formatter):
67 ## select lexer
68 l= None
69 if hasattr(self, 'filetype'):
70 l= get_lexer_by_name(self.filetype)#, stripall=True)
71 else:
72 l= guess_lexer(self.raw)
73 f= HtmlFormatter(linenos=True)#, cssclass="source")
74 r= highlight(self.raw, l, f)
75 ## printout stylesheet... dumb...
76 self.request.write(u'<style>')
77 self.request.write(f.get_style_defs())
78 self.request.write(u'</style>')
79 ## write result htmlcode
80 self.request.write(r.expandtabs())
81 #EOF
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.You are not allowed to attach a file to this page.