# $Id$
"""\file
A wiki-processor for encapsulating wiki text inside a box.
The box will have a legend and a modifiable color.

Example:
{{{
#!LegendBox
#!color: blue
#!legend: My Title
Here comes the actual text that comes inside the box.
You can even use wiki-formatting in here.
}}}

You can modify the default COLOR, LEGEND and the STYLE in this plugin file.
"""
from trac.wiki import format_to_html, format_to_oneliner

STYLE  = 'margin-top: 2px; color:black; background-color:%s; '\
         'border: solid black 1px'
COLOR  = 'white'
LEGEND = 'Items'

from genshi.builder import tag
from trac.wiki.macros import WikiMacroBase

class LegendBoxMacro(WikiMacroBase):

    def expand_macro(self, formatter, name, content):
	lines  = content.split('\n')
	color  = COLOR
	legend = LEGEND
	offset = 0
	for l in lines[:2]:
	    if l.startswith('#!color'):
		color = l.split(':',1)[-1]
		offset += 1
	    elif l.startswith('#!legend'):
		legend = l.split(':',1)[-1]
		offset += 1
	content = '\n'.join(lines[offset:])
        style = STYLE % color
        return tag.fieldset(
                tag.legend(format_to_oneliner(self.env, formatter.context,
                                              legend), style=style), 
                format_to_html(self.env, formatter.context, content), 
                               style=style)

