"""
    MoinMoin - LicenseInfo macro

    Display RDFa and a license icon for a page; if no argument is
    supplied, shows license for the current page.  An alternative
    page name may be specified.
    
    @copyright: 2007 Nathan R. Yergler <nathan@yergler.net>
    @license: GNU GPL, see COPYING for details.
"""

from MoinMoin.wikiutil import importPlugin
from MoinMoin.Page import Page

def generate_license_rdfa(license_info):

    return """
<a about="%(page_href)s" href="%(license_uri)s" title="%(license_name)s" rel="cc:license">
   <img src="%(license_img)s" border="0" alt="%(license_name)s"/>
</a>""" % license_info

def license_markup(request, pagename,
                   default="No license found."):
    
    """Return the license information for a page.  Optionally takes
    one argument, a pagename; if no pagename is provided, returns the
    license information for the current page."""


    LicensePage = importPlugin(request.cfg, 'action', 'LicensePage',
                               'LicensePage')
    SiteLicense = importPlugin(request.cfg, 'action', 'WikiLicense',
                               'WikiLicense')

    # get the license information from the sub-page
    page_action = LicensePage(pagename, request)
    site_action = SiteLicense(pagename, request)
    
    license_info = page_action.get_license() or site_action.get_license()

    # if no license exists, don't return anything
    if license_info == {}:
        return default

    # determine the URI of the page we're talking about
    license_info['page_href'] = Page(request, pagename).url(request)

    # return the RDFa-compliant block for the license
    return generate_license_rdfa(license_info)

def execute(macro, args):

    return license_markup(macro.request,
                          args or macro.formatter.page.page_name)
