Edgewall Software

ProcessorBazaar: ShellExample.3.py

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

gentoo doc alike Shell Example Code Listing Processor v3

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<option_space>\s+)(?P<option>-[a-zA-Z0-9]\b|--[^ \t]+)(?P<option_desc>.*)$'
8                '|^(?P<note>\(.*\))$'
9                , re.M)
10regexp_db_qstr = '"[^"\\\\]*(?:\\\\.[^"\\\\]*){0,250}"';
11regexp_si_qstr = '\'[^\'\\\\]*(?:\\\\.[^\'\\\\]*){0,250}\'';
12regexp_string = re.compile('(?:%s|%s)' % (regexp_db_qstr, regexp_si_qstr))
13def execute(hdf, txt, env):
14        # args will be null if the macro is called without parenthesis.
15        css = ''
16        if hdf:
17                if not hdf.has_key("macro.ShellExample.outputcss"):
18                        hdf["macro.ShellExample.outputcss"] = True
19                        css = '''<style type="text/css">
20.code-input          {color:blue}
21.code-input          {color:blue}
22.code-root           {color:red;}
23.code-note           {color:#d80000;}
24.code-path, .code-user {color:#449999; font-weight: bold}
25</style>'''
26        txt = txt and escape(txt).replace('&#34;', '"') or ''
27        def stringcallback(match):
28                return '<span class="code-string">' + match.group(0) + '</span>'
29
30        def callback(match):
31                m = match.group('cli')
32                if m:
33                        path = match.group('path')
34                        if path:
35                                line = '<span class="code-path">' + path + '</span>'
36                        else:
37                                line = ''
38
39                        input = regexp_string.sub(stringcallback, match.group('input'))
40                        input = '<span class="code-input">' + input + '</span>'
41                        if m == '# ':
42                                line = line + '<span class="code-root">' + m + input + '</span>'
43                        else:
44                                line = line + '<span class="code-user">' + m + input + '</span>'
45                        return line
46                m = match.group('option')
47                if m:
48                        space = match.group('option_space')
49                        desc  = match.group('option_desc')
50                        return space + '<span class="code-func">' + m + '</span>' + desc
51                m = match.group('note')
52                if m:
53                        return '<span class="code-note">' + m + '</span>'
54                return match.group(0)
55        return css + '<div class="code"><pre>' + regexp.sub(callback, txt)+ '</pre></div>';