Switch to unified view

a/Allura/allura/lib/macro.py b/Allura/allura/lib/macro.py
...
...
9
9
10
log = logging.getLogger(__name__)
10
log = logging.getLogger(__name__)
11
11
12
_macros = {}
12
_macros = {}
13
13
14
def macro(func):
14
class macro(object):
15
    _macros[func.__name__] = func
16
    return func
17
15
18
def parse(s):
16
    def __init__(self, context=None):
19
    try:
17
        self._context = context
20
        if s.startswith('quote '):
18
21
            return '[[' + s[len('quote '):] + ']]'
19
    def __call__(self, func):
20
        _macros[func.__name__] = (func, self._context)
21
        return func
22
23
class parse(object):
24
25
    def __init__(self, context):
26
        self._context = context
27
28
    def __call__(self, s):
22
        try:
29
        try:
30
            if s.startswith('quote '):
31
                return '[[' + s[len('quote '):] + ']]'
32
            try:
23
            parts = [ unicode(x, 'utf-8') for x in shlex.split(s.encode('utf-8')) ]
33
                parts = [ unicode(x, 'utf-8') for x in shlex.split(s.encode('utf-8')) ]
24
            if not parts: return None
34
                if not parts: return '[[' + s + ']]'
25
            macro = _macros.get(parts[0], None)
35
                macro = self._lookup_macro(parts[0])
26
            if not macro: return None
36
                if not macro: return  '[[' + s + ']]'
27
            for t in parts[1:]:
37
                for t in parts[1:]:
28
                if '=' not in t:
38
                    if '=' not in t:
29
                    return '[-%s: missing =-]' % ' '.join(parts)
39
                        return '[-%s: missing =-]' % ' '.join(parts)
30
            args = dict(t.split('=', 1) for t in parts[1:])
40
                args = dict(t.split('=', 1) for t in parts[1:])
31
            response = macro(**h.encode_keys(args))
41
                response = macro(**h.encode_keys(args))
32
            return response
42
                return response
33
        except (ValueError, TypeError), ex:
43
            except (ValueError, TypeError), ex:
34
            msg = cgi.escape(u'[[%s]] (%s)' % (s, repr(ex)))
44
                msg = cgi.escape(u'[[%s]] (%s)' % (s, repr(ex)))
35
            return '\n<div class="error"><pre><code>%s</code></pre></div>' % msg
45
                return '\n<div class="error"><pre><code>%s</code></pre></div>' % msg
36
    except Exception, ex:
46
        except Exception, ex:
37
        raise
47
            raise
38
        return '[[Error parsing %s: %s]]' % (s, ex)
48
            return '[[Error parsing %s: %s]]' % (s, ex)
39
49
40
@macro
50
    def _lookup_macro(self, s):
51
        macro, context = _macros.get(s, None)
52
        if context is None or context == self._context:
53
            return macro
54
        else:
55
            return None
56
57
@macro('neighborhood-wiki')
41
def projects(category=None, display_mode='grid', sort='last_updated'):
58
def projects(category=None, display_mode='grid', sort='last_updated'):
42
    from allura.lib.widgets.project_list import ProjectList
59
    from allura.lib.widgets.project_list import ProjectList
43
    from allura import model as M
60
    from allura import model as M
44
    q = dict(
61
    q = dict(
45
        neighborhood_id=c.project.neighborhood_id,
62
        neighborhood_id=c.project.neighborhood_id,
...
...
58
    pl = ProjectList()
75
    pl = ProjectList()
59
    g.resource_manager.register(pl)
76
    g.resource_manager.register(pl)
60
    response = pl.display(projects=pq.all(), display_mode=display_mode)
77
    response = pl.display(projects=pq.all(), display_mode=display_mode)
61
    return response
78
    return response
62
79
63
@macro
80
@macro()
64
def include(ref=None, **kw):
81
def include(ref=None, **kw):
65
    from allura import model as M
82
    from allura import model as M
66
    from allura.lib.widgets.macros import Include
83
    from allura.lib.widgets.macros import Include
67
    if ref is None:
84
    if ref is None:
68
        return '[-include-]'
85
        return '[-include-]'
...
...
80
    sb = Include()
97
    sb = Include()
81
    g.resource_manager.register(sb)
98
    g.resource_manager.register(sb)
82
    response = sb.display(artifact=artifact, attrs=kw)
99
    response = sb.display(artifact=artifact, attrs=kw)
83
    return response
100
    return response
84
101
85
@macro
102
@macro()
86
def img(src=None, **kw):
103
def img(src=None, **kw):
87
    attrs = ('%s="%s"' % t for t in kw.iteritems())
104
    attrs = ('%s="%s"' % t for t in kw.iteritems())
88
    included = request.environ.setdefault('allura.macro.att_embedded', set())
105
    included = request.environ.setdefault('allura.macro.att_embedded', set())
89
    included.add(src)
106
    included.add(src)
90
    if '://' in src:
107
    if '://' in src: