Edgewall Software

ProcessorBazaar: ShellExample.py

File ShellExample.py, 1.5 kB (added by mOo, 2 years ago)

Shell Example Code Listing Processor

Line 
1"""Preprocessor Highlight ShellExample."""
2from trac.util import escape
3import re
4
5regexp = re.compile(
6        '^(?P<path>~ |~?/[^ ]+ )?(?P<cli>[#$] )(?P<input>.*)$'
7        '|^(?P<comment>\(.*\))$'
8        , re.M)
9
10def execute(hdf, txt, env):
11    # args will be null if the macro is called without parenthesis.
12    css = ''
13    if hdf:
14        if not hdf.has_key("macro.ShellExample.outputcss"):
15            hdf["macro.ShellExample.outputcss"] = True
16            css = '''<style type="text/css">
17.code-input            {color:blue}
18.code-input            {color:blue}
19.code-root             {color:red;}
20/*.code-comment          {color:#d80000;} */
21.code-path, .code-user {color:#449999; font-weight: bold}
22</style>'''
23    txt = txt and escape(txt) or ''
24    def callback(match):
25        m = match.group('cli')
26        if m:
27            path = match.group('path')
28            if path:
29                line = '<span class="code-path">' + path + '</span>'
30            else:
31                line = ''
32
33            input = '<span class="code-input">' + match.group('input') + '</span>'
34            if m == '# ':
35                line = line + '<span class="code-root">' + m + input + '</span>'
36            else:
37                line = line + '<span class="code-user">' + m + input + '</span>'
38            return line
39        m = match.group('comment')
40        if m:
41            return '<span class="code-comment">' + m + '</span>'
42        return match.group(0)
43    return css + '<pre><div class="code">' + regexp.sub(callback, txt)+ '</div></pre>';