Edgewall Software

ProcessorBazaar: LegendBox.py

File LegendBox.py, 1.2 kB (added by Jan Finell, 4 years ago)
Line 
1'''
2A wiki-processor for encapsulating wiki text inside a box.
3The box will have a legend and a modifiable color.
4
5Example:
6
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
17'''
18
19from trac.WikiFormatter import wiki_to_html
20
21STYLE  = 'margin-top: 2px; color:black; background-color:%s; '\
22         'border: solid black 1px'
23COLOR  = 'white'
24LEGEND = 'Items'
25
26def execute(hdf, txt, env):   
27    lines  = txt.split('\n')
28    color  = COLOR
29    legend = LEGEND
30    offset = 0
31    for l in lines[:2]:
32        if l.startswith('#!color'):
33            color = l.split(':',1)[-1]
34            offset += 1
35        elif l.startswith('#!legend'):
36            legend = l.split(':',1)[-1]
37            offset += 1
38   
39    return _build_field_set(hdf, '\n'.join(lines[offset:]), env, legend, color)
40
41def _build_field_set(hdf, txt, env, legend, color):
42    style = STYLE % color
43    html = '<fieldset style="%s"><legend style="%s">'\
44           '%s</legend>%s</fieldset>' % (style, style, legend, wiki_to_html(txt, hdf, env, env.get_db_cnx()))
45    return html