Edgewall Software

ProcessorBazaar: LegendBox-0.12.py

File LegendBox-0.12.py, 1.5 KB (added by cboos, 3 years ago)

the same, but for recent trunk, with support for the new style of argument passing from [8372]

Line 
1# $Id$
2"""\file
3A wiki-processor for encapsulating wiki text inside a box.
4The box will have a legend and a modifiable color.
5
6Example:
7{{{
8#!LegendBox
9#!color: blue
10#!legend: My Title
11Here comes the actual text that comes inside the box.
12You can even use wiki-formatting in here.
13}}}
14
15You can modify the default COLOR, LEGEND and the STYLE in this plugin file.
16"""
17from trac.wiki import format_to_html, format_to_oneliner
18
19STYLE  = 'margin-top: 2px; color:black; background-color:%s; '\
20         'border: solid black 1px'
21COLOR  = 'white'
22LEGEND = 'Items'
23
24from genshi.builder import tag
25from trac.wiki.macros import WikiMacroBase
26
27class LegendBoxMacro(WikiMacroBase):
28
29    def expand_macro(self, formatter, name, content, args):
30        color  = args.get('color', COLOR)
31        legend = args.get('legend', LEGEND)
32        # -- Backward compatibility with LegendBox-0.11.py
33        lines  = content.split('\n')
34        offset = 0
35        for l in lines[:2]:
36            if l.startswith('#!color'):
37                color = l.split(':',1)[-1]
38                offset += 1
39            elif l.startswith('#!legend'):
40                legend = l.split(':',1)[-1]
41                offset += 1
42        content = '\n'.join(lines[offset:])
43        # -- End of backward compat code
44        style = STYLE % color
45        return tag.fieldset(
46                tag.legend(format_to_oneliner(self.env, formatter.context,
47                                              legend), style=style), 
48                format_to_html(self.env, formatter.context, content), 
49                               style=style)