Edgewall Software

ProcessorBazaar: LegendBox-0.11.2.py

File LegendBox-0.11.2.py, 1.3 KB (added by cboos, 3 years ago)

make it a bit more "0.11" like...

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):
30        lines  = content.split('\n')
31        color  = COLOR
32        legend = LEGEND
33        offset = 0
34        for l in lines[:2]:
35            if l.startswith('#!color'):
36                color = l.split(':',1)[-1]
37                offset += 1
38            elif l.startswith('#!legend'):
39                legend = l.split(':',1)[-1]
40                offset += 1
41        content = '\n'.join(lines[offset:])
42        style = STYLE % color
43        return tag.fieldset(
44                tag.legend(format_to_oneliner(self.env, formatter.context,
45                                              legend), style=style), 
46                format_to_html(self.env, formatter.context, content), 
47                               style=style)